Passed
Push — getclaims ( a434c3...798844 )
by no
03:32
created

ItemTest::testSetClaims()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 17
rs 9.4285
cc 1
eloc 11
nc 1
nop 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A ItemTest::testAddSiteLinkOverridesOldLinks() 0 10 1
1
<?php
2
3
namespace Wikibase\DataModel\Tests\Entity;
4
5
use Wikibase\DataModel\Claim\Claims;
6
use Wikibase\DataModel\Entity\Item;
7
use Wikibase\DataModel\Entity\ItemId;
8
use Wikibase\DataModel\Entity\PropertyId;
9
use Wikibase\DataModel\SiteLink;
10
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
11
use Wikibase\DataModel\Snak\PropertySomeValueSnak;
12
use Wikibase\DataModel\Statement\Statement;
13
use Wikibase\DataModel\Statement\StatementList;
14
15
/**
16
 * @covers Wikibase\DataModel\Entity\Item
17
 * @covers Wikibase\DataModel\Entity\Entity
18
 *
19
 * Some tests for this class are located in ItemMultilangTextsTest,
20
 * ItemNewEmptyTest and ItemNewFromArrayTest.
21
 *
22
 * @since 0.1
23
 *
24
 * @group Wikibase
25
 * @group WikibaseItem
26
 * @group WikibaseDataModel
27
 * @group WikibaseItemTest
28
 *
29
 * @licence GNU GPL v2+
30
 * @author Jeroen De Dauw < [email protected] >
31
 * @author John Erling Blad < [email protected] >
32
 * @author Michał Łazowik
33
 */
34
class ItemTest extends EntityTest {
0 ignored issues
show
Deprecated Code introduced by
The class Wikibase\DataModel\Tests\Entity\EntityTest has been deprecated with message: This test class is to be phased out, and should not be used from outside of the component!

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
35
36
	/**
37
	 * @see EntityTest::getNewEmpty
38
	 *
39
	 * @since 0.1
40
	 *
41
	 * @return Item
42
	 */
43
	protected function getNewEmpty() {
44
		return new Item();
45
	}
46
47
	public function testGetId() {
48
		$item = new Item();
49
		$this->assertNull( $item->getId() );
50
51
		$item->setId( new ItemId( 'Q1' ) );
52
		$this->assertEquals( new ItemId( 'Q1' ), $item->getId() );
53
54
		$item->setId( null );
55
		$this->assertNull( $item->getId() );
56
57
		$item = new Item( new ItemId( 'Q2' ) );
58
		$this->assertEquals( new ItemId( 'Q2' ), $item->getId() );
59
	}
60
61
	public function testSetIdUsingNumber() {
62
		$item = new Item();
63
		$item->setId( 42 );
64
		$this->assertEquals( new ItemId( 'Q42' ), $item->getId() );
65
	}
66
67
	public function itemProvider() {
68
		$items = array();
69
70
		$items[] = new Item();
71
72
		$item = new Item();
73
		$item->setDescription( 'en', 'foo' );
74
		$items[] = $item;
75
76
		$item = new Item();
77
		$item->setDescription( 'en', 'foo' );
78
		$item->setDescription( 'de', 'foo' );
79
		$item->setLabel( 'en', 'foo' );
80
		$item->setAliases( 'de', array( 'bar', 'baz' ) );
81
		$items[] = $item;
82
83
		/** @var Item $item */
84
		$item = $item->copy();
85
		$item->getStatements()->addNewStatement(
86
			new PropertyNoValueSnak( new PropertyId( 'P42' ) )
87
		);
88
		$items[] = $item;
89
90
		$argLists = array();
91
92
		foreach ( $items as $item ) {
93
			$argLists[] = array( $item );
94
		}
95
96
		return $argLists;
97
	}
98
99
	public function testGetSiteLinkWithNonSetSiteId() {
100
		$item = new Item();
101
102
		$this->setExpectedException( 'OutOfBoundsException' );
103
		$item->getSiteLinkList()->getBySiteId( 'enwiki' );
104
	}
105
106
	/**
107
	 * @dataProvider simpleSiteLinkProvider
108
	 */
109
	public function testAddSiteLink( SiteLink $siteLink ) {
110
		$item = new Item();
111
112
		$item->getSiteLinkList()->addSiteLink( $siteLink );
113
114
		$this->assertEquals(
115
			$siteLink,
116
			$item->getSiteLinkList()->getBySiteId( $siteLink->getSiteId() )
117
		);
118
	}
119
120
	public function simpleSiteLinkProvider() {
121
		$argLists = array();
122
123
		$argLists[] = array(
124
			new SiteLink(
125
				'enwiki',
126
				'Wikidata',
127
				array(
128
					new ItemId( 'Q42' )
129
				)
130
			)
131
		);
132
		$argLists[] = array(
133
			new SiteLink(
134
				'nlwiki',
135
				'Wikidata'
136
			)
137
		);
138
		$argLists[] = array(
139
			new SiteLink(
140
				'enwiki',
141
				'Nyan!',
142
				array(
143
					new ItemId( 'Q42' ),
144
					new ItemId( 'Q149' )
145
				)
146
			)
147
		);
148
		$argLists[] = array(
149
			new SiteLink(
150
				'foo bar',
151
				'baz bah',
152
				array(
153
					new ItemId( 'Q3' ),
154
					new ItemId( 'Q7' )
155
				)
156
			)
157
		);
158
159
		return $argLists;
160
	}
161
162
	/**
163
	 * @dataProvider simpleSiteLinksProvider
164
	 */
165
	public function testGetSiteLinks() {
166
		$siteLinks = func_get_args();
167
		$item = new Item();
168
169
		foreach ( $siteLinks as $siteLink ) {
170
			$item->getSiteLinkList()->addSiteLink( $siteLink );
171
		}
172
173
		$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...
174
		$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...
175
	}
176
177
	public function simpleSiteLinksProvider() {
178
		$argLists = array();
179
180
		$argLists[] = array();
181
182
		$argLists[] = array( new SiteLink( 'enwiki', 'Wikidata', array( new ItemId( 'Q42' ) ) ) );
183
184
		$argLists[] = array(
185
			new SiteLink( 'enwiki', 'Wikidata' ),
186
			new SiteLink( 'nlwiki', 'Wikidata', array( new ItemId( 'Q3' ) ) )
187
		);
188
189
		$argLists[] = array(
190
			new SiteLink( 'enwiki', 'Wikidata' ),
191
			new SiteLink( 'nlwiki', 'Wikidata' ),
192
			new SiteLink( 'foo bar', 'baz bah', array( new ItemId( 'Q2' ) ) )
193
		);
194
195
		return $argLists;
196
	}
197
198
	public function testHasLinkToSiteForFalse() {
199
		$item = new Item();
200
		$item->getSiteLinkList()->addNewSiteLink( 'ENWIKI', 'Wikidata', array( new ItemId( 'Q42' ) ) );
201
202
		$this->assertFalse( $item->getSiteLinkList()->hasLinkWithSiteId( 'enwiki' ) );
203
		$this->assertFalse( $item->getSiteLinkList()->hasLinkWithSiteId( 'dewiki' ) );
204
		$this->assertFalse( $item->getSiteLinkList()->hasLinkWithSiteId( 'foo bar' ) );
205
	}
206
207
	public function testHasLinkToSiteForTrue() {
208
		$item = new Item();
209
		$item->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Wikidata', array( new ItemId( 'Q42' ) ) );
210
		$item->getSiteLinkList()->addNewSiteLink( 'dewiki', 'Wikidata' );
211
		$item->getSiteLinkList()->addNewSiteLink( 'foo bar', 'Wikidata' );
212
213
		$this->assertTrue( $item->getSiteLinkList()->hasLinkWithSiteId( 'enwiki' ) );
214
		$this->assertTrue( $item->getSiteLinkList()->hasLinkWithSiteId( 'dewiki' ) );
215
		$this->assertTrue( $item->getSiteLinkList()->hasLinkWithSiteId( 'foo bar' ) );
216
	}
217
218
	public function testEmptyItemReturnsEmptySiteLinkList() {
219
		$item = new Item();
220
		$this->assertTrue( $item->getSiteLinkList()->isEmpty() );
221
	}
222
223
	public function testAddSiteLinkOverridesOldLinks() {
224
		$item = new Item();
225
226
		$item->getSiteLinkList()->addNewSiteLink( 'kittens', 'foo' );
227
228
		$newLink = new SiteLink( 'kittens', 'bar' );
229
		$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...
230
231
		$this->assertTrue( $item->getSiteLinkList()->getBySiteId( 'kittens' )->equals( $newLink ) );
232
	}
233
234
	public function testEmptyItemIsEmpty() {
235
		$item = new Item();
236
		$this->assertTrue( $item->isEmpty() );
237
	}
238
239
	public function testItemWithIdIsEmpty() {
240
		$item = new Item( new ItemId( 'Q1337' ) );
241
		$this->assertTrue( $item->isEmpty() );
242
	}
243
244
	public function testItemWithStuffIsNotEmpty() {
245
		$item = new Item();
246
		$item->getFingerprint()->setAliasGroup( 'en', array( 'foo' ) );
247
		$this->assertFalse( $item->isEmpty() );
248
249
		$item = new Item();
250
		$item->getSiteLinkList()->addNewSiteLink( 'en', 'o_O' );
251
		$this->assertFalse( $item->isEmpty() );
252
253
		$item = new Item();
254
		$item->getStatements()->addStatement( $this->newStatement() );
255
		$this->assertFalse( $item->isEmpty() );
256
	}
257
258
	public function testItemWithSitelinksHasSitelinks() {
259
		$item = new Item();
260
		$item->getSiteLinkList()->addNewSiteLink( 'en', 'foo' );
261
		$this->assertFalse( $item->getSiteLinkList()->isEmpty() );
262
	}
263
264
	public function testItemWithoutSitelinksHasNoSitelinks() {
265
		$item = new Item();
266
		$this->assertTrue( $item->getSiteLinkList()->isEmpty() );
267
	}
268
269
	private function newStatement() {
270
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
271
		$statement->setGuid( 'kittens' );
272
		return $statement;
273
	}
274
275
	public function testClearRemovesAllButId() {
276
		$item = new Item( new ItemId( 'Q42' ) );
277
		$item->getFingerprint()->setLabel( 'en', 'foo' );
278
		$item->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Foo' );
279
		$item->getStatements()->addStatement( $this->newStatement() );
280
281
		$item->clear();
282
283
		$this->assertEquals( new ItemId( 'Q42' ), $item->getId() );
284
		$this->assertTrue( $item->getFingerprint()->isEmpty() );
285
		$this->assertTrue( $item->getSiteLinkList()->isEmpty() );
286
		$this->assertTrue( $item->getStatements()->isEmpty() );
287
	}
288
289
	public function testEmptyConstructor() {
290
		$item = new Item();
291
292
		$this->assertNull( $item->getId() );
293
		$this->assertTrue( $item->getFingerprint()->isEmpty() );
294
		$this->assertTrue( $item->getSiteLinkList()->isEmpty() );
295
		$this->assertTrue( $item->getStatements()->isEmpty() );
296
	}
297
298
	public function testCanConstructWithStatementList() {
299
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
300
		$statement->setGuid( 'meh' );
301
302
		$statements = new StatementList( $statement );
303
304
		$item = new Item( null, null, null, $statements );
305
306
		$this->assertEquals(
307
			$statements,
308
			$item->getStatements()
309
		);
310
	}
311
312
	public function testSetStatements() {
313
		$item = new Item();
314
		$item->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) );
315
316
		$item->setStatements( new StatementList() );
317
		$this->assertTrue( $item->getStatements()->isEmpty() );
318
	}
319
320
	public function testGetStatementsReturnsCorrectTypeAfterClear() {
321
		$item = new Item();
322
		$item->clear();
323
324
		$this->assertTrue( $item->getStatements()->isEmpty() );
325
	}
326
327
	public function equalsProvider() {
328
		$firstItem = new Item();
329
		$firstItem->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) );
330
331
		$secondItem = new Item();
332
		$secondItem->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) );
333
334
		$secondItemWithId = $secondItem->copy();
335
		$secondItemWithId->setId( 42 );
336
337
		$differentId = $secondItemWithId->copy();
338
		$differentId->setId( 43 );
339
340
		return array(
341
			array( new Item(), new Item() ),
342
			array( $firstItem, $secondItem ),
343
			array( $secondItem, $secondItemWithId ),
344
			array( $secondItemWithId, $differentId ),
345
		);
346
	}
347
348
	/**
349
	 * @dataProvider equalsProvider
350
	 */
351
	public function testEquals( Item $firstItem, Item $secondItem ) {
352
		$this->assertTrue( $firstItem->equals( $secondItem ) );
353
		$this->assertTrue( $secondItem->equals( $firstItem ) );
354
	}
355
356
	private function getBaseItem() {
357
		$item = new Item( new ItemId( 'Q42' ) );
358
		$item->getFingerprint()->setLabel( 'en', 'Same' );
359
		$item->getFingerprint()->setDescription( 'en', 'Same' );
360
		$item->getFingerprint()->setAliasGroup( 'en', array( 'Same' ) );
361
		$item->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Same' );
362
		$item->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) );
363
364
		return $item;
365
	}
366
367
	public function notEqualsProvider() {
368
		$differentLabel = $this->getBaseItem();
369
		$differentLabel->getFingerprint()->setLabel( 'en', 'Different' );
370
371
		$differentDescription = $this->getBaseItem();
372
		$differentDescription->getFingerprint()->setDescription( 'en', 'Different' );
373
374
		$differentAlias = $this->getBaseItem();
375
		$differentAlias->getFingerprint()->setAliasGroup( 'en', array( 'Different' ) );
376
377
		$differentSiteLink = $this->getBaseItem();
378
		$differentSiteLink->getSiteLinkList()->removeLinkWithSiteId( 'enwiki' );
379
		$differentSiteLink->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Different' );
380
381
		$differentStatement = $this->getBaseItem();
382
		$differentStatement->setStatements( new StatementList() );
383
		$differentStatement->getStatements()->addNewStatement( new PropertyNoValueSnak( 24 ) );
384
385
		$item = $this->getBaseItem();
386
387
		return array(
388
			'empty' => array( $item, new Item() ),
389
			'label' => array( $item, $differentLabel ),
390
			'description' => array( $item, $differentDescription ),
391
			'alias' => array( $item, $differentAlias ),
392
			'siteLink' => array( $item, $differentSiteLink ),
393
			'statement' => array( $item, $differentStatement ),
394
		);
395
	}
396
397
	/**
398
	 * @dataProvider notEqualsProvider
399
	 */
400
	public function testNotEquals( Item $firstItem, Item $secondItem ) {
401
		$this->assertFalse( $firstItem->equals( $secondItem ) );
402
		$this->assertFalse( $secondItem->equals( $firstItem ) );
403
	}
404
405
}
406