Completed
Pull Request — master (#631)
by Bene
06:01 queued 03:03
created

ItemTest::labelProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
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->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->getLabel( $languageCode ) );
0 ignored issues
show
Bug introduced by
The method getLabel() does not seem to exist on object<Wikibase\DataModel\Entity\Item>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
453
454
		$entity->setLabel( $languageCode, $moarText );
455
456
		$this->assertEquals( $moarText, $entity->getLabel( $languageCode ) );
0 ignored issues
show
Bug introduced by
The method getLabel() does not seem to exist on object<Wikibase\DataModel\Entity\Item>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
457
	}
458
459
	/**
460
	 * @dataProvider labelProvider
461
	 * @param string $languageCode
462
	 * @param string $labelText
463
	 */
464
	public function testGetLabel( $languageCode, $labelText ) {
465
		$entity = $this->getNewEmpty();
466
467
		$this->assertFalse( $entity->getLabel( $languageCode ) );
0 ignored issues
show
Bug introduced by
The method getLabel() does not seem to exist on object<Wikibase\DataModel\Entity\Item>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
468
469
		$entity->setLabel( $languageCode, $labelText );
470
471
		$this->assertEquals( $labelText, $entity->getLabel( $languageCode ) );
0 ignored issues
show
Bug introduced by
The method getLabel() does not seem to exist on object<Wikibase\DataModel\Entity\Item>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
472
	}
473
474
	/**
475
	 * @dataProvider labelProvider
476
	 * @param string $languageCode
477
	 * @param string $labelText
478
	 */
479
	public function testRemoveLabel( $languageCode, $labelText ) {
480
		$entity = $this->getNewEmpty();
481
		$entity->setLabel( $languageCode, $labelText );
482
		$entity->removeLabel( $languageCode );
0 ignored issues
show
Bug introduced by
The method removeLabel() does not seem to exist on object<Wikibase\DataModel\Entity\Item>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
483
		$this->assertFalse( $entity->getLabel( $languageCode ) );
0 ignored issues
show
Bug introduced by
The method getLabel() does not seem to exist on object<Wikibase\DataModel\Entity\Item>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
484
	}
485
486
	public function descriptionProvider() {
487
		return array(
488
			array( 'en', 'spam' ),
489
			array( 'en', 'spam', 'spam' ),
490
			array( 'de', 'foo bar baz' ),
491
		);
492
	}
493
494
	/**
495
	 * @dataProvider descriptionProvider
496
	 * @param string $languageCode
497
	 * @param string $description
498
	 * @param string $moarText
499
	 */
500
	public function testSetDescription( $languageCode, $description, $moarText = 'ohi there' ) {
501
		$entity = $this->getNewEmpty();
502
503
		$entity->setDescription( $languageCode, $description );
504
505
		$this->assertEquals( $description, $entity->getDescription( $languageCode ) );
0 ignored issues
show
Bug introduced by
The method getDescription() does not seem to exist on object<Wikibase\DataModel\Entity\Item>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
506
507
		$entity->setDescription( $languageCode, $moarText );
508
509
		$this->assertEquals( $moarText, $entity->getDescription( $languageCode ) );
0 ignored issues
show
Bug introduced by
The method getDescription() does not seem to exist on object<Wikibase\DataModel\Entity\Item>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
510
	}
511
512
	/**
513
	 * @dataProvider descriptionProvider
514
	 * @param string $languageCode
515
	 * @param string $description
516
	 */
517
	public function testGetDescription( $languageCode, $description ) {
518
		$entity = $this->getNewEmpty();
519
520
		$this->assertFalse( $entity->getDescription( $languageCode ) );
0 ignored issues
show
Bug introduced by
The method getDescription() does not seem to exist on object<Wikibase\DataModel\Entity\Item>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
521
522
		$entity->setDescription( $languageCode, $description );
523
524
		$this->assertEquals( $description, $entity->getDescription( $languageCode ) );
0 ignored issues
show
Bug introduced by
The method getDescription() does not seem to exist on object<Wikibase\DataModel\Entity\Item>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
525
	}
526
527
	/**
528
	 * @dataProvider descriptionProvider
529
	 * @param string $languageCode
530
	 * @param string $description
531
	 */
532
	public function testRemoveDescription( $languageCode, $description ) {
533
		$entity = $this->getNewEmpty();
534
		$entity->setDescription( $languageCode, $description );
535
		$entity->removeDescription( $languageCode );
0 ignored issues
show
Bug introduced by
The method removeDescription() does not seem to exist on object<Wikibase\DataModel\Entity\Item>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
536
		$this->assertFalse( $entity->getDescription( $languageCode ) );
0 ignored issues
show
Bug introduced by
The method getDescription() does not seem to exist on object<Wikibase\DataModel\Entity\Item>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
537
	}
538
539
	public function aliasesProvider() {
540
		return array(
541
			array( array(
542
				       'en' => array( array( 'spam' ) )
543
			       ) ),
544
			array( array(
545
				       'en' => array( array( 'foo', 'bar', 'baz' ) )
546
			       ) ),
547
			array( array(
548
				       'en' => array( array( 'foo', 'bar' ), array( 'baz', 'spam' ) )
549
			       ) ),
550
			array( array(
551
				       'en' => array( array( 'foo', 'bar', 'baz' ) ),
552
				       'de' => array( array( 'foobar' ), array( 'baz' ) ),
553
			       ) ),
554
			// with duplicates
555
			array( array(
556
				       'en' => array( array( 'spam', 'ham', 'ham' ) )
557
			       ) ),
558
			array( array(
559
				       'en' => array( array( 'foo', 'bar' ), array( 'bar', 'spam' ) )
560
			       ) ),
561
		);
562
	}
563
564
	/**
565
	 * @dataProvider aliasesProvider
566
	 */
567
	public function testAddAliases( array $aliasesLists ) {
568
		$entity = $this->getNewEmpty();
569
570
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
571
			foreach ( $aliasesList as $aliases ) {
572
				$entity->addAliases( $langCode, $aliases );
0 ignored issues
show
Bug introduced by
The method addAliases() does not seem to exist on object<Wikibase\DataModel\Entity\Item>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
573
			}
574
		}
575
576
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
577
			$expected = array_values( array_unique( call_user_func_array( 'array_merge', $aliasesList ) ) );
578
			asort( $expected );
579
580
			$actual = $entity->getAliases( $langCode );
0 ignored issues
show
Bug introduced by
The method getAliases() does not seem to exist on object<Wikibase\DataModel\Entity\Item>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
581
			asort( $actual );
582
583
			$this->assertEquals( $expected, $actual );
584
		}
585
	}
586
587
	/**
588
	 * @dataProvider aliasesProvider
589
	 */
590
	public function testSetAliases( array $aliasesLists ) {
591
		$entity = $this->getNewEmpty();
592
593
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
594
			foreach ( $aliasesList as $aliases ) {
595
				$entity->setAliases( $langCode, $aliases );
596
			}
597
		}
598
599
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
600
			$expected = array_values( array_unique( array_pop( $aliasesList ) ) );
601
			asort( $aliasesList );
602
603
			$actual = $entity->getAliases( $langCode );
0 ignored issues
show
Bug introduced by
The method getAliases() does not seem to exist on object<Wikibase\DataModel\Entity\Item>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
604
			asort( $actual );
605
606
			$this->assertEquals( $expected, $actual );
607
		}
608
	}
609
610
	/**
611
	 * @dataProvider aliasesProvider
612
	 */
613
	public function testSetEmptyAlias( array $aliasesLists ) {
614
		$entity = $this->getNewEmpty();
615
616
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
617
			foreach ( $aliasesList as $aliases ) {
618
				$entity->setAliases( $langCode, $aliases );
619
			}
620
		}
621
		$entity->setAliases( 'zh', array( 'wind', 'air', '', 'fire' ) );
622
		$entity->setAliases( 'zu', array( '', '' ) );
623
624
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
625
			$expected = array_values( array_unique( array_pop( $aliasesList ) ) );
626
			asort( $aliasesList );
627
628
			$actual = $entity->getAliases( $langCode );
0 ignored issues
show
Bug introduced by
The method getAliases() does not seem to exist on object<Wikibase\DataModel\Entity\Item>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
629
			asort( $actual );
630
631
			$this->assertEquals( $expected, $actual );
632
		}
633
	}
634
635
	/**
636
	 * @dataProvider aliasesProvider
637
	 */
638
	public function testSetAllAliases( array $aliasGroups ) {
639
		$entity = $this->getNewEmpty();
640
		$entity->addAliases( 'zh', array( 'qwertyuiop123', '321poiuytrewq' ) );
0 ignored issues
show
Bug introduced by
The method addAliases() does not seem to exist on object<Wikibase\DataModel\Entity\Item>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
641
642
		$aliasesToSet = array();
643
		foreach ( $aliasGroups as $langCode => $aliasGroup ) {
644
			foreach ( $aliasGroup as $aliases ) {
645
				$aliasesToSet[$langCode] = $aliases;
646
			}
647
		}
648
649
		$entity->setAllAliases( $aliasesToSet );
0 ignored issues
show
Bug introduced by
The method setAllAliases() does not seem to exist on object<Wikibase\DataModel\Entity\Item>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
650
651
		foreach ( $aliasGroups as $langCode => $aliasGroup ) {
652
			$expected = array_values( array_unique( array_pop( $aliasGroup ) ) );
653
			asort( $aliasGroup );
654
655
			$actual = $entity->getFingerprint()->getAliasGroups()->getByLanguage( $langCode )->getAliases();
656
			asort( $actual );
657
658
			$this->assertEquals( $expected, $actual );
659
		}
660
661
		/** @var AliasGroup $aliasGroup */
662
		foreach ( $entity->getFingerprint()->getAliasGroups() as $langCode => $aliasGroup ) {
663
			$this->assertEquals( $aliasGroup->getAliases(), array_unique( $aliasesToSet[$langCode] ) );
664
		}
665
	}
666
667
	public function testGetAliases() {
668
		$entity = $this->getNewEmpty();
669
		$aliases = array( 'a', 'b' );
670
671
		$entity->getFingerprint()->setAliasGroup( 'en', $aliases );
672
673
		$this->assertEquals(
674
			$aliases,
675
			$entity->getAliases( 'en' )
0 ignored issues
show
Bug introduced by
The method getAliases() does not seem to exist on object<Wikibase\DataModel\Entity\Item>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
676
		);
677
	}
678
679
	public function duplicateAliasesProvider() {
680
		return array(
681
			array( array(
682
				       'en' => array( array( 'foo', 'bar', 'baz' ), array( 'foo', 'bar', 'baz' ) )
683
			       ) ),
684
			array( array(
685
				       'en' => array( array( 'foo', 'bar', 'baz' ), array( 'foo', 'bar' ) )
686
			       ) ),
687
			array( array(
688
				       'en' => array( array( 'foo', 'bar' ), array( 'foo', 'bar', 'baz' ) )
689
			       ) ),
690
			array( array(
691
				       'en' => array( array( 'foo', 'bar' ), array( 'bar', 'baz' ) ),
692
				       'de' => array( array(), array( 'foo' ) ),
693
				       'nl' => array( array( 'foo' ), array() ),
694
			       ) ),
695
			array( array(
696
				       'en' => array( array( 'foo', 'bar', 'baz' ), array( 'foo', 'bar', 'baz', 'foo', 'bar' ) )
697
			       ) ),
698
		);
699
	}
700
701
	/**
702
	 * @dataProvider duplicateAliasesProvider
703
	 */
704
	public function testRemoveAliases( array $aliasesLists ) {
705
		$entity = $this->getNewEmpty();
706
707
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
708
			$aliases = array_shift( $aliasesList );
709
			$removedAliases = array_shift( $aliasesList );
710
711
			$entity->setAliases( $langCode, $aliases );
712
			$entity->removeAliases( $langCode, $removedAliases );
0 ignored issues
show
Bug introduced by
The method removeAliases() does not seem to exist on object<Wikibase\DataModel\Entity\Item>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
713
714
			$expected = array_values( array_diff( $aliases, $removedAliases ) );
715
			$actual = $entity->getAliases( $langCode );
0 ignored issues
show
Bug introduced by
The method getAliases() does not seem to exist on object<Wikibase\DataModel\Entity\Item>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
716
717
			asort( $expected );
718
			asort( $actual );
719
720
			$this->assertEquals( $expected, $actual );
721
		}
722
	}
723
724
	public function instanceProvider() {
725
		$entities = array();
726
727
		// empty
728
		$entity = $this->getNewEmpty();
729
		$entities[] = $entity;
730
731
		// ID only
732
		$entity = clone $entity;
733
		$entity->setId( 44 );
734
735
		$entities[] = $entity;
736
737
		// with labels and stuff
738
		$entity = $this->getNewEmpty();
739
		$entity->setAliases( 'en', array( 'o', 'noez' ) );
740
		$entity->setLabel( 'de', 'spam' );
741
		$entity->setDescription( 'en', 'foo bar baz' );
742
743
		$entities[] = $entity;
744
745
		// with labels etc and ID
746
		$entity = clone $entity;
747
		$entity->setId( 42 );
748
749
		$entities[] = $entity;
750
751
		$argLists = array();
752
753
		foreach ( $entities as $entity ) {
754
			$argLists[] = array( $entity );
755
		}
756
757
		return $argLists;
758
	}
759
760
	/**
761
	 * @dataProvider instanceProvider
762
	 * @param Item $entity
763
	 */
764
	public function testCopy( Item $entity ) {
765
		$copy = $entity->copy();
766
767
		// The equality method alone is not enough since it does not check the IDs.
768
		$this->assertTrue( $entity->equals( $copy ) );
769
		$this->assertEquals( $entity->getId(), $copy->getId() );
770
771
		$this->assertNotSame( $entity, $copy );
772
	}
773
774
	public function testCopyRetainsLabels() {
775
		$item = new Item();
776
777
		$item->getFingerprint()->setLabel( 'en', 'foo' );
778
		$item->getFingerprint()->setLabel( 'de', 'bar' );
779
780
		$newItem = $item->copy();
781
782
		$this->assertTrue( $newItem->getFingerprint()->getLabels()->hasTermForLanguage( 'en' ) );
783
		$this->assertTrue( $newItem->getFingerprint()->getLabels()->hasTermForLanguage( 'de' ) );
784
	}
785
786
	/**
787
	 * @dataProvider instanceProvider
788
	 * @param Item $entity
789
	 */
790
	public function testSerialize( Item $entity ) {
791
		$string = serialize( $entity );
792
793
		$this->assertInternalType( 'string', $string );
794
795
		$instance = unserialize( $string );
796
797
		$this->assertTrue( $entity->equals( $instance ) );
798
		$this->assertEquals( $entity->getId(), $instance->getId() );
799
	}
800
801
	public function testWhenNoStuffIsSet_getFingerprintReturnsEmptyFingerprint() {
802
		$entity = $this->getNewEmpty();
803
804
		$this->assertEquals(
805
			new Fingerprint(),
806
			$entity->getFingerprint()
807
		);
808
	}
809
810
	public function testWhenLabelsAreSet_getFingerprintReturnsFingerprintWithLabels() {
811
		$entity = $this->getNewEmpty();
812
813
		$entity->setLabel( 'en', 'foo' );
814
		$entity->setLabel( 'de', 'bar' );
815
816
		$this->assertEquals(
817
			new Fingerprint(
818
				new TermList( array(
819
					new Term( 'en', 'foo' ),
820
					new Term( 'de', 'bar' ),
821
				) )
822
			),
823
			$entity->getFingerprint()
824
		);
825
	}
826
827
	public function testWhenTermsAreSet_getFingerprintReturnsFingerprintWithTerms() {
828
		$entity = $this->getNewEmpty();
829
830
		$entity->setLabel( 'en', 'foo' );
831
		$entity->setDescription( 'en', 'foo bar' );
832
		$entity->setAliases( 'en', array( 'foo', 'bar' ) );
833
834
		$this->assertEquals(
835
			new Fingerprint(
836
				new TermList( array(
837
					new Term( 'en', 'foo' ),
838
				) ),
839
				new TermList( array(
840
					new Term( 'en', 'foo bar' )
841
				) ),
842
				new AliasGroupList( array(
843
					new AliasGroup( 'en', array( 'foo', 'bar' ) )
844
				) )
845
			),
846
			$entity->getFingerprint()
847
		);
848
	}
849
850
	public function testGivenEmptyFingerprint_noTermsAreSet() {
851
		$entity = $this->getNewEmpty();
852
		$entity->setFingerprint( new Fingerprint() );
853
854
		$this->assertHasNoTerms( $entity );
855
	}
856
857
	private function assertHasNoTerms( Item $entity ) {
858
		$this->assertEquals( array(), $entity->getLabels() );
0 ignored issues
show
Bug introduced by
The method getLabels() does not seem to exist on object<Wikibase\DataModel\Entity\Item>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
859
		$this->assertEquals( array(), $entity->getDescriptions() );
0 ignored issues
show
Bug introduced by
The method getDescriptions() does not seem to exist on object<Wikibase\DataModel\Entity\Item>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
860
		$this->assertEquals( array(), $entity->getAllAliases() );
0 ignored issues
show
Bug introduced by
The method getAllAliases() does not seem to exist on object<Wikibase\DataModel\Entity\Item>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
861
	}
862
863
	public function testGivenEmptyFingerprint_existingTermsAreRemoved() {
864
		$entity = $this->getNewEmpty();
865
866
		$entity->setLabel( 'en', 'foo' );
867
		$entity->setDescription( 'en', 'foo bar' );
868
		$entity->setAliases( 'en', array( 'foo', 'bar' ) );
869
870
		$entity->setFingerprint( new Fingerprint() );
871
872
		$this->assertHasNoTerms( $entity );
873
	}
874
875
	public function testWhenSettingFingerprint_getFingerprintReturnsIt() {
876
		$fingerprint = new Fingerprint(
877
			new TermList( array(
878
				new Term( 'en', 'english label' ),
879
			) ),
880
			new TermList( array(
881
				new Term( 'en', 'english description' )
882
			) ),
883
			new AliasGroupList( array(
884
				new AliasGroup( 'en', array( 'first en alias', 'second en alias' ) )
885
			) )
886
		);
887
888
		$entity = $this->getNewEmpty();
889
		$entity->setFingerprint( $fingerprint );
890
		$newFingerprint = $entity->getFingerprint();
891
892
		$this->assertEquals( $fingerprint, $newFingerprint );
893
	}
894
895
}
896