Completed
Push — master ( 029489...c23678 )
by Daniel
34s
created

ItemTest::testGetAliases()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 7
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
 * @covers Wikibase\DataModel\Entity\Entity
22
 *
23
 * @group Wikibase
24
 * @group WikibaseItem
25
 * @group WikibaseDataModel
26
 * @group WikibaseItemTest
27
 *
28
 * @licence GNU GPL v2+
29
 * @author Jeroen De Dauw < [email protected] >
30
 * @author John Erling Blad < [email protected] >
31
 * @author Michał Łazowik
32
 */
33
class ItemTest extends PHPUnit_Framework_TestCase {
34
35
	private function getNewEmpty() {
36
		return new Item();
37
	}
38
39
	public function testGetId() {
40
		$item = new Item();
41
		$this->assertNull( $item->getId() );
42
43
		$item->setId( new ItemId( 'Q1' ) );
44
		$this->assertEquals( new ItemId( 'Q1' ), $item->getId() );
45
46
		$item->setId( null );
47
		$this->assertNull( $item->getId() );
48
49
		$item = new Item( new ItemId( 'Q2' ) );
50
		$this->assertEquals( new ItemId( 'Q2' ), $item->getId() );
51
	}
52
53
	public function testSetIdUsingNumber() {
54
		$item = new Item();
55
		$item->setId( 42 );
56
		$this->assertEquals( new ItemId( 'Q42' ), $item->getId() );
57
	}
58
59
	public function testGetSiteLinkWithNonSetSiteId() {
60
		$item = new Item();
61
62
		$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...
63
		$item->getSiteLinkList()->getBySiteId( 'enwiki' );
64
	}
65
66
	/**
67
	 * @dataProvider simpleSiteLinkProvider
68
	 */
69
	public function testAddSiteLink( SiteLink $siteLink ) {
70
		$item = new Item();
71
72
		$item->getSiteLinkList()->addSiteLink( $siteLink );
73
74
		$this->assertEquals(
75
			$siteLink,
76
			$item->getSiteLinkList()->getBySiteId( $siteLink->getSiteId() )
77
		);
78
	}
79
80
	public function simpleSiteLinkProvider() {
81
		$argLists = array();
82
83
		$argLists[] = array(
84
			new SiteLink(
85
				'enwiki',
86
				'Wikidata',
87
				array(
88
					new ItemId( 'Q42' )
89
				)
90
			)
91
		);
92
		$argLists[] = array(
93
			new SiteLink(
94
				'nlwiki',
95
				'Wikidata'
96
			)
97
		);
98
		$argLists[] = array(
99
			new SiteLink(
100
				'enwiki',
101
				'Nyan!',
102
				array(
103
					new ItemId( 'Q42' ),
104
					new ItemId( 'Q149' )
105
				)
106
			)
107
		);
108
		$argLists[] = array(
109
			new SiteLink(
110
				'foo bar',
111
				'baz bah',
112
				array(
113
					new ItemId( 'Q3' ),
114
					new ItemId( 'Q7' )
115
				)
116
			)
117
		);
118
119
		return $argLists;
120
	}
121
122
	/**
123
	 * @dataProvider simpleSiteLinksProvider
124
	 */
125
	public function testGetSiteLinks() {
126
		$siteLinks = func_get_args();
127
		$item = new Item();
128
129
		foreach ( $siteLinks as $siteLink ) {
130
			$item->getSiteLinkList()->addSiteLink( $siteLink );
131
		}
132
133
		$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...
134
		$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...
135
	}
136
137
	public function simpleSiteLinksProvider() {
138
		$argLists = array();
139
140
		$argLists[] = array();
141
142
		$argLists[] = array( new SiteLink( 'enwiki', 'Wikidata', array( new ItemId( 'Q42' ) ) ) );
143
144
		$argLists[] = array(
145
			new SiteLink( 'enwiki', 'Wikidata' ),
146
			new SiteLink( 'nlwiki', 'Wikidata', array( new ItemId( 'Q3' ) ) )
147
		);
148
149
		$argLists[] = array(
150
			new SiteLink( 'enwiki', 'Wikidata' ),
151
			new SiteLink( 'nlwiki', 'Wikidata' ),
152
			new SiteLink( 'foo bar', 'baz bah', array( new ItemId( 'Q2' ) ) )
153
		);
154
155
		return $argLists;
156
	}
157
158
	public function testHasLinkToSiteForFalse() {
159
		$item = new Item();
160
		$item->getSiteLinkList()->addNewSiteLink( 'ENWIKI', 'Wikidata', array( new ItemId( 'Q42' ) ) );
161
162
		$this->assertFalse( $item->getSiteLinkList()->hasLinkWithSiteId( 'enwiki' ) );
163
		$this->assertFalse( $item->getSiteLinkList()->hasLinkWithSiteId( 'dewiki' ) );
164
		$this->assertFalse( $item->getSiteLinkList()->hasLinkWithSiteId( 'foo bar' ) );
165
	}
166
167
	public function testHasLinkToSiteForTrue() {
168
		$item = new Item();
169
		$item->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Wikidata', array( new ItemId( 'Q42' ) ) );
170
		$item->getSiteLinkList()->addNewSiteLink( 'dewiki', 'Wikidata' );
171
		$item->getSiteLinkList()->addNewSiteLink( 'foo bar', 'Wikidata' );
172
173
		$this->assertTrue( $item->getSiteLinkList()->hasLinkWithSiteId( 'enwiki' ) );
174
		$this->assertTrue( $item->getSiteLinkList()->hasLinkWithSiteId( 'dewiki' ) );
175
		$this->assertTrue( $item->getSiteLinkList()->hasLinkWithSiteId( 'foo bar' ) );
176
	}
177
178
	public function testEmptyItemReturnsEmptySiteLinkList() {
179
		$item = new Item();
180
		$this->assertTrue( $item->getSiteLinkList()->isEmpty() );
181
	}
182
183
	public function testAddSiteLinkOverridesOldLinks() {
184
		$item = new Item();
185
186
		$item->getSiteLinkList()->addNewSiteLink( 'kittens', 'foo' );
187
188
		$newLink = new SiteLink( 'kittens', 'bar' );
189
		$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...
190
191
		$this->assertTrue( $item->getSiteLinkList()->getBySiteId( 'kittens' )->equals( $newLink ) );
192
	}
193
194
	public function testEmptyItemIsEmpty() {
195
		$item = new Item();
196
		$this->assertTrue( $item->isEmpty() );
197
	}
198
199
	public function testItemWithIdIsEmpty() {
200
		$item = new Item( new ItemId( 'Q1337' ) );
201
		$this->assertTrue( $item->isEmpty() );
202
	}
203
204
	public function testItemWithStuffIsNotEmpty() {
205
		$item = new Item();
206
		$item->getFingerprint()->setAliasGroup( 'en', array( 'foo' ) );
207
		$this->assertFalse( $item->isEmpty() );
208
209
		$item = new Item();
210
		$item->getSiteLinkList()->addNewSiteLink( 'en', 'o_O' );
211
		$this->assertFalse( $item->isEmpty() );
212
213
		$item = new Item();
214
		$item->getStatements()->addStatement( $this->newStatement() );
215
		$this->assertFalse( $item->isEmpty() );
216
	}
217
218
	public function testItemWithSitelinksHasSitelinks() {
219
		$item = new Item();
220
		$item->getSiteLinkList()->addNewSiteLink( 'en', 'foo' );
221
		$this->assertFalse( $item->getSiteLinkList()->isEmpty() );
222
	}
223
224
	public function testItemWithoutSitelinksHasNoSitelinks() {
225
		$item = new Item();
226
		$this->assertTrue( $item->getSiteLinkList()->isEmpty() );
227
	}
228
229
	private function newStatement() {
230
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
231
		$statement->setGuid( 'kittens' );
232
		return $statement;
233
	}
234
235
	public function testClearRemovesAllButId() {
236
		$item = new Item( new ItemId( 'Q42' ) );
237
		$item->getFingerprint()->setLabel( 'en', 'foo' );
238
		$item->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Foo' );
239
		$item->getStatements()->addStatement( $this->newStatement() );
240
241
		$item->clear();
242
243
		$this->assertEquals( new ItemId( 'Q42' ), $item->getId() );
244
		$this->assertTrue( $item->getFingerprint()->isEmpty() );
245
		$this->assertTrue( $item->getSiteLinkList()->isEmpty() );
246
		$this->assertTrue( $item->getStatements()->isEmpty() );
247
	}
248
249
	public function testEmptyConstructor() {
250
		$item = new Item();
251
252
		$this->assertNull( $item->getId() );
253
		$this->assertTrue( $item->getFingerprint()->isEmpty() );
254
		$this->assertTrue( $item->getSiteLinkList()->isEmpty() );
255
		$this->assertTrue( $item->getStatements()->isEmpty() );
256
	}
257
258
	public function testCanConstructWithStatementList() {
259
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
260
		$statement->setGuid( 'meh' );
261
262
		$statements = new StatementList( $statement );
263
264
		$item = new Item( null, null, null, $statements );
265
266
		$this->assertEquals(
267
			$statements,
268
			$item->getStatements()
269
		);
270
	}
271
272
	public function testSetStatements() {
273
		$item = new Item();
274
		$item->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) );
275
276
		$item->setStatements( new StatementList() );
277
		$this->assertTrue( $item->getStatements()->isEmpty() );
278
	}
279
280
	public function testGetStatementsReturnsCorrectTypeAfterClear() {
281
		$item = new Item();
282
		$item->clear();
283
284
		$this->assertTrue( $item->getStatements()->isEmpty() );
285
	}
286
287
	public function equalsProvider() {
288
		$firstItem = new Item();
289
		$firstItem->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) );
290
291
		$secondItem = new Item();
292
		$secondItem->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) );
293
294
		$secondItemWithId = $secondItem->copy();
295
		$secondItemWithId->setId( 42 );
296
297
		$differentId = $secondItemWithId->copy();
298
		$differentId->setId( 43 );
299
300
		return array(
301
			array( new Item(), new Item() ),
302
			array( $firstItem, $secondItem ),
303
			array( $secondItem, $secondItemWithId ),
304
			array( $secondItemWithId, $differentId ),
305
		);
306
	}
307
308
	/**
309
	 * @dataProvider equalsProvider
310
	 */
311
	public function testEquals( Item $firstItem, Item $secondItem ) {
312
		$this->assertTrue( $firstItem->equals( $secondItem ) );
313
		$this->assertTrue( $secondItem->equals( $firstItem ) );
314
	}
315
316
	private function getBaseItem() {
317
		$item = new Item( new ItemId( 'Q42' ) );
318
		$item->getFingerprint()->setLabel( 'en', 'Same' );
319
		$item->getFingerprint()->setDescription( 'en', 'Same' );
320
		$item->getFingerprint()->setAliasGroup( 'en', array( 'Same' ) );
321
		$item->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Same' );
322
		$item->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) );
323
324
		return $item;
325
	}
326
327
	public function notEqualsProvider() {
328
		$differentLabel = $this->getBaseItem();
329
		$differentLabel->getFingerprint()->setLabel( 'en', 'Different' );
330
331
		$differentDescription = $this->getBaseItem();
332
		$differentDescription->getFingerprint()->setDescription( 'en', 'Different' );
333
334
		$differentAlias = $this->getBaseItem();
335
		$differentAlias->getFingerprint()->setAliasGroup( 'en', array( 'Different' ) );
336
337
		$differentSiteLink = $this->getBaseItem();
338
		$differentSiteLink->getSiteLinkList()->removeLinkWithSiteId( 'enwiki' );
339
		$differentSiteLink->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Different' );
340
341
		$differentStatement = $this->getBaseItem();
342
		$differentStatement->setStatements( new StatementList() );
343
		$differentStatement->getStatements()->addNewStatement( new PropertyNoValueSnak( 24 ) );
344
345
		$item = $this->getBaseItem();
346
347
		return array(
348
			'empty' => array( $item, new Item() ),
349
			'label' => array( $item, $differentLabel ),
350
			'description' => array( $item, $differentDescription ),
351
			'alias' => array( $item, $differentAlias ),
352
			'siteLink' => array( $item, $differentSiteLink ),
353
			'statement' => array( $item, $differentStatement ),
354
		);
355
	}
356
357
	/**
358
	 * @dataProvider notEqualsProvider
359
	 */
360
	public function testNotEquals( Item $firstItem, Item $secondItem ) {
361
		$this->assertFalse( $firstItem->equals( $secondItem ) );
362
		$this->assertFalse( $secondItem->equals( $firstItem ) );
363
	}
364
365
	public function cloneProvider() {
366
		$item = new Item( new ItemId( 'Q1' ) );
367
		$item->setLabel( 'en', 'original' );
368
		$item->getStatements()->addNewStatement( new PropertyNoValueSnak( 1 ) );
369
		$item->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Original' );
370
371
		return array(
372
			'copy' => array( $item, $item->copy() ),
373
			'native clone' => array( $item, clone $item ),
374
		);
375
	}
376
377
	/**
378
	 * @dataProvider cloneProvider
379
	 */
380
	public function testCloneIsEqualButNotIdentical( Item $original, Item $clone ) {
381
		$this->assertNotSame( $original, $clone );
382
		$this->assertTrue( $original->equals( $clone ) );
383
		$this->assertSame(
384
			$original->getId(),
385
			$clone->getId(),
386
			'id is immutable and must not be cloned'
387
		);
388
389
		// The clone must not reference the same mutable objects
390
		$this->assertNotSame( $original->getFingerprint(), $clone->getFingerprint() );
391
		$this->assertNotSame( $original->getStatements(), $clone->getStatements() );
392
		$this->assertNotSame(
393
			$original->getStatements()->getFirstStatementWithGuid( null ),
394
			$clone->getStatements()->getFirstStatementWithGuid( null )
395
		);
396
		$this->assertNotSame( $original->getSiteLinkList(), $clone->getSiteLinkList() );
397
		$this->assertSame(
398
			$original->getSiteLinkList()->getBySiteId( 'enwiki' ),
399
			$clone->getSiteLinkList()->getBySiteId( 'enwiki' ),
400
			'SiteLink is immutable and must not be cloned'
401
		);
402
	}
403
404
	/**
405
	 * @dataProvider cloneProvider
406
	 */
407
	public function testOriginalDoesNotChangeWithClone( Item $original, Item $clone ) {
408
		$originalStatement = $original->getStatements()->getFirstStatementWithGuid( null );
409
		$clonedStatement = $clone->getStatements()->getFirstStatementWithGuid( null );
410
411
		$clone->setLabel( 'en', 'clone' );
412
		$clone->setDescription( 'en', 'clone' );
413
		$clone->setAliases( 'en', array( 'clone' ) );
414
		$clonedStatement->setGuid( 'clone' );
415
		$clonedStatement->setMainSnak( new PropertySomeValueSnak( 666 ) );
416
		$clonedStatement->setRank( Statement::RANK_DEPRECATED );
417
		$clonedStatement->getQualifiers()->addSnak( new PropertyNoValueSnak( 1 ) );
418
		$clonedStatement->getReferences()->addNewReference( new PropertyNoValueSnak( 1 ) );
419
		$clone->getSiteLinkList()->removeLinkWithSiteId( 'enwiki' );
420
421
		$this->assertSame( 'original', $original->getFingerprint()->getLabel( 'en' )->getText() );
422
		$this->assertFalse( $original->getFingerprint()->hasDescription( 'en' ) );
423
		$this->assertFalse( $original->getFingerprint()->hasAliasGroup( 'en' ) );
424
		$this->assertNull( $originalStatement->getGuid() );
425
		$this->assertSame( 'novalue', $originalStatement->getMainSnak()->getType() );
426
		$this->assertSame( Statement::RANK_NORMAL, $originalStatement->getRank() );
427
		$this->assertTrue( $originalStatement->getQualifiers()->isEmpty() );
428
		$this->assertTrue( $originalStatement->getReferences()->isEmpty() );
429
		$this->assertFalse( $original->getSiteLinkList()->isEmpty() );
430
	}
431
432
	// Below are tests copied from EntityTest
433
434
	public function labelProvider() {
435
		return array(
436
			array( 'en', 'spam' ),
437
			array( 'en', 'spam', 'spam' ),
438
			array( 'de', 'foo bar baz' ),
439
		);
440
	}
441
442
	/**
443
	 * @dataProvider labelProvider
444
	 * @param string $languageCode
445
	 * @param string $labelText
446
	 * @param string $moarText
447
	 */
448
	public function testSetLabel( $languageCode, $labelText, $moarText = 'ohi there' ) {
449
		$entity = $this->getNewEmpty();
450
451
		$entity->setLabel( $languageCode, $labelText );
452
453
		$this->assertEquals( $labelText, $entity->getLabel( $languageCode ) );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::getLabel() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
454
455
		$entity->setLabel( $languageCode, $moarText );
456
457
		$this->assertEquals( $moarText, $entity->getLabel( $languageCode ) );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::getLabel() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
458
	}
459
460
	/**
461
	 * @dataProvider labelProvider
462
	 * @param string $languageCode
463
	 * @param string $labelText
464
	 */
465
	public function testGetLabel( $languageCode, $labelText ) {
466
		$entity = $this->getNewEmpty();
467
468
		$this->assertFalse( $entity->getLabel( $languageCode ) );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::getLabel() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
469
470
		$entity->setLabel( $languageCode, $labelText );
471
472
		$this->assertEquals( $labelText, $entity->getLabel( $languageCode ) );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::getLabel() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
473
	}
474
475
	/**
476
	 * @dataProvider labelProvider
477
	 * @param string $languageCode
478
	 * @param string $labelText
479
	 */
480
	public function testRemoveLabel( $languageCode, $labelText ) {
481
		$entity = $this->getNewEmpty();
482
		$entity->setLabel( $languageCode, $labelText );
483
		$entity->removeLabel( $languageCode );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::removeLabel() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
484
		$this->assertFalse( $entity->getLabel( $languageCode ) );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::getLabel() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
485
	}
486
487
	public function descriptionProvider() {
488
		return array(
489
			array( 'en', 'spam' ),
490
			array( 'en', 'spam', 'spam' ),
491
			array( 'de', 'foo bar baz' ),
492
		);
493
	}
494
495
	/**
496
	 * @dataProvider descriptionProvider
497
	 * @param string $languageCode
498
	 * @param string $description
499
	 * @param string $moarText
500
	 */
501
	public function testSetDescription( $languageCode, $description, $moarText = 'ohi there' ) {
502
		$entity = $this->getNewEmpty();
503
504
		$entity->setDescription( $languageCode, $description );
505
506
		$this->assertEquals( $description, $entity->getDescription( $languageCode ) );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::getDescription() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
507
508
		$entity->setDescription( $languageCode, $moarText );
509
510
		$this->assertEquals( $moarText, $entity->getDescription( $languageCode ) );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::getDescription() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
511
	}
512
513
	/**
514
	 * @dataProvider descriptionProvider
515
	 * @param string $languageCode
516
	 * @param string $description
517
	 */
518
	public function testGetDescription( $languageCode, $description ) {
519
		$entity = $this->getNewEmpty();
520
521
		$this->assertFalse( $entity->getDescription( $languageCode ) );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::getDescription() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
522
523
		$entity->setDescription( $languageCode, $description );
524
525
		$this->assertEquals( $description, $entity->getDescription( $languageCode ) );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::getDescription() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
526
	}
527
528
	/**
529
	 * @dataProvider descriptionProvider
530
	 * @param string $languageCode
531
	 * @param string $description
532
	 */
533
	public function testRemoveDescription( $languageCode, $description ) {
534
		$entity = $this->getNewEmpty();
535
		$entity->setDescription( $languageCode, $description );
536
		$entity->removeDescription( $languageCode );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entit...ty::removeDescription() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
537
		$this->assertFalse( $entity->getDescription( $languageCode ) );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::getDescription() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
538
	}
539
540
	public function aliasesProvider() {
541
		return array(
542
			array( array(
543
				       'en' => array( array( 'spam' ) )
544
			       ) ),
545
			array( array(
546
				       'en' => array( array( 'foo', 'bar', 'baz' ) )
547
			       ) ),
548
			array( array(
549
				       'en' => array( array( 'foo', 'bar' ), array( 'baz', 'spam' ) )
550
			       ) ),
551
			array( array(
552
				       'en' => array( array( 'foo', 'bar', 'baz' ) ),
553
				       'de' => array( array( 'foobar' ), array( 'baz' ) ),
554
			       ) ),
555
			// with duplicates
556
			array( array(
557
				       'en' => array( array( 'spam', 'ham', 'ham' ) )
558
			       ) ),
559
			array( array(
560
				       'en' => array( array( 'foo', 'bar' ), array( 'bar', 'spam' ) )
561
			       ) ),
562
		);
563
	}
564
565
	/**
566
	 * @dataProvider aliasesProvider
567
	 */
568
	public function testAddAliases( array $aliasesLists ) {
569
		$entity = $this->getNewEmpty();
570
571
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
572
			foreach ( $aliasesList as $aliases ) {
573
				$entity->addAliases( $langCode, $aliases );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::addAliases() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
574
			}
575
		}
576
577
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
578
			$expected = array_values( array_unique( call_user_func_array( 'array_merge', $aliasesList ) ) );
579
			asort( $expected );
580
581
			$actual = $entity->getAliases( $langCode );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::getAliases() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
582
			asort( $actual );
583
584
			$this->assertEquals( $expected, $actual );
585
		}
586
	}
587
588
	/**
589
	 * @dataProvider aliasesProvider
590
	 */
591
	public function testSetAliases( array $aliasesLists ) {
592
		$entity = $this->getNewEmpty();
593
594
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
595
			foreach ( $aliasesList as $aliases ) {
596
				$entity->setAliases( $langCode, $aliases );
597
			}
598
		}
599
600
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
601
			$expected = array_values( array_unique( array_pop( $aliasesList ) ) );
602
			asort( $aliasesList );
603
604
			$actual = $entity->getAliases( $langCode );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::getAliases() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
605
			asort( $actual );
606
607
			$this->assertEquals( $expected, $actual );
608
		}
609
	}
610
611
	/**
612
	 * @dataProvider aliasesProvider
613
	 */
614
	public function testSetEmptyAlias( array $aliasesLists ) {
615
		$entity = $this->getNewEmpty();
616
617
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
618
			foreach ( $aliasesList as $aliases ) {
619
				$entity->setAliases( $langCode, $aliases );
620
			}
621
		}
622
		$entity->setAliases( 'zh', array( 'wind', 'air', '', 'fire' ) );
623
		$entity->setAliases( 'zu', array( '', '' ) );
624
625
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
626
			$expected = array_values( array_unique( array_pop( $aliasesList ) ) );
627
			asort( $aliasesList );
628
629
			$actual = $entity->getAliases( $langCode );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::getAliases() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
630
			asort( $actual );
631
632
			$this->assertEquals( $expected, $actual );
633
		}
634
	}
635
636
	/**
637
	 * @dataProvider aliasesProvider
638
	 */
639
	public function testSetAllAliases( array $aliasGroups ) {
640
		$entity = $this->getNewEmpty();
641
		$entity->addAliases( 'zh', array( 'qwertyuiop123', '321poiuytrewq' ) );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::addAliases() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
642
643
		$aliasesToSet = array();
644
		foreach ( $aliasGroups as $langCode => $aliasGroup ) {
645
			foreach ( $aliasGroup as $aliases ) {
646
				$aliasesToSet[$langCode] = $aliases;
647
			}
648
		}
649
650
		$entity->setAllAliases( $aliasesToSet );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::setAllAliases() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
651
652
		foreach ( $aliasGroups as $langCode => $aliasGroup ) {
653
			$expected = array_values( array_unique( array_pop( $aliasGroup ) ) );
654
			asort( $aliasGroup );
655
656
			$actual = $entity->getFingerprint()->getAliasGroups()->getByLanguage( $langCode )->getAliases();
657
			asort( $actual );
658
659
			$this->assertEquals( $expected, $actual );
660
		}
661
662
		/** @var AliasGroup $aliasGroup */
663
		foreach ( $entity->getFingerprint()->getAliasGroups() as $langCode => $aliasGroup ) {
664
			$this->assertEquals( $aliasGroup->getAliases(), array_unique( $aliasesToSet[$langCode] ) );
665
		}
666
	}
667
668
	public function testGetAliases() {
669
		$entity = $this->getNewEmpty();
670
		$aliases = array( 'a', 'b' );
671
672
		$entity->getFingerprint()->setAliasGroup( 'en', $aliases );
673
674
		$this->assertEquals(
675
			$aliases,
676
			$entity->getAliases( 'en' )
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::getAliases() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
677
		);
678
	}
679
680
	public function duplicateAliasesProvider() {
681
		return array(
682
			array( array(
683
				       'en' => array( array( 'foo', 'bar', 'baz' ), array( 'foo', 'bar', 'baz' ) )
684
			       ) ),
685
			array( array(
686
				       'en' => array( array( 'foo', 'bar', 'baz' ), array( 'foo', 'bar' ) )
687
			       ) ),
688
			array( array(
689
				       'en' => array( array( 'foo', 'bar' ), array( 'foo', 'bar', 'baz' ) )
690
			       ) ),
691
			array( array(
692
				       'en' => array( array( 'foo', 'bar' ), array( 'bar', 'baz' ) ),
693
				       'de' => array( array(), array( 'foo' ) ),
694
				       'nl' => array( array( 'foo' ), array() ),
695
			       ) ),
696
			array( array(
697
				       'en' => array( array( 'foo', 'bar', 'baz' ), array( 'foo', 'bar', 'baz', 'foo', 'bar' ) )
698
			       ) ),
699
		);
700
	}
701
702
	/**
703
	 * @dataProvider duplicateAliasesProvider
704
	 */
705
	public function testRemoveAliases( array $aliasesLists ) {
706
		$entity = $this->getNewEmpty();
707
708
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
709
			$aliases = array_shift( $aliasesList );
710
			$removedAliases = array_shift( $aliasesList );
711
712
			$entity->setAliases( $langCode, $aliases );
713
			$entity->removeAliases( $langCode, $removedAliases );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::removeAliases() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
714
715
			$expected = array_values( array_diff( $aliases, $removedAliases ) );
716
			$actual = $entity->getAliases( $langCode );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::getAliases() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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

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...
860
		$this->assertEquals( array(), $entity->getDescriptions() );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entit...tity::getDescriptions() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
861
		$this->assertEquals( array(), $entity->getAllAliases() );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::getAllAliases() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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