Completed
Pull Request — master (#632)
by Bene
27:38 queued 24:21
created

ItemTest::testCloneIsEqualButNotIdentical()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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