4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
28 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
30 class CachingSiteStoreTest
extends MediaWikiTestCase
{
33 * @covers CachingSiteStore::getSites
35 public function testGetSites() {
36 $testSites = TestSites
::getSites();
38 $store = new CachingSiteStore(
39 $this->getHashSiteStore( $testSites ),
40 ObjectCache
::getLocalClusterInstance()
43 $sites = $store->getSites();
45 $this->assertInstanceOf( SiteList
::class, $sites );
50 foreach ( $sites as $site ) {
51 $this->assertInstanceOf( Site
::class, $site );
54 foreach ( $testSites as $site ) {
55 if ( $site->getGlobalId() !== null ) {
56 $this->assertTrue( $sites->hasSite( $site->getGlobalId() ) );
62 * @covers CachingSiteStore::saveSites
64 public function testSaveSites() {
65 $store = new CachingSiteStore(
66 new HashSiteStore(), ObjectCache
::getLocalClusterInstance()
72 $site->setGlobalId( 'ertrywuutr' );
73 $site->setLanguageCode( 'en' );
76 $site = new MediaWikiSite();
77 $site->setGlobalId( 'sdfhxujgkfpth' );
78 $site->setLanguageCode( 'nl' );
81 $this->assertTrue( $store->saveSites( $sites ) );
83 $site = $store->getSite( 'ertrywuutr' );
84 $this->assertInstanceOf( Site
::class, $site );
85 $this->assertEquals( 'en', $site->getLanguageCode() );
87 $site = $store->getSite( 'sdfhxujgkfpth' );
88 $this->assertInstanceOf( Site
::class, $site );
89 $this->assertEquals( 'nl', $site->getLanguageCode() );
93 * @covers CachingSiteStore::reset
95 public function testReset() {
96 $dbSiteStore = $this->getMockBuilder( SiteStore
::class )
97 ->disableOriginalConstructor()
100 $dbSiteStore->expects( $this->any() )
101 ->method( 'getSite' )
102 ->will( $this->returnValue( $this->getTestSite() ) );
104 $dbSiteStore->expects( $this->any() )
105 ->method( 'getSites' )
106 ->will( $this->returnCallback( function () {
107 $siteList = new SiteList();
108 $siteList->setSite( $this->getTestSite() );
113 $store = new CachingSiteStore( $dbSiteStore, ObjectCache
::getLocalClusterInstance() );
115 // initialize internal cache
116 $this->assertGreaterThan( 0, $store->getSites()->count(), 'count sites' );
118 $store->getSite( 'enwiki' )->setLanguageCode( 'en-ca' );
120 // sanity check: $store should have the new language code for 'enwiki'
121 $this->assertEquals( 'en-ca', $store->getSite( 'enwiki' )->getLanguageCode(), 'sanity check' );
126 // the internal cache of $store should be updated, and now pulling
127 // the site from the 'fallback' DBSiteStore with the original language code.
128 $this->assertEquals( 'en', $store->getSite( 'enwiki' )->getLanguageCode(), 'reset' );
131 public function getTestSite() {
132 $enwiki = new MediaWikiSite();
133 $enwiki->setGlobalId( 'enwiki' );
134 $enwiki->setLanguageCode( 'en' );
140 * @covers CachingSiteStore::clear
142 public function testClear() {
143 $store = new CachingSiteStore(
144 new HashSiteStore(), ObjectCache
::getLocalClusterInstance()
146 $this->assertTrue( $store->clear() );
148 $site = $store->getSite( 'enwiki' );
149 $this->assertNull( $site );
151 $sites = $store->getSites();
152 $this->assertEquals( 0, $sites->count() );
156 * @param Site[] $sites
160 private function getHashSiteStore( array $sites ) {
161 $siteStore = new HashSiteStore();
162 $siteStore->saveSites( $sites );