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