Completed
Pull Request — master (#632)
by Bene
75:46 queued 65:37
created

ItemTest::getBaseItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

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