3 use MediaWiki\MediaWikiServices
;
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 $lb = MediaWikiServices
::getInstance()->getDBLoadBalancer();
41 return new DBSiteStore( $lb );
45 * @covers DBSiteStore::getSites
47 public function testGetSites() {
48 $expectedSites = TestSites
::getSites();
49 TestSites
::insertIntoDb();
51 $store = $this->newDBSiteStore();
53 $sites = $store->getSites();
55 $this->assertInstanceOf( SiteList
::class, $sites );
60 foreach ( $sites as $site ) {
61 $this->assertInstanceOf( Site
::class, $site );
64 foreach ( $expectedSites as $site ) {
65 if ( $site->getGlobalId() !== null ) {
66 $this->assertTrue( $sites->hasSite( $site->getGlobalId() ) );
72 * @covers DBSiteStore::saveSites
74 public function testSaveSites() {
75 $store = $this->newDBSiteStore();
80 $site->setGlobalId( 'ertrywuutr' );
81 $site->setLanguageCode( 'en' );
84 $site = new MediaWikiSite();
85 $site->setGlobalId( 'sdfhxujgkfpth' );
86 $site->setLanguageCode( 'nl' );
89 $this->assertTrue( $store->saveSites( $sites ) );
91 $site = $store->getSite( 'ertrywuutr' );
92 $this->assertInstanceOf( Site
::class, $site );
93 $this->assertEquals( 'en', $site->getLanguageCode() );
94 $this->assertTrue( is_int( $site->getInternalId() ) );
95 $this->assertTrue( $site->getInternalId() >= 0 );
97 $site = $store->getSite( 'sdfhxujgkfpth' );
98 $this->assertInstanceOf( Site
::class, $site );
99 $this->assertEquals( 'nl', $site->getLanguageCode() );
100 $this->assertTrue( is_int( $site->getInternalId() ) );
101 $this->assertTrue( $site->getInternalId() >= 0 );
105 * @covers DBSiteStore::reset
107 public function testReset() {
108 $store1 = $this->newDBSiteStore();
109 $store2 = $this->newDBSiteStore();
111 // initialize internal cache
112 $this->assertGreaterThan( 0, $store1->getSites()->count() );
113 $this->assertGreaterThan( 0, $store2->getSites()->count() );
115 // Clear actual data. Will purge the external cache and reset the internal
116 // cache in $store1, but not the internal cache in store2.
117 $this->assertTrue( $store1->clear() );
119 // sanity check: $store2 should have a stale cache now
120 $this->assertNotNull( $store2->getSite( 'enwiki' ) );
125 // ...now the internal cache of $store2 should be updated and thus empty.
126 $site = $store2->getSite( 'enwiki' );
127 $this->assertNull( $site );
131 * @covers DBSiteStore::clear
133 public function testClear() {
134 $store = $this->newDBSiteStore();
135 $this->assertTrue( $store->clear() );
137 $site = $store->getSite( 'enwiki' );
138 $this->assertNull( $site );
140 $sites = $store->getSites();
141 $this->assertEquals( 0, $sites->count() );
145 * @covers DBSiteStore::getSites
147 public function testGetSitesDefaultOrder() {
148 $store = $this->newDBSiteStore();
150 $siteB->setGlobalId( 'B' );
152 $siteA->setGlobalId( 'A' );
153 $store->saveSites( [ $siteB, $siteA ] );
155 $sites = $store->getSites();
156 $siteIdentifiers = [];
157 /** @var Site $site */
158 foreach ( $sites as $site ) {
159 $siteIdentifiers[] = $site->getGlobalId();
161 $this->assertSame( [ 'A', 'B' ], $siteIdentifiers );
163 // Note: SiteList::getGlobalIdentifiers uses an other internal state. Iteration must be
164 // tested separately.
165 $this->assertSame( [ 'A', 'B' ], $sites->getGlobalIdentifiers() );