|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wikibase\EntityStore\MongoDB; |
|
4
|
|
|
|
|
5
|
|
|
use Deserializers\Exceptions\DeserializationException; |
|
6
|
|
|
use MongoBinData; |
|
7
|
|
|
use Wikibase\DataModel\Entity\BasicEntityIdParser; |
|
8
|
|
|
use Wikibase\DataModel\Entity\Item; |
|
9
|
|
|
use Wikibase\DataModel\Entity\ItemId; |
|
10
|
|
|
use Wikibase\DataModel\Term\AliasGroup; |
|
11
|
|
|
use Wikibase\DataModel\Term\AliasGroupList; |
|
12
|
|
|
use Wikibase\DataModel\Term\Fingerprint; |
|
13
|
|
|
use Wikibase\DataModel\Term\Term; |
|
14
|
|
|
use Wikibase\DataModel\Term\TermList; |
|
15
|
|
|
use Wikibase\EntityStore\EntityStore; |
|
16
|
|
|
use Wikibase\EntityStore\EntityStoreOptions; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @covers Wikibase\EntityStore\MongoDB\MongoDBDocumentBuilder |
|
20
|
|
|
* |
|
21
|
|
|
* @licence GPLv2+ |
|
22
|
|
|
* @author Thomas Pellissier Tanon |
|
23
|
|
|
*/ |
|
24
|
|
|
class MongoDBDocumentBuilderTest extends \PHPUnit_Framework_TestCase { |
|
25
|
|
|
|
|
26
|
|
|
public function testBuildDocumentForEntity() { |
|
27
|
|
|
$item = new Item( |
|
28
|
|
|
new ItemId( 'Q1' ), |
|
29
|
|
|
new Fingerprint( |
|
30
|
|
|
new TermList( [ new Term( 'en', 'foo ' ) ] ), |
|
31
|
|
|
new TermList( [ new Term( 'en', 'bar' ) ] ), |
|
32
|
|
|
new AliasGroupList( [ new AliasGroup( 'fr', [ 'BAZée', 'bat' ] ) ] ) |
|
33
|
|
|
) |
|
34
|
|
|
); |
|
35
|
|
|
|
|
36
|
|
|
$entitySerializerMock = $this->getMock( 'Serializers\Serializer' ); |
|
37
|
|
|
$entitySerializerMock->expects( $this->once() ) |
|
38
|
|
|
->method( 'serialize' ) |
|
39
|
|
|
->with( $this->equalTo( $item ) ) |
|
40
|
|
|
->willReturn( [ |
|
41
|
|
|
'type' => 'item', |
|
42
|
|
|
'id' => 'Q1', |
|
43
|
|
|
'labels' => [ |
|
44
|
|
|
'en' => [ 'language' => 'en', 'value' => 'foo ' ] |
|
45
|
|
|
], |
|
46
|
|
|
'descriptions' => [ |
|
47
|
|
|
'en' => [ 'language' => 'en', 'value' => 'bar' ], |
|
48
|
|
|
], |
|
49
|
|
|
'aliases' => [ |
|
50
|
|
|
'fr' => [ |
|
51
|
|
|
[ 'language' => 'fr', 'value' => 'BAZée' ], |
|
52
|
|
|
[ 'language' => 'fr', 'value' => 'bat' ] |
|
53
|
|
|
] |
|
54
|
|
|
], |
|
55
|
|
|
'claims' => [ |
|
56
|
|
|
'P1' => [ |
|
57
|
|
|
[ |
|
58
|
|
|
'mainsnak' => [ |
|
59
|
|
|
'snaktype' => 'value', |
|
60
|
|
|
'property' => 'P1', |
|
61
|
|
|
'datavalue' => [ |
|
62
|
|
|
'value' => 'foo', |
|
63
|
|
|
'type' => 'string' |
|
64
|
|
|
] |
|
65
|
|
|
] |
|
66
|
|
|
], |
|
67
|
|
|
[ |
|
68
|
|
|
'mainsnak' => [ |
|
69
|
|
|
'snaktype' => 'value', |
|
70
|
|
|
'property' => 'P1', |
|
71
|
|
|
'datavalue' => [ |
|
72
|
|
|
'value' => str_repeat( '0123456789', 20 ), |
|
73
|
|
|
'type' => 'string' |
|
74
|
|
|
] |
|
75
|
|
|
] |
|
76
|
|
|
], |
|
77
|
|
|
[ |
|
78
|
|
|
'mainsnak' => [ |
|
79
|
|
|
'snaktype' => 'value', |
|
80
|
|
|
'property' => 'P1', |
|
81
|
|
|
'datavalue' => [ |
|
82
|
|
|
'value' => [ 'entity-type' => 'item', 'numeric-id' => 1 ], |
|
83
|
|
|
'type' => 'wikibase-entityid' |
|
84
|
|
|
] |
|
85
|
|
|
] |
|
86
|
|
|
], |
|
87
|
|
|
[ |
|
88
|
|
|
'mainsnak' => [ |
|
89
|
|
|
'snaktype' => 'value', |
|
90
|
|
|
'property' => 'P1', |
|
91
|
|
|
'datavalue' => [ |
|
92
|
|
|
'value' => [ 'entity-type' => 'property', 'numeric-id' => 1 ], |
|
93
|
|
|
'type' => 'wikibase-entityid' |
|
94
|
|
|
] |
|
95
|
|
|
] |
|
96
|
|
|
] |
|
97
|
|
|
], |
|
98
|
|
|
'P2' => [ |
|
99
|
|
|
[ |
|
100
|
|
|
'mainsnak' => [ |
|
101
|
|
|
'snaktype' => 'value', |
|
102
|
|
|
'property' => 'P2', |
|
103
|
|
|
'datavalue' => [ |
|
104
|
|
|
'value' => [ 'time' => '+00000001952-03-11T00:00:00Z' ], |
|
105
|
|
|
'type' => 'time' |
|
106
|
|
|
] |
|
107
|
|
|
] |
|
108
|
|
|
], |
|
109
|
|
|
[ |
|
110
|
|
|
'mainsnak' => [ |
|
111
|
|
|
'snaktype' => 'value', |
|
112
|
|
|
'property' => 'P2', |
|
113
|
|
|
'datavalue' => [ |
|
114
|
|
|
'value' => [ 'latitude' => 1, 'longitude' => 1 ], |
|
115
|
|
|
'type' => 'globecoordinate' |
|
116
|
|
|
] |
|
117
|
|
|
] |
|
118
|
|
|
] |
|
119
|
|
|
] |
|
120
|
|
|
] |
|
121
|
|
|
] ); |
|
122
|
|
|
|
|
123
|
|
|
$entityDeserializerMock = $this->getMock( 'Deserializers\Deserializer' ); |
|
124
|
|
|
|
|
125
|
|
|
$documentBuilder = new MongoDBDocumentBuilder( |
|
126
|
|
|
$entitySerializerMock, |
|
127
|
|
|
$entityDeserializerMock, |
|
128
|
|
|
new BasicEntityIdParser(), |
|
129
|
|
|
new EntityStoreOptions([ EntityStore::OPTION_LANGUAGES => null ] ) |
|
130
|
|
|
); |
|
131
|
|
|
|
|
132
|
|
|
$this->assertEquals( |
|
133
|
|
|
[ |
|
134
|
|
|
'_id' => 'Q1', |
|
135
|
|
|
'type' => 'item', |
|
136
|
|
|
'id' => 'Q1', |
|
137
|
|
|
'labels' => [ |
|
138
|
|
|
'en' => [ 'language' => 'en', 'value' => 'foo ' ], |
|
139
|
|
|
], |
|
140
|
|
|
'descriptions' => [ |
|
141
|
|
|
'en' => [ 'language' => 'en', 'value' => 'bar' ], |
|
142
|
|
|
], |
|
143
|
|
|
'aliases' => [ |
|
144
|
|
|
'fr' => [ |
|
145
|
|
|
[ 'language' => 'fr', 'value' => 'BAZée' ], |
|
146
|
|
|
[ 'language' => 'fr', 'value' => 'bat' ] |
|
147
|
|
|
] |
|
148
|
|
|
], |
|
149
|
|
|
'claims' => [ |
|
150
|
|
|
'P1' => [ |
|
151
|
|
|
[ |
|
152
|
|
|
'mainsnak' => [ |
|
153
|
|
|
'snaktype' => 'value', |
|
154
|
|
|
'property' => 'P1', |
|
155
|
|
|
'datavalue' => [ |
|
156
|
|
|
'value' => 'foo', |
|
157
|
|
|
'type' => 'string' |
|
158
|
|
|
] |
|
159
|
|
|
] |
|
160
|
|
|
], |
|
161
|
|
|
[ |
|
162
|
|
|
'mainsnak' => [ |
|
163
|
|
|
'snaktype' => 'value', |
|
164
|
|
|
'property' => 'P1', |
|
165
|
|
|
'datavalue' => [ |
|
166
|
|
|
'value' => str_repeat( '0123456789', 20 ), |
|
167
|
|
|
'type' => 'string' |
|
168
|
|
|
] |
|
169
|
|
|
] |
|
170
|
|
|
], |
|
171
|
|
|
[ |
|
172
|
|
|
'mainsnak' => [ |
|
173
|
|
|
'snaktype' => 'value', |
|
174
|
|
|
'property' => 'P1', |
|
175
|
|
|
'datavalue' => [ |
|
176
|
|
|
'value' => [ 'entity-type' => 'item', 'numeric-id' => 1 ], |
|
177
|
|
|
'type' => 'wikibase-entityid' |
|
178
|
|
|
] |
|
179
|
|
|
] |
|
180
|
|
|
], |
|
181
|
|
|
[ |
|
182
|
|
|
'mainsnak' => [ |
|
183
|
|
|
'snaktype' => 'value', |
|
184
|
|
|
'property' => 'P1', |
|
185
|
|
|
'datavalue' => [ |
|
186
|
|
|
'value' => [ 'entity-type' => 'property', 'numeric-id' => 1 ], |
|
187
|
|
|
'type' => 'wikibase-entityid' |
|
188
|
|
|
] |
|
189
|
|
|
] |
|
190
|
|
|
] |
|
191
|
|
|
], |
|
192
|
|
|
'P2' => [ |
|
193
|
|
|
[ |
|
194
|
|
|
'mainsnak' => [ |
|
195
|
|
|
'snaktype' => 'value', |
|
196
|
|
|
'property' => 'P2', |
|
197
|
|
|
'datavalue' => [ |
|
198
|
|
|
'value' => [ 'time' => '+00000001952-03-11T00:00:00Z' ], |
|
199
|
|
|
'type' => 'time' |
|
200
|
|
|
] |
|
201
|
|
|
] |
|
202
|
|
|
], |
|
203
|
|
|
[ |
|
204
|
|
|
'mainsnak' => [ |
|
205
|
|
|
'snaktype' => 'value', |
|
206
|
|
|
'property' => 'P2', |
|
207
|
|
|
'datavalue' => [ |
|
208
|
|
|
'value' => [ 'latitude' => 1, 'longitude' => 1 ], |
|
209
|
|
|
'type' => 'globecoordinate' |
|
210
|
|
|
] |
|
211
|
|
|
] |
|
212
|
|
|
] |
|
213
|
|
|
] |
|
214
|
|
|
], |
|
215
|
|
|
'sterms' => [ |
|
216
|
|
|
'en' => [ new MongoBinData( md5( 'foo', true ), MongoBinData::GENERIC ) ], |
|
217
|
|
|
'fr' => [ |
|
218
|
|
|
new MongoBinData( md5( 'bazée', true ), MongoBinData::GENERIC ), |
|
219
|
|
|
new MongoBinData( md5( 'bat', true ), MongoBinData::GENERIC ) |
|
220
|
|
|
] |
|
221
|
|
|
], |
|
222
|
|
|
'sclaims' => [ |
|
223
|
|
|
'string' => [ 'P1-foo', 'P1-c902a17556796a9f97afa23bad130b04' ], |
|
224
|
|
|
'wikibase-entityid' => [ 'P1-Q1', 'P1-P1' ], |
|
225
|
|
|
'time' => [ 'P2-+00000001952-03-11T00:00:00Z' ] |
|
226
|
|
|
] |
|
227
|
|
|
], |
|
228
|
|
|
$documentBuilder->buildDocumentForEntity( $item ) |
|
229
|
|
|
); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
public function testBuildDocumentForEntityWithLanguageOption() { |
|
233
|
|
|
$item = new Item( |
|
234
|
|
|
new ItemId( 'Q1' ), |
|
235
|
|
|
new Fingerprint( |
|
236
|
|
|
new TermList( [ new Term( 'en', 'foo' ), new Term( 'de', 'bar' ) ] ), |
|
237
|
|
|
new TermList( [ new Term( 'en', 'bar' ) ] ), |
|
238
|
|
|
new AliasGroupList( [ |
|
239
|
|
|
new AliasGroup( 'fr', [ 'baz', 'bat' ] ), |
|
240
|
|
|
new AliasGroup( 'it', [ 'foo' ] ) |
|
241
|
|
|
] ) |
|
242
|
|
|
) |
|
243
|
|
|
); |
|
244
|
|
|
|
|
245
|
|
|
$entitySerializerMock = $this->getMock( 'Serializers\Serializer' ); |
|
246
|
|
|
$entitySerializerMock->expects( $this->once() ) |
|
247
|
|
|
->method( 'serialize' ) |
|
248
|
|
|
->with( $this->equalTo( $item ) ) |
|
249
|
|
|
->willReturn( [ |
|
250
|
|
|
'type' => 'item', |
|
251
|
|
|
'id' => 'Q1', |
|
252
|
|
|
'labels' => [ |
|
253
|
|
|
'en' => [ 'language' => 'en', 'value' => 'foo ' ], |
|
254
|
|
|
'de' => [ 'language' => 'en', 'value' => 'bar' ], |
|
255
|
|
|
], |
|
256
|
|
|
'descriptions' => [ |
|
257
|
|
|
'en' => [ 'language' => 'en', 'value' => 'bar' ], |
|
258
|
|
|
], |
|
259
|
|
|
'aliases' => [ |
|
260
|
|
|
'fr' => [ |
|
261
|
|
|
[ 'language' => 'fr', 'value' => 'BAzée' ], |
|
262
|
|
|
[ 'language' => 'fr', 'value' => 'bat' ] |
|
263
|
|
|
], |
|
264
|
|
|
'it' => [ [ 'language' => 'it', 'value' => 'goo' ] ] |
|
265
|
|
|
] |
|
266
|
|
|
] ); |
|
267
|
|
|
|
|
268
|
|
|
$entityDeserializerMock = $this->getMock( 'Deserializers\Deserializer' ); |
|
269
|
|
|
|
|
270
|
|
|
$documentBuilder = new MongoDBDocumentBuilder( |
|
271
|
|
|
$entitySerializerMock, |
|
272
|
|
|
$entityDeserializerMock, |
|
273
|
|
|
new BasicEntityIdParser(), |
|
274
|
|
|
new EntityStoreOptions([ EntityStore::OPTION_LANGUAGES => [ 'en', 'fr' ] ] ) |
|
275
|
|
|
); |
|
276
|
|
|
|
|
277
|
|
|
$this->assertEquals( |
|
278
|
|
|
[ |
|
279
|
|
|
'_id' => 'Q1', |
|
280
|
|
|
'id' => 'Q1', |
|
281
|
|
|
'type' => 'item', |
|
282
|
|
|
'labels' => [ |
|
283
|
|
|
'en' => [ 'language' => 'en', 'value' => 'foo ' ], |
|
284
|
|
|
], |
|
285
|
|
|
'descriptions' => [ |
|
286
|
|
|
'en' => [ 'language' => 'en', 'value' => 'bar' ], |
|
287
|
|
|
], |
|
288
|
|
|
'aliases' => [ |
|
289
|
|
|
'fr' => [ |
|
290
|
|
|
[ 'language' => 'fr', 'value' => 'BAzée' ], |
|
291
|
|
|
[ 'language' => 'fr', 'value' => 'bat' ] |
|
292
|
|
|
] |
|
293
|
|
|
], |
|
294
|
|
|
'sterms' => [ |
|
295
|
|
|
'en' => [ new MongoBinData( md5( 'foo', true ), MongoBinData::GENERIC ) ], |
|
296
|
|
|
'fr' => [ |
|
297
|
|
|
new MongoBinData( md5( 'bazée', true ), MongoBinData::GENERIC ), |
|
298
|
|
|
new MongoBinData( md5( 'bat', true ), MongoBinData::GENERIC ) |
|
299
|
|
|
] |
|
300
|
|
|
], |
|
301
|
|
|
'sclaims' => [] |
|
302
|
|
|
], |
|
303
|
|
|
$documentBuilder->buildDocumentForEntity( $item ) |
|
304
|
|
|
); |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
public function testBuildEntityForDocument() { |
|
308
|
|
|
$item = new Item( new ItemId( 'Q1' ) ); |
|
309
|
|
|
|
|
310
|
|
|
$entitySerializerMock = $this->getMock( 'Serializers\Serializer' ); |
|
311
|
|
|
|
|
312
|
|
|
$entityDeserializerMock = $this->getMock( 'Deserializers\Deserializer' ); |
|
313
|
|
|
$entityDeserializerMock->expects( $this->once() ) |
|
314
|
|
|
->method( 'deserialize' ) |
|
315
|
|
|
->with( $this->equalTo( [ 'id' => 'Q1' ] ) ) |
|
316
|
|
|
->willReturn( $item ); |
|
317
|
|
|
|
|
318
|
|
|
$documentBuilder = new MongoDBDocumentBuilder( |
|
319
|
|
|
$entitySerializerMock, |
|
320
|
|
|
$entityDeserializerMock, |
|
321
|
|
|
new BasicEntityIdParser(), |
|
322
|
|
|
new EntityStoreOptions([ EntityStore::OPTION_LANGUAGES => null ] ) |
|
323
|
|
|
); |
|
324
|
|
|
|
|
325
|
|
|
$this->assertEquals( |
|
326
|
|
|
$item, |
|
327
|
|
|
$documentBuilder->buildEntityForDocument( [ 'id' => 'Q1' ] ) |
|
328
|
|
|
); |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
public function testBuildEntityForDocumentWithException() { |
|
332
|
|
|
$entitySerializerMock = $this->getMock( 'Serializers\Serializer' ); |
|
333
|
|
|
|
|
334
|
|
|
$entityDeserializerMock = $this->getMock( 'Deserializers\Deserializer' ); |
|
335
|
|
|
$entityDeserializerMock->expects( $this->once() ) |
|
336
|
|
|
->method( 'deserialize' ) |
|
337
|
|
|
->with( $this->equalTo( [ 'i' => 'Q1' ] ) ) |
|
338
|
|
|
->willThrowException( new DeserializationException() ); |
|
339
|
|
|
|
|
340
|
|
|
$documentBuilder = new MongoDBDocumentBuilder( |
|
341
|
|
|
$entitySerializerMock, |
|
342
|
|
|
$entityDeserializerMock, |
|
343
|
|
|
new BasicEntityIdParser(), |
|
344
|
|
|
new EntityStoreOptions([ EntityStore::OPTION_LANGUAGES => null ] ) |
|
345
|
|
|
); |
|
346
|
|
|
|
|
347
|
|
|
$this->assertEquals( |
|
348
|
|
|
null, |
|
349
|
|
|
$documentBuilder->buildEntityForDocument( [ 'i' => 'Q1' ] ) |
|
350
|
|
|
); |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
/** |
|
354
|
|
|
* @dataProvider cleanTextForSearchProvider |
|
355
|
|
|
*/ |
|
356
|
|
|
public function testBuildTermForSearch( $text, $cleaned ) { |
|
357
|
|
|
$entitySerializerMock = $this->getMock( 'Serializers\Serializer' ); |
|
358
|
|
|
$entityDeserializerMock = $this->getMock( 'Deserializers\Deserializer' ); |
|
359
|
|
|
$documentBuilder = new MongoDBDocumentBuilder( |
|
360
|
|
|
$entitySerializerMock, |
|
361
|
|
|
$entityDeserializerMock, |
|
362
|
|
|
new BasicEntityIdParser(), |
|
363
|
|
|
new EntityStoreOptions([ EntityStore::OPTION_LANGUAGES => null ] ) |
|
364
|
|
|
); |
|
365
|
|
|
|
|
366
|
|
|
$this->assertEquals( |
|
367
|
|
|
$cleaned, |
|
368
|
|
|
$documentBuilder->cleanTextForSearch( $text ) |
|
369
|
|
|
); |
|
370
|
|
|
} |
|
371
|
|
|
|
|
372
|
|
|
public function cleanTextForSearchProvider() { |
|
373
|
|
|
return [ |
|
374
|
|
|
[ |
|
375
|
|
|
'test', |
|
376
|
|
|
new MongoBinData( md5( 'test', true ), MongoBinData::GENERIC ) |
|
377
|
|
|
], |
|
378
|
|
|
[ |
|
379
|
|
|
'TODO', |
|
380
|
|
|
new MongoBinData( md5( 'todo', true ), MongoBinData::GENERIC ) |
|
381
|
|
|
], |
|
382
|
|
|
[ |
|
383
|
|
|
'Être', |
|
384
|
|
|
new MongoBinData( md5( 'être', true ), MongoBinData::GENERIC ) |
|
385
|
|
|
], |
|
386
|
|
|
]; |
|
387
|
|
|
} |
|
388
|
|
|
|
|
389
|
|
|
public function testBuildEntityIdForDocument() { |
|
390
|
|
|
$entitySerializerMock = $this->getMock( 'Serializers\Serializer' ); |
|
391
|
|
|
$entityDeserializerMock = $this->getMock( 'Deserializers\Deserializer' ); |
|
392
|
|
|
$documentBuilder = new MongoDBDocumentBuilder( |
|
393
|
|
|
$entitySerializerMock, |
|
394
|
|
|
$entityDeserializerMock, |
|
395
|
|
|
new BasicEntityIdParser(), |
|
396
|
|
|
new EntityStoreOptions([ EntityStore::OPTION_LANGUAGES => null ] ) |
|
397
|
|
|
); |
|
398
|
|
|
|
|
399
|
|
|
$this->assertEquals( |
|
400
|
|
|
new ItemId( 'Q42' ), |
|
401
|
|
|
$documentBuilder->buildEntityIdForDocument( [ '_id' => 'Q42' ] ) |
|
402
|
|
|
); |
|
403
|
|
|
} |
|
404
|
|
|
|
|
405
|
|
|
public function testBuildEntityIdForDocumentWithException() { |
|
406
|
|
|
$entitySerializerMock = $this->getMock( 'Serializers\Serializer' ); |
|
407
|
|
|
$entityDeserializerMock = $this->getMock( 'Deserializers\Deserializer' ); |
|
408
|
|
|
$documentBuilder = new MongoDBDocumentBuilder( |
|
409
|
|
|
$entitySerializerMock, |
|
410
|
|
|
$entityDeserializerMock, |
|
411
|
|
|
new BasicEntityIdParser(), |
|
412
|
|
|
new EntityStoreOptions([ EntityStore::OPTION_LANGUAGES => null ] ) |
|
413
|
|
|
); |
|
414
|
|
|
|
|
415
|
|
|
$this->setExpectedException( 'Wikibase\DataModel\Entity\EntityIdParsingException' ); |
|
416
|
|
|
$documentBuilder->buildEntityIdForDocument( [] ); |
|
417
|
|
|
} |
|
418
|
|
|
} |
|
419
|
|
|
|