Passed
Push — item-property-terms-provider ( 0a5dfb...058b7f )
by Bene
03:02
created

testWhenTermsAreSet_getFingerprintReturnsFingerprintWithTerms()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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