1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\Lib\Tests; |
4
|
|
|
|
5
|
|
|
use MediaWikiIntegrationTestCase; |
6
|
|
|
use Wikibase\DataModel\Entity\EntityId; |
7
|
|
|
use Wikibase\DataModel\Entity\EntityRedirect; |
8
|
|
|
use Wikibase\DataModel\Entity\Item; |
9
|
|
|
use Wikibase\DataModel\Entity\ItemId; |
10
|
|
|
use Wikibase\DataModel\Entity\Property; |
11
|
|
|
use Wikibase\DataModel\Entity\PropertyId; |
12
|
|
|
use Wikibase\Lib\Store\EntityRevision; |
13
|
|
|
use Wikibase\Lib\Store\EntityRevisionLookup; |
14
|
|
|
use Wikibase\Lib\Store\RevisionedUnresolvedRedirectException; |
15
|
|
|
use Wikibase\Lib\Store\StorageException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Base class for testing EntityRevisionLookup implementations |
19
|
|
|
* |
20
|
|
|
* @covers \Wikibase\Lib\Store\EntityRevisionLookup |
21
|
|
|
* |
22
|
|
|
* @group Wikibase |
23
|
|
|
* |
24
|
|
|
* @license GPL-2.0-or-later |
25
|
|
|
* @author Daniel Kinzler |
26
|
|
|
*/ |
27
|
|
|
abstract class EntityRevisionLookupTestCase extends MediaWikiIntegrationTestCase { |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return EntityRevision[] |
31
|
|
|
*/ |
32
|
|
|
protected function getTestRevisions() { |
33
|
|
|
$entities = []; |
34
|
|
|
|
35
|
|
|
$item = new Item( new ItemId( 'Q42' ) ); |
36
|
|
|
|
37
|
|
|
$entities[11] = new EntityRevision( $item, 11, '20130101001100' ); |
38
|
|
|
|
39
|
|
|
$item = new Item( new ItemId( 'Q42' ) ); |
40
|
|
|
$item->setLabel( 'en', "Foo" ); |
41
|
|
|
|
42
|
|
|
$entities[12] = new EntityRevision( $item, 12, '20130101001200' ); |
43
|
|
|
|
44
|
|
|
$prop = new Property( new PropertyId( 'P753' ), null, 'string' ); |
45
|
|
|
|
46
|
|
|
$entities[13] = new EntityRevision( $prop, 13, '20130101001300' ); |
47
|
|
|
|
48
|
|
|
return $entities; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return EntityRedirect[] |
53
|
|
|
*/ |
54
|
|
|
protected function getTestRedirects() { |
55
|
|
|
return [ |
56
|
|
|
new EntityRedirect( new ItemId( 'Q23' ), new ItemId( 'Q42' ) ), |
57
|
|
|
]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function resolveLogicalRevision( $revision ) { |
61
|
|
|
return $revision; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return EntityRevisionLookup |
66
|
|
|
*/ |
67
|
|
|
protected function getEntityRevisionLookup() { |
68
|
|
|
$revisions = $this->getTestRevisions(); |
69
|
|
|
$redirects = $this->getTestRedirects(); |
70
|
|
|
|
71
|
|
|
$lookup = $this->newEntityRevisionLookup( $revisions, $redirects ); |
72
|
|
|
|
73
|
|
|
return $lookup; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param EntityRevision[] $entityRevisions |
78
|
|
|
* @param EntityRedirect[] $entityRedirects |
79
|
|
|
* |
80
|
|
|
* @return EntityRevisionLookup |
81
|
|
|
*/ |
82
|
|
|
abstract protected function newEntityRevisionLookup( array $entityRevisions, array $entityRedirects ); |
83
|
|
|
|
84
|
|
|
public function provideGetEntityRevision() { |
85
|
|
|
$cases = [ |
86
|
|
|
[ // #0: any revision |
87
|
|
|
new ItemId( 'q42' ), 0, true, |
88
|
|
|
], |
89
|
|
|
[ // #1: first revision |
90
|
|
|
new ItemId( 'q42' ), 11, true, |
91
|
|
|
], |
92
|
|
|
[ // #2: second revision |
93
|
|
|
new ItemId( 'q42' ), 12, true, |
94
|
|
|
], |
95
|
|
|
[ // #3: bad revision |
96
|
|
|
new ItemId( 'q42' ), 600000, false, StorageException::class, |
97
|
|
|
], |
98
|
|
|
[ // #4: wrong type |
99
|
|
|
new ItemId( 'q753' ), 0, false, |
100
|
|
|
], |
101
|
|
|
[ // #5: mismatching revision |
102
|
|
|
new PropertyId( 'p753' ), 11, false, StorageException::class, |
103
|
|
|
], |
104
|
|
|
[ // #6: some revision |
105
|
|
|
new PropertyId( 'p753' ), 0, true, |
106
|
|
|
], |
107
|
|
|
]; |
108
|
|
|
|
109
|
|
|
return $cases; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @dataProvider provideGetEntityRevision |
114
|
|
|
* |
115
|
|
|
* @param EntityId $id The entity to get |
116
|
|
|
* @param int $revision The revision to get (or 0 for latest) |
117
|
|
|
* @param bool $shouldExist |
118
|
|
|
* @param string|null $expectException |
119
|
|
|
*/ |
120
|
|
|
public function testGetEntityRevision( EntityId $id, $revision, $shouldExist, $expectException = null ) { |
121
|
|
|
if ( $expectException !== null ) { |
122
|
|
|
$this->expectException( $expectException ); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$revision = $this->resolveLogicalRevision( $revision ); |
126
|
|
|
|
127
|
|
|
$lookup = $this->getEntityRevisionLookup(); |
128
|
|
|
$entityRev = $lookup->getEntityRevision( $id, $revision ); |
129
|
|
|
|
130
|
|
|
if ( $shouldExist == true ) { |
|
|
|
|
131
|
|
|
$this->assertNotNull( $entityRev, "ID " . $id->__toString() ); |
132
|
|
|
$this->assertEquals( $id->__toString(), $entityRev->getEntity()->getId()->__toString() ); |
133
|
|
|
} else { |
134
|
|
|
$this->assertNull( $entityRev, "ID " . $id->__toString() ); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function provideGetEntityRevision_redirect() { |
139
|
|
|
foreach ( $this->getTestRedirects() as $redirect ) { |
140
|
|
|
yield [ $redirect->getEntityId(), $redirect->getTargetId() ]; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @dataProvider provideGetEntityRevision_redirect |
146
|
|
|
*/ |
147
|
|
|
public function testGetEntityRevision_redirect( EntityId $entityId, EntityId $expectedRedirect ) { |
148
|
|
|
$lookup = $this->getEntityRevisionLookup(); |
149
|
|
|
|
150
|
|
|
try { |
151
|
|
|
$lookup->getEntityRevision( $entityId ); |
152
|
|
|
$this->fail( 'Expected an UnresolvedRedirectException exception when looking up a redirect.' ); |
153
|
|
|
} catch ( RevisionedUnresolvedRedirectException $ex ) { |
154
|
|
|
$this->assertEquals( $expectedRedirect, $ex->getRedirectTargetId() ); |
155
|
|
|
$this->assertGreaterThan( 0, $ex->getRevisionId() ); |
156
|
|
|
$this->assertNotEmpty( $ex->getRevisionTimestamp() ); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function provideGetLatestRevisionId() { |
161
|
|
|
$cases = [ |
162
|
|
|
[ // #0 |
163
|
|
|
new ItemId( 'q42' ), 12, |
164
|
|
|
], |
165
|
|
|
[ // #1 |
166
|
|
|
new PropertyId( 'p753' ), 13, |
167
|
|
|
], |
168
|
|
|
]; |
169
|
|
|
|
170
|
|
|
return $cases; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @dataProvider provideGetLatestRevisionId |
175
|
|
|
* |
176
|
|
|
* @param EntityId $id The entity to check |
177
|
|
|
* @param int $expected |
178
|
|
|
*/ |
179
|
|
|
public function testGetLatestRevisionId( EntityId $id, $expected ) { |
180
|
|
|
$shouldNotBeCalled = function () { |
181
|
|
|
$this->fail( 'Should not be called' ); |
182
|
|
|
}; |
183
|
|
|
|
184
|
|
|
$lookup = $this->getEntityRevisionLookup(); |
185
|
|
|
$result = $lookup->getLatestRevisionId( $id ); |
186
|
|
|
$gotRevisionId = $result->onRedirect( $shouldNotBeCalled ) |
187
|
|
|
->onNonexistentEntity( $shouldNotBeCalled ) |
188
|
|
|
->onConcreteRevision( 'intval' ) |
189
|
|
|
->map(); |
190
|
|
|
|
191
|
|
|
$expected = $this->resolveLogicalRevision( $expected ); |
192
|
|
|
|
193
|
|
|
$this->assertEquals( $expected, $gotRevisionId ); |
194
|
|
|
|
195
|
|
|
$entityRev = $lookup->getEntityRevision( $id ); |
196
|
|
|
$this->assertInstanceOf( EntityRevision::class, $entityRev ); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function testGetLatestRevisionForMissing() { |
200
|
|
|
$shouldNotBeCalled = function () { |
201
|
|
|
$this->fail( 'Should not be called' ); |
202
|
|
|
}; |
203
|
|
|
|
204
|
|
|
$lookup = $this->getEntityRevisionLookup(); |
205
|
|
|
$itemId = new ItemId( 'Q753' ); |
206
|
|
|
|
207
|
|
|
$result = $lookup->getLatestRevisionId( $itemId ); |
208
|
|
|
$gotRevision = $result->onRedirect( $shouldNotBeCalled ) |
209
|
|
|
->onNonexistentEntity( |
210
|
|
|
function () { |
211
|
|
|
return 'non-existent'; |
212
|
|
|
} |
213
|
|
|
) |
214
|
|
|
->onConcreteRevision( $shouldNotBeCalled ) |
215
|
|
|
->map(); |
216
|
|
|
|
217
|
|
|
$this->assertEquals( 'non-existent', $gotRevision ); |
218
|
|
|
|
219
|
|
|
$entityRev = $lookup->getEntityRevision( $itemId ); |
220
|
|
|
$this->assertNull( $entityRev ); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
} |
224
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.