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