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