Passed
Push — currentLimits ( af2764...193e29 )
by no
03:56
created

ItemTest::simpleSiteLinksProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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