Passed
Push — entity-clear ( 1a048b...ca6eb6 )
by Bene
03:03
created

ItemTest::testEquals()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 2
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 WikibaseItem
24
 * @group WikibaseDataModel
25
 * @group WikibaseItemTest
26
 *
27
 * @licence GNU GPL v2+
28
 * @author Jeroen De Dauw < [email protected] >
29
 * @author John Erling Blad < [email protected] >
30
 * @author Michał Łazowik
31
 */
32
class ItemTest extends PHPUnit_Framework_TestCase {
33
34
	private function getNewEmpty() {
35
		return new Item();
36
	}
37
38
	public function testGetId() {
39
		$item = new Item();
40
		$this->assertNull( $item->getId() );
41
42
		$item->setId( new ItemId( 'Q1' ) );
43
		$this->assertEquals( new ItemId( 'Q1' ), $item->getId() );
44
45
		$item->setId( null );
46
		$this->assertNull( $item->getId() );
47
48
		$item = new Item( new ItemId( 'Q2' ) );
49
		$this->assertEquals( new ItemId( 'Q2' ), $item->getId() );
50
	}
51
52
	public function testSetIdUsingNumber() {
53
		$item = new Item();
54
		$item->setId( 42 );
55
		$this->assertEquals( new ItemId( 'Q42' ), $item->getId() );
56
	}
57
58
	public function testGetSiteLinkWithNonSetSiteId() {
59
		$item = new Item();
60
61
		$this->setExpectedException( 'OutOfBoundsException' );
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

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