3 namespace MediaWiki\Tests\Storage
;
8 use MediaWiki\Storage\NameTableAccessException
;
9 use MediaWiki\Storage\NameTableStore
;
10 use MediaWikiTestCase
;
11 use Psr\Log\NullLogger
;
13 use Wikimedia\Rdbms\Database
;
14 use Wikimedia\Rdbms\LoadBalancer
;
15 use Wikimedia\TestingAccessWrapper
;
20 * @covers \MediaWiki\Storage\NameTableStore
22 class NameTableStoreTest
extends MediaWikiTestCase
{
24 public function setUp() {
25 $this->tablesUsed
[] = 'slot_roles';
29 protected function addCoreDBData() {
30 // The default implementation causes the slot_roles to already have content. Skip that.
33 private function populateTable( $values ) {
35 foreach ( $values as $name ) {
36 $insertValues[] = [ 'role_name' => $name ];
38 $this->db
->insert( 'slot_roles', $insertValues );
41 private function getHashWANObjectCache( $cacheBag ) {
42 return new WANObjectCache( [ 'cache' => $cacheBag ] );
47 * @return \PHPUnit_Framework_MockObject_MockObject|LoadBalancer
49 private function getMockLoadBalancer( $db ) {
50 $mock = $this->getMockBuilder( LoadBalancer
::class )
51 ->disableOriginalConstructor()
53 $mock->expects( $this->any() )
54 ->method( 'getConnection' )
59 private function getCallCheckingDb( $insertCalls, $selectCalls ) {
60 $mock = $this->getMockBuilder( Database
::class )
61 ->disableOriginalConstructor()
63 $mock->expects( $this->exactly( $insertCalls ) )
65 ->willReturnCallback( function () {
66 return call_user_func_array( [ $this->db
, 'insert' ], func_get_args() );
68 $mock->expects( $this->exactly( $selectCalls ) )
70 ->willReturnCallback( function () {
71 return call_user_func_array( [ $this->db
, 'select' ], func_get_args() );
73 $mock->expects( $this->exactly( $insertCalls ) )
74 ->method( 'affectedRows' )
75 ->willReturnCallback( function () {
76 return call_user_func_array( [ $this->db
, 'affectedRows' ], func_get_args() );
78 $mock->expects( $this->any() )
79 ->method( 'insertId' )
80 ->willReturnCallback( function () {
81 return call_user_func_array( [ $this->db
, 'insertId' ], func_get_args() );
86 private function getNameTableSqlStore(
90 $normalizationCallback = null,
91 $insertCallback = null
93 return new NameTableStore(
94 $this->getMockLoadBalancer( $this->getCallCheckingDb( $insertCalls, $selectCalls ) ),
95 $this->getHashWANObjectCache( $cacheBag ),
97 'slot_roles', 'role_id', 'role_name',
98 $normalizationCallback,
104 public function provideGetAndAcquireId() {
106 'no wancache, empty table' =>
107 [ new EmptyBagOStuff(), true, 1, [], 'foo', 1 ],
108 'no wancache, one matching value' =>
109 [ new EmptyBagOStuff(), false, 1, [ 'foo' ], 'foo', 1 ],
110 'no wancache, one not matching value' =>
111 [ new EmptyBagOStuff(), true, 1, [ 'bar' ], 'foo', 2 ],
112 'no wancache, multiple, one matching value' =>
113 [ new EmptyBagOStuff(), false, 1, [ 'foo', 'bar' ], 'bar', 2 ],
114 'no wancache, multiple, no matching value' =>
115 [ new EmptyBagOStuff(), true, 1, [ 'foo', 'bar' ], 'baz', 3 ],
116 'wancache, empty table' =>
117 [ new HashBagOStuff(), true, 1, [], 'foo', 1 ],
118 'wancache, one matching value' =>
119 [ new HashBagOStuff(), false, 1, [ 'foo' ], 'foo', 1 ],
120 'wancache, one not matching value' =>
121 [ new HashBagOStuff(), true, 1, [ 'bar' ], 'foo', 2 ],
122 'wancache, multiple, one matching value' =>
123 [ new HashBagOStuff(), false, 1, [ 'foo', 'bar' ], 'bar', 2 ],
124 'wancache, multiple, no matching value' =>
125 [ new HashBagOStuff(), true, 1, [ 'foo', 'bar' ], 'baz', 3 ],
130 * @dataProvider provideGetAndAcquireId
131 * @param BagOStuff $cacheBag to use in the WANObjectCache service
132 * @param bool $needsInsert Does the value we are testing need to be inserted?
133 * @param int $selectCalls Number of times the select DB method will be called
134 * @param string[] $existingValues to be added to the db table
135 * @param string $name name to acquire
136 * @param int $expectedId the id we expect the name to have
138 public function testGetAndAcquireId(
146 // Make sure the table is empty!
147 $this->truncateTable( 'slot_roles' );
149 $this->populateTable( $existingValues );
150 $store = $this->getNameTableSqlStore( $cacheBag, (int)$needsInsert, $selectCalls );
152 // Some names will not initially exist
154 $result = $store->getId( $name );
155 $this->assertSame( $expectedId, $result );
156 } catch ( NameTableAccessException
$e ) {
157 if ( $needsInsert ) {
158 $this->assertTrue( true ); // Expected exception
160 $this->fail( 'Did not expect an exception, but got one: ' . $e->getMessage() );
164 // All names should return their id here
165 $this->assertSame( $expectedId, $store->acquireId( $name ) );
167 // acquireId inserted these names, so now everything should exist with getId
168 $this->assertSame( $expectedId, $store->getId( $name ) );
170 // calling getId again will also still work, and not result in more selects
171 $this->assertSame( $expectedId, $store->getId( $name ) );
174 public function provideTestGetAndAcquireIdNameNormalization() {
175 yield
[ 'A', 'a', 'strtolower' ];
176 yield
[ 'b', 'B', 'strtoupper' ];
184 yield
[ 'ZZ', 'ZZ-a', __CLASS__
. '::appendDashAToString' ];
187 public static function appendDashAToString( $string ) {
188 return $string . '-a';
192 * @dataProvider provideTestGetAndAcquireIdNameNormalization
194 public function testGetAndAcquireIdNameNormalization(
197 $normalizationCallback
199 $store = $this->getNameTableSqlStore(
200 new EmptyBagOStuff(),
203 $normalizationCallback
205 $acquiredId = $store->acquireId( $nameIn );
206 $this->assertSame( $nameOut, $store->getName( $acquiredId ) );
209 public function provideGetName() {
211 [ new HashBagOStuff(), 3, 3 ],
212 [ new EmptyBagOStuff(), 3, 3 ],
217 * @dataProvider provideGetName
219 public function testGetName( $cacheBag, $insertCalls, $selectCalls ) {
220 $store = $this->getNameTableSqlStore( $cacheBag, $insertCalls, $selectCalls );
222 // Get 1 ID and make sure getName returns correctly
223 $fooId = $store->acquireId( 'foo' );
224 $this->assertSame( 'foo', $store->getName( $fooId ) );
226 // Get another ID and make sure getName returns correctly
227 $barId = $store->acquireId( 'bar' );
228 $this->assertSame( 'bar', $store->getName( $barId ) );
230 // Blitz the cache and make sure it still returns
231 TestingAccessWrapper
::newFromObject( $store )->tableCache
= null;
232 $this->assertSame( 'foo', $store->getName( $fooId ) );
233 $this->assertSame( 'bar', $store->getName( $barId ) );
235 // Blitz the cache again and get another ID and make sure getName returns correctly
236 TestingAccessWrapper
::newFromObject( $store )->tableCache
= null;
237 $bazId = $store->acquireId( 'baz' );
238 $this->assertSame( 'baz', $store->getName( $bazId ) );
239 $this->assertSame( 'baz', $store->getName( $bazId ) );
242 public function testGetName_masterFallback() {
243 $store = $this->getNameTableSqlStore( new EmptyBagOStuff(), 1, 2 );
246 $fooId = $store->acquireId( 'foo' );
248 // Empty the process cache, getCachedTable() will now return this empty array
249 TestingAccessWrapper
::newFromObject( $store )->tableCache
= [];
251 // getName should fallback to master, which is why we assert 2 selectCalls above
252 $this->assertSame( 'foo', $store->getName( $fooId ) );
255 public function testGetMap_empty() {
256 $this->populateTable( [] );
257 $store = $this->getNameTableSqlStore( new HashBagOStuff(), 0, 1 );
258 $table = $store->getMap();
259 $this->assertSame( [], $table );
262 public function testGetMap_twoValues() {
263 $this->populateTable( [ 'foo', 'bar' ] );
264 $store = $this->getNameTableSqlStore( new HashBagOStuff(), 0, 1 );
266 // We are using a cache, so 2 calls should only result in 1 select on the db
268 $table = $store->getMap();
270 $expected = [ 1 => 'foo', 2 => 'bar' ];
271 $this->assertSame( $expected, $table );
272 // Make sure the table returned is the same as the cached table
273 $this->assertSame( $expected, TestingAccessWrapper
::newFromObject( $store )->tableCache
);
276 public function testReloadMap() {
277 $this->populateTable( [ 'foo' ] );
278 $store = $this->getNameTableSqlStore( new HashBagOStuff(), 0, 2 );
281 $this->assertCount( 1, $store->getMap() );
283 // add more stuff to the table, so the cache gets out of sync
284 $this->populateTable( [ 'bar' ] );
286 $expected = [ 1 => 'foo', 2 => 'bar' ];
287 $this->assertSame( $expected, $store->reloadMap() );
288 $this->assertSame( $expected, $store->getMap() );
291 public function testCacheRaceCondition() {
292 $wanHashBag = new HashBagOStuff();
293 $store1 = $this->getNameTableSqlStore( $wanHashBag, 1, 1 );
294 $store2 = $this->getNameTableSqlStore( $wanHashBag, 1, 0 );
295 $store3 = $this->getNameTableSqlStore( $wanHashBag, 1, 1 );
297 // Cache the current table in the instances we will use
298 // This simulates multiple requests running simultaneously
303 // Store 2 separate names using different instances
304 $fooId = $store1->acquireId( 'foo' );
305 $barId = $store2->acquireId( 'bar' );
307 // Each of these instances should be aware of what they have inserted
308 $this->assertSame( $fooId, $store1->acquireId( 'foo' ) );
309 $this->assertSame( $barId, $store2->acquireId( 'bar' ) );
311 // A new store should be able to get both of these new Ids
312 // Note: before there was a race condition here where acquireId( 'bar' ) would update the
313 // cache with data missing the 'foo' key that it was not aware of
314 $store4 = $this->getNameTableSqlStore( $wanHashBag, 0, 1 );
315 $this->assertSame( $fooId, $store4->getId( 'foo' ) );
316 $this->assertSame( $barId, $store4->getId( 'bar' ) );
318 // If a store with old cached data tries to acquire these we will get the same ids.
319 $this->assertSame( $fooId, $store3->acquireId( 'foo' ) );
320 $this->assertSame( $barId, $store3->acquireId( 'bar' ) );
323 public function testGetAndAcquireIdInsertCallback() {
324 // FIXME: fails under postgres
325 $this->markTestSkippedIfDbType( 'postgres' );
327 $store = $this->getNameTableSqlStore(
328 new EmptyBagOStuff(),
332 function ( $insertFields ) {
333 $insertFields['role_id'] = 7251;
334 return $insertFields;
337 $this->assertSame( 7251, $store->acquireId( 'A' ) );