Merge "rdbms: add "secret" parameter to ChronologyProtector to use HMAC client IDs"
[lhc/web/wiklou.git] / tests / phpunit / includes / Storage / NameTableStoreTest.php
1 <?php
2
3 namespace MediaWiki\Tests\Storage;
4
5 use BagOStuff;
6 use EmptyBagOStuff;
7 use HashBagOStuff;
8 use MediaWiki\Storage\NameTableAccessException;
9 use MediaWiki\Storage\NameTableStore;
10 use MediaWikiTestCase;
11 use Psr\Log\NullLogger;
12 use WANObjectCache;
13 use Wikimedia\Rdbms\Database;
14 use Wikimedia\Rdbms\LoadBalancer;
15 use Wikimedia\TestingAccessWrapper;
16
17 /**
18 * @author Addshore
19 * @group Database
20 * @covers \MediaWiki\Storage\NameTableStore
21 */
22 class NameTableStoreTest extends MediaWikiTestCase {
23
24 public function setUp() {
25 $this->tablesUsed[] = 'slot_roles';
26 parent::setUp();
27 }
28
29 protected function addCoreDBData() {
30 // The default implementation causes the slot_roles to already have content. Skip that.
31 }
32
33 private function populateTable( $values ) {
34 $insertValues = [];
35 foreach ( $values as $name ) {
36 $insertValues[] = [ 'role_name' => $name ];
37 }
38 $this->db->insert( 'slot_roles', $insertValues );
39 }
40
41 private function getHashWANObjectCache( $cacheBag ) {
42 return new WANObjectCache( [ 'cache' => $cacheBag ] );
43 }
44
45 /**
46 * @param $db
47 * @return \PHPUnit_Framework_MockObject_MockObject|LoadBalancer
48 */
49 private function getMockLoadBalancer( $db ) {
50 $mock = $this->getMockBuilder( LoadBalancer::class )
51 ->disableOriginalConstructor()
52 ->getMock();
53 $mock->expects( $this->any() )
54 ->method( 'getConnection' )
55 ->willReturn( $db );
56 return $mock;
57 }
58
59 private function getCallCheckingDb( $insertCalls, $selectCalls ) {
60 $mock = $this->getMockBuilder( Database::class )
61 ->disableOriginalConstructor()
62 ->getMock();
63 $mock->expects( $this->exactly( $insertCalls ) )
64 ->method( 'insert' )
65 ->willReturnCallback( function ( ...$args ) {
66 return call_user_func_array( [ $this->db, 'insert' ], $args );
67 } );
68 $mock->expects( $this->exactly( $selectCalls ) )
69 ->method( 'select' )
70 ->willReturnCallback( function ( ...$args ) {
71 return call_user_func_array( [ $this->db, 'select' ], $args );
72 } );
73 $mock->expects( $this->exactly( $insertCalls ) )
74 ->method( 'affectedRows' )
75 ->willReturnCallback( function ( ...$args ) {
76 return call_user_func_array( [ $this->db, 'affectedRows' ], $args );
77 } );
78 $mock->expects( $this->any() )
79 ->method( 'insertId' )
80 ->willReturnCallback( function ( ...$args ) {
81 return call_user_func_array( [ $this->db, 'insertId' ], $args );
82 } );
83 $mock->expects( $this->any() )
84 ->method( 'query' )
85 ->willReturn( [] );
86 $mock->expects( $this->any() )
87 ->method( 'isOpen' )
88 ->willReturn( true );
89 $wrapper = TestingAccessWrapper::newFromObject( $mock );
90 $wrapper->queryLogger = new NullLogger();
91 return $mock;
92 }
93
94 private function getNameTableSqlStore(
95 BagOStuff $cacheBag,
96 $insertCalls,
97 $selectCalls,
98 $normalizationCallback = null,
99 $insertCallback = null
100 ) {
101 return new NameTableStore(
102 $this->getMockLoadBalancer( $this->getCallCheckingDb( $insertCalls, $selectCalls ) ),
103 $this->getHashWANObjectCache( $cacheBag ),
104 new NullLogger(),
105 'slot_roles', 'role_id', 'role_name',
106 $normalizationCallback,
107 false,
108 $insertCallback
109 );
110 }
111
112 public function provideGetAndAcquireId() {
113 return [
114 'no wancache, empty table' =>
115 [ new EmptyBagOStuff(), true, 1, [], 'foo', 1 ],
116 'no wancache, one matching value' =>
117 [ new EmptyBagOStuff(), false, 1, [ 'foo' ], 'foo', 1 ],
118 'no wancache, one not matching value' =>
119 [ new EmptyBagOStuff(), true, 1, [ 'bar' ], 'foo', 2 ],
120 'no wancache, multiple, one matching value' =>
121 [ new EmptyBagOStuff(), false, 1, [ 'foo', 'bar' ], 'bar', 2 ],
122 'no wancache, multiple, no matching value' =>
123 [ new EmptyBagOStuff(), true, 1, [ 'foo', 'bar' ], 'baz', 3 ],
124 'wancache, empty table' =>
125 [ new HashBagOStuff(), true, 1, [], 'foo', 1 ],
126 'wancache, one matching value' =>
127 [ new HashBagOStuff(), false, 1, [ 'foo' ], 'foo', 1 ],
128 'wancache, one not matching value' =>
129 [ new HashBagOStuff(), true, 1, [ 'bar' ], 'foo', 2 ],
130 'wancache, multiple, one matching value' =>
131 [ new HashBagOStuff(), false, 1, [ 'foo', 'bar' ], 'bar', 2 ],
132 'wancache, multiple, no matching value' =>
133 [ new HashBagOStuff(), true, 1, [ 'foo', 'bar' ], 'baz', 3 ],
134 ];
135 }
136
137 /**
138 * @dataProvider provideGetAndAcquireId
139 * @param BagOStuff $cacheBag to use in the WANObjectCache service
140 * @param bool $needsInsert Does the value we are testing need to be inserted?
141 * @param int $selectCalls Number of times the select DB method will be called
142 * @param string[] $existingValues to be added to the db table
143 * @param string $name name to acquire
144 * @param int $expectedId the id we expect the name to have
145 */
146 public function testGetAndAcquireId(
147 $cacheBag,
148 $needsInsert,
149 $selectCalls,
150 $existingValues,
151 $name,
152 $expectedId
153 ) {
154 // Make sure the table is empty!
155 $this->truncateTable( 'slot_roles' );
156
157 $this->populateTable( $existingValues );
158 $store = $this->getNameTableSqlStore( $cacheBag, (int)$needsInsert, $selectCalls );
159
160 // Some names will not initially exist
161 try {
162 $result = $store->getId( $name );
163 $this->assertSame( $expectedId, $result );
164 } catch ( NameTableAccessException $e ) {
165 if ( $needsInsert ) {
166 $this->assertTrue( true ); // Expected exception
167 } else {
168 $this->fail( 'Did not expect an exception, but got one: ' . $e->getMessage() );
169 }
170 }
171
172 // All names should return their id here
173 $this->assertSame( $expectedId, $store->acquireId( $name ) );
174
175 // acquireId inserted these names, so now everything should exist with getId
176 $this->assertSame( $expectedId, $store->getId( $name ) );
177
178 // calling getId again will also still work, and not result in more selects
179 $this->assertSame( $expectedId, $store->getId( $name ) );
180 }
181
182 public function provideTestGetAndAcquireIdNameNormalization() {
183 yield [ 'A', 'a', 'strtolower' ];
184 yield [ 'b', 'B', 'strtoupper' ];
185 yield [
186 'X',
187 'X',
188 function ( $name ) {
189 return $name;
190 }
191 ];
192 yield [ 'ZZ', 'ZZ-a', __CLASS__ . '::appendDashAToString' ];
193 }
194
195 public static function appendDashAToString( $string ) {
196 return $string . '-a';
197 }
198
199 /**
200 * @dataProvider provideTestGetAndAcquireIdNameNormalization
201 */
202 public function testGetAndAcquireIdNameNormalization(
203 $nameIn,
204 $nameOut,
205 $normalizationCallback
206 ) {
207 $store = $this->getNameTableSqlStore(
208 new EmptyBagOStuff(),
209 1,
210 1,
211 $normalizationCallback
212 );
213 $acquiredId = $store->acquireId( $nameIn );
214 $this->assertSame( $nameOut, $store->getName( $acquiredId ) );
215 }
216
217 public function provideGetName() {
218 return [
219 [ new HashBagOStuff(), 3, 2 ],
220 [ new EmptyBagOStuff(), 3, 3 ],
221 ];
222 }
223
224 /**
225 * @dataProvider provideGetName
226 */
227 public function testGetName( BagOStuff $cacheBag, $insertCalls, $selectCalls ) {
228 $now = microtime( true );
229 $cacheBag->setMockTime( $now );
230 // Check for operations to in-memory cache (IMC) and persistent cache (PC)
231 $store = $this->getNameTableSqlStore( $cacheBag, $insertCalls, $selectCalls );
232
233 // Get 1 ID and make sure getName returns correctly
234 $fooId = $store->acquireId( 'foo' ); // regen PC, set IMC, update IMC, tombstone PC
235 $now += 0.01;
236 $this->assertSame( 'foo', $store->getName( $fooId ) ); // use IMC
237 $now += 0.01;
238
239 // Get another ID and make sure getName returns correctly
240 $barId = $store->acquireId( 'bar' ); // update IMC, tombstone PC
241 $now += 0.01;
242 $this->assertSame( 'bar', $store->getName( $barId ) ); // use IMC
243 $now += 0.01;
244
245 // Blitz the cache and make sure it still returns
246 TestingAccessWrapper::newFromObject( $store )->tableCache = null; // clear IMC
247 $this->assertSame( 'foo', $store->getName( $fooId ) ); // regen interim PC, set IMC
248 $this->assertSame( 'bar', $store->getName( $barId ) ); // use IMC
249
250 // Blitz the cache again and get another ID and make sure getName returns correctly
251 TestingAccessWrapper::newFromObject( $store )->tableCache = null; // clear IMC
252 $bazId = $store->acquireId( 'baz' ); // set IMC using interim PC, update IMC, tombstone PC
253 $now += 0.01;
254 $this->assertSame( 'baz', $store->getName( $bazId ) ); // uses IMC
255 $this->assertSame( 'baz', $store->getName( $bazId ) ); // uses IMC
256 }
257
258 public function testGetName_masterFallback() {
259 $store = $this->getNameTableSqlStore( new EmptyBagOStuff(), 1, 2 );
260
261 // Insert a new name
262 $fooId = $store->acquireId( 'foo' );
263
264 // Empty the process cache, getCachedTable() will now return this empty array
265 TestingAccessWrapper::newFromObject( $store )->tableCache = [];
266
267 // getName should fallback to master, which is why we assert 2 selectCalls above
268 $this->assertSame( 'foo', $store->getName( $fooId ) );
269 }
270
271 public function testGetMap_empty() {
272 $this->populateTable( [] );
273 $store = $this->getNameTableSqlStore( new HashBagOStuff(), 0, 1 );
274 $table = $store->getMap();
275 $this->assertSame( [], $table );
276 }
277
278 public function testGetMap_twoValues() {
279 $this->populateTable( [ 'foo', 'bar' ] );
280 $store = $this->getNameTableSqlStore( new HashBagOStuff(), 0, 1 );
281
282 // We are using a cache, so 2 calls should only result in 1 select on the db
283 $store->getMap();
284 $table = $store->getMap();
285
286 $expected = [ 1 => 'foo', 2 => 'bar' ];
287 $this->assertSame( $expected, $table );
288 // Make sure the table returned is the same as the cached table
289 $this->assertSame( $expected, TestingAccessWrapper::newFromObject( $store )->tableCache );
290 }
291
292 public function testReloadMap() {
293 $this->populateTable( [ 'foo' ] );
294 $store = $this->getNameTableSqlStore( new HashBagOStuff(), 0, 2 );
295
296 // force load
297 $this->assertCount( 1, $store->getMap() );
298
299 // add more stuff to the table, so the cache gets out of sync
300 $this->populateTable( [ 'bar' ] );
301
302 $expected = [ 1 => 'foo', 2 => 'bar' ];
303 $this->assertSame( $expected, $store->reloadMap() );
304 $this->assertSame( $expected, $store->getMap() );
305 }
306
307 public function testCacheRaceCondition() {
308 $wanHashBag = new HashBagOStuff();
309 $store1 = $this->getNameTableSqlStore( $wanHashBag, 1, 1 );
310 $store2 = $this->getNameTableSqlStore( $wanHashBag, 1, 0 );
311 $store3 = $this->getNameTableSqlStore( $wanHashBag, 1, 1 );
312
313 // Cache the current table in the instances we will use
314 // This simulates multiple requests running simultaneously
315 $store1->getMap();
316 $store2->getMap();
317 $store3->getMap();
318
319 // Store 2 separate names using different instances
320 $fooId = $store1->acquireId( 'foo' );
321 $barId = $store2->acquireId( 'bar' );
322
323 // Each of these instances should be aware of what they have inserted
324 $this->assertSame( $fooId, $store1->acquireId( 'foo' ) );
325 $this->assertSame( $barId, $store2->acquireId( 'bar' ) );
326
327 // A new store should be able to get both of these new Ids
328 // Note: before there was a race condition here where acquireId( 'bar' ) would update the
329 // cache with data missing the 'foo' key that it was not aware of
330 $store4 = $this->getNameTableSqlStore( $wanHashBag, 0, 1 );
331 $this->assertSame( $fooId, $store4->getId( 'foo' ) );
332 $this->assertSame( $barId, $store4->getId( 'bar' ) );
333
334 // If a store with old cached data tries to acquire these we will get the same ids.
335 $this->assertSame( $fooId, $store3->acquireId( 'foo' ) );
336 $this->assertSame( $barId, $store3->acquireId( 'bar' ) );
337 }
338
339 public function testGetAndAcquireIdInsertCallback() {
340 // FIXME: fails under postgres
341 $this->markTestSkippedIfDbType( 'postgres' );
342
343 $store = $this->getNameTableSqlStore(
344 new EmptyBagOStuff(),
345 1,
346 1,
347 null,
348 function ( $insertFields ) {
349 $insertFields['role_id'] = 7251;
350 return $insertFields;
351 }
352 );
353 $this->assertSame( 7251, $store->acquireId( 'A' ) );
354 }
355
356 }