Test Failed
Push — clear ( 90ec73...ed29c3 )
by no
04:02
created

ItemTest::testClearRemovesAllButId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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