Completed
Pull Request — master (#630)
by Bene
06:30 queued 03:17
created

ItemTest::testGetFingerprint_emptyFingerprint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace Wikibase\DataModel\Tests\Entity;
4
5
use Wikibase\DataModel\Entity\Item;
6
use Wikibase\DataModel\Entity\ItemId;
7
use Wikibase\DataModel\SiteLink;
8
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
9
use Wikibase\DataModel\Snak\PropertySomeValueSnak;
10
use Wikibase\DataModel\Statement\Statement;
11
use Wikibase\DataModel\Statement\StatementList;
12
use Wikibase\DataModel\Term\AliasGroup;
13
use Wikibase\DataModel\Term\AliasGroupList;
14
use Wikibase\DataModel\Term\Fingerprint;
15
use Wikibase\DataModel\Term\Term;
16
use Wikibase\DataModel\Term\TermList;
17
18
/**
19
 * @covers Wikibase\DataModel\Entity\Item
20
 *
21
 * @group Wikibase
22
 * @group WikibaseItem
23
 * @group WikibaseDataModel
24
 * @group WikibaseItemTest
25
 *
26
 * @licence GNU GPL v2+
27
 * @author Jeroen De Dauw < [email protected] >
28
 * @author John Erling Blad < [email protected] >
29
 * @author Michał Łazowik
30
 * @author Bene* < [email protected] >
31
 */
32
class ItemTest extends \PHPUnit_Framework_TestCase {
33
34
	public function testGetId() {
35
		$item = new Item();
36
		$this->assertNull( $item->getId() );
37
38
		$item->setId( new ItemId( 'Q1' ) );
39
		$this->assertEquals( new ItemId( 'Q1' ), $item->getId() );
40
41
		$item->setId( null );
42
		$this->assertNull( $item->getId() );
43
44
		$item = new Item( new ItemId( 'Q2' ) );
45
		$this->assertEquals( new ItemId( 'Q2' ), $item->getId() );
46
	}
47
48
	public function testSetIdUsingNumber() {
49
		$item = new Item();
50
		$item->setId( 42 );
51
		$this->assertEquals( new ItemId( 'Q42' ), $item->getId() );
52
	}
53
54
	public function testGetFingerprint_emptyFingerprint() {
55
		$item = new Item();
56
57
		$this->assertEquals(
58
			new Fingerprint(),
59
			$item->getFingerprint()
60
		);
61
	}
62
63
	public function testGetFingerprint_termsAreSet() {
64
		$item = new Item();
65
66
		$item->setLabel( 'en', 'foo' );
67
		$item->setDescription( 'en', 'foo bar' );
68
		$item->setAliases( 'en', array( 'foo', 'bar' ) );
69
70
		$this->assertEquals(
71
			new Fingerprint(
72
				new TermList( array(
73
					new Term( 'en', 'foo' ),
74
				) ),
75
				new TermList( array(
76
					new Term( 'en', 'foo bar' )
77
				) ),
78
				new AliasGroupList( array(
79
					new AliasGroup( 'en', array( 'foo', 'bar' ) )
80
				) )
81
			),
82
			$item->getFingerprint()
83
		);
84
	}
85
86
	public function testSetFingerprint_getFingerprintReturnsIt() {
87
		$item = new Item();
88
		$fingerprint = new Fingerprint();
89
90
		$item->setFingerprint( $fingerprint );
91
		$this->assertSame( $fingerprint, $item->getFingerprint() );
92
	}
93
94
	public function testGetSiteLinkWithNonSetSiteId() {
95
		$item = new Item();
96
97
		$this->setExpectedException( 'OutOfBoundsException' );
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
98
		$item->getSiteLinkList()->getBySiteId( 'enwiki' );
99
	}
100
101
	/**
102
	 * @dataProvider simpleSiteLinkProvider
103
	 */
104
	public function testAddSiteLink( SiteLink $siteLink ) {
105
		$item = new Item();
106
107
		$item->getSiteLinkList()->addSiteLink( $siteLink );
108
109
		$this->assertEquals(
110
			$siteLink,
111
			$item->getSiteLinkList()->getBySiteId( $siteLink->getSiteId() )
112
		);
113
	}
114
115
	public function simpleSiteLinkProvider() {
116
		$argLists = array();
117
118
		$argLists[] = array(
119
			new SiteLink(
120
				'enwiki',
121
				'Wikidata',
122
				array(
123
					new ItemId( 'Q42' )
124
				)
125
			)
126
		);
127
		$argLists[] = array(
128
			new SiteLink(
129
				'nlwiki',
130
				'Wikidata'
131
			)
132
		);
133
		$argLists[] = array(
134
			new SiteLink(
135
				'enwiki',
136
				'Nyan!',
137
				array(
138
					new ItemId( 'Q42' ),
139
					new ItemId( 'Q149' )
140
				)
141
			)
142
		);
143
		$argLists[] = array(
144
			new SiteLink(
145
				'foo bar',
146
				'baz bah',
147
				array(
148
					new ItemId( 'Q3' ),
149
					new ItemId( 'Q7' )
150
				)
151
			)
152
		);
153
154
		return $argLists;
155
	}
156
157
	/**
158
	 * @dataProvider simpleSiteLinksProvider
159
	 */
160
	public function testGetSiteLinks() {
161
		$siteLinks = func_get_args();
162
		$item = new Item();
163
164
		foreach ( $siteLinks as $siteLink ) {
165
			$item->getSiteLinkList()->addSiteLink( $siteLink );
166
		}
167
168
		$this->assertInternalType( 'array', $item->getSiteLinks() );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Item::getSiteLinks() has been deprecated with message: since 0.8, use getSiteLinkList() instead,

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
169
		$this->assertEquals( $siteLinks, $item->getSiteLinks() );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Item::getSiteLinks() has been deprecated with message: since 0.8, use getSiteLinkList() instead,

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
170
	}
171
172
	public function simpleSiteLinksProvider() {
173
		$argLists = array();
174
175
		$argLists[] = array();
176
177
		$argLists[] = array( new SiteLink( 'enwiki', 'Wikidata', array( new ItemId( 'Q42' ) ) ) );
178
179
		$argLists[] = array(
180
			new SiteLink( 'enwiki', 'Wikidata' ),
181
			new SiteLink( 'nlwiki', 'Wikidata', array( new ItemId( 'Q3' ) ) )
182
		);
183
184
		$argLists[] = array(
185
			new SiteLink( 'enwiki', 'Wikidata' ),
186
			new SiteLink( 'nlwiki', 'Wikidata' ),
187
			new SiteLink( 'foo bar', 'baz bah', array( new ItemId( 'Q2' ) ) )
188
		);
189
190
		return $argLists;
191
	}
192
193
	public function testHasLinkToSiteForFalse() {
194
		$item = new Item();
195
		$item->getSiteLinkList()->addNewSiteLink( 'ENWIKI', 'Wikidata', array( new ItemId( 'Q42' ) ) );
196
197
		$this->assertFalse( $item->getSiteLinkList()->hasLinkWithSiteId( 'enwiki' ) );
198
		$this->assertFalse( $item->getSiteLinkList()->hasLinkWithSiteId( 'dewiki' ) );
199
		$this->assertFalse( $item->getSiteLinkList()->hasLinkWithSiteId( 'foo bar' ) );
200
	}
201
202
	public function testHasLinkToSiteForTrue() {
203
		$item = new Item();
204
		$item->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Wikidata', array( new ItemId( 'Q42' ) ) );
205
		$item->getSiteLinkList()->addNewSiteLink( 'dewiki', 'Wikidata' );
206
		$item->getSiteLinkList()->addNewSiteLink( 'foo bar', 'Wikidata' );
207
208
		$this->assertTrue( $item->getSiteLinkList()->hasLinkWithSiteId( 'enwiki' ) );
209
		$this->assertTrue( $item->getSiteLinkList()->hasLinkWithSiteId( 'dewiki' ) );
210
		$this->assertTrue( $item->getSiteLinkList()->hasLinkWithSiteId( 'foo bar' ) );
211
	}
212
213
	public function testEmptyItemReturnsEmptySiteLinkList() {
214
		$item = new Item();
215
		$this->assertTrue( $item->getSiteLinkList()->isEmpty() );
216
	}
217
218
	public function testAddSiteLinkOverridesOldLinks() {
219
		$item = new Item();
220
221
		$item->getSiteLinkList()->addNewSiteLink( 'kittens', 'foo' );
222
223
		$newLink = new SiteLink( 'kittens', 'bar' );
224
		$item->addSiteLink( $newLink );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Item::addSiteLink() has been deprecated with message: since 0.8, use getSiteLinkList()->addSiteLink() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
225
226
		$this->assertTrue( $item->getSiteLinkList()->getBySiteId( 'kittens' )->equals( $newLink ) );
227
	}
228
229
	public function testEmptyItemIsEmpty() {
230
		$item = new Item();
231
		$this->assertTrue( $item->isEmpty() );
232
	}
233
234
	public function testItemWithIdIsEmpty() {
235
		$item = new Item( new ItemId( 'Q1337' ) );
236
		$this->assertTrue( $item->isEmpty() );
237
	}
238
239
	public function testItemWithStuffIsNotEmpty() {
240
		$item = new Item();
241
		$item->getFingerprint()->setAliasGroup( 'en', array( 'foo' ) );
242
		$this->assertFalse( $item->isEmpty() );
243
244
		$item = new Item();
245
		$item->getSiteLinkList()->addNewSiteLink( 'en', 'o_O' );
246
		$this->assertFalse( $item->isEmpty() );
247
248
		$item = new Item();
249
		$item->getStatements()->addStatement( $this->newStatement() );
250
		$this->assertFalse( $item->isEmpty() );
251
	}
252
253
	public function testItemWithSitelinksHasSitelinks() {
254
		$item = new Item();
255
		$item->getSiteLinkList()->addNewSiteLink( 'en', 'foo' );
256
		$this->assertFalse( $item->getSiteLinkList()->isEmpty() );
257
	}
258
259
	public function testItemWithoutSitelinksHasNoSitelinks() {
260
		$item = new Item();
261
		$this->assertTrue( $item->getSiteLinkList()->isEmpty() );
262
	}
263
264
	private function newStatement() {
265
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
266
		$statement->setGuid( 'kittens' );
267
		return $statement;
268
	}
269
270
	public function testClearRemovesAllButId() {
271
		$item = new Item( new ItemId( 'Q42' ) );
272
		$item->getFingerprint()->setLabel( 'en', 'foo' );
273
		$item->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Foo' );
274
		$item->getStatements()->addStatement( $this->newStatement() );
275
276
		$item->clear();
277
278
		$this->assertEquals( new ItemId( 'Q42' ), $item->getId() );
279
		$this->assertTrue( $item->getFingerprint()->isEmpty() );
280
		$this->assertTrue( $item->getSiteLinkList()->isEmpty() );
281
		$this->assertTrue( $item->getStatements()->isEmpty() );
282
	}
283
284
	public function testEmptyConstructor() {
285
		$item = new Item();
286
287
		$this->assertNull( $item->getId() );
288
		$this->assertTrue( $item->getFingerprint()->isEmpty() );
289
		$this->assertTrue( $item->getSiteLinkList()->isEmpty() );
290
		$this->assertTrue( $item->getStatements()->isEmpty() );
291
	}
292
293
	public function testCanConstructWithStatementList() {
294
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
295
		$statement->setGuid( 'meh' );
296
297
		$statements = new StatementList( $statement );
298
299
		$item = new Item( null, null, null, $statements );
300
301
		$this->assertEquals(
302
			$statements,
303
			$item->getStatements()
304
		);
305
	}
306
307
	public function testSetStatements() {
308
		$item = new Item();
309
		$item->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) );
310
311
		$item->setStatements( new StatementList() );
312
		$this->assertTrue( $item->getStatements()->isEmpty() );
313
	}
314
315
	public function testGetStatementsReturnsCorrectTypeAfterClear() {
316
		$item = new Item();
317
		$item->clear();
318
319
		$this->assertTrue( $item->getStatements()->isEmpty() );
320
	}
321
322
	public function equalsProvider() {
323
		$firstItem = new Item();
324
		$firstItem->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) );
325
326
		$secondItem = new Item();
327
		$secondItem->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) );
328
329
		$secondItemWithId = $secondItem->copy();
330
		$secondItemWithId->setId( 42 );
331
332
		$differentId = $secondItemWithId->copy();
333
		$differentId->setId( 43 );
334
335
		return array(
336
			array( new Item(), new Item() ),
337
			array( $firstItem, $secondItem ),
338
			array( $secondItem, $secondItemWithId ),
339
			array( $secondItemWithId, $differentId ),
340
		);
341
	}
342
343
	/**
344
	 * @dataProvider equalsProvider
345
	 */
346
	public function testEquals( Item $firstItem, Item $secondItem ) {
347
		$this->assertTrue( $firstItem->equals( $secondItem ) );
348
		$this->assertTrue( $secondItem->equals( $firstItem ) );
349
	}
350
351
	private function getBaseItem() {
352
		$item = new Item( new ItemId( 'Q42' ) );
353
		$item->getFingerprint()->setLabel( 'en', 'Same' );
354
		$item->getFingerprint()->setDescription( 'en', 'Same' );
355
		$item->getFingerprint()->setAliasGroup( 'en', array( 'Same' ) );
356
		$item->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Same' );
357
		$item->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) );
358
359
		return $item;
360
	}
361
362
	public function notEqualsProvider() {
363
		$differentLabel = $this->getBaseItem();
364
		$differentLabel->getFingerprint()->setLabel( 'en', 'Different' );
365
366
		$differentDescription = $this->getBaseItem();
367
		$differentDescription->getFingerprint()->setDescription( 'en', 'Different' );
368
369
		$differentAlias = $this->getBaseItem();
370
		$differentAlias->getFingerprint()->setAliasGroup( 'en', array( 'Different' ) );
371
372
		$differentSiteLink = $this->getBaseItem();
373
		$differentSiteLink->getSiteLinkList()->removeLinkWithSiteId( 'enwiki' );
374
		$differentSiteLink->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Different' );
375
376
		$differentStatement = $this->getBaseItem();
377
		$differentStatement->setStatements( new StatementList() );
378
		$differentStatement->getStatements()->addNewStatement( new PropertyNoValueSnak( 24 ) );
379
380
		$item = $this->getBaseItem();
381
382
		return array(
383
			'empty' => array( $item, new Item() ),
384
			'label' => array( $item, $differentLabel ),
385
			'description' => array( $item, $differentDescription ),
386
			'alias' => array( $item, $differentAlias ),
387
			'siteLink' => array( $item, $differentSiteLink ),
388
			'statement' => array( $item, $differentStatement ),
389
		);
390
	}
391
392
	/**
393
	 * @dataProvider notEqualsProvider
394
	 */
395
	public function testNotEquals( Item $firstItem, Item $secondItem ) {
396
		$this->assertFalse( $firstItem->equals( $secondItem ) );
397
		$this->assertFalse( $secondItem->equals( $firstItem ) );
398
	}
399
400
	public function cloneProvider() {
401
		$item = new Item( new ItemId( 'Q1' ) );
402
		$item->setLabel( 'en', 'original' );
403
		$item->getStatements()->addNewStatement( new PropertyNoValueSnak( 1 ) );
404
		$item->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Original' );
405
406
		return array(
407
			'copy' => array( $item, $item->copy() ),
408
			'native clone' => array( $item, clone $item ),
409
		);
410
	}
411
412
	/**
413
	 * @dataProvider cloneProvider
414
	 */
415
	public function testCloneIsEqualButNotIdentical( Item $original, Item $clone ) {
416
		$this->assertNotSame( $original, $clone );
417
		$this->assertTrue( $original->equals( $clone ) );
418
		$this->assertSame(
419
			$original->getId(),
420
			$clone->getId(),
421
			'id is immutable and must not be cloned'
422
		);
423
424
		// The clone must not reference the same mutable objects
425
		$this->assertNotSame( $original->getFingerprint(), $clone->getFingerprint() );
426
		$this->assertNotSame( $original->getStatements(), $clone->getStatements() );
427
		$this->assertNotSame(
428
			$original->getStatements()->getFirstStatementWithGuid( null ),
429
			$clone->getStatements()->getFirstStatementWithGuid( null )
430
		);
431
		$this->assertNotSame( $original->getSiteLinkList(), $clone->getSiteLinkList() );
432
		$this->assertSame(
433
			$original->getSiteLinkList()->getBySiteId( 'enwiki' ),
434
			$clone->getSiteLinkList()->getBySiteId( 'enwiki' ),
435
			'SiteLink is immutable and must not be cloned'
436
		);
437
	}
438
439
	/**
440
	 * @dataProvider cloneProvider
441
	 */
442
	public function testOriginalDoesNotChangeWithClone( Item $original, Item $clone ) {
443
		$originalStatement = $original->getStatements()->getFirstStatementWithGuid( null );
444
		$clonedStatement = $clone->getStatements()->getFirstStatementWithGuid( null );
445
446
		$clone->setLabel( 'en', 'clone' );
447
		$clone->setDescription( 'en', 'clone' );
448
		$clone->setAliases( 'en', array( 'clone' ) );
449
		$clonedStatement->setGuid( 'clone' );
450
		$clonedStatement->setMainSnak( new PropertySomeValueSnak( 666 ) );
451
		$clonedStatement->setRank( Statement::RANK_DEPRECATED );
452
		$clonedStatement->getQualifiers()->addSnak( new PropertyNoValueSnak( 1 ) );
453
		$clonedStatement->getReferences()->addNewReference( new PropertyNoValueSnak( 1 ) );
454
		$clone->getSiteLinkList()->removeLinkWithSiteId( 'enwiki' );
455
456
		$this->assertSame( 'original', $original->getFingerprint()->getLabel( 'en' )->getText() );
457
		$this->assertFalse( $original->getFingerprint()->hasDescription( 'en' ) );
458
		$this->assertFalse( $original->getFingerprint()->hasAliasGroup( 'en' ) );
459
		$this->assertNull( $originalStatement->getGuid() );
460
		$this->assertSame( 'novalue', $originalStatement->getMainSnak()->getType() );
461
		$this->assertSame( Statement::RANK_NORMAL, $originalStatement->getRank() );
462
		$this->assertTrue( $originalStatement->getQualifiers()->isEmpty() );
463
		$this->assertTrue( $originalStatement->getReferences()->isEmpty() );
464
		$this->assertFalse( $original->getSiteLinkList()->isEmpty() );
465
	}
466
467
}
468