4 * Tests for the DBSiteStore class.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
30 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
32 class DBSiteStoreTest
extends MediaWikiTestCase
{
37 private function newDBSiteStore() {
38 // NOTE: Use the real DB load balancer for now. Eventually, the test framework should
39 // provide a LoadBalancer that is safe to use in unit tests.
40 return new DBSiteStore( wfGetLB() );
44 * @covers DBSiteStore::getSites
46 public function testGetSites() {
47 $expectedSites = TestSites
::getSites();
48 TestSites
::insertIntoDb();
50 $store = $this->newDBSiteStore();
52 $sites = $store->getSites();
54 $this->assertInstanceOf( 'SiteList', $sites );
59 foreach ( $sites as $site ) {
60 $this->assertInstanceOf( 'Site', $site );
63 foreach ( $expectedSites as $site ) {
64 if ( $site->getGlobalId() !== null ) {
65 $this->assertTrue( $sites->hasSite( $site->getGlobalId() ) );
71 * @covers DBSiteStore::getSites
72 * @covers DBSiteStore::setLanguageCodeMapping
74 public function testLanguageCodeMapping() {
75 TestSites
::insertIntoDb();
77 $store = $this->newDBSiteStore();
78 $store->setLanguageCodeMapping( [ 'no' => 'nb' ] );
80 $site = $store->getSite( 'nowiki' );
81 $this->assertEquals( $site->getLanguageCode(), 'nb' );
85 * @covers DBSiteStore::saveSites
87 public function testSaveSites() {
88 $store = $this->newDBSiteStore();
93 $site->setGlobalId( 'ertrywuutr' );
94 $site->setLanguageCode( 'en' );
97 $site = new MediaWikiSite();
98 $site->setGlobalId( 'sdfhxujgkfpth' );
99 $site->setLanguageCode( 'nl' );
102 $this->assertTrue( $store->saveSites( $sites ) );
104 $site = $store->getSite( 'ertrywuutr' );
105 $this->assertInstanceOf( 'Site', $site );
106 $this->assertEquals( 'en', $site->getLanguageCode() );
107 $this->assertTrue( is_integer( $site->getInternalId() ) );
108 $this->assertTrue( $site->getInternalId() >= 0 );
110 $site = $store->getSite( 'sdfhxujgkfpth' );
111 $this->assertInstanceOf( 'Site', $site );
112 $this->assertEquals( 'nl', $site->getLanguageCode() );
113 $this->assertTrue( is_integer( $site->getInternalId() ) );
114 $this->assertTrue( $site->getInternalId() >= 0 );
118 * @covers DBSiteStore::reset
120 public function testReset() {
121 $store1 = $this->newDBSiteStore();
122 $store2 = $this->newDBSiteStore();
124 // initialize internal cache
125 $this->assertGreaterThan( 0, $store1->getSites()->count() );
126 $this->assertGreaterThan( 0, $store2->getSites()->count() );
128 // Clear actual data. Will purge the external cache and reset the internal
129 // cache in $store1, but not the internal cache in store2.
130 $this->assertTrue( $store1->clear() );
132 // sanity check: $store2 should have a stale cache now
133 $this->assertNotNull( $store2->getSite( 'enwiki' ) );
138 // ...now the internal cache of $store2 should be updated and thus empty.
139 $site = $store2->getSite( 'enwiki' );
140 $this->assertNull( $site );
144 * @covers DBSiteStore::clear
146 public function testClear() {
147 $store = $this->newDBSiteStore();
148 $this->assertTrue( $store->clear() );
150 $site = $store->getSite( 'enwiki' );
151 $this->assertNull( $site );
153 $sites = $store->getSites();
154 $this->assertEquals( 0, $sites->count() );
158 * @covers DBSiteStore::getSites
160 public function testGetSitesDefaultOrder() {
161 $store = $this->newDBSiteStore();
163 $siteB->setGlobalId( 'B' );
165 $siteA->setGlobalId( 'A' );
166 $store->saveSites( [ $siteB, $siteA ] );
168 $sites = $store->getSites();
169 $siteIdentifiers = [];
170 /** @var Site $site */
171 foreach ( $sites as $site ) {
172 $siteIdentifiers[] = $site->getGlobalId();
174 $this->assertSame( [ 'A', 'B' ], $siteIdentifiers );
176 // Note: SiteList::getGlobalIdentifiers uses an other internal state. Iteration must be
177 // tested separately.
178 $this->assertSame( [ 'A', 'B' ], $sites->getGlobalIdentifiers() );