LocalisationCache: try harder to use LCStoreCDB
[lhc/web/wiklou.git] / tests / phpunit / includes / site / CachingSiteStoreTest.php
1 <?php
2
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.
8 *
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.
13 *
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
18 *
19 * @file
20 * @since 1.25
21 *
22 * @ingroup Site
23 * @ingroup Test
24 *
25 * @group Site
26 * @group Database
27 *
28 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
29 */
30 class CachingSiteStoreTest extends MediaWikiTestCase {
31
32 /**
33 * @covers CachingSiteStore::getSites
34 */
35 public function testGetSites() {
36 $testSites = TestSites::getSites();
37
38 $store = new CachingSiteStore(
39 $this->getHashSiteStore( $testSites ),
40 wfGetMainCache()
41 );
42
43 $sites = $store->getSites();
44
45 $this->assertInstanceOf( 'SiteList', $sites );
46
47 /**
48 * @var Site $site
49 */
50 foreach ( $sites as $site ) {
51 $this->assertInstanceOf( 'Site', $site );
52 }
53
54 foreach ( $testSites as $site ) {
55 if ( $site->getGlobalId() !== null ) {
56 $this->assertTrue( $sites->hasSite( $site->getGlobalId() ) );
57 }
58 }
59 }
60
61 /**
62 * @covers CachingSiteStore::saveSites
63 */
64 public function testSaveSites() {
65 $store = new CachingSiteStore( new HashSiteStore(), wfGetMainCache() );
66
67 $sites = array();
68
69 $site = new Site();
70 $site->setGlobalId( 'ertrywuutr' );
71 $site->setLanguageCode( 'en' );
72 $sites[] = $site;
73
74 $site = new MediaWikiSite();
75 $site->setGlobalId( 'sdfhxujgkfpth' );
76 $site->setLanguageCode( 'nl' );
77 $sites[] = $site;
78
79 $this->assertTrue( $store->saveSites( $sites ) );
80
81 $site = $store->getSite( 'ertrywuutr' );
82 $this->assertInstanceOf( 'Site', $site );
83 $this->assertEquals( 'en', $site->getLanguageCode() );
84
85 $site = $store->getSite( 'sdfhxujgkfpth' );
86 $this->assertInstanceOf( 'Site', $site );
87 $this->assertEquals( 'nl', $site->getLanguageCode() );
88 }
89
90 /**
91 * @covers CachingSiteStore::reset
92 */
93 public function testReset() {
94 $dbSiteStore = $this->getMockBuilder( 'SiteStore' )
95 ->disableOriginalConstructor()
96 ->getMock();
97
98 // php 5.3 compatibility!
99 $that = $this;
100
101 $dbSiteStore->expects( $this->any() )
102 ->method( 'getSite' )
103 ->will( $this->returnValue( $that->getTestSite() ) );
104
105 $dbSiteStore->expects( $this->any() )
106 ->method( 'getSites' )
107 ->will( $this->returnCallback( function() use ( $that ) {
108 $siteList = new SiteList();
109 $siteList->setSite( $that->getTestSite() );
110
111 return $siteList;
112 } ) );
113
114 $store = new CachingSiteStore( $dbSiteStore, wfGetMainCache() );
115
116 // initialize internal cache
117 $this->assertGreaterThan( 0, $store->getSites()->count(), 'count sites' );
118
119 $store->getSite( 'enwiki' )->setLanguageCode( 'en-ca' );
120
121 // sanity check: $store should have the new language code for 'enwiki'
122 $this->assertEquals( 'en-ca', $store->getSite( 'enwiki' )->getLanguageCode(), 'sanity check' );
123
124 // purge cache
125 $store->reset();
126
127 // the internal cache of $store should be updated, and now pulling
128 // the site from the 'fallback' DBSiteStore with the original language code.
129 $this->assertEquals( 'en', $store->getSite( 'enwiki' )->getLanguageCode(), 'reset' );
130 }
131
132 public function getTestSite() {
133 $enwiki = new MediaWikiSite();
134 $enwiki->setGlobalId( 'enwiki' );
135 $enwiki->setLanguageCode( 'en' );
136
137 return $enwiki;
138 }
139
140 /**
141 * @covers CachingSiteStore::clear
142 */
143 public function testClear() {
144 $store = new CachingSiteStore( new HashSiteStore(), wfGetMainCache() );
145 $this->assertTrue( $store->clear() );
146
147 $site = $store->getSite( 'enwiki' );
148 $this->assertNull( $site );
149
150 $sites = $store->getSites();
151 $this->assertEquals( 0, $sites->count() );
152 }
153
154 private function getHashSiteStore( array $sites ) {
155 $siteStore = new HashSiteStore();
156 $siteStore->saveSites( $sites );
157
158 return $siteStore;
159 }
160
161 }