Completed
Push — 7.x ( dc310f...f1ce8e )
by adam
02:55
created

ItemTest::clearableProvider()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
c 0
b 0
f 0
rs 8.8571
cc 1
eloc 18
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
	/**
121
	 * @dataProvider simpleSiteLinksProvider
122
	 */
123
	public function testGetSiteLinks() {
124
		$siteLinks = func_get_args();
125
		$item = new Item();
126
127
		foreach ( $siteLinks as $siteLink ) {
128
			$item->getSiteLinkList()->addSiteLink( $siteLink );
129
		}
130
131
		$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...
132
		$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...
133
	}
134
135
	public function simpleSiteLinksProvider() {
136
		$argLists = [];
137
138
		$argLists[] = [];
139
140
		$argLists[] = [ new SiteLink( 'enwiki', 'Wikidata', [ new ItemId( 'Q42' ) ] ) ];
141
142
		$argLists[] = [
143
			new SiteLink( 'enwiki', 'Wikidata' ),
144
			new SiteLink( 'nlwiki', 'Wikidata', [ new ItemId( 'Q3' ) ] )
145
		];
146
147
		$argLists[] = [
148
			new SiteLink( 'enwiki', 'Wikidata' ),
149
			new SiteLink( 'nlwiki', 'Wikidata' ),
150
			new SiteLink( 'foo bar', 'baz bah', [ new ItemId( 'Q2' ) ] )
151
		];
152
153
		return $argLists;
154
	}
155
156
	public function testHasLinkToSiteForFalse() {
157
		$item = new Item();
158
		$item->getSiteLinkList()->addNewSiteLink( 'ENWIKI', 'Wikidata', [ new ItemId( 'Q42' ) ] );
159
160
		$this->assertFalse( $item->getSiteLinkList()->hasLinkWithSiteId( 'enwiki' ) );
161
		$this->assertFalse( $item->getSiteLinkList()->hasLinkWithSiteId( 'dewiki' ) );
162
		$this->assertFalse( $item->getSiteLinkList()->hasLinkWithSiteId( 'foo bar' ) );
163
	}
164
165
	public function testHasLinkToSiteForTrue() {
166
		$item = new Item();
167
		$item->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Wikidata', [ new ItemId( 'Q42' ) ] );
168
		$item->getSiteLinkList()->addNewSiteLink( 'dewiki', 'Wikidata' );
169
		$item->getSiteLinkList()->addNewSiteLink( 'foo bar', 'Wikidata' );
170
171
		$this->assertTrue( $item->getSiteLinkList()->hasLinkWithSiteId( 'enwiki' ) );
172
		$this->assertTrue( $item->getSiteLinkList()->hasLinkWithSiteId( 'dewiki' ) );
173
		$this->assertTrue( $item->getSiteLinkList()->hasLinkWithSiteId( 'foo bar' ) );
174
	}
175
176
	public function testEmptyItemReturnsEmptySiteLinkList() {
177
		$item = new Item();
178
		$this->assertTrue( $item->getSiteLinkList()->isEmpty() );
179
	}
180
181
	public function testAddSiteLinkOverridesOldLinks() {
182
		$item = new Item();
183
184
		$item->getSiteLinkList()->addNewSiteLink( 'kittens', 'foo' );
185
186
		$newLink = new SiteLink( 'kittens', 'bar' );
187
		$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...
188
189
		$this->assertTrue( $item->getSiteLinkList()->getBySiteId( 'kittens' )->equals( $newLink ) );
190
	}
191
192
	public function testEmptyItemIsEmpty() {
193
		$item = new Item();
194
		$this->assertTrue( $item->isEmpty() );
195
	}
196
197
	public function testItemWithIdIsEmpty() {
198
		$item = new Item( new ItemId( 'Q1337' ) );
199
		$this->assertTrue( $item->isEmpty() );
200
	}
201
202
	public function testItemWithStuffIsNotEmpty() {
203
		$item = new Item();
204
		$item->setAliases( 'en', [ 'foo' ] );
205
		$this->assertFalse( $item->isEmpty() );
206
207
		$item = new Item();
208
		$item->getSiteLinkList()->addNewSiteLink( 'en', 'o_O' );
209
		$this->assertFalse( $item->isEmpty() );
210
211
		$item = new Item();
212
		$item->getStatements()->addStatement( $this->newStatement() );
213
		$this->assertFalse( $item->isEmpty() );
214
	}
215
216
	public function testItemWithSitelinksHasSitelinks() {
217
		$item = new Item();
218
		$item->getSiteLinkList()->addNewSiteLink( 'en', 'foo' );
219
		$this->assertFalse( $item->getSiteLinkList()->isEmpty() );
220
	}
221
222
	public function testItemWithoutSitelinksHasNoSitelinks() {
223
		$item = new Item();
224
		$this->assertTrue( $item->getSiteLinkList()->isEmpty() );
225
	}
226
227
	private function newStatement() {
228
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
229
		$statement->setGuid( 'kittens' );
230
		return $statement;
231
	}
232
233
	public function testEmptyConstructor() {
234
		$item = new Item();
235
236
		$this->assertNull( $item->getId() );
237
		$this->assertTrue( $item->getFingerprint()->isEmpty() );
238
		$this->assertTrue( $item->getLabels()->isEmpty() );
239
		$this->assertTrue( $item->getDescriptions()->isEmpty() );
240
		$this->assertTrue( $item->getAliasGroups()->isEmpty() );
241
		$this->assertTrue( $item->getSiteLinkList()->isEmpty() );
242
		$this->assertTrue( $item->getStatements()->isEmpty() );
243
	}
244
245
	public function testCanConstructWithStatementList() {
246
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
247
		$statement->setGuid( 'meh' );
248
249
		$statements = new StatementList( $statement );
250
251
		$item = new Item( null, null, null, $statements );
252
253
		$this->assertEquals(
254
			$statements,
255
			$item->getStatements()
256
		);
257
	}
258
259
	public function testSetStatements() {
260
		$item = new Item();
261
		$item->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) );
262
263
		$item->setStatements( new StatementList() );
264
		$this->assertTrue( $item->getStatements()->isEmpty() );
265
	}
266
267
	public function equalsProvider() {
268
		$firstItem = new Item();
269
		$firstItem->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) );
270
271
		$secondItem = new Item();
272
		$secondItem->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) );
273
274
		$secondItemWithId = $secondItem->copy();
275
		$secondItemWithId->setId( 42 );
276
277
		$differentId = $secondItemWithId->copy();
278
		$differentId->setId( 43 );
279
280
		return [
281
			[ new Item(), new Item() ],
282
			[ $firstItem, $secondItem ],
283
			[ $secondItem, $secondItemWithId ],
284
			[ $secondItemWithId, $differentId ],
285
		];
286
	}
287
288
	/**
289
	 * @dataProvider equalsProvider
290
	 */
291
	public function testEquals( Item $firstItem, Item $secondItem ) {
292
		$this->assertTrue( $firstItem->equals( $secondItem ) );
293
		$this->assertTrue( $secondItem->equals( $firstItem ) );
294
	}
295
296
	/**
297
	 * @return Item
298
	 */
299
	private function getBaseItem() {
300
		$item = new Item( new ItemId( 'Q42' ) );
301
		$item->setLabel( 'en', 'Same' );
302
		$item->setDescription( 'en', 'Same' );
303
		$item->setAliases( 'en', [ 'Same' ] );
304
		$item->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Same' );
305
		$item->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) );
306
307
		return $item;
308
	}
309
310
	public function notEqualsProvider() {
311
		$differentLabel = $this->getBaseItem();
312
		$differentLabel->setLabel( 'en', 'Different' );
313
314
		$differentDescription = $this->getBaseItem();
315
		$differentDescription->setDescription( 'en', 'Different' );
316
317
		$differentAlias = $this->getBaseItem();
318
		$differentAlias->setAliases( 'en', [ 'Different' ] );
319
320
		$differentSiteLink = $this->getBaseItem();
321
		$differentSiteLink->getSiteLinkList()->removeLinkWithSiteId( 'enwiki' );
322
		$differentSiteLink->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Different' );
323
324
		$differentStatement = $this->getBaseItem();
325
		$differentStatement->setStatements( new StatementList() );
326
		$differentStatement->getStatements()->addNewStatement( new PropertyNoValueSnak( 24 ) );
327
328
		$item = $this->getBaseItem();
329
330
		return [
331
			'empty' => [ $item, new Item() ],
332
			'label' => [ $item, $differentLabel ],
333
			'description' => [ $item, $differentDescription ],
334
			'alias' => [ $item, $differentAlias ],
335
			'siteLink' => [ $item, $differentSiteLink ],
336
			'statement' => [ $item, $differentStatement ],
337
		];
338
	}
339
340
	/**
341
	 * @dataProvider notEqualsProvider
342
	 */
343
	public function testNotEquals( Item $firstItem, Item $secondItem ) {
344
		$this->assertFalse( $firstItem->equals( $secondItem ) );
345
		$this->assertFalse( $secondItem->equals( $firstItem ) );
346
	}
347
348
	public function cloneProvider() {
349
		$item = new Item( new ItemId( 'Q1' ) );
350
		$item->setLabel( 'en', 'original' );
351
		$item->getStatements()->addNewStatement( new PropertyNoValueSnak( 1 ) );
352
		$item->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Original' );
353
354
		return [
355
			'copy' => [ $item, $item->copy() ],
356
			'native clone' => [ $item, clone $item ],
357
		];
358
	}
359
360
	/**
361
	 * @dataProvider cloneProvider
362
	 */
363
	public function testCloneIsEqualButNotIdentical( Item $original, Item $clone ) {
364
		$this->assertNotSame( $original, $clone );
365
		$this->assertTrue( $original->equals( $clone ) );
366
		$this->assertSame(
367
			$original->getId(),
368
			$clone->getId(),
369
			'id is immutable and must not be cloned'
370
		);
371
372
		// The clone must not reference the same mutable objects
373
		$this->assertNotSame( $original->getFingerprint(), $clone->getFingerprint() );
374
		$this->assertNotSame( $original->getStatements(), $clone->getStatements() );
375
		$this->assertNotSame(
376
			$original->getStatements()->getFirstStatementWithGuid( null ),
377
			$clone->getStatements()->getFirstStatementWithGuid( null )
378
		);
379
		$this->assertNotSame( $original->getSiteLinkList(), $clone->getSiteLinkList() );
380
		$this->assertSame(
381
			$original->getSiteLinkList()->getBySiteId( 'enwiki' ),
382
			$clone->getSiteLinkList()->getBySiteId( 'enwiki' ),
383
			'SiteLink is immutable and must not be cloned'
384
		);
385
	}
386
387
	/**
388
	 * @dataProvider cloneProvider
389
	 */
390
	public function testOriginalDoesNotChangeWithClone( Item $original, Item $clone ) {
391
		$originalStatement = $original->getStatements()->getFirstStatementWithGuid( null );
392
		$clonedStatement = $clone->getStatements()->getFirstStatementWithGuid( null );
393
394
		$clone->setLabel( 'en', 'clone' );
395
		$clone->setDescription( 'en', 'clone' );
396
		$clone->setAliases( 'en', [ 'clone' ] );
397
		$clonedStatement->setGuid( 'clone' );
398
		$clonedStatement->setMainSnak( new PropertySomeValueSnak( 666 ) );
399
		$clonedStatement->setRank( Statement::RANK_DEPRECATED );
400
		$clonedStatement->getQualifiers()->addSnak( new PropertyNoValueSnak( 1 ) );
401
		$clonedStatement->getReferences()->addNewReference( new PropertyNoValueSnak( 1 ) );
402
		$clone->getSiteLinkList()->removeLinkWithSiteId( 'enwiki' );
403
404
		$this->assertSame( 'original', $original->getFingerprint()->getLabel( 'en' )->getText() );
405
		$this->assertFalse( $original->getFingerprint()->hasDescription( 'en' ) );
406
		$this->assertFalse( $original->getFingerprint()->hasAliasGroup( 'en' ) );
407
		$this->assertNull( $originalStatement->getGuid() );
408
		$this->assertSame( 'novalue', $originalStatement->getMainSnak()->getType() );
409
		$this->assertSame( Statement::RANK_NORMAL, $originalStatement->getRank() );
410
		$this->assertTrue( $originalStatement->getQualifiers()->isEmpty() );
411
		$this->assertTrue( $originalStatement->getReferences()->isEmpty() );
412
		$this->assertFalse( $original->getSiteLinkList()->isEmpty() );
413
	}
414
415
	// Below are tests copied from EntityTest
416
417
	public function labelProvider() {
418
		return [
419
			[ 'en', 'spam' ],
420
			[ 'en', 'spam', 'spam' ],
421
			[ 'de', 'foo bar baz' ],
422
		];
423
	}
424
425
	/**
426
	 * @dataProvider labelProvider
427
	 * @param string $languageCode
428
	 * @param string $labelText
429
	 * @param string $moarText
430
	 */
431
	public function testSetLabel( $languageCode, $labelText, $moarText = 'ohi there' ) {
432
		$entity = $this->getNewEmpty();
433
434
		$entity->setLabel( $languageCode, $labelText );
435
436
		$this->assertSame( $labelText, $entity->getFingerprint()->getLabel( $languageCode )->getText() );
437
438
		$entity->setLabel( $languageCode, $moarText );
439
440
		$this->assertSame( $moarText, $entity->getFingerprint()->getLabel( $languageCode )->getText() );
441
	}
442
443
	public function descriptionProvider() {
444
		return [
445
			[ 'en', 'spam' ],
446
			[ 'en', 'spam', 'spam' ],
447
			[ 'de', 'foo bar baz' ],
448
		];
449
	}
450
451
	/**
452
	 * @dataProvider descriptionProvider
453
	 * @param string $languageCode
454
	 * @param string $description
455
	 * @param string $moarText
456
	 */
457
	public function testSetDescription( $languageCode, $description, $moarText = 'ohi there' ) {
458
		$entity = $this->getNewEmpty();
459
460
		$entity->setDescription( $languageCode, $description );
461
462
		$this->assertSame( $description, $entity->getFingerprint()->getDescription( $languageCode )->getText() );
463
464
		$entity->setDescription( $languageCode, $moarText );
465
466
		$this->assertSame( $moarText, $entity->getFingerprint()->getDescription( $languageCode )->getText() );
467
	}
468
469
	public function aliasesProvider() {
470
		return [
471
			[ [
472
				       'en' => [ [ 'spam' ] ]
473
			       ] ],
474
			[ [
475
				       'en' => [ [ 'foo', 'bar', 'baz' ] ]
476
			       ] ],
477
			[ [
478
				       'en' => [ [ 'foo', 'bar' ], [ 'baz', 'spam' ] ]
479
			       ] ],
480
			[ [
481
				       'en' => [ [ 'foo', 'bar', 'baz' ] ],
482
				       'de' => [ [ 'foobar' ], [ 'baz' ] ],
483
			       ] ],
484
			// with duplicates
485
			[ [
486
				       'en' => [ [ 'spam', 'ham', 'ham' ] ]
487
			       ] ],
488
			[ [
489
				       'en' => [ [ 'foo', 'bar' ], [ 'bar', 'spam' ] ]
490
			       ] ],
491
		];
492
	}
493
494
	/**
495
	 * @dataProvider aliasesProvider
496
	 */
497
	public function testSetAliases( array $aliasesLists ) {
498
		$entity = $this->getNewEmpty();
499
500
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
501
			foreach ( $aliasesList as $aliases ) {
502
				$entity->setAliases( $langCode, $aliases );
503
			}
504
		}
505
506
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
507
			$expected = array_values( array_unique( array_pop( $aliasesList ) ) );
508
			$actual = $entity->getFingerprint()->getAliasGroup( $langCode )->getAliases();
509
			$this->assertSame( $expected, $actual );
510
		}
511
	}
512
513
	public function testSetEmptyAlias() {
514
		$item = new Item();
515
516
		$item->setAliases( 'en', [ 'wind', 'air', '', 'fire' ] );
517
		$this->assertSame(
518
			[ 'wind', 'air', 'fire' ],
519
			$item->getAliasGroups()->getByLanguage( 'en' )->getAliases()
520
		);
521
522
		$item->setAliases( 'en', [ '', '' ] );
523
		$this->assertFalse( $item->getAliasGroups()->hasGroupForLanguage( 'en' ) );
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