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 */
30class CachingSiteStoreTest extends \MediaWikiIntegrationTestCase {
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			ObjectCache::getLocalClusterInstance()
41		);
42
43		$sites = $store->getSites();
44
45		$this->assertInstanceOf( SiteList::class, $sites );
46
47		/**
48		 * @var Site $site
49		 */
50		foreach ( $sites as $site ) {
51			$this->assertInstanceOf( Site::class, $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(
66			new HashSiteStore(), ObjectCache::getLocalClusterInstance()
67		);
68
69		$sites = [];
70
71		$site = new Site();
72		$site->setGlobalId( 'ertrywuutr' );
73		$site->setLanguageCode( 'en' );
74		$sites[] = $site;
75
76		$site = new MediaWikiSite();
77		$site->setGlobalId( 'sdfhxujgkfpth' );
78		$site->setLanguageCode( 'nl' );
79		$sites[] = $site;
80
81		$this->assertTrue( $store->saveSites( $sites ) );
82
83		$site = $store->getSite( 'ertrywuutr' );
84		$this->assertInstanceOf( Site::class, $site );
85		$this->assertEquals( 'en', $site->getLanguageCode() );
86
87		$site = $store->getSite( 'sdfhxujgkfpth' );
88		$this->assertInstanceOf( Site::class, $site );
89		$this->assertEquals( 'nl', $site->getLanguageCode() );
90	}
91
92	/**
93	 * @covers CachingSiteStore::reset
94	 */
95	public function testReset() {
96		$dbSiteStore = $this->getMockBuilder( SiteStore::class )
97			->disableOriginalConstructor()
98			->getMock();
99
100		$dbSiteStore->expects( $this->any() )
101			->method( 'getSite' )
102			->will( $this->returnValue( $this->getTestSite() ) );
103
104		$dbSiteStore->expects( $this->any() )
105			->method( 'getSites' )
106			->will( $this->returnCallback( function () {
107				$siteList = new SiteList();
108				$siteList->setSite( $this->getTestSite() );
109
110				return $siteList;
111			} ) );
112
113		$store = new CachingSiteStore( $dbSiteStore, ObjectCache::getLocalClusterInstance() );
114
115		// initialize internal cache
116		$this->assertGreaterThan( 0, $store->getSites()->count(), 'count sites' );
117
118		$store->getSite( 'enwiki' )->setLanguageCode( 'en-ca' );
119
120		// sanity check: $store should have the new language code for 'enwiki'
121		$this->assertEquals( 'en-ca', $store->getSite( 'enwiki' )->getLanguageCode(), 'sanity check' );
122
123		// purge cache
124		$store->reset();
125
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' );
129	}
130
131	public function getTestSite() {
132		$enwiki = new MediaWikiSite();
133		$enwiki->setGlobalId( 'enwiki' );
134		$enwiki->setLanguageCode( 'en' );
135
136		return $enwiki;
137	}
138
139	/**
140	 * @covers CachingSiteStore::clear
141	 */
142	public function testClear() {
143		$store = new CachingSiteStore(
144			new HashSiteStore(), ObjectCache::getLocalClusterInstance()
145		);
146		$this->assertTrue( $store->clear() );
147
148		$site = $store->getSite( 'enwiki' );
149		$this->assertNull( $site );
150
151		$sites = $store->getSites();
152		$this->assertSame( 0, $sites->count() );
153	}
154
155	/**
156	 * @param Site[] $sites
157	 *
158	 * @return SiteStore
159	 */
160	private function getHashSiteStore( array $sites ) {
161		$siteStore = new HashSiteStore();
162		$siteStore->saveSites( $sites );
163
164		return $siteStore;
165	}
166
167}
168