|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel\Tests\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use OutOfBoundsException; |
|
6
|
|
|
use PHPUnit_Framework_TestCase; |
|
7
|
|
|
use Wikibase\DataModel\Entity\Item; |
|
8
|
|
|
use Wikibase\DataModel\Entity\ItemId; |
|
9
|
|
|
use Wikibase\DataModel\SiteLink; |
|
10
|
|
|
use Wikibase\DataModel\Snak\PropertyNoValueSnak; |
|
11
|
|
|
use Wikibase\DataModel\Snak\PropertySomeValueSnak; |
|
12
|
|
|
use Wikibase\DataModel\Statement\Statement; |
|
13
|
|
|
use Wikibase\DataModel\Statement\StatementList; |
|
14
|
|
|
use Wikibase\DataModel\Term\AliasGroup; |
|
15
|
|
|
use Wikibase\DataModel\Term\AliasGroupList; |
|
16
|
|
|
use Wikibase\DataModel\Term\Fingerprint; |
|
17
|
|
|
use Wikibase\DataModel\Term\Term; |
|
18
|
|
|
use Wikibase\DataModel\Term\TermList; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @covers Wikibase\DataModel\Entity\Item |
|
22
|
|
|
* |
|
23
|
|
|
* @group Wikibase |
|
24
|
|
|
* @group WikibaseDataModel |
|
25
|
|
|
* |
|
26
|
|
|
* @license GPL-2.0+ |
|
27
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
28
|
|
|
* @author John Erling Blad < [email protected] > |
|
29
|
|
|
* @author Michał Łazowik |
|
30
|
|
|
*/ |
|
31
|
|
|
class ItemTest extends PHPUnit_Framework_TestCase { |
|
32
|
|
|
|
|
33
|
|
|
private function getNewEmpty() { |
|
34
|
|
|
return new Item(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function testGetId() { |
|
38
|
|
|
$item = new Item(); |
|
39
|
|
|
$this->assertNull( $item->getId() ); |
|
40
|
|
|
|
|
41
|
|
|
$item->setId( new ItemId( 'Q1' ) ); |
|
42
|
|
|
$this->assertEquals( new ItemId( 'Q1' ), $item->getId() ); |
|
43
|
|
|
|
|
44
|
|
|
$item->setId( null ); |
|
45
|
|
|
$this->assertNull( $item->getId() ); |
|
46
|
|
|
|
|
47
|
|
|
$item = new Item( new ItemId( 'Q2' ) ); |
|
48
|
|
|
$this->assertEquals( new ItemId( 'Q2' ), $item->getId() ); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function testGetSiteLinkWithNonSetSiteId() { |
|
52
|
|
|
$item = new Item(); |
|
53
|
|
|
|
|
54
|
|
|
$this->setExpectedException( OutOfBoundsException::class ); |
|
55
|
|
|
$item->getSiteLinkList()->getBySiteId( 'enwiki' ); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @dataProvider simpleSiteLinkProvider |
|
60
|
|
|
*/ |
|
61
|
|
|
public function testAddSiteLink( SiteLink $siteLink ) { |
|
62
|
|
|
$item = new Item(); |
|
63
|
|
|
|
|
64
|
|
|
$item->getSiteLinkList()->addSiteLink( $siteLink ); |
|
65
|
|
|
|
|
66
|
|
|
$this->assertEquals( |
|
67
|
|
|
$siteLink, |
|
68
|
|
|
$item->getSiteLinkList()->getBySiteId( $siteLink->getSiteId() ) |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function simpleSiteLinkProvider() { |
|
73
|
|
|
$argLists = []; |
|
74
|
|
|
|
|
75
|
|
|
$argLists[] = [ |
|
76
|
|
|
new SiteLink( |
|
77
|
|
|
'enwiki', |
|
78
|
|
|
'Wikidata', |
|
79
|
|
|
[ |
|
80
|
|
|
new ItemId( 'Q42' ) |
|
81
|
|
|
] |
|
82
|
|
|
) |
|
83
|
|
|
]; |
|
84
|
|
|
$argLists[] = [ |
|
85
|
|
|
new SiteLink( |
|
86
|
|
|
'nlwiki', |
|
87
|
|
|
'Wikidata' |
|
88
|
|
|
) |
|
89
|
|
|
]; |
|
90
|
|
|
$argLists[] = [ |
|
91
|
|
|
new SiteLink( |
|
92
|
|
|
'enwiki', |
|
93
|
|
|
'Nyan!', |
|
94
|
|
|
[ |
|
95
|
|
|
new ItemId( 'Q42' ), |
|
96
|
|
|
new ItemId( 'Q149' ) |
|
97
|
|
|
] |
|
98
|
|
|
) |
|
99
|
|
|
]; |
|
100
|
|
|
$argLists[] = [ |
|
101
|
|
|
new SiteLink( |
|
102
|
|
|
'foo bar', |
|
103
|
|
|
'baz bah', |
|
104
|
|
|
[ |
|
105
|
|
|
new ItemId( 'Q3' ), |
|
106
|
|
|
new ItemId( 'Q7' ) |
|
107
|
|
|
] |
|
108
|
|
|
) |
|
109
|
|
|
]; |
|
110
|
|
|
|
|
111
|
|
|
return $argLists; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function simpleSiteLinksProvider() { |
|
115
|
|
|
$argLists = []; |
|
116
|
|
|
|
|
117
|
|
|
$argLists[] = []; |
|
118
|
|
|
|
|
119
|
|
|
$argLists[] = [ new SiteLink( 'enwiki', 'Wikidata', [ new ItemId( 'Q42' ) ] ) ]; |
|
120
|
|
|
|
|
121
|
|
|
$argLists[] = [ |
|
122
|
|
|
new SiteLink( 'enwiki', 'Wikidata' ), |
|
123
|
|
|
new SiteLink( 'nlwiki', 'Wikidata', [ new ItemId( 'Q3' ) ] ) |
|
124
|
|
|
]; |
|
125
|
|
|
|
|
126
|
|
|
$argLists[] = [ |
|
127
|
|
|
new SiteLink( 'enwiki', 'Wikidata' ), |
|
128
|
|
|
new SiteLink( 'nlwiki', 'Wikidata' ), |
|
129
|
|
|
new SiteLink( 'foo bar', 'baz bah', [ new ItemId( 'Q2' ) ] ) |
|
130
|
|
|
]; |
|
131
|
|
|
|
|
132
|
|
|
return $argLists; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function testHasLinkToSiteForFalse() { |
|
136
|
|
|
$item = new Item(); |
|
137
|
|
|
$item->getSiteLinkList()->addNewSiteLink( 'ENWIKI', 'Wikidata', [ new ItemId( 'Q42' ) ] ); |
|
138
|
|
|
|
|
139
|
|
|
$this->assertFalse( $item->getSiteLinkList()->hasLinkWithSiteId( 'enwiki' ) ); |
|
140
|
|
|
$this->assertFalse( $item->getSiteLinkList()->hasLinkWithSiteId( 'dewiki' ) ); |
|
141
|
|
|
$this->assertFalse( $item->getSiteLinkList()->hasLinkWithSiteId( 'foo bar' ) ); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
public function testHasLinkToSiteForTrue() { |
|
145
|
|
|
$item = new Item(); |
|
146
|
|
|
$item->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Wikidata', [ new ItemId( 'Q42' ) ] ); |
|
147
|
|
|
$item->getSiteLinkList()->addNewSiteLink( 'dewiki', 'Wikidata' ); |
|
148
|
|
|
$item->getSiteLinkList()->addNewSiteLink( 'foo bar', 'Wikidata' ); |
|
149
|
|
|
|
|
150
|
|
|
$this->assertTrue( $item->getSiteLinkList()->hasLinkWithSiteId( 'enwiki' ) ); |
|
151
|
|
|
$this->assertTrue( $item->getSiteLinkList()->hasLinkWithSiteId( 'dewiki' ) ); |
|
152
|
|
|
$this->assertTrue( $item->getSiteLinkList()->hasLinkWithSiteId( 'foo bar' ) ); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function testEmptyItemReturnsEmptySiteLinkList() { |
|
156
|
|
|
$item = new Item(); |
|
157
|
|
|
$this->assertTrue( $item->getSiteLinkList()->isEmpty() ); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
public function testAddSiteLinkOverridesOldLinks() { |
|
161
|
|
|
$item = new Item(); |
|
162
|
|
|
|
|
163
|
|
|
$item->getSiteLinkList()->addNewSiteLink( 'kittens', 'foo' ); |
|
164
|
|
|
|
|
165
|
|
|
$newLink = new SiteLink( 'kittens', 'bar' ); |
|
166
|
|
|
$item->addSiteLink( $newLink ); |
|
167
|
|
|
|
|
168
|
|
|
$this->assertTrue( $item->getSiteLinkList()->getBySiteId( 'kittens' )->equals( $newLink ) ); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
public function testEmptyItemIsEmpty() { |
|
172
|
|
|
$item = new Item(); |
|
173
|
|
|
$this->assertTrue( $item->isEmpty() ); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
public function testItemWithIdIsEmpty() { |
|
177
|
|
|
$item = new Item( new ItemId( 'Q1337' ) ); |
|
178
|
|
|
$this->assertTrue( $item->isEmpty() ); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
public function testItemWithStuffIsNotEmpty() { |
|
182
|
|
|
$item = new Item(); |
|
183
|
|
|
$item->setAliases( 'en', [ 'foo' ] ); |
|
184
|
|
|
$this->assertFalse( $item->isEmpty() ); |
|
185
|
|
|
|
|
186
|
|
|
$item = new Item(); |
|
187
|
|
|
$item->getSiteLinkList()->addNewSiteLink( 'en', 'o_O' ); |
|
188
|
|
|
$this->assertFalse( $item->isEmpty() ); |
|
189
|
|
|
|
|
190
|
|
|
$item = new Item(); |
|
191
|
|
|
$item->getStatements()->addStatement( $this->newStatement() ); |
|
192
|
|
|
$this->assertFalse( $item->isEmpty() ); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
public function testItemWithSitelinksHasSitelinks() { |
|
196
|
|
|
$item = new Item(); |
|
197
|
|
|
$item->getSiteLinkList()->addNewSiteLink( 'en', 'foo' ); |
|
198
|
|
|
$this->assertFalse( $item->getSiteLinkList()->isEmpty() ); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
public function testItemWithoutSitelinksHasNoSitelinks() { |
|
202
|
|
|
$item = new Item(); |
|
203
|
|
|
$this->assertTrue( $item->getSiteLinkList()->isEmpty() ); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
private function newStatement() { |
|
207
|
|
|
$statement = new Statement( new PropertyNoValueSnak( 42 ) ); |
|
208
|
|
|
$statement->setGuid( 'kittens' ); |
|
209
|
|
|
return $statement; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
public function testEmptyConstructor() { |
|
213
|
|
|
$item = new Item(); |
|
214
|
|
|
|
|
215
|
|
|
$this->assertNull( $item->getId() ); |
|
216
|
|
|
$this->assertTrue( $item->getFingerprint()->isEmpty() ); |
|
217
|
|
|
$this->assertTrue( $item->getLabels()->isEmpty() ); |
|
218
|
|
|
$this->assertTrue( $item->getDescriptions()->isEmpty() ); |
|
219
|
|
|
$this->assertTrue( $item->getAliasGroups()->isEmpty() ); |
|
220
|
|
|
$this->assertTrue( $item->getSiteLinkList()->isEmpty() ); |
|
221
|
|
|
$this->assertTrue( $item->getStatements()->isEmpty() ); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
public function testCanConstructWithStatementList() { |
|
225
|
|
|
$statement = new Statement( new PropertyNoValueSnak( 42 ) ); |
|
226
|
|
|
$statement->setGuid( 'meh' ); |
|
227
|
|
|
|
|
228
|
|
|
$statements = new StatementList( $statement ); |
|
229
|
|
|
|
|
230
|
|
|
$item = new Item( null, null, null, $statements ); |
|
231
|
|
|
|
|
232
|
|
|
$this->assertEquals( |
|
233
|
|
|
$statements, |
|
234
|
|
|
$item->getStatements() |
|
235
|
|
|
); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
public function testSetStatements() { |
|
239
|
|
|
$item = new Item(); |
|
240
|
|
|
$item->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) ); |
|
241
|
|
|
|
|
242
|
|
|
$item->setStatements( new StatementList() ); |
|
243
|
|
|
$this->assertTrue( $item->getStatements()->isEmpty() ); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
public function equalsProvider() { |
|
247
|
|
|
$firstItem = new Item(); |
|
248
|
|
|
$firstItem->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) ); |
|
249
|
|
|
|
|
250
|
|
|
$secondItem = new Item(); |
|
251
|
|
|
$secondItem->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) ); |
|
252
|
|
|
|
|
253
|
|
|
$secondItemWithId = $secondItem->copy(); |
|
254
|
|
|
$secondItemWithId->setId( new ItemId( 'Q42' ) ); |
|
255
|
|
|
|
|
256
|
|
|
$differentId = $secondItemWithId->copy(); |
|
257
|
|
|
$differentId->setId( new ItemId( 'Q43' ) ); |
|
258
|
|
|
|
|
259
|
|
|
return [ |
|
260
|
|
|
[ new Item(), new Item() ], |
|
261
|
|
|
[ $firstItem, $secondItem ], |
|
262
|
|
|
[ $secondItem, $secondItemWithId ], |
|
263
|
|
|
[ $secondItemWithId, $differentId ], |
|
264
|
|
|
]; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* @dataProvider equalsProvider |
|
269
|
|
|
*/ |
|
270
|
|
|
public function testEquals( Item $firstItem, Item $secondItem ) { |
|
271
|
|
|
$this->assertTrue( $firstItem->equals( $secondItem ) ); |
|
272
|
|
|
$this->assertTrue( $secondItem->equals( $firstItem ) ); |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* @return Item |
|
277
|
|
|
*/ |
|
278
|
|
|
private function getBaseItem() { |
|
279
|
|
|
$item = new Item( new ItemId( 'Q42' ) ); |
|
280
|
|
|
$item->setLabel( 'en', 'Same' ); |
|
281
|
|
|
$item->setDescription( 'en', 'Same' ); |
|
282
|
|
|
$item->setAliases( 'en', [ 'Same' ] ); |
|
283
|
|
|
$item->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Same' ); |
|
284
|
|
|
$item->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) ); |
|
285
|
|
|
|
|
286
|
|
|
return $item; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
public function notEqualsProvider() { |
|
290
|
|
|
$differentLabel = $this->getBaseItem(); |
|
291
|
|
|
$differentLabel->setLabel( 'en', 'Different' ); |
|
292
|
|
|
|
|
293
|
|
|
$differentDescription = $this->getBaseItem(); |
|
294
|
|
|
$differentDescription->setDescription( 'en', 'Different' ); |
|
295
|
|
|
|
|
296
|
|
|
$differentAlias = $this->getBaseItem(); |
|
297
|
|
|
$differentAlias->setAliases( 'en', [ 'Different' ] ); |
|
298
|
|
|
|
|
299
|
|
|
$differentSiteLink = $this->getBaseItem(); |
|
300
|
|
|
$differentSiteLink->getSiteLinkList()->removeLinkWithSiteId( 'enwiki' ); |
|
301
|
|
|
$differentSiteLink->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Different' ); |
|
302
|
|
|
|
|
303
|
|
|
$differentStatement = $this->getBaseItem(); |
|
304
|
|
|
$differentStatement->setStatements( new StatementList() ); |
|
305
|
|
|
$differentStatement->getStatements()->addNewStatement( new PropertyNoValueSnak( 24 ) ); |
|
306
|
|
|
|
|
307
|
|
|
$item = $this->getBaseItem(); |
|
308
|
|
|
|
|
309
|
|
|
return [ |
|
310
|
|
|
'empty' => [ $item, new Item() ], |
|
311
|
|
|
'label' => [ $item, $differentLabel ], |
|
312
|
|
|
'description' => [ $item, $differentDescription ], |
|
313
|
|
|
'alias' => [ $item, $differentAlias ], |
|
314
|
|
|
'siteLink' => [ $item, $differentSiteLink ], |
|
315
|
|
|
'statement' => [ $item, $differentStatement ], |
|
316
|
|
|
]; |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
/** |
|
320
|
|
|
* @dataProvider notEqualsProvider |
|
321
|
|
|
*/ |
|
322
|
|
|
public function testNotEquals( Item $firstItem, Item $secondItem ) { |
|
323
|
|
|
$this->assertFalse( $firstItem->equals( $secondItem ) ); |
|
324
|
|
|
$this->assertFalse( $secondItem->equals( $firstItem ) ); |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
public function cloneProvider() { |
|
328
|
|
|
$item = new Item( new ItemId( 'Q1' ) ); |
|
329
|
|
|
$item->setLabel( 'en', 'original' ); |
|
330
|
|
|
$item->getStatements()->addNewStatement( new PropertyNoValueSnak( 1 ) ); |
|
331
|
|
|
$item->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Original' ); |
|
332
|
|
|
|
|
333
|
|
|
return [ |
|
334
|
|
|
'copy' => [ $item, $item->copy() ], |
|
335
|
|
|
'native clone' => [ $item, clone $item ], |
|
336
|
|
|
]; |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
/** |
|
340
|
|
|
* @dataProvider cloneProvider |
|
341
|
|
|
*/ |
|
342
|
|
|
public function testCloneIsEqualButNotIdentical( Item $original, Item $clone ) { |
|
343
|
|
|
$this->assertNotSame( $original, $clone ); |
|
344
|
|
|
$this->assertTrue( $original->equals( $clone ) ); |
|
345
|
|
|
$this->assertSame( |
|
346
|
|
|
$original->getId(), |
|
347
|
|
|
$clone->getId(), |
|
348
|
|
|
'id is immutable and must not be cloned' |
|
349
|
|
|
); |
|
350
|
|
|
|
|
351
|
|
|
// The clone must not reference the same mutable objects |
|
352
|
|
|
$this->assertNotSame( $original->getFingerprint(), $clone->getFingerprint() ); |
|
353
|
|
|
$this->assertNotSame( $original->getStatements(), $clone->getStatements() ); |
|
354
|
|
|
$this->assertNotSame( |
|
355
|
|
|
$original->getStatements()->getFirstStatementWithGuid( null ), |
|
356
|
|
|
$clone->getStatements()->getFirstStatementWithGuid( null ) |
|
357
|
|
|
); |
|
358
|
|
|
$this->assertNotSame( $original->getSiteLinkList(), $clone->getSiteLinkList() ); |
|
359
|
|
|
$this->assertSame( |
|
360
|
|
|
$original->getSiteLinkList()->getBySiteId( 'enwiki' ), |
|
361
|
|
|
$clone->getSiteLinkList()->getBySiteId( 'enwiki' ), |
|
362
|
|
|
'SiteLink is immutable and must not be cloned' |
|
363
|
|
|
); |
|
364
|
|
|
} |
|
365
|
|
|
|
|
366
|
|
|
/** |
|
367
|
|
|
* @dataProvider cloneProvider |
|
368
|
|
|
*/ |
|
369
|
|
|
public function testOriginalDoesNotChangeWithClone( Item $original, Item $clone ) { |
|
370
|
|
|
$originalStatement = $original->getStatements()->getFirstStatementWithGuid( null ); |
|
371
|
|
|
$clonedStatement = $clone->getStatements()->getFirstStatementWithGuid( null ); |
|
372
|
|
|
|
|
373
|
|
|
$clone->setLabel( 'en', 'clone' ); |
|
374
|
|
|
$clone->setDescription( 'en', 'clone' ); |
|
375
|
|
|
$clone->setAliases( 'en', [ 'clone' ] ); |
|
376
|
|
|
$clonedStatement->setGuid( 'clone' ); |
|
377
|
|
|
$clonedStatement->setMainSnak( new PropertySomeValueSnak( 666 ) ); |
|
378
|
|
|
$clonedStatement->setRank( Statement::RANK_DEPRECATED ); |
|
379
|
|
|
$clonedStatement->getQualifiers()->addSnak( new PropertyNoValueSnak( 1 ) ); |
|
380
|
|
|
$clonedStatement->getReferences()->addNewReference( new PropertyNoValueSnak( 1 ) ); |
|
381
|
|
|
$clone->getSiteLinkList()->removeLinkWithSiteId( 'enwiki' ); |
|
382
|
|
|
|
|
383
|
|
|
$this->assertSame( 'original', $original->getFingerprint()->getLabel( 'en' )->getText() ); |
|
384
|
|
|
$this->assertFalse( $original->getFingerprint()->hasDescription( 'en' ) ); |
|
385
|
|
|
$this->assertFalse( $original->getFingerprint()->hasAliasGroup( 'en' ) ); |
|
386
|
|
|
$this->assertNull( $originalStatement->getGuid() ); |
|
387
|
|
|
$this->assertSame( 'novalue', $originalStatement->getMainSnak()->getType() ); |
|
388
|
|
|
$this->assertSame( Statement::RANK_NORMAL, $originalStatement->getRank() ); |
|
389
|
|
|
$this->assertTrue( $originalStatement->getQualifiers()->isEmpty() ); |
|
390
|
|
|
$this->assertTrue( $originalStatement->getReferences()->isEmpty() ); |
|
391
|
|
|
$this->assertFalse( $original->getSiteLinkList()->isEmpty() ); |
|
392
|
|
|
} |
|
393
|
|
|
|
|
394
|
|
|
// Below are tests copied from EntityTest |
|
395
|
|
|
|
|
396
|
|
|
public function labelProvider() { |
|
397
|
|
|
return [ |
|
398
|
|
|
[ 'en', 'spam' ], |
|
399
|
|
|
[ 'en', 'spam', 'spam' ], |
|
400
|
|
|
[ 'de', 'foo bar baz' ], |
|
401
|
|
|
]; |
|
402
|
|
|
} |
|
403
|
|
|
|
|
404
|
|
|
/** |
|
405
|
|
|
* @dataProvider labelProvider |
|
406
|
|
|
* @param string $languageCode |
|
407
|
|
|
* @param string $labelText |
|
408
|
|
|
* @param string $moarText |
|
409
|
|
|
*/ |
|
410
|
|
|
public function testSetLabel( $languageCode, $labelText, $moarText = 'ohi there' ) { |
|
411
|
|
|
$entity = $this->getNewEmpty(); |
|
412
|
|
|
|
|
413
|
|
|
$entity->setLabel( $languageCode, $labelText ); |
|
414
|
|
|
|
|
415
|
|
|
$this->assertSame( $labelText, $entity->getFingerprint()->getLabel( $languageCode )->getText() ); |
|
416
|
|
|
|
|
417
|
|
|
$entity->setLabel( $languageCode, $moarText ); |
|
418
|
|
|
|
|
419
|
|
|
$this->assertSame( $moarText, $entity->getFingerprint()->getLabel( $languageCode )->getText() ); |
|
420
|
|
|
} |
|
421
|
|
|
|
|
422
|
|
|
public function descriptionProvider() { |
|
423
|
|
|
return [ |
|
424
|
|
|
[ 'en', 'spam' ], |
|
425
|
|
|
[ 'en', 'spam', 'spam' ], |
|
426
|
|
|
[ 'de', 'foo bar baz' ], |
|
427
|
|
|
]; |
|
428
|
|
|
} |
|
429
|
|
|
|
|
430
|
|
|
/** |
|
431
|
|
|
* @dataProvider descriptionProvider |
|
432
|
|
|
* @param string $languageCode |
|
433
|
|
|
* @param string $description |
|
434
|
|
|
* @param string $moarText |
|
435
|
|
|
*/ |
|
436
|
|
|
public function testSetDescription( $languageCode, $description, $moarText = 'ohi there' ) { |
|
437
|
|
|
$entity = $this->getNewEmpty(); |
|
438
|
|
|
|
|
439
|
|
|
$entity->setDescription( $languageCode, $description ); |
|
440
|
|
|
|
|
441
|
|
|
$this->assertSame( $description, $entity->getFingerprint()->getDescription( $languageCode )->getText() ); |
|
442
|
|
|
|
|
443
|
|
|
$entity->setDescription( $languageCode, $moarText ); |
|
444
|
|
|
|
|
445
|
|
|
$this->assertSame( $moarText, $entity->getFingerprint()->getDescription( $languageCode )->getText() ); |
|
446
|
|
|
} |
|
447
|
|
|
|
|
448
|
|
|
public function aliasesProvider() { |
|
449
|
|
|
return [ |
|
450
|
|
|
[ [ |
|
451
|
|
|
'en' => [ [ 'spam' ] ] |
|
452
|
|
|
] ], |
|
453
|
|
|
[ [ |
|
454
|
|
|
'en' => [ [ 'foo', 'bar', 'baz' ] ] |
|
455
|
|
|
] ], |
|
456
|
|
|
[ [ |
|
457
|
|
|
'en' => [ [ 'foo', 'bar' ], [ 'baz', 'spam' ] ] |
|
458
|
|
|
] ], |
|
459
|
|
|
[ [ |
|
460
|
|
|
'en' => [ [ 'foo', 'bar', 'baz' ] ], |
|
461
|
|
|
'de' => [ [ 'foobar' ], [ 'baz' ] ], |
|
462
|
|
|
] ], |
|
463
|
|
|
// with duplicates |
|
464
|
|
|
[ [ |
|
465
|
|
|
'en' => [ [ 'spam', 'ham', 'ham' ] ] |
|
466
|
|
|
] ], |
|
467
|
|
|
[ [ |
|
468
|
|
|
'en' => [ [ 'foo', 'bar' ], [ 'bar', 'spam' ] ] |
|
469
|
|
|
] ], |
|
470
|
|
|
]; |
|
471
|
|
|
} |
|
472
|
|
|
|
|
473
|
|
|
/** |
|
474
|
|
|
* @dataProvider aliasesProvider |
|
475
|
|
|
*/ |
|
476
|
|
|
public function testSetAliases( array $aliasesLists ) { |
|
477
|
|
|
$entity = $this->getNewEmpty(); |
|
478
|
|
|
|
|
479
|
|
|
foreach ( $aliasesLists as $langCode => $aliasesList ) { |
|
480
|
|
|
foreach ( $aliasesList as $aliases ) { |
|
481
|
|
|
$entity->setAliases( $langCode, $aliases ); |
|
482
|
|
|
} |
|
483
|
|
|
} |
|
484
|
|
|
|
|
485
|
|
|
foreach ( $aliasesLists as $langCode => $aliasesList ) { |
|
486
|
|
|
$expected = array_values( array_unique( array_pop( $aliasesList ) ) ); |
|
487
|
|
|
$actual = $entity->getFingerprint()->getAliasGroup( $langCode )->getAliases(); |
|
488
|
|
|
$this->assertSame( $expected, $actual ); |
|
489
|
|
|
} |
|
490
|
|
|
} |
|
491
|
|
|
|
|
492
|
|
|
/** |
|
493
|
|
|
* @dataProvider aliasesProvider |
|
494
|
|
|
*/ |
|
495
|
|
|
public function testSetEmptyAlias( array $aliasesLists ) { |
|
496
|
|
|
$entity = $this->getNewEmpty(); |
|
497
|
|
|
|
|
498
|
|
|
foreach ( $aliasesLists as $langCode => $aliasesList ) { |
|
499
|
|
|
foreach ( $aliasesList as $aliases ) { |
|
500
|
|
|
$entity->setAliases( $langCode, $aliases ); |
|
501
|
|
|
} |
|
502
|
|
|
} |
|
503
|
|
|
$entity->setAliases( 'zh', [ 'wind', 'air', '', 'fire' ] ); |
|
504
|
|
|
$entity->setAliases( 'zu', [ '', '' ] ); |
|
505
|
|
|
|
|
506
|
|
|
foreach ( $aliasesLists as $langCode => $aliasesList ) { |
|
507
|
|
|
$expected = array_values( array_unique( array_pop( $aliasesList ) ) ); |
|
508
|
|
|
asort( $aliasesList ); |
|
509
|
|
|
|
|
510
|
|
|
$actual = $entity->getFingerprint()->getAliasGroup( $langCode )->getAliases(); |
|
511
|
|
|
asort( $actual ); |
|
512
|
|
|
|
|
513
|
|
|
$this->assertEquals( $expected, $actual ); |
|
514
|
|
|
} |
|
515
|
|
|
} |
|
516
|
|
|
|
|
517
|
|
|
public function instanceProvider() { |
|
518
|
|
|
$entities = []; |
|
519
|
|
|
|
|
520
|
|
|
// empty |
|
521
|
|
|
$entity = $this->getNewEmpty(); |
|
522
|
|
|
$entities[] = $entity; |
|
523
|
|
|
|
|
524
|
|
|
// ID only |
|
525
|
|
|
$entity = clone $entity; |
|
526
|
|
|
$entity->setId( new ItemId( 'Q44' ) ); |
|
527
|
|
|
|
|
528
|
|
|
$entities[] = $entity; |
|
529
|
|
|
|
|
530
|
|
|
// with labels and stuff |
|
531
|
|
|
$entity = $this->getNewEmpty(); |
|
532
|
|
|
$entity->setAliases( 'en', [ 'o', 'noez' ] ); |
|
533
|
|
|
$entity->setLabel( 'de', 'spam' ); |
|
534
|
|
|
$entity->setDescription( 'en', 'foo bar baz' ); |
|
535
|
|
|
|
|
536
|
|
|
$entities[] = $entity; |
|
537
|
|
|
|
|
538
|
|
|
// with labels etc and ID |
|
539
|
|
|
$entity = clone $entity; |
|
540
|
|
|
$entity->setId( new ItemId( 'Q42' ) ); |
|
541
|
|
|
|
|
542
|
|
|
$entities[] = $entity; |
|
543
|
|
|
|
|
544
|
|
|
$argLists = []; |
|
545
|
|
|
|
|
546
|
|
|
foreach ( $entities as $entity ) { |
|
547
|
|
|
$argLists[] = [ $entity ]; |
|
548
|
|
|
} |
|
549
|
|
|
|
|
550
|
|
|
return $argLists; |
|
551
|
|
|
} |
|
552
|
|
|
|
|
553
|
|
|
/** |
|
554
|
|
|
* @dataProvider instanceProvider |
|
555
|
|
|
* @param Item $entity |
|
556
|
|
|
*/ |
|
557
|
|
|
public function testCopy( Item $entity ) { |
|
558
|
|
|
$copy = $entity->copy(); |
|
559
|
|
|
|
|
560
|
|
|
// The equality method alone is not enough since it does not check the IDs. |
|
561
|
|
|
$this->assertTrue( $entity->equals( $copy ) ); |
|
562
|
|
|
$this->assertEquals( $entity->getId(), $copy->getId() ); |
|
563
|
|
|
|
|
564
|
|
|
$this->assertNotSame( $entity, $copy ); |
|
565
|
|
|
} |
|
566
|
|
|
|
|
567
|
|
|
public function testCopyRetainsLabels() { |
|
568
|
|
|
$item = new Item(); |
|
569
|
|
|
|
|
570
|
|
|
$item->getFingerprint()->setLabel( 'en', 'foo' ); |
|
571
|
|
|
$item->getFingerprint()->setLabel( 'de', 'bar' ); |
|
572
|
|
|
|
|
573
|
|
|
$newItem = $item->copy(); |
|
574
|
|
|
|
|
575
|
|
|
$this->assertTrue( $newItem->getFingerprint()->getLabels()->hasTermForLanguage( 'en' ) ); |
|
576
|
|
|
$this->assertTrue( $newItem->getFingerprint()->getLabels()->hasTermForLanguage( 'de' ) ); |
|
577
|
|
|
} |
|
578
|
|
|
|
|
579
|
|
|
/** |
|
580
|
|
|
* @dataProvider instanceProvider |
|
581
|
|
|
* @param Item $entity |
|
582
|
|
|
*/ |
|
583
|
|
|
public function testSerialize( Item $entity ) { |
|
584
|
|
|
$string = serialize( $entity ); |
|
585
|
|
|
|
|
586
|
|
|
$this->assertInternalType( 'string', $string ); |
|
587
|
|
|
|
|
588
|
|
|
$instance = unserialize( $string ); |
|
589
|
|
|
|
|
590
|
|
|
$this->assertTrue( $entity->equals( $instance ) ); |
|
591
|
|
|
$this->assertEquals( $entity->getId(), $instance->getId() ); |
|
592
|
|
|
} |
|
593
|
|
|
|
|
594
|
|
|
public function testWhenNoStuffIsSet_getFingerprintReturnsEmptyFingerprint() { |
|
595
|
|
|
$entity = $this->getNewEmpty(); |
|
596
|
|
|
|
|
597
|
|
|
$this->assertEquals( |
|
598
|
|
|
new Fingerprint(), |
|
599
|
|
|
$entity->getFingerprint() |
|
600
|
|
|
); |
|
601
|
|
|
} |
|
602
|
|
|
|
|
603
|
|
|
public function testWhenLabelsAreSet_getFingerprintReturnsFingerprintWithLabels() { |
|
604
|
|
|
$entity = $this->getNewEmpty(); |
|
605
|
|
|
|
|
606
|
|
|
$entity->setLabel( 'en', 'foo' ); |
|
607
|
|
|
$entity->setLabel( 'de', 'bar' ); |
|
608
|
|
|
|
|
609
|
|
|
$this->assertEquals( |
|
610
|
|
|
new Fingerprint( |
|
611
|
|
|
new TermList( [ |
|
612
|
|
|
new Term( 'en', 'foo' ), |
|
613
|
|
|
new Term( 'de', 'bar' ), |
|
614
|
|
|
] ) |
|
615
|
|
|
), |
|
616
|
|
|
$entity->getFingerprint() |
|
617
|
|
|
); |
|
618
|
|
|
} |
|
619
|
|
|
|
|
620
|
|
|
public function testWhenTermsAreSet_getFingerprintReturnsFingerprintWithTerms() { |
|
621
|
|
|
$entity = $this->getNewEmpty(); |
|
622
|
|
|
|
|
623
|
|
|
$entity->setLabel( 'en', 'foo' ); |
|
624
|
|
|
$entity->setDescription( 'en', 'foo bar' ); |
|
625
|
|
|
$entity->setAliases( 'en', [ 'foo', 'bar' ] ); |
|
626
|
|
|
|
|
627
|
|
|
$this->assertEquals( |
|
628
|
|
|
new Fingerprint( |
|
629
|
|
|
new TermList( [ |
|
630
|
|
|
new Term( 'en', 'foo' ), |
|
631
|
|
|
] ), |
|
632
|
|
|
new TermList( [ |
|
633
|
|
|
new Term( 'en', 'foo bar' ) |
|
634
|
|
|
] ), |
|
635
|
|
|
new AliasGroupList( [ |
|
636
|
|
|
new AliasGroup( 'en', [ 'foo', 'bar' ] ) |
|
637
|
|
|
] ) |
|
638
|
|
|
), |
|
639
|
|
|
$entity->getFingerprint() |
|
640
|
|
|
); |
|
641
|
|
|
} |
|
642
|
|
|
|
|
643
|
|
|
public function testGivenEmptyFingerprint_noTermsAreSet() { |
|
644
|
|
|
$entity = $this->getNewEmpty(); |
|
645
|
|
|
$entity->setFingerprint( new Fingerprint() ); |
|
646
|
|
|
|
|
647
|
|
|
$this->assertTrue( $entity->getFingerprint()->isEmpty() ); |
|
648
|
|
|
} |
|
649
|
|
|
|
|
650
|
|
|
public function testGivenEmptyFingerprint_existingTermsAreRemoved() { |
|
651
|
|
|
$entity = $this->getNewEmpty(); |
|
652
|
|
|
|
|
653
|
|
|
$entity->setLabel( 'en', 'foo' ); |
|
654
|
|
|
$entity->setDescription( 'en', 'foo bar' ); |
|
655
|
|
|
$entity->setAliases( 'en', [ 'foo', 'bar' ] ); |
|
656
|
|
|
|
|
657
|
|
|
$entity->setFingerprint( new Fingerprint() ); |
|
658
|
|
|
|
|
659
|
|
|
$this->assertTrue( $entity->getFingerprint()->isEmpty() ); |
|
660
|
|
|
} |
|
661
|
|
|
|
|
662
|
|
|
public function testWhenSettingFingerprint_getFingerprintReturnsIt() { |
|
663
|
|
|
$fingerprint = new Fingerprint( |
|
664
|
|
|
new TermList( [ |
|
665
|
|
|
new Term( 'en', 'english label' ), |
|
666
|
|
|
] ), |
|
667
|
|
|
new TermList( [ |
|
668
|
|
|
new Term( 'en', 'english description' ) |
|
669
|
|
|
] ), |
|
670
|
|
|
new AliasGroupList( [ |
|
671
|
|
|
new AliasGroup( 'en', [ 'first en alias', 'second en alias' ] ) |
|
672
|
|
|
] ) |
|
673
|
|
|
); |
|
674
|
|
|
|
|
675
|
|
|
$entity = $this->getNewEmpty(); |
|
676
|
|
|
$entity->setFingerprint( $fingerprint ); |
|
677
|
|
|
$newFingerprint = $entity->getFingerprint(); |
|
678
|
|
|
|
|
679
|
|
|
$this->assertSame( $fingerprint, $newFingerprint ); |
|
680
|
|
|
} |
|
681
|
|
|
|
|
682
|
|
|
public function testGetLabels() { |
|
683
|
|
|
$item = new Item(); |
|
684
|
|
|
$item->setLabel( 'en', 'foo' ); |
|
685
|
|
|
|
|
686
|
|
|
$this->assertEquals( |
|
687
|
|
|
new TermList( [ |
|
688
|
|
|
new Term( 'en', 'foo' ) |
|
689
|
|
|
] ), |
|
690
|
|
|
$item->getLabels() |
|
691
|
|
|
); |
|
692
|
|
|
} |
|
693
|
|
|
|
|
694
|
|
|
public function testGetDescriptions() { |
|
695
|
|
|
$item = new Item(); |
|
696
|
|
|
$item->setDescription( 'en', 'foo bar' ); |
|
697
|
|
|
|
|
698
|
|
|
$this->assertEquals( |
|
699
|
|
|
new TermList( [ |
|
700
|
|
|
new Term( 'en', 'foo bar' ) |
|
701
|
|
|
] ), |
|
702
|
|
|
$item->getDescriptions() |
|
703
|
|
|
); |
|
704
|
|
|
} |
|
705
|
|
|
|
|
706
|
|
|
public function testGetAliasGroups() { |
|
707
|
|
|
$item = new Item(); |
|
708
|
|
|
$item->setAliases( 'en', [ 'foo', 'bar' ] ); |
|
709
|
|
|
|
|
710
|
|
|
$this->assertEquals( |
|
711
|
|
|
new AliasGroupList( [ |
|
712
|
|
|
new AliasGroup( 'en', [ 'foo', 'bar' ] ) |
|
713
|
|
|
] ), |
|
714
|
|
|
$item->getAliasGroups() |
|
715
|
|
|
); |
|
716
|
|
|
} |
|
717
|
|
|
|
|
718
|
|
|
public function testGetLabels_sameListAsFingerprint() { |
|
719
|
|
|
$item = new Item(); |
|
720
|
|
|
|
|
721
|
|
|
$this->assertSame( |
|
722
|
|
|
$item->getFingerprint()->getLabels(), |
|
723
|
|
|
$item->getLabels() |
|
724
|
|
|
); |
|
725
|
|
|
} |
|
726
|
|
|
|
|
727
|
|
|
public function testGetDescriptions_sameListAsFingerprint() { |
|
728
|
|
|
$item = new Item(); |
|
729
|
|
|
|
|
730
|
|
|
$this->assertSame( |
|
731
|
|
|
$item->getFingerprint()->getDescriptions(), |
|
732
|
|
|
$item->getDescriptions() |
|
733
|
|
|
); |
|
734
|
|
|
} |
|
735
|
|
|
|
|
736
|
|
|
public function testGetAliasGroups_sameListAsFingerprint() { |
|
737
|
|
|
$item = new Item(); |
|
738
|
|
|
|
|
739
|
|
|
$this->assertSame( |
|
740
|
|
|
$item->getFingerprint()->getAliasGroups(), |
|
741
|
|
|
$item->getAliasGroups() |
|
742
|
|
|
); |
|
743
|
|
|
} |
|
744
|
|
|
|
|
745
|
|
|
} |
|
746
|
|
|
|