Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / tests / phpunit / includes / site / CachingSiteStoreTest.php
blob7e37907be0c7e396a2a8c51e1ef06a211acf3d34
1 <?php
3 /**
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
19 * @file
20 * @since 1.25
22 * @ingroup Site
23 * @ingroup Test
25 * @group Site
26 * @group Database
28 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
30 class CachingSiteStoreTest extends MediaWikiTestCase {
32 /**
33 * @covers CachingSiteStore::getSites
35 public function testGetSites() {
36 $testSites = TestSites::getSites();
38 $store = new CachingSiteStore(
39 $this->getHashSiteStore( $testSites ),
40 wfGetMainCache()
43 $sites = $store->getSites();
45 $this->assertInstanceOf( 'SiteList', $sites );
47 /**
48 * @var Site $site
50 foreach ( $sites as $site ) {
51 $this->assertInstanceOf( 'Site', $site );
54 foreach ( $testSites as $site ) {
55 if ( $site->getGlobalId() !== null ) {
56 $this->assertTrue( $sites->hasSite( $site->getGlobalId() ) );
61 /**
62 * @covers CachingSiteStore::saveSites
64 public function testSaveSites() {
65 $store = new CachingSiteStore( new HashSiteStore(), wfGetMainCache() );
67 $sites = [];
69 $site = new Site();
70 $site->setGlobalId( 'ertrywuutr' );
71 $site->setLanguageCode( 'en' );
72 $sites[] = $site;
74 $site = new MediaWikiSite();
75 $site->setGlobalId( 'sdfhxujgkfpth' );
76 $site->setLanguageCode( 'nl' );
77 $sites[] = $site;
79 $this->assertTrue( $store->saveSites( $sites ) );
81 $site = $store->getSite( 'ertrywuutr' );
82 $this->assertInstanceOf( 'Site', $site );
83 $this->assertEquals( 'en', $site->getLanguageCode() );
85 $site = $store->getSite( 'sdfhxujgkfpth' );
86 $this->assertInstanceOf( 'Site', $site );
87 $this->assertEquals( 'nl', $site->getLanguageCode() );
90 /**
91 * @covers CachingSiteStore::reset
93 public function testReset() {
94 $dbSiteStore = $this->getMockBuilder( 'SiteStore' )
95 ->disableOriginalConstructor()
96 ->getMock();
98 $dbSiteStore->expects( $this->any() )
99 ->method( 'getSite' )
100 ->will( $this->returnValue( $this->getTestSite() ) );
102 $dbSiteStore->expects( $this->any() )
103 ->method( 'getSites' )
104 ->will( $this->returnCallback( function() {
105 $siteList = new SiteList();
106 $siteList->setSite( $this->getTestSite() );
108 return $siteList;
109 } ) );
111 $store = new CachingSiteStore( $dbSiteStore, wfGetMainCache() );
113 // initialize internal cache
114 $this->assertGreaterThan( 0, $store->getSites()->count(), 'count sites' );
116 $store->getSite( 'enwiki' )->setLanguageCode( 'en-ca' );
118 // sanity check: $store should have the new language code for 'enwiki'
119 $this->assertEquals( 'en-ca', $store->getSite( 'enwiki' )->getLanguageCode(), 'sanity check' );
121 // purge cache
122 $store->reset();
124 // the internal cache of $store should be updated, and now pulling
125 // the site from the 'fallback' DBSiteStore with the original language code.
126 $this->assertEquals( 'en', $store->getSite( 'enwiki' )->getLanguageCode(), 'reset' );
129 public function getTestSite() {
130 $enwiki = new MediaWikiSite();
131 $enwiki->setGlobalId( 'enwiki' );
132 $enwiki->setLanguageCode( 'en' );
134 return $enwiki;
138 * @covers CachingSiteStore::clear
140 public function testClear() {
141 $store = new CachingSiteStore( new HashSiteStore(), wfGetMainCache() );
142 $this->assertTrue( $store->clear() );
144 $site = $store->getSite( 'enwiki' );
145 $this->assertNull( $site );
147 $sites = $store->getSites();
148 $this->assertEquals( 0, $sites->count() );
152 * @param Site[] $sites
154 * @return SiteStore
156 private function getHashSiteStore( array $sites ) {
157 $siteStore = new HashSiteStore();
158 $siteStore->saveSites( $sites );
160 return $siteStore;