3 use MediaWiki\MediaWikiServices
;
6 * Tests for the DBSiteStore class.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
32 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
34 class DBSiteStoreTest
extends MediaWikiTestCase
{
39 private function newDBSiteStore() {
40 // NOTE: Use the real DB load balancer for now. Eventually, the test framework should
41 // provide a LoadBalancer that is safe to use in unit tests.
42 $lb = MediaWikiServices
::getInstance()->getDBLoadBalancer();
43 return new DBSiteStore( $lb );
47 * @covers DBSiteStore::getSites
49 public function testGetSites() {
50 $expectedSites = TestSites
::getSites();
51 TestSites
::insertIntoDb();
53 $store = $this->newDBSiteStore();
55 $sites = $store->getSites();
57 $this->assertInstanceOf( SiteList
::class, $sites );
62 foreach ( $sites as $site ) {
63 $this->assertInstanceOf( Site
::class, $site );
66 foreach ( $expectedSites as $site ) {
67 if ( $site->getGlobalId() !== null ) {
68 $this->assertTrue( $sites->hasSite( $site->getGlobalId() ) );
74 * @covers DBSiteStore::saveSites
76 public function testSaveSites() {
77 $store = $this->newDBSiteStore();
82 $site->setGlobalId( 'ertrywuutr' );
83 $site->setLanguageCode( 'en' );
86 $site = new MediaWikiSite();
87 $site->setGlobalId( 'sdfhxujgkfpth' );
88 $site->setLanguageCode( 'nl' );
91 $this->assertTrue( $store->saveSites( $sites ) );
93 $site = $store->getSite( 'ertrywuutr' );
94 $this->assertInstanceOf( Site
::class, $site );
95 $this->assertEquals( 'en', $site->getLanguageCode() );
96 $this->assertTrue( is_int( $site->getInternalId() ) );
97 $this->assertTrue( $site->getInternalId() >= 0 );
99 $site = $store->getSite( 'sdfhxujgkfpth' );
100 $this->assertInstanceOf( Site
::class, $site );
101 $this->assertEquals( 'nl', $site->getLanguageCode() );
102 $this->assertTrue( is_int( $site->getInternalId() ) );
103 $this->assertTrue( $site->getInternalId() >= 0 );
107 * @covers DBSiteStore::reset
109 public function testReset() {
110 $store1 = $this->newDBSiteStore();
111 $store2 = $this->newDBSiteStore();
113 // initialize internal cache
114 $this->assertGreaterThan( 0, $store1->getSites()->count() );
115 $this->assertGreaterThan( 0, $store2->getSites()->count() );
117 // Clear actual data. Will purge the external cache and reset the internal
118 // cache in $store1, but not the internal cache in store2.
119 $this->assertTrue( $store1->clear() );
121 // sanity check: $store2 should have a stale cache now
122 $this->assertNotNull( $store2->getSite( 'enwiki' ) );
127 // ...now the internal cache of $store2 should be updated and thus empty.
128 $site = $store2->getSite( 'enwiki' );
129 $this->assertNull( $site );
133 * @covers DBSiteStore::clear
135 public function testClear() {
136 $store = $this->newDBSiteStore();
137 $this->assertTrue( $store->clear() );
139 $site = $store->getSite( 'enwiki' );
140 $this->assertNull( $site );
142 $sites = $store->getSites();
143 $this->assertEquals( 0, $sites->count() );
147 * @covers DBSiteStore::getSites
149 public function testGetSitesDefaultOrder() {
150 $store = $this->newDBSiteStore();
152 $siteB->setGlobalId( 'B' );
154 $siteA->setGlobalId( 'A' );
155 $store->saveSites( [ $siteB, $siteA ] );
157 $sites = $store->getSites();
158 $siteIdentifiers = [];
159 /** @var Site $site */
160 foreach ( $sites as $site ) {
161 $siteIdentifiers[] = $site->getGlobalId();
163 $this->assertSame( [ 'A', 'B' ], $siteIdentifiers );
165 // Note: SiteList::getGlobalIdentifiers uses an other internal state. Iteration must be
166 // tested separately.
167 $this->assertSame( [ 'A', 'B' ], $sites->getGlobalIdentifiers() );