Completed
Pull Request — master (#614)
by Bene
10:35 queued 07:28
created

ItemTest::testClearRemovesAllButId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

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