Completed
Push — entity-clear ( afee34...e01dde )
by Bene
03:31
created

ItemTest::testClearRemovesAllButId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 1
eloc 10
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\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 testEmptyConstructor() {
274
		$item = new Item();
275
276
		$this->assertNull( $item->getId() );
277
		$this->assertTrue( $item->getFingerprint()->isEmpty() );
278
		$this->assertTrue( $item->getSiteLinkList()->isEmpty() );
279
		$this->assertTrue( $item->getStatements()->isEmpty() );
280
	}
281
282
	public function testCanConstructWithStatementList() {
283
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
284
		$statement->setGuid( 'meh' );
285
286
		$statements = new StatementList( $statement );
287
288
		$item = new Item( null, null, null, $statements );
289
290
		$this->assertEquals(
291
			$statements,
292
			$item->getStatements()
293
		);
294
	}
295
296
	public function testSetStatements() {
297
		$item = new Item();
298
		$item->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) );
299
300
		$item->setStatements( new StatementList() );
301
		$this->assertTrue( $item->getStatements()->isEmpty() );
302
	}
303
304
	public function equalsProvider() {
305
		$firstItem = new Item();
306
		$firstItem->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) );
307
308
		$secondItem = new Item();
309
		$secondItem->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) );
310
311
		$secondItemWithId = $secondItem->copy();
312
		$secondItemWithId->setId( 42 );
313
314
		$differentId = $secondItemWithId->copy();
315
		$differentId->setId( 43 );
316
317
		return array(
318
			array( new Item(), new Item() ),
319
			array( $firstItem, $secondItem ),
320
			array( $secondItem, $secondItemWithId ),
321
			array( $secondItemWithId, $differentId ),
322
		);
323
	}
324
325
	/**
326
	 * @dataProvider equalsProvider
327
	 */
328
	public function testEquals( Item $firstItem, Item $secondItem ) {
329
		$this->assertTrue( $firstItem->equals( $secondItem ) );
330
		$this->assertTrue( $secondItem->equals( $firstItem ) );
331
	}
332
333
	private function getBaseItem() {
334
		$item = new Item( new ItemId( 'Q42' ) );
335
		$item->getFingerprint()->setLabel( 'en', 'Same' );
336
		$item->getFingerprint()->setDescription( 'en', 'Same' );
337
		$item->getFingerprint()->setAliasGroup( 'en', array( 'Same' ) );
338
		$item->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Same' );
339
		$item->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) );
340
341
		return $item;
342
	}
343
344
	public function notEqualsProvider() {
345
		$differentLabel = $this->getBaseItem();
346
		$differentLabel->getFingerprint()->setLabel( 'en', 'Different' );
347
348
		$differentDescription = $this->getBaseItem();
349
		$differentDescription->getFingerprint()->setDescription( 'en', 'Different' );
350
351
		$differentAlias = $this->getBaseItem();
352
		$differentAlias->getFingerprint()->setAliasGroup( 'en', array( 'Different' ) );
353
354
		$differentSiteLink = $this->getBaseItem();
355
		$differentSiteLink->getSiteLinkList()->removeLinkWithSiteId( 'enwiki' );
356
		$differentSiteLink->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Different' );
357
358
		$differentStatement = $this->getBaseItem();
359
		$differentStatement->setStatements( new StatementList() );
360
		$differentStatement->getStatements()->addNewStatement( new PropertyNoValueSnak( 24 ) );
361
362
		$item = $this->getBaseItem();
363
364
		return array(
365
			'empty' => array( $item, new Item() ),
366
			'label' => array( $item, $differentLabel ),
367
			'description' => array( $item, $differentDescription ),
368
			'alias' => array( $item, $differentAlias ),
369
			'siteLink' => array( $item, $differentSiteLink ),
370
			'statement' => array( $item, $differentStatement ),
371
		);
372
	}
373
374
	/**
375
	 * @dataProvider notEqualsProvider
376
	 */
377
	public function testNotEquals( Item $firstItem, Item $secondItem ) {
378
		$this->assertFalse( $firstItem->equals( $secondItem ) );
379
		$this->assertFalse( $secondItem->equals( $firstItem ) );
380
	}
381
382
}
383