Completed
Push — master ( c8b4d1...73e2db )
by adam
06:49
created

testWhenLabelsAreSet_getFingerprintReturnsFingerprintWithLabels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
3
namespace Wikibase\DataModel\Tests\Entity;
4
5
use OutOfBoundsException;
6
use PHPUnit_Framework_TestCase;
7
use Wikibase\DataModel\Entity\Item;
8
use Wikibase\DataModel\Entity\ItemId;
9
use Wikibase\DataModel\SiteLink;
10
use Wikibase\DataModel\SiteLinkList;
11
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
12
use Wikibase\DataModel\Snak\PropertySomeValueSnak;
13
use Wikibase\DataModel\Statement\Statement;
14
use Wikibase\DataModel\Statement\StatementList;
15
use Wikibase\DataModel\Term\AliasGroup;
16
use Wikibase\DataModel\Term\AliasGroupList;
17
use Wikibase\DataModel\Term\Fingerprint;
18
use Wikibase\DataModel\Term\Term;
19
use Wikibase\DataModel\Term\TermList;
20
21
/**
22
 * @covers Wikibase\DataModel\Entity\Item
23
 *
24
 * @group Wikibase
25
 * @group WikibaseDataModel
26
 *
27
 * @license GPL-2.0+
28
 * @author Jeroen De Dauw < [email protected] >
29
 * @author John Erling Blad < [email protected] >
30
 * @author Michał Łazowik
31
 */
32
class ItemTest extends PHPUnit_Framework_TestCase {
33
34
	private function getNewEmpty() {
35
		return new Item();
36
	}
37
38
	public function testGetId() {
39
		$item = new Item();
40
		$this->assertNull( $item->getId() );
41
42
		$item->setId( new ItemId( 'Q1' ) );
43
		$this->assertEquals( new ItemId( 'Q1' ), $item->getId() );
44
45
		$item->setId( null );
46
		$this->assertNull( $item->getId() );
47
48
		$item = new Item( new ItemId( 'Q2' ) );
49
		$this->assertEquals( new ItemId( 'Q2' ), $item->getId() );
50
	}
51
52
	public function testGetSiteLinkWithNonSetSiteId() {
53
		$item = new Item();
54
55
		$this->setExpectedException( OutOfBoundsException::class );
56
		$item->getSiteLinkList()->getBySiteId( 'enwiki' );
57
	}
58
59
	/**
60
	 * @dataProvider simpleSiteLinkProvider
61
	 */
62
	public function testAddSiteLink( SiteLink $siteLink ) {
63
		$item = new Item();
64
65
		$item->getSiteLinkList()->addSiteLink( $siteLink );
66
67
		$this->assertEquals(
68
			$siteLink,
69
			$item->getSiteLinkList()->getBySiteId( $siteLink->getSiteId() )
70
		);
71
	}
72
73
	public function simpleSiteLinkProvider() {
74
		$argLists = [];
75
76
		$argLists[] = [
77
			new SiteLink(
78
				'enwiki',
79
				'Wikidata',
80
				[
81
					new ItemId( 'Q42' )
82
				]
83
			)
84
		];
85
		$argLists[] = [
86
			new SiteLink(
87
				'nlwiki',
88
				'Wikidata'
89
			)
90
		];
91
		$argLists[] = [
92
			new SiteLink(
93
				'enwiki',
94
				'Nyan!',
95
				[
96
					new ItemId( 'Q42' ),
97
					new ItemId( 'Q149' )
98
				]
99
			)
100
		];
101
		$argLists[] = [
102
			new SiteLink(
103
				'foo bar',
104
				'baz bah',
105
				[
106
					new ItemId( 'Q3' ),
107
					new ItemId( 'Q7' )
108
				]
109
			)
110
		];
111
112
		return $argLists;
113
	}
114
115
	public function simpleSiteLinksProvider() {
116
		$argLists = [];
117
118
		$argLists[] = [];
119
120
		$argLists[] = [ new SiteLink( 'enwiki', 'Wikidata', [ new ItemId( 'Q42' ) ] ) ];
121
122
		$argLists[] = [
123
			new SiteLink( 'enwiki', 'Wikidata' ),
124
			new SiteLink( 'nlwiki', 'Wikidata', [ new ItemId( 'Q3' ) ] )
125
		];
126
127
		$argLists[] = [
128
			new SiteLink( 'enwiki', 'Wikidata' ),
129
			new SiteLink( 'nlwiki', 'Wikidata' ),
130
			new SiteLink( 'foo bar', 'baz bah', [ new ItemId( 'Q2' ) ] )
131
		];
132
133
		return $argLists;
134
	}
135
136
	public function testHasLinkToSiteForFalse() {
137
		$item = new Item();
138
		$item->getSiteLinkList()->addNewSiteLink( 'ENWIKI', 'Wikidata', [ new ItemId( 'Q42' ) ] );
139
140
		$this->assertFalse( $item->getSiteLinkList()->hasLinkWithSiteId( 'enwiki' ) );
141
		$this->assertFalse( $item->getSiteLinkList()->hasLinkWithSiteId( 'dewiki' ) );
142
		$this->assertFalse( $item->getSiteLinkList()->hasLinkWithSiteId( 'foo bar' ) );
143
	}
144
145
	public function testHasLinkToSiteForTrue() {
146
		$item = new Item();
147
		$item->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Wikidata', [ new ItemId( 'Q42' ) ] );
148
		$item->getSiteLinkList()->addNewSiteLink( 'dewiki', 'Wikidata' );
149
		$item->getSiteLinkList()->addNewSiteLink( 'foo bar', 'Wikidata' );
150
151
		$this->assertTrue( $item->getSiteLinkList()->hasLinkWithSiteId( 'enwiki' ) );
152
		$this->assertTrue( $item->getSiteLinkList()->hasLinkWithSiteId( 'dewiki' ) );
153
		$this->assertTrue( $item->getSiteLinkList()->hasLinkWithSiteId( 'foo bar' ) );
154
	}
155
156
	public function testEmptyItemReturnsEmptySiteLinkList() {
157
		$item = new Item();
158
		$this->assertTrue( $item->getSiteLinkList()->isEmpty() );
159
	}
160
161
	public function testAddSiteLinkOverridesOldLinks() {
162
		$item = new Item();
163
164
		$item->getSiteLinkList()->addNewSiteLink( 'kittens', 'foo' );
165
166
		$newLink = new SiteLink( 'kittens', 'bar' );
167
		$item->addSiteLink( $newLink );
168
169
		$this->assertTrue( $item->getSiteLinkList()->getBySiteId( 'kittens' )->equals( $newLink ) );
170
	}
171
172
	public function testEmptyItemIsEmpty() {
173
		$item = new Item();
174
		$this->assertTrue( $item->isEmpty() );
175
	}
176
177
	public function testItemWithIdIsEmpty() {
178
		$item = new Item( new ItemId( 'Q1337' ) );
179
		$this->assertTrue( $item->isEmpty() );
180
	}
181
182
	public function testItemWithStuffIsNotEmpty() {
183
		$item = new Item();
184
		$item->setAliases( 'en', [ 'foo' ] );
185
		$this->assertFalse( $item->isEmpty() );
186
187
		$item = new Item();
188
		$item->getSiteLinkList()->addNewSiteLink( 'en', 'o_O' );
189
		$this->assertFalse( $item->isEmpty() );
190
191
		$item = new Item();
192
		$item->getStatements()->addStatement( $this->newStatement() );
193
		$this->assertFalse( $item->isEmpty() );
194
	}
195
196
	public function testItemWithSitelinksHasSitelinks() {
197
		$item = new Item();
198
		$item->getSiteLinkList()->addNewSiteLink( 'en', 'foo' );
199
		$this->assertFalse( $item->getSiteLinkList()->isEmpty() );
200
	}
201
202
	public function testItemWithoutSitelinksHasNoSitelinks() {
203
		$item = new Item();
204
		$this->assertTrue( $item->getSiteLinkList()->isEmpty() );
205
	}
206
207
	private function newStatement() {
208
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
209
		$statement->setGuid( 'kittens' );
210
		return $statement;
211
	}
212
213
	public function testEmptyConstructor() {
214
		$item = new Item();
215
216
		$this->assertNull( $item->getId() );
217
		$this->assertTrue( $item->getFingerprint()->isEmpty() );
218
		$this->assertTrue( $item->getLabels()->isEmpty() );
219
		$this->assertTrue( $item->getDescriptions()->isEmpty() );
220
		$this->assertTrue( $item->getAliasGroups()->isEmpty() );
221
		$this->assertTrue( $item->getSiteLinkList()->isEmpty() );
222
		$this->assertTrue( $item->getStatements()->isEmpty() );
223
	}
224
225
	public function testCanConstructWithStatementList() {
226
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
227
		$statement->setGuid( 'meh' );
228
229
		$statements = new StatementList( $statement );
230
231
		$item = new Item( null, null, null, $statements );
232
233
		$this->assertEquals(
234
			$statements,
235
			$item->getStatements()
236
		);
237
	}
238
239
	public function testSetStatements() {
240
		$item = new Item();
241
		$item->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) );
242
243
		$item->setStatements( new StatementList() );
244
		$this->assertTrue( $item->getStatements()->isEmpty() );
245
	}
246
247
	public function equalsProvider() {
248
		$firstItem = new Item();
249
		$firstItem->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) );
250
251
		$secondItem = new Item();
252
		$secondItem->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) );
253
254
		$secondItemWithId = $secondItem->copy();
255
		$secondItemWithId->setId( new ItemId( 'Q42' ) );
256
257
		$differentId = $secondItemWithId->copy();
258
		$differentId->setId( new ItemId( 'Q43' ) );
259
260
		return [
261
			[ new Item(), new Item() ],
262
			[ $firstItem, $secondItem ],
263
			[ $secondItem, $secondItemWithId ],
264
			[ $secondItemWithId, $differentId ],
265
		];
266
	}
267
268
	/**
269
	 * @dataProvider equalsProvider
270
	 */
271
	public function testEquals( Item $firstItem, Item $secondItem ) {
272
		$this->assertTrue( $firstItem->equals( $secondItem ) );
273
		$this->assertTrue( $secondItem->equals( $firstItem ) );
274
	}
275
276
	/**
277
	 * @return Item
278
	 */
279
	private function getBaseItem() {
280
		$item = new Item( new ItemId( 'Q42' ) );
281
		$item->setLabel( 'en', 'Same' );
282
		$item->setDescription( 'en', 'Same' );
283
		$item->setAliases( 'en', [ 'Same' ] );
284
		$item->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Same' );
285
		$item->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) );
286
287
		return $item;
288
	}
289
290
	public function notEqualsProvider() {
291
		$differentLabel = $this->getBaseItem();
292
		$differentLabel->setLabel( 'en', 'Different' );
293
294
		$differentDescription = $this->getBaseItem();
295
		$differentDescription->setDescription( 'en', 'Different' );
296
297
		$differentAlias = $this->getBaseItem();
298
		$differentAlias->setAliases( 'en', [ 'Different' ] );
299
300
		$differentSiteLink = $this->getBaseItem();
301
		$differentSiteLink->getSiteLinkList()->removeLinkWithSiteId( 'enwiki' );
302
		$differentSiteLink->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Different' );
303
304
		$differentStatement = $this->getBaseItem();
305
		$differentStatement->setStatements( new StatementList() );
306
		$differentStatement->getStatements()->addNewStatement( new PropertyNoValueSnak( 24 ) );
307
308
		$item = $this->getBaseItem();
309
310
		return [
311
			'empty' => [ $item, new Item() ],
312
			'label' => [ $item, $differentLabel ],
313
			'description' => [ $item, $differentDescription ],
314
			'alias' => [ $item, $differentAlias ],
315
			'siteLink' => [ $item, $differentSiteLink ],
316
			'statement' => [ $item, $differentStatement ],
317
		];
318
	}
319
320
	/**
321
	 * @dataProvider notEqualsProvider
322
	 */
323
	public function testNotEquals( Item $firstItem, Item $secondItem ) {
324
		$this->assertFalse( $firstItem->equals( $secondItem ) );
325
		$this->assertFalse( $secondItem->equals( $firstItem ) );
326
	}
327
328
	public function cloneProvider() {
329
		$item = new Item( new ItemId( 'Q1' ) );
330
		$item->setLabel( 'en', 'original' );
331
		$item->getStatements()->addNewStatement( new PropertyNoValueSnak( 1 ) );
332
		$item->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Original' );
333
334
		return [
335
			'copy' => [ $item, $item->copy() ],
336
			'native clone' => [ $item, clone $item ],
337
		];
338
	}
339
340
	/**
341
	 * @dataProvider cloneProvider
342
	 */
343
	public function testCloneIsEqualButNotIdentical( Item $original, Item $clone ) {
344
		$this->assertNotSame( $original, $clone );
345
		$this->assertTrue( $original->equals( $clone ) );
346
		$this->assertSame(
347
			$original->getId(),
348
			$clone->getId(),
349
			'id is immutable and must not be cloned'
350
		);
351
352
		// The clone must not reference the same mutable objects
353
		$this->assertNotSame( $original->getFingerprint(), $clone->getFingerprint() );
354
		$this->assertNotSame( $original->getStatements(), $clone->getStatements() );
355
		$this->assertNotSame(
356
			$original->getStatements()->getFirstStatementWithGuid( null ),
357
			$clone->getStatements()->getFirstStatementWithGuid( null )
358
		);
359
		$this->assertNotSame( $original->getSiteLinkList(), $clone->getSiteLinkList() );
360
		$this->assertSame(
361
			$original->getSiteLinkList()->getBySiteId( 'enwiki' ),
362
			$clone->getSiteLinkList()->getBySiteId( 'enwiki' ),
363
			'SiteLink is immutable and must not be cloned'
364
		);
365
	}
366
367
	/**
368
	 * @dataProvider cloneProvider
369
	 */
370
	public function testOriginalDoesNotChangeWithClone( Item $original, Item $clone ) {
371
		$originalStatement = $original->getStatements()->getFirstStatementWithGuid( null );
372
		$clonedStatement = $clone->getStatements()->getFirstStatementWithGuid( null );
373
374
		$clone->setLabel( 'en', 'clone' );
375
		$clone->setDescription( 'en', 'clone' );
376
		$clone->setAliases( 'en', [ 'clone' ] );
377
		$clonedStatement->setGuid( 'clone' );
378
		$clonedStatement->setMainSnak( new PropertySomeValueSnak( 666 ) );
379
		$clonedStatement->setRank( Statement::RANK_DEPRECATED );
380
		$clonedStatement->getQualifiers()->addSnak( new PropertyNoValueSnak( 1 ) );
381
		$clonedStatement->getReferences()->addNewReference( new PropertyNoValueSnak( 1 ) );
382
		$clone->getSiteLinkList()->removeLinkWithSiteId( 'enwiki' );
383
384
		$this->assertSame( 'original', $original->getFingerprint()->getLabel( 'en' )->getText() );
385
		$this->assertFalse( $original->getFingerprint()->hasDescription( 'en' ) );
386
		$this->assertFalse( $original->getFingerprint()->hasAliasGroup( 'en' ) );
387
		$this->assertNull( $originalStatement->getGuid() );
388
		$this->assertSame( 'novalue', $originalStatement->getMainSnak()->getType() );
389
		$this->assertSame( Statement::RANK_NORMAL, $originalStatement->getRank() );
390
		$this->assertTrue( $originalStatement->getQualifiers()->isEmpty() );
391
		$this->assertTrue( $originalStatement->getReferences()->isEmpty() );
392
		$this->assertFalse( $original->getSiteLinkList()->isEmpty() );
393
	}
394
395
	// Below are tests copied from EntityTest
396
397
	public function labelProvider() {
398
		return [
399
			[ 'en', 'spam' ],
400
			[ 'en', 'spam', 'spam' ],
401
			[ 'de', 'foo bar baz' ],
402
		];
403
	}
404
405
	/**
406
	 * @dataProvider labelProvider
407
	 * @param string $languageCode
408
	 * @param string $labelText
409
	 * @param string $moarText
410
	 */
411
	public function testSetLabel( $languageCode, $labelText, $moarText = 'ohi there' ) {
412
		$entity = $this->getNewEmpty();
413
414
		$entity->setLabel( $languageCode, $labelText );
415
416
		$this->assertSame( $labelText, $entity->getFingerprint()->getLabel( $languageCode )->getText() );
417
418
		$entity->setLabel( $languageCode, $moarText );
419
420
		$this->assertSame( $moarText, $entity->getFingerprint()->getLabel( $languageCode )->getText() );
421
	}
422
423
	public function descriptionProvider() {
424
		return [
425
			[ 'en', 'spam' ],
426
			[ 'en', 'spam', 'spam' ],
427
			[ 'de', 'foo bar baz' ],
428
		];
429
	}
430
431
	/**
432
	 * @dataProvider descriptionProvider
433
	 * @param string $languageCode
434
	 * @param string $description
435
	 * @param string $moarText
436
	 */
437
	public function testSetDescription( $languageCode, $description, $moarText = 'ohi there' ) {
438
		$entity = $this->getNewEmpty();
439
440
		$entity->setDescription( $languageCode, $description );
441
442
		$this->assertSame( $description, $entity->getFingerprint()->getDescription( $languageCode )->getText() );
443
444
		$entity->setDescription( $languageCode, $moarText );
445
446
		$this->assertSame( $moarText, $entity->getFingerprint()->getDescription( $languageCode )->getText() );
447
	}
448
449
	public function aliasesProvider() {
450
		return [
451
			[ [
452
				'en' => [ [ 'spam' ] ],
453
			] ],
454
			[ [
455
				'en' => [ [ 'foo', 'bar', 'baz' ] ],
456
			] ],
457
			[ [
458
				'en' => [ [ 'foo', 'bar' ], [ 'baz', 'spam' ] ],
459
			] ],
460
			[ [
461
				'en' => [ [ 'foo', 'bar', 'baz' ] ],
462
				'de' => [ [ 'foobar' ], [ 'baz' ] ],
463
			] ],
464
			// with duplicates
465
			[ [
466
				'en' => [ [ 'spam', 'ham', 'ham' ] ],
467
			] ],
468
			[ [
469
				'en' => [ [ 'foo', 'bar' ], [ 'bar', 'spam' ] ],
470
			] ],
471
		];
472
	}
473
474
	/**
475
	 * @dataProvider aliasesProvider
476
	 */
477
	public function testSetAliases( array $aliasesLists ) {
478
		$entity = $this->getNewEmpty();
479
480
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
481
			foreach ( $aliasesList as $aliases ) {
482
				$entity->setAliases( $langCode, $aliases );
483
			}
484
		}
485
486
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
487
			$expected = array_values( array_unique( array_pop( $aliasesList ) ) );
488
			$actual = $entity->getFingerprint()->getAliasGroup( $langCode )->getAliases();
489
			$this->assertSame( $expected, $actual );
490
		}
491
	}
492
493
	/**
494
	 * @dataProvider aliasesProvider
495
	 */
496
	public function testSetEmptyAlias( array $aliasesLists ) {
497
		$entity = $this->getNewEmpty();
498
499
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
500
			foreach ( $aliasesList as $aliases ) {
501
				$entity->setAliases( $langCode, $aliases );
502
			}
503
		}
504
		$entity->setAliases( 'zh', [ 'wind', 'air', '', 'fire' ] );
505
		$entity->setAliases( 'zu', [ '', '' ] );
506
507
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
508
			$expected = array_values( array_unique( array_pop( $aliasesList ) ) );
509
			asort( $aliasesList );
510
511
			$actual = $entity->getFingerprint()->getAliasGroup( $langCode )->getAliases();
512
			asort( $actual );
513
514
			$this->assertEquals( $expected, $actual );
515
		}
516
	}
517
518
	public function instanceProvider() {
519
		$entities = [];
520
521
		// empty
522
		$entity = $this->getNewEmpty();
523
		$entities[] = $entity;
524
525
		// ID only
526
		$entity = clone $entity;
527
		$entity->setId( new ItemId( 'Q44' ) );
528
529
		$entities[] = $entity;
530
531
		// with labels and stuff
532
		$entity = $this->getNewEmpty();
533
		$entity->setAliases( 'en', [ 'o', 'noez' ] );
534
		$entity->setLabel( 'de', 'spam' );
535
		$entity->setDescription( 'en', 'foo bar baz' );
536
537
		$entities[] = $entity;
538
539
		// with labels etc and ID
540
		$entity = clone $entity;
541
		$entity->setId( new ItemId( 'Q42' ) );
542
543
		$entities[] = $entity;
544
545
		$argLists = [];
546
547
		foreach ( $entities as $entity ) {
548
			$argLists[] = [ $entity ];
549
		}
550
551
		return $argLists;
552
	}
553
554
	/**
555
	 * @dataProvider instanceProvider
556
	 * @param Item $entity
557
	 */
558
	public function testCopy( Item $entity ) {
559
		$copy = $entity->copy();
560
561
		// The equality method alone is not enough since it does not check the IDs.
562
		$this->assertTrue( $entity->equals( $copy ) );
563
		$this->assertEquals( $entity->getId(), $copy->getId() );
564
565
		$this->assertNotSame( $entity, $copy );
566
	}
567
568
	public function testCopyRetainsLabels() {
569
		$item = new Item();
570
571
		$item->getFingerprint()->setLabel( 'en', 'foo' );
572
		$item->getFingerprint()->setLabel( 'de', 'bar' );
573
574
		$newItem = $item->copy();
575
576
		$this->assertTrue( $newItem->getFingerprint()->getLabels()->hasTermForLanguage( 'en' ) );
577
		$this->assertTrue( $newItem->getFingerprint()->getLabels()->hasTermForLanguage( 'de' ) );
578
	}
579
580
	/**
581
	 * @dataProvider instanceProvider
582
	 * @param Item $entity
583
	 */
584
	public function testSerialize( Item $entity ) {
585
		$string = serialize( $entity );
586
587
		$this->assertInternalType( 'string', $string );
588
589
		$instance = unserialize( $string );
590
591
		$this->assertTrue( $entity->equals( $instance ) );
592
		$this->assertEquals( $entity->getId(), $instance->getId() );
593
	}
594
595
	public function testWhenNoStuffIsSet_getFingerprintReturnsEmptyFingerprint() {
596
		$entity = $this->getNewEmpty();
597
598
		$this->assertEquals(
599
			new Fingerprint(),
600
			$entity->getFingerprint()
601
		);
602
	}
603
604
	public function testWhenLabelsAreSet_getFingerprintReturnsFingerprintWithLabels() {
605
		$entity = $this->getNewEmpty();
606
607
		$entity->setLabel( 'en', 'foo' );
608
		$entity->setLabel( 'de', 'bar' );
609
610
		$this->assertEquals(
611
			new Fingerprint(
612
				new TermList( [
613
					new Term( 'en', 'foo' ),
614
					new Term( 'de', 'bar' ),
615
				] )
616
			),
617
			$entity->getFingerprint()
618
		);
619
	}
620
621
	public function testWhenTermsAreSet_getFingerprintReturnsFingerprintWithTerms() {
622
		$entity = $this->getNewEmpty();
623
624
		$entity->setLabel( 'en', 'foo' );
625
		$entity->setDescription( 'en', 'foo bar' );
626
		$entity->setAliases( 'en', [ 'foo', 'bar' ] );
627
628
		$this->assertEquals(
629
			new Fingerprint(
630
				new TermList( [
631
					new Term( 'en', 'foo' ),
632
				] ),
633
				new TermList( [
634
					new Term( 'en', 'foo bar' )
635
				] ),
636
				new AliasGroupList( [
637
					new AliasGroup( 'en', [ 'foo', 'bar' ] )
638
				] )
639
			),
640
			$entity->getFingerprint()
641
		);
642
	}
643
644
	public function testGivenEmptyFingerprint_noTermsAreSet() {
645
		$entity = $this->getNewEmpty();
646
		$entity->setFingerprint( new Fingerprint() );
647
648
		$this->assertTrue( $entity->getFingerprint()->isEmpty() );
649
	}
650
651
	public function testGivenEmptyFingerprint_existingTermsAreRemoved() {
652
		$entity = $this->getNewEmpty();
653
654
		$entity->setLabel( 'en', 'foo' );
655
		$entity->setDescription( 'en', 'foo bar' );
656
		$entity->setAliases( 'en', [ 'foo', 'bar' ] );
657
658
		$entity->setFingerprint( new Fingerprint() );
659
660
		$this->assertTrue( $entity->getFingerprint()->isEmpty() );
661
	}
662
663
	public function testWhenSettingFingerprint_getFingerprintReturnsIt() {
664
		$fingerprint = new Fingerprint(
665
			new TermList( [
666
				new Term( 'en', 'english label' ),
667
			] ),
668
			new TermList( [
669
				new Term( 'en', 'english description' )
670
			] ),
671
			new AliasGroupList( [
672
				new AliasGroup( 'en', [ 'first en alias', 'second en alias' ] )
673
			] )
674
		);
675
676
		$entity = $this->getNewEmpty();
677
		$entity->setFingerprint( $fingerprint );
678
		$newFingerprint = $entity->getFingerprint();
679
680
		$this->assertSame( $fingerprint, $newFingerprint );
681
	}
682
683
	public function testGetLabels() {
684
		$item = new Item();
685
		$item->setLabel( 'en', 'foo' );
686
687
		$this->assertEquals(
688
			new TermList( [
689
				new Term( 'en', 'foo' )
690
			] ),
691
			$item->getLabels()
692
		);
693
	}
694
695
	public function testGetDescriptions() {
696
		$item = new Item();
697
		$item->setDescription( 'en', 'foo bar' );
698
699
		$this->assertEquals(
700
			new TermList( [
701
				new Term( 'en', 'foo bar' )
702
			] ),
703
			$item->getDescriptions()
704
		);
705
	}
706
707
	public function testGetAliasGroups() {
708
		$item = new Item();
709
		$item->setAliases( 'en', [ 'foo', 'bar' ] );
710
711
		$this->assertEquals(
712
			new AliasGroupList( [
713
				new AliasGroup( 'en', [ 'foo', 'bar' ] )
714
			] ),
715
			$item->getAliasGroups()
716
		);
717
	}
718
719
	public function testGetLabels_sameListAsFingerprint() {
720
		$item = new Item();
721
722
		$this->assertSame(
723
			$item->getFingerprint()->getLabels(),
724
			$item->getLabels()
725
		);
726
	}
727
728
	public function testGetDescriptions_sameListAsFingerprint() {
729
		$item = new Item();
730
731
		$this->assertSame(
732
			$item->getFingerprint()->getDescriptions(),
733
			$item->getDescriptions()
734
		);
735
	}
736
737
	public function testGetAliasGroups_sameListAsFingerprint() {
738
		$item = new Item();
739
740
		$this->assertSame(
741
			$item->getFingerprint()->getAliasGroups(),
742
			$item->getAliasGroups()
743
		);
744
	}
745
746
	/**
747
	 * @dataProvider clearableProvider
748
	 */
749
	public function testClear( Item $item ) {
750
		$clone = $item->copy();
751
752
		$item->clear();
753
754
		$this->assertEquals( $clone->getId(), $item->getId(), 'cleared Item should keep its id' );
755
		$this->assertTrue( $item->isEmpty(), 'cleared Item should be empty' );
756
	}
757
758
	public function clearableProvider() {
759
		return [
760
			'empty' => [ new Item( new ItemId( 'Q23' ) ), ],
761
			'with fingerprint' => [
762
				new Item(
763
					new ItemId( 'Q42' ),
764
					new Fingerprint( new TermList( [ new Term( 'en', 'foo' ) ] ) )
765
				),
766
			],
767
			'with sitelink' => [
768
				new Item(
769
					new ItemId( 'Q123' ),
770
					null,
771
					new SiteLinkList( [ new SiteLink( 'enwiki', 'Wikidata' ) ] )
772
				)
773
			],
774
			'with statement' => [
775
				new Item(
776
					new ItemId( 'Q321' ),
777
					null,
778
					null,
779
					new StatementList( [ $this->newStatement() ] )
780
				)
781
			]
782
		];
783
	}
784
785
}
786