Passed
Push — int32EntityId ( fa80fb )
by no
05:13
created

ItemTest::testItemWithStuffIsNotEmpty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

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