3 namespace MediaWiki\Tests\Site
;
5 use MediaWiki\Site\DBSiteStore
;
6 use MediaWiki\Site\MediaWikiSite
;
7 use MediaWiki\Site\Site
;
8 use MediaWiki\Site\SiteList
;
9 use MediaWikiIntegrationTestCase
;
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 * http://www.gnu.org/copyleft/gpl.html
36 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
38 class DBSiteStoreTest
extends MediaWikiIntegrationTestCase
{
43 private function newDBSiteStore() {
44 return new DBSiteStore( $this->getServiceContainer()->getConnectionProvider() );
48 * @covers \MediaWiki\Site\DBSiteStore::getSites
50 public function testGetSites() {
51 $expectedSites = TestSites
::getSites();
52 TestSites
::insertIntoDb();
54 $store = $this->newDBSiteStore();
56 $sites = $store->getSites();
58 $this->assertInstanceOf( SiteList
::class, $sites );
63 foreach ( $sites as $site ) {
64 $this->assertInstanceOf( Site
::class, $site );
67 foreach ( $expectedSites as $site ) {
68 if ( $site->getGlobalId() !== null ) {
69 $this->assertTrue( $sites->hasSite( $site->getGlobalId() ) );
75 * @covers \MediaWiki\Site\DBSiteStore::saveSites
77 public function testSaveSites() {
78 $store = $this->newDBSiteStore();
83 $site->setGlobalId( 'ertrywuutr' );
84 $site->setLanguageCode( 'en' );
87 $site = new MediaWikiSite();
88 $site->setGlobalId( 'sdfhxujgkfpth' );
89 $site->setLanguageCode( 'nl' );
92 $this->assertTrue( $store->saveSites( $sites ) );
94 $site = $store->getSite( 'ertrywuutr' );
95 $this->assertInstanceOf( Site
::class, $site );
96 $this->assertEquals( 'en', $site->getLanguageCode() );
97 $this->assertIsInt( $site->getInternalId() );
98 $this->assertGreaterThanOrEqual( 0, $site->getInternalId() );
100 $site = $store->getSite( 'sdfhxujgkfpth' );
101 $this->assertInstanceOf( Site
::class, $site );
102 $this->assertEquals( 'nl', $site->getLanguageCode() );
103 $this->assertIsInt( $site->getInternalId() );
104 $this->assertGreaterThanOrEqual( 0, $site->getInternalId() );
108 * @covers \MediaWiki\Site\DBSiteStore::reset
110 public function testReset() {
111 TestSites
::insertIntoDb();
112 $store1 = $this->newDBSiteStore();
113 $store2 = $this->newDBSiteStore();
115 // initialize internal cache
116 $this->assertGreaterThan( 0, $store1->getSites()->count() );
117 $this->assertGreaterThan( 0, $store2->getSites()->count() );
119 // Clear actual data. Will purge the external cache and reset the internal
120 // cache in $store1, but not the internal cache in store2.
123 // check: $store2 should have a stale cache now
124 $this->assertNotNull( $store2->getSite( 'enwiki' ) );
129 // ...now the internal cache of $store2 should be updated and thus empty.
130 $site = $store2->getSite( 'enwiki' );
131 $this->assertNull( $site );
135 * @covers \MediaWiki\Site\DBSiteStore::clear
137 public function testClear() {
138 $store = $this->newDBSiteStore();
141 $site = $store->getSite( 'enwiki' );
142 $this->assertNull( $site );
144 $sites = $store->getSites();
145 $this->assertSame( 0, $sites->count() );
149 * @covers \MediaWiki\Site\DBSiteStore::getSites
151 public function testGetSitesDefaultOrder() {
152 $store = $this->newDBSiteStore();
154 $siteB->setGlobalId( 'B' );
156 $siteA->setGlobalId( 'A' );
157 $store->saveSites( [ $siteB, $siteA ] );
159 $sites = $store->getSites();
160 $siteIdentifiers = [];
161 /** @var Site $site */
162 foreach ( $sites as $site ) {
163 $siteIdentifiers[] = $site->getGlobalId();
165 $this->assertSame( [ 'A', 'B' ], $siteIdentifiers );
167 // Note: SiteList::getGlobalIdentifiers uses an other internal state. Iteration must be
168 // tested separately.
169 $this->assertSame( [ 'A', 'B' ], $sites->getGlobalIdentifiers() );