mediawiki.api: Adopt async-await and assert.rejects() in various tests
[mediawiki.git] / tests / phpunit / includes / site / DBSiteStoreTest.php
blob6403321f3f0174fe95e26302ae47531eecb0e10d
1 <?php
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;
11 /**
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
27 * @file
28 * @since 1.21
30 * @ingroup Site
31 * @ingroup Test
33 * @group Site
34 * @group Database
36 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
38 class DBSiteStoreTest extends MediaWikiIntegrationTestCase {
40 /**
41 * @return DBSiteStore
43 private function newDBSiteStore() {
44 return new DBSiteStore( $this->getServiceContainer()->getConnectionProvider() );
47 /**
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 );
60 /**
61 * @var Site $site
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() ) );
74 /**
75 * @covers \MediaWiki\Site\DBSiteStore::saveSites
77 public function testSaveSites() {
78 $store = $this->newDBSiteStore();
80 $sites = [];
82 $site = new Site();
83 $site->setGlobalId( 'ertrywuutr' );
84 $site->setLanguageCode( 'en' );
85 $sites[] = $site;
87 $site = new MediaWikiSite();
88 $site->setGlobalId( 'sdfhxujgkfpth' );
89 $site->setLanguageCode( 'nl' );
90 $sites[] = $site;
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.
121 $store1->clear();
123 // check: $store2 should have a stale cache now
124 $this->assertNotNull( $store2->getSite( 'enwiki' ) );
126 // purge cache
127 $store2->reset();
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();
139 $store->clear();
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();
153 $siteB = new Site();
154 $siteB->setGlobalId( 'B' );
155 $siteA = new Site();
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() );