2014-08-16 00:26:42 +00:00
|
|
|
<?php
|
|
|
|
|
2022-11-02 20:14:24 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Cache\TitleLocalCache;
|
2023-12-11 15:33:08 +00:00
|
|
|
use MediaWiki\Title\Title;
|
cache: Reduce responsibility of subclass and remove unused parts
* Reduce responsibility of resolve() methods to only supplying
the resolves values.
Moved logic for populating the cache and clearing the queue
to the base class, and made 'lookups' private.
* The second parameter to LocalCache::add() is unused, and never passed.
Removed to avoid confusion.
* The getTargets() method is unused. Removed.
* The getLookups() method is unused. Removed.
* The internal 'lookups' member was being used both for its keys and its
values, but never at the same time. This seemed risky, especially in
EchoRevisionLocalCache::resolve() where the associative array was passed
directly to the 'where' clause of IDatabase::select(), which shouldn't
espect keys when creating the 'IN' clause.
Using only values would keep value types flexible, but would require
use of the less efficient in_array().
Keeping both keys and values and calling array_values() would work.
Using only keys also works and is simpler, so long only ints are used.
* The tests were swapping 'targets' MapCacheLRU with a HashBagOStuff.
Following-up 4939bff7, this was forgotten, but works because the two
called methods (get and set) exist in both, but still seems odd.
Fixed by using TestingAccessWrapper to act on the existing object
instead of swapping it out.
* Improved tests by asserting more of the observed behaviour and impact.
Change-Id: I530eeac8bf3b407b8c633e0e20c7d35cc49f7a9f
2018-08-17 01:45:57 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
|
|
|
|
2018-01-24 00:31:53 +00:00
|
|
|
/**
|
2022-11-02 20:14:24 +00:00
|
|
|
* @covers MediaWiki\Extension\Notifications\Cache\TitleLocalCache
|
2018-09-14 19:26:45 +00:00
|
|
|
* @group Database
|
2018-01-24 00:31:53 +00:00
|
|
|
*/
|
2022-11-02 20:14:24 +00:00
|
|
|
class TitleLocalCacheTest extends MediaWikiIntegrationTestCase {
|
2014-08-16 00:26:42 +00:00
|
|
|
|
cache: Reduce responsibility of subclass and remove unused parts
* Reduce responsibility of resolve() methods to only supplying
the resolves values.
Moved logic for populating the cache and clearing the queue
to the base class, and made 'lookups' private.
* The second parameter to LocalCache::add() is unused, and never passed.
Removed to avoid confusion.
* The getTargets() method is unused. Removed.
* The getLookups() method is unused. Removed.
* The internal 'lookups' member was being used both for its keys and its
values, but never at the same time. This seemed risky, especially in
EchoRevisionLocalCache::resolve() where the associative array was passed
directly to the 'where' clause of IDatabase::select(), which shouldn't
espect keys when creating the 'IN' clause.
Using only values would keep value types flexible, but would require
use of the less efficient in_array().
Keeping both keys and values and calling array_values() would work.
Using only keys also works and is simpler, so long only ints are used.
* The tests were swapping 'targets' MapCacheLRU with a HashBagOStuff.
Following-up 4939bff7, this was forgotten, but works because the two
called methods (get and set) exist in both, but still seems odd.
Fixed by using TestingAccessWrapper to act on the existing object
instead of swapping it out.
* Improved tests by asserting more of the observed behaviour and impact.
Change-Id: I530eeac8bf3b407b8c633e0e20c7d35cc49f7a9f
2018-08-17 01:45:57 +00:00
|
|
|
public function testAdd() {
|
2024-03-17 20:24:30 +00:00
|
|
|
$cache = $this->mockTitleLocalCache();
|
cache: Reduce responsibility of subclass and remove unused parts
* Reduce responsibility of resolve() methods to only supplying
the resolves values.
Moved logic for populating the cache and clearing the queue
to the base class, and made 'lookups' private.
* The second parameter to LocalCache::add() is unused, and never passed.
Removed to avoid confusion.
* The getTargets() method is unused. Removed.
* The getLookups() method is unused. Removed.
* The internal 'lookups' member was being used both for its keys and its
values, but never at the same time. This seemed risky, especially in
EchoRevisionLocalCache::resolve() where the associative array was passed
directly to the 'where' clause of IDatabase::select(), which shouldn't
espect keys when creating the 'IN' clause.
Using only values would keep value types flexible, but would require
use of the less efficient in_array().
Keeping both keys and values and calling array_values() would work.
Using only keys also works and is simpler, so long only ints are used.
* The tests were swapping 'targets' MapCacheLRU with a HashBagOStuff.
Following-up 4939bff7, this was forgotten, but works because the two
called methods (get and set) exist in both, but still seems odd.
Fixed by using TestingAccessWrapper to act on the existing object
instead of swapping it out.
* Improved tests by asserting more of the observed behaviour and impact.
Change-Id: I530eeac8bf3b407b8c633e0e20c7d35cc49f7a9f
2018-08-17 01:45:57 +00:00
|
|
|
|
2014-08-16 00:26:42 +00:00
|
|
|
$cache->add( 1 );
|
cache: Reduce responsibility of subclass and remove unused parts
* Reduce responsibility of resolve() methods to only supplying
the resolves values.
Moved logic for populating the cache and clearing the queue
to the base class, and made 'lookups' private.
* The second parameter to LocalCache::add() is unused, and never passed.
Removed to avoid confusion.
* The getTargets() method is unused. Removed.
* The getLookups() method is unused. Removed.
* The internal 'lookups' member was being used both for its keys and its
values, but never at the same time. This seemed risky, especially in
EchoRevisionLocalCache::resolve() where the associative array was passed
directly to the 'where' clause of IDatabase::select(), which shouldn't
espect keys when creating the 'IN' clause.
Using only values would keep value types flexible, but would require
use of the less efficient in_array().
Keeping both keys and values and calling array_values() would work.
Using only keys also works and is simpler, so long only ints are used.
* The tests were swapping 'targets' MapCacheLRU with a HashBagOStuff.
Following-up 4939bff7, this was forgotten, but works because the two
called methods (get and set) exist in both, but still seems odd.
Fixed by using TestingAccessWrapper to act on the existing object
instead of swapping it out.
* Improved tests by asserting more of the observed behaviour and impact.
Change-Id: I530eeac8bf3b407b8c633e0e20c7d35cc49f7a9f
2018-08-17 01:45:57 +00:00
|
|
|
$cache->add( 9 );
|
|
|
|
|
|
|
|
// Resolutions should be batched
|
2022-09-29 13:41:35 +00:00
|
|
|
$cache->expects( $this->once() )->method( 'resolve' )
|
cache: Reduce responsibility of subclass and remove unused parts
* Reduce responsibility of resolve() methods to only supplying
the resolves values.
Moved logic for populating the cache and clearing the queue
to the base class, and made 'lookups' private.
* The second parameter to LocalCache::add() is unused, and never passed.
Removed to avoid confusion.
* The getTargets() method is unused. Removed.
* The getLookups() method is unused. Removed.
* The internal 'lookups' member was being used both for its keys and its
values, but never at the same time. This seemed risky, especially in
EchoRevisionLocalCache::resolve() where the associative array was passed
directly to the 'where' clause of IDatabase::select(), which shouldn't
espect keys when creating the 'IN' clause.
Using only values would keep value types flexible, but would require
use of the less efficient in_array().
Keeping both keys and values and calling array_values() would work.
Using only keys also works and is simpler, so long only ints are used.
* The tests were swapping 'targets' MapCacheLRU with a HashBagOStuff.
Following-up 4939bff7, this was forgotten, but works because the two
called methods (get and set) exist in both, but still seems odd.
Fixed by using TestingAccessWrapper to act on the existing object
instead of swapping it out.
* Improved tests by asserting more of the observed behaviour and impact.
Change-Id: I530eeac8bf3b407b8c633e0e20c7d35cc49f7a9f
2018-08-17 01:45:57 +00:00
|
|
|
->with( [ 1, 9 ] )->willReturn( [] );
|
|
|
|
|
|
|
|
// Trigger
|
|
|
|
$cache->get( 9 );
|
2014-08-16 00:26:42 +00:00
|
|
|
}
|
|
|
|
|
cache: Reduce responsibility of subclass and remove unused parts
* Reduce responsibility of resolve() methods to only supplying
the resolves values.
Moved logic for populating the cache and clearing the queue
to the base class, and made 'lookups' private.
* The second parameter to LocalCache::add() is unused, and never passed.
Removed to avoid confusion.
* The getTargets() method is unused. Removed.
* The getLookups() method is unused. Removed.
* The internal 'lookups' member was being used both for its keys and its
values, but never at the same time. This seemed risky, especially in
EchoRevisionLocalCache::resolve() where the associative array was passed
directly to the 'where' clause of IDatabase::select(), which shouldn't
espect keys when creating the 'IN' clause.
Using only values would keep value types flexible, but would require
use of the less efficient in_array().
Keeping both keys and values and calling array_values() would work.
Using only keys also works and is simpler, so long only ints are used.
* The tests were swapping 'targets' MapCacheLRU with a HashBagOStuff.
Following-up 4939bff7, this was forgotten, but works because the two
called methods (get and set) exist in both, but still seems odd.
Fixed by using TestingAccessWrapper to act on the existing object
instead of swapping it out.
* Improved tests by asserting more of the observed behaviour and impact.
Change-Id: I530eeac8bf3b407b8c633e0e20c7d35cc49f7a9f
2018-08-17 01:45:57 +00:00
|
|
|
public function testGet() {
|
2024-03-17 20:24:30 +00:00
|
|
|
$cache = $this->mockTitleLocalCache();
|
cache: Reduce responsibility of subclass and remove unused parts
* Reduce responsibility of resolve() methods to only supplying
the resolves values.
Moved logic for populating the cache and clearing the queue
to the base class, and made 'lookups' private.
* The second parameter to LocalCache::add() is unused, and never passed.
Removed to avoid confusion.
* The getTargets() method is unused. Removed.
* The getLookups() method is unused. Removed.
* The internal 'lookups' member was being used both for its keys and its
values, but never at the same time. This seemed risky, especially in
EchoRevisionLocalCache::resolve() where the associative array was passed
directly to the 'where' clause of IDatabase::select(), which shouldn't
espect keys when creating the 'IN' clause.
Using only values would keep value types flexible, but would require
use of the less efficient in_array().
Keeping both keys and values and calling array_values() would work.
Using only keys also works and is simpler, so long only ints are used.
* The tests were swapping 'targets' MapCacheLRU with a HashBagOStuff.
Following-up 4939bff7, this was forgotten, but works because the two
called methods (get and set) exist in both, but still seems odd.
Fixed by using TestingAccessWrapper to act on the existing object
instead of swapping it out.
* Improved tests by asserting more of the observed behaviour and impact.
Change-Id: I530eeac8bf3b407b8c633e0e20c7d35cc49f7a9f
2018-08-17 01:45:57 +00:00
|
|
|
$cachePriv = TestingAccessWrapper::newFromObject( $cache );
|
2015-11-03 06:15:20 +00:00
|
|
|
|
2015-01-30 00:44:53 +00:00
|
|
|
// First title included in cache
|
2022-11-02 20:14:24 +00:00
|
|
|
$res1 = $this->insertPage( 'TitleLocalCacheTest_testGet1' );
|
cache: Reduce responsibility of subclass and remove unused parts
* Reduce responsibility of resolve() methods to only supplying
the resolves values.
Moved logic for populating the cache and clearing the queue
to the base class, and made 'lookups' private.
* The second parameter to LocalCache::add() is unused, and never passed.
Removed to avoid confusion.
* The getTargets() method is unused. Removed.
* The getLookups() method is unused. Removed.
* The internal 'lookups' member was being used both for its keys and its
values, but never at the same time. This seemed risky, especially in
EchoRevisionLocalCache::resolve() where the associative array was passed
directly to the 'where' clause of IDatabase::select(), which shouldn't
espect keys when creating the 'IN' clause.
Using only values would keep value types flexible, but would require
use of the less efficient in_array().
Keeping both keys and values and calling array_values() would work.
Using only keys also works and is simpler, so long only ints are used.
* The tests were swapping 'targets' MapCacheLRU with a HashBagOStuff.
Following-up 4939bff7, this was forgotten, but works because the two
called methods (get and set) exist in both, but still seems odd.
Fixed by using TestingAccessWrapper to act on the existing object
instead of swapping it out.
* Improved tests by asserting more of the observed behaviour and impact.
Change-Id: I530eeac8bf3b407b8c633e0e20c7d35cc49f7a9f
2018-08-17 01:45:57 +00:00
|
|
|
$cachePriv->targets->set( $res1['id'], $res1['title'] );
|
2015-11-03 06:15:20 +00:00
|
|
|
// Second title not in internal cache, resolves from db.
|
2022-11-02 20:14:24 +00:00
|
|
|
$res2 = $this->insertPage( 'TitleLocalCacheTest_testGet2' );
|
2022-09-29 13:41:35 +00:00
|
|
|
$cache->expects( $this->once() )->method( 'resolve' )
|
cache: Reduce responsibility of subclass and remove unused parts
* Reduce responsibility of resolve() methods to only supplying
the resolves values.
Moved logic for populating the cache and clearing the queue
to the base class, and made 'lookups' private.
* The second parameter to LocalCache::add() is unused, and never passed.
Removed to avoid confusion.
* The getTargets() method is unused. Removed.
* The getLookups() method is unused. Removed.
* The internal 'lookups' member was being used both for its keys and its
values, but never at the same time. This seemed risky, especially in
EchoRevisionLocalCache::resolve() where the associative array was passed
directly to the 'where' clause of IDatabase::select(), which shouldn't
espect keys when creating the 'IN' clause.
Using only values would keep value types flexible, but would require
use of the less efficient in_array().
Keeping both keys and values and calling array_values() would work.
Using only keys also works and is simpler, so long only ints are used.
* The tests were swapping 'targets' MapCacheLRU with a HashBagOStuff.
Following-up 4939bff7, this was forgotten, but works because the two
called methods (get and set) exist in both, but still seems odd.
Fixed by using TestingAccessWrapper to act on the existing object
instead of swapping it out.
* Improved tests by asserting more of the observed behaviour and impact.
Change-Id: I530eeac8bf3b407b8c633e0e20c7d35cc49f7a9f
2018-08-17 01:45:57 +00:00
|
|
|
->with( [ $res2['id'] ] )
|
|
|
|
->willReturn( [ $res2['id'] => $res2['title'] ] );
|
|
|
|
|
|
|
|
// Register demand for both
|
|
|
|
$cache->add( $res1['id'] );
|
|
|
|
$cache->add( $res2['id'] );
|
|
|
|
|
|
|
|
// Should not call resolve() for first title
|
|
|
|
$this->assertSame( $res1['title'], $cache->get( $res1['id'] ), 'First title' );
|
|
|
|
|
|
|
|
// Should resolve() for second title
|
|
|
|
$this->assertSame( $res2['title'], $cache->get( $res2['id'] ), 'Second title' );
|
2014-08-16 00:26:42 +00:00
|
|
|
}
|
|
|
|
|
cache: Reduce responsibility of subclass and remove unused parts
* Reduce responsibility of resolve() methods to only supplying
the resolves values.
Moved logic for populating the cache and clearing the queue
to the base class, and made 'lookups' private.
* The second parameter to LocalCache::add() is unused, and never passed.
Removed to avoid confusion.
* The getTargets() method is unused. Removed.
* The getLookups() method is unused. Removed.
* The internal 'lookups' member was being used both for its keys and its
values, but never at the same time. This seemed risky, especially in
EchoRevisionLocalCache::resolve() where the associative array was passed
directly to the 'where' clause of IDatabase::select(), which shouldn't
espect keys when creating the 'IN' clause.
Using only values would keep value types flexible, but would require
use of the less efficient in_array().
Keeping both keys and values and calling array_values() would work.
Using only keys also works and is simpler, so long only ints are used.
* The tests were swapping 'targets' MapCacheLRU with a HashBagOStuff.
Following-up 4939bff7, this was forgotten, but works because the two
called methods (get and set) exist in both, but still seems odd.
Fixed by using TestingAccessWrapper to act on the existing object
instead of swapping it out.
* Improved tests by asserting more of the observed behaviour and impact.
Change-Id: I530eeac8bf3b407b8c633e0e20c7d35cc49f7a9f
2018-08-17 01:45:57 +00:00
|
|
|
public function testClearAll() {
|
2024-03-17 20:24:30 +00:00
|
|
|
$cache = $this->mockTitleLocalCache();
|
2014-08-16 00:26:42 +00:00
|
|
|
|
cache: Reduce responsibility of subclass and remove unused parts
* Reduce responsibility of resolve() methods to only supplying
the resolves values.
Moved logic for populating the cache and clearing the queue
to the base class, and made 'lookups' private.
* The second parameter to LocalCache::add() is unused, and never passed.
Removed to avoid confusion.
* The getTargets() method is unused. Removed.
* The getLookups() method is unused. Removed.
* The internal 'lookups' member was being used both for its keys and its
values, but never at the same time. This seemed risky, especially in
EchoRevisionLocalCache::resolve() where the associative array was passed
directly to the 'where' clause of IDatabase::select(), which shouldn't
espect keys when creating the 'IN' clause.
Using only values would keep value types flexible, but would require
use of the less efficient in_array().
Keeping both keys and values and calling array_values() would work.
Using only keys also works and is simpler, so long only ints are used.
* The tests were swapping 'targets' MapCacheLRU with a HashBagOStuff.
Following-up 4939bff7, this was forgotten, but works because the two
called methods (get and set) exist in both, but still seems odd.
Fixed by using TestingAccessWrapper to act on the existing object
instead of swapping it out.
* Improved tests by asserting more of the observed behaviour and impact.
Change-Id: I530eeac8bf3b407b8c633e0e20c7d35cc49f7a9f
2018-08-17 01:45:57 +00:00
|
|
|
// Add 1 to cache
|
|
|
|
$cachePriv = TestingAccessWrapper::newFromObject( $cache );
|
|
|
|
$cachePriv->targets->set( 1, $this->mockTitle() );
|
|
|
|
// Add 2 and 3 to demand
|
|
|
|
$cache->add( 2 );
|
|
|
|
$cache->add( 3 );
|
2014-08-16 00:26:42 +00:00
|
|
|
$cache->clearAll();
|
cache: Reduce responsibility of subclass and remove unused parts
* Reduce responsibility of resolve() methods to only supplying
the resolves values.
Moved logic for populating the cache and clearing the queue
to the base class, and made 'lookups' private.
* The second parameter to LocalCache::add() is unused, and never passed.
Removed to avoid confusion.
* The getTargets() method is unused. Removed.
* The getLookups() method is unused. Removed.
* The internal 'lookups' member was being used both for its keys and its
values, but never at the same time. This seemed risky, especially in
EchoRevisionLocalCache::resolve() where the associative array was passed
directly to the 'where' clause of IDatabase::select(), which shouldn't
espect keys when creating the 'IN' clause.
Using only values would keep value types flexible, but would require
use of the less efficient in_array().
Keeping both keys and values and calling array_values() would work.
Using only keys also works and is simpler, so long only ints are used.
* The tests were swapping 'targets' MapCacheLRU with a HashBagOStuff.
Following-up 4939bff7, this was forgotten, but works because the two
called methods (get and set) exist in both, but still seems odd.
Fixed by using TestingAccessWrapper to act on the existing object
instead of swapping it out.
* Improved tests by asserting more of the observed behaviour and impact.
Change-Id: I530eeac8bf3b407b8c633e0e20c7d35cc49f7a9f
2018-08-17 01:45:57 +00:00
|
|
|
|
2019-10-23 10:28:30 +00:00
|
|
|
$this->assertNull( $cache->get( 1 ), 'Cache was cleared' );
|
cache: Reduce responsibility of subclass and remove unused parts
* Reduce responsibility of resolve() methods to only supplying
the resolves values.
Moved logic for populating the cache and clearing the queue
to the base class, and made 'lookups' private.
* The second parameter to LocalCache::add() is unused, and never passed.
Removed to avoid confusion.
* The getTargets() method is unused. Removed.
* The getLookups() method is unused. Removed.
* The internal 'lookups' member was being used both for its keys and its
values, but never at the same time. This seemed risky, especially in
EchoRevisionLocalCache::resolve() where the associative array was passed
directly to the 'where' clause of IDatabase::select(), which shouldn't
espect keys when creating the 'IN' clause.
Using only values would keep value types flexible, but would require
use of the less efficient in_array().
Keeping both keys and values and calling array_values() would work.
Using only keys also works and is simpler, so long only ints are used.
* The tests were swapping 'targets' MapCacheLRU with a HashBagOStuff.
Following-up 4939bff7, this was forgotten, but works because the two
called methods (get and set) exist in both, but still seems odd.
Fixed by using TestingAccessWrapper to act on the existing object
instead of swapping it out.
* Improved tests by asserting more of the observed behaviour and impact.
Change-Id: I530eeac8bf3b407b8c633e0e20c7d35cc49f7a9f
2018-08-17 01:45:57 +00:00
|
|
|
|
|
|
|
// Lookups batch was cleared
|
2022-09-29 13:41:35 +00:00
|
|
|
$cache->expects( $this->once() )->method( 'resolve' )
|
cache: Reduce responsibility of subclass and remove unused parts
* Reduce responsibility of resolve() methods to only supplying
the resolves values.
Moved logic for populating the cache and clearing the queue
to the base class, and made 'lookups' private.
* The second parameter to LocalCache::add() is unused, and never passed.
Removed to avoid confusion.
* The getTargets() method is unused. Removed.
* The getLookups() method is unused. Removed.
* The internal 'lookups' member was being used both for its keys and its
values, but never at the same time. This seemed risky, especially in
EchoRevisionLocalCache::resolve() where the associative array was passed
directly to the 'where' clause of IDatabase::select(), which shouldn't
espect keys when creating the 'IN' clause.
Using only values would keep value types flexible, but would require
use of the less efficient in_array().
Keeping both keys and values and calling array_values() would work.
Using only keys also works and is simpler, so long only ints are used.
* The tests were swapping 'targets' MapCacheLRU with a HashBagOStuff.
Following-up 4939bff7, this was forgotten, but works because the two
called methods (get and set) exist in both, but still seems odd.
Fixed by using TestingAccessWrapper to act on the existing object
instead of swapping it out.
* Improved tests by asserting more of the observed behaviour and impact.
Change-Id: I530eeac8bf3b407b8c633e0e20c7d35cc49f7a9f
2018-08-17 01:45:57 +00:00
|
|
|
->with( [ 4 ] )
|
|
|
|
->willReturn( [] );
|
|
|
|
$cache->add( 4 );
|
|
|
|
$cache->get( 4 );
|
2014-08-16 00:26:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-06-17 17:00:05 +00:00
|
|
|
* @return Title
|
2014-08-16 00:26:42 +00:00
|
|
|
*/
|
|
|
|
protected function mockTitle() {
|
2022-09-29 13:41:35 +00:00
|
|
|
$title = $this->createMock( Title::class );
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-08-16 00:26:42 +00:00
|
|
|
return $title;
|
|
|
|
}
|
2024-03-17 20:24:30 +00:00
|
|
|
|
|
|
|
private function mockTitleLocalCache(): TitleLocalCache {
|
|
|
|
$services = $this->getServiceContainer();
|
|
|
|
return $this->getMockBuilder( TitleLocalCache::class )
|
|
|
|
->setConstructorArgs( [
|
|
|
|
$services->getPageStore(),
|
|
|
|
$services->getTitleFactory()
|
|
|
|
] )
|
|
|
|
->onlyMethods( [ 'resolve' ] )->getMock();
|
|
|
|
}
|
2014-08-16 00:26:42 +00:00
|
|
|
}
|