Passed
Push — release500 ( 6f22d3...711bfb )
by no
02:56
created

ItemTest::testAddSiteLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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