Passed
Push — micro-opti-entityid-stuffz ( ca1ae8...81f418 )
by Leszek
03:49
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 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
	/**
514
	 * @dataProvider aliasesProvider
515
	 */
516
	public function testSetEmptyAlias( array $aliasesLists ) {
517
		$entity = $this->getNewEmpty();
518
519
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
520
			foreach ( $aliasesList as $aliases ) {
521
				$entity->setAliases( $langCode, $aliases );
522
			}
523
		}
524
		$entity->setAliases( 'zh', [ 'wind', 'air', '', 'fire' ] );
525
		$entity->setAliases( 'zu', [ '', '' ] );
526
527
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
528
			$expected = array_values( array_unique( array_pop( $aliasesList ) ) );
529
			asort( $aliasesList );
530
531
			$actual = $entity->getFingerprint()->getAliasGroup( $langCode )->getAliases();
532
			asort( $actual );
533
534
			$this->assertEquals( $expected, $actual );
535
		}
536
	}
537
538
	public function instanceProvider() {
539
		$entities = [];
540
541
		// empty
542
		$entity = $this->getNewEmpty();
543
		$entities[] = $entity;
544
545
		// ID only
546
		$entity = clone $entity;
547
		$entity->setId( 44 );
548
549
		$entities[] = $entity;
550
551
		// with labels and stuff
552
		$entity = $this->getNewEmpty();
553
		$entity->setAliases( 'en', [ 'o', 'noez' ] );
554
		$entity->setLabel( 'de', 'spam' );
555
		$entity->setDescription( 'en', 'foo bar baz' );
556
557
		$entities[] = $entity;
558
559
		// with labels etc and ID
560
		$entity = clone $entity;
561
		$entity->setId( 42 );
562
563
		$entities[] = $entity;
564
565
		$argLists = [];
566
567
		foreach ( $entities as $entity ) {
568
			$argLists[] = [ $entity ];
569
		}
570
571
		return $argLists;
572
	}
573
574
	/**
575
	 * @dataProvider instanceProvider
576
	 * @param Item $entity
577
	 */
578
	public function testCopy( Item $entity ) {
579
		$copy = $entity->copy();
580
581
		// The equality method alone is not enough since it does not check the IDs.
582
		$this->assertTrue( $entity->equals( $copy ) );
583
		$this->assertEquals( $entity->getId(), $copy->getId() );
584
585
		$this->assertNotSame( $entity, $copy );
586
	}
587
588
	public function testCopyRetainsLabels() {
589
		$item = new Item();
590
591
		$item->getFingerprint()->setLabel( 'en', 'foo' );
592
		$item->getFingerprint()->setLabel( 'de', 'bar' );
593
594
		$newItem = $item->copy();
595
596
		$this->assertTrue( $newItem->getFingerprint()->getLabels()->hasTermForLanguage( 'en' ) );
597
		$this->assertTrue( $newItem->getFingerprint()->getLabels()->hasTermForLanguage( 'de' ) );
598
	}
599
600
	/**
601
	 * @dataProvider instanceProvider
602
	 * @param Item $entity
603
	 */
604
	public function testSerialize( Item $entity ) {
605
		$string = serialize( $entity );
606
607
		$this->assertInternalType( 'string', $string );
608
609
		$instance = unserialize( $string );
610
611
		$this->assertTrue( $entity->equals( $instance ) );
612
		$this->assertEquals( $entity->getId(), $instance->getId() );
613
	}
614
615
	public function testWhenNoStuffIsSet_getFingerprintReturnsEmptyFingerprint() {
616
		$entity = $this->getNewEmpty();
617
618
		$this->assertEquals(
619
			new Fingerprint(),
620
			$entity->getFingerprint()
621
		);
622
	}
623
624
	public function testWhenLabelsAreSet_getFingerprintReturnsFingerprintWithLabels() {
625
		$entity = $this->getNewEmpty();
626
627
		$entity->setLabel( 'en', 'foo' );
628
		$entity->setLabel( 'de', 'bar' );
629
630
		$this->assertEquals(
631
			new Fingerprint(
632
				new TermList( [
633
					new Term( 'en', 'foo' ),
634
					new Term( 'de', 'bar' ),
635
				] )
636
			),
637
			$entity->getFingerprint()
638
		);
639
	}
640
641
	public function testWhenTermsAreSet_getFingerprintReturnsFingerprintWithTerms() {
642
		$entity = $this->getNewEmpty();
643
644
		$entity->setLabel( 'en', 'foo' );
645
		$entity->setDescription( 'en', 'foo bar' );
646
		$entity->setAliases( 'en', [ 'foo', 'bar' ] );
647
648
		$this->assertEquals(
649
			new Fingerprint(
650
				new TermList( [
651
					new Term( 'en', 'foo' ),
652
				] ),
653
				new TermList( [
654
					new Term( 'en', 'foo bar' )
655
				] ),
656
				new AliasGroupList( [
657
					new AliasGroup( 'en', [ 'foo', 'bar' ] )
658
				] )
659
			),
660
			$entity->getFingerprint()
661
		);
662
	}
663
664
	public function testGivenEmptyFingerprint_noTermsAreSet() {
665
		$entity = $this->getNewEmpty();
666
		$entity->setFingerprint( new Fingerprint() );
667
668
		$this->assertTrue( $entity->getFingerprint()->isEmpty() );
669
	}
670
671
	public function testGivenEmptyFingerprint_existingTermsAreRemoved() {
672
		$entity = $this->getNewEmpty();
673
674
		$entity->setLabel( 'en', 'foo' );
675
		$entity->setDescription( 'en', 'foo bar' );
676
		$entity->setAliases( 'en', [ 'foo', 'bar' ] );
677
678
		$entity->setFingerprint( new Fingerprint() );
679
680
		$this->assertTrue( $entity->getFingerprint()->isEmpty() );
681
	}
682
683
	public function testWhenSettingFingerprint_getFingerprintReturnsIt() {
684
		$fingerprint = new Fingerprint(
685
			new TermList( [
686
				new Term( 'en', 'english label' ),
687
			] ),
688
			new TermList( [
689
				new Term( 'en', 'english description' )
690
			] ),
691
			new AliasGroupList( [
692
				new AliasGroup( 'en', [ 'first en alias', 'second en alias' ] )
693
			] )
694
		);
695
696
		$entity = $this->getNewEmpty();
697
		$entity->setFingerprint( $fingerprint );
698
		$newFingerprint = $entity->getFingerprint();
699
700
		$this->assertSame( $fingerprint, $newFingerprint );
701
	}
702
703
	public function testGetLabels() {
704
		$item = new Item();
705
		$item->setLabel( 'en', 'foo' );
706
707
		$this->assertEquals(
708
			new TermList( [
709
				new Term( 'en', 'foo' )
710
			] ),
711
			$item->getLabels()
712
		);
713
	}
714
715
	public function testGetDescriptions() {
716
		$item = new Item();
717
		$item->setDescription( 'en', 'foo bar' );
718
719
		$this->assertEquals(
720
			new TermList( [
721
				new Term( 'en', 'foo bar' )
722
			] ),
723
			$item->getDescriptions()
724
		);
725
	}
726
727
	public function testGetAliasGroups() {
728
		$item = new Item();
729
		$item->setAliases( 'en', [ 'foo', 'bar' ] );
730
731
		$this->assertEquals(
732
			new AliasGroupList( [
733
				new AliasGroup( 'en', [ 'foo', 'bar' ] )
734
			] ),
735
			$item->getAliasGroups()
736
		);
737
	}
738
739
	public function testGetLabels_sameListAsFingerprint() {
740
		$item = new Item();
741
742
		$this->assertSame(
743
			$item->getFingerprint()->getLabels(),
744
			$item->getLabels()
745
		);
746
	}
747
748
	public function testGetDescriptions_sameListAsFingerprint() {
749
		$item = new Item();
750
751
		$this->assertSame(
752
			$item->getFingerprint()->getDescriptions(),
753
			$item->getDescriptions()
754
		);
755
	}
756
757
	public function testGetAliasGroups_sameListAsFingerprint() {
758
		$item = new Item();
759
760
		$this->assertSame(
761
			$item->getFingerprint()->getAliasGroups(),
762
			$item->getAliasGroups()
763
		);
764
	}
765
766
}
767