Passed
Push — itemGetSiteLinks ( a80808 )
by no
03:14
created

ItemTest::testGetSiteLinks()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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