ItemTest::clearableProvider()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

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