Completed
Pull Request — master (#630)
by Bene
07:36 queued 03:44
created

testSetFingerprint_getFingerprintReturnsIt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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