2015-02-06 02:00:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
2022-09-26 20:59:56 +00:00
|
|
|
* @covers CachingSiteStore
|
2015-02-06 02:00:26 +00:00
|
|
|
* @group Site
|
|
|
|
|
* @group Database
|
|
|
|
|
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
|
|
|
|
|
*/
|
2019-07-08 13:25:31 +00:00
|
|
|
class CachingSiteStoreTest extends \MediaWikiIntegrationTestCase {
|
2015-02-06 02:00:26 +00:00
|
|
|
|
|
|
|
|
public function testGetSites() {
|
|
|
|
|
$testSites = TestSites::getSites();
|
|
|
|
|
|
|
|
|
|
$store = new CachingSiteStore(
|
|
|
|
|
$this->getHashSiteStore( $testSites ),
|
2019-04-28 19:41:17 +00:00
|
|
|
ObjectCache::getLocalClusterInstance()
|
2015-02-06 02:00:26 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$sites = $store->getSites();
|
|
|
|
|
|
2018-01-13 00:02:09 +00:00
|
|
|
$this->assertInstanceOf( SiteList::class, $sites );
|
2015-02-06 02:00:26 +00:00
|
|
|
|
2022-09-26 20:59:56 +00:00
|
|
|
/** @var Site $site */
|
2015-02-06 02:00:26 +00:00
|
|
|
foreach ( $sites as $site ) {
|
2018-01-13 00:02:09 +00:00
|
|
|
$this->assertInstanceOf( Site::class, $site );
|
2015-02-06 02:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ( $testSites as $site ) {
|
|
|
|
|
if ( $site->getGlobalId() !== null ) {
|
|
|
|
|
$this->assertTrue( $sites->hasSite( $site->getGlobalId() ) );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testSaveSites() {
|
2019-04-28 19:41:17 +00:00
|
|
|
$store = new CachingSiteStore(
|
|
|
|
|
new HashSiteStore(), ObjectCache::getLocalClusterInstance()
|
|
|
|
|
);
|
2015-02-06 02:00:26 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$sites = [];
|
2015-02-06 02:00:26 +00:00
|
|
|
|
|
|
|
|
$site = new Site();
|
|
|
|
|
$site->setGlobalId( 'ertrywuutr' );
|
|
|
|
|
$site->setLanguageCode( 'en' );
|
|
|
|
|
$sites[] = $site;
|
|
|
|
|
|
|
|
|
|
$site = new MediaWikiSite();
|
|
|
|
|
$site->setGlobalId( 'sdfhxujgkfpth' );
|
|
|
|
|
$site->setLanguageCode( 'nl' );
|
|
|
|
|
$sites[] = $site;
|
|
|
|
|
|
|
|
|
|
$this->assertTrue( $store->saveSites( $sites ) );
|
|
|
|
|
|
|
|
|
|
$site = $store->getSite( 'ertrywuutr' );
|
2018-01-13 00:02:09 +00:00
|
|
|
$this->assertInstanceOf( Site::class, $site );
|
2015-02-06 02:00:26 +00:00
|
|
|
$this->assertEquals( 'en', $site->getLanguageCode() );
|
|
|
|
|
|
|
|
|
|
$site = $store->getSite( 'sdfhxujgkfpth' );
|
2018-01-13 00:02:09 +00:00
|
|
|
$this->assertInstanceOf( Site::class, $site );
|
2015-02-06 02:00:26 +00:00
|
|
|
$this->assertEquals( 'nl', $site->getLanguageCode() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testReset() {
|
2022-07-14 12:42:07 +00:00
|
|
|
$dbSiteStore = $this->createMock( SiteStore::class );
|
2015-02-06 02:00:26 +00:00
|
|
|
|
2021-04-22 08:28:11 +00:00
|
|
|
$dbSiteStore->method( 'getSite' )
|
|
|
|
|
->willReturn( $this->getTestSite() );
|
2015-02-06 02:00:26 +00:00
|
|
|
|
2021-04-22 08:40:46 +00:00
|
|
|
$dbSiteStore->method( 'getSites' )
|
2022-06-05 23:39:02 +00:00
|
|
|
->willReturnCallback( function () {
|
2015-02-06 02:00:26 +00:00
|
|
|
$siteList = new SiteList();
|
2016-02-04 21:09:07 +00:00
|
|
|
$siteList->setSite( $this->getTestSite() );
|
2015-02-06 02:00:26 +00:00
|
|
|
|
|
|
|
|
return $siteList;
|
2022-06-05 23:39:02 +00:00
|
|
|
} );
|
2015-02-06 02:00:26 +00:00
|
|
|
|
2019-04-28 19:41:17 +00:00
|
|
|
$store = new CachingSiteStore( $dbSiteStore, ObjectCache::getLocalClusterInstance() );
|
2015-02-06 02:00:26 +00:00
|
|
|
|
|
|
|
|
// initialize internal cache
|
|
|
|
|
$this->assertGreaterThan( 0, $store->getSites()->count(), 'count sites' );
|
|
|
|
|
|
|
|
|
|
$store->getSite( 'enwiki' )->setLanguageCode( 'en-ca' );
|
|
|
|
|
|
2021-11-21 16:23:11 +00:00
|
|
|
// check: $store should have the new language code for 'enwiki'
|
2021-11-21 19:13:24 +00:00
|
|
|
$this->assertEquals( 'en-ca', $store->getSite( 'enwiki' )->getLanguageCode() );
|
2015-02-06 02:00:26 +00:00
|
|
|
|
|
|
|
|
// purge cache
|
|
|
|
|
$store->reset();
|
|
|
|
|
|
|
|
|
|
// the internal cache of $store should be updated, and now pulling
|
|
|
|
|
// the site from the 'fallback' DBSiteStore with the original language code.
|
|
|
|
|
$this->assertEquals( 'en', $store->getSite( 'enwiki' )->getLanguageCode(), 'reset' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getTestSite() {
|
|
|
|
|
$enwiki = new MediaWikiSite();
|
|
|
|
|
$enwiki->setGlobalId( 'enwiki' );
|
|
|
|
|
$enwiki->setLanguageCode( 'en' );
|
|
|
|
|
return $enwiki;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testClear() {
|
2019-04-28 19:41:17 +00:00
|
|
|
$store = new CachingSiteStore(
|
|
|
|
|
new HashSiteStore(), ObjectCache::getLocalClusterInstance()
|
|
|
|
|
);
|
2015-02-06 02:00:26 +00:00
|
|
|
$this->assertTrue( $store->clear() );
|
|
|
|
|
|
|
|
|
|
$site = $store->getSite( 'enwiki' );
|
|
|
|
|
$this->assertNull( $site );
|
|
|
|
|
|
|
|
|
|
$sites = $store->getSites();
|
2019-09-17 14:31:49 +00:00
|
|
|
$this->assertSame( 0, $sites->count() );
|
2015-02-06 02:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
2017-01-10 10:51:49 +00:00
|
|
|
/**
|
|
|
|
|
* @param Site[] $sites
|
|
|
|
|
* @return SiteStore
|
|
|
|
|
*/
|
2015-02-06 02:00:26 +00:00
|
|
|
private function getHashSiteStore( array $sites ) {
|
|
|
|
|
$siteStore = new HashSiteStore();
|
|
|
|
|
$siteStore->saveSites( $sites );
|
|
|
|
|
return $siteStore;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|