Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / tests / phpunit / includes / site / DBSiteStoreTest.php
blob32dd7f282cef7063854781d545754d0539953a98
1 <?php
3 /**
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
21 * @file
22 * @since 1.21
24 * @ingroup Site
25 * @ingroup Test
27 * @group Site
28 * @group Database
30 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
32 class DBSiteStoreTest extends MediaWikiTestCase {
34 /**
35 * @return DBSiteStore
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() );
43 /**
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 );
56 /**
57 * @var Site $site
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() ) );
70 /**
71 * @covers DBSiteStore::saveSites
73 public function testSaveSites() {
74 $store = $this->newDBSiteStore();
76 $sites = [];
78 $site = new Site();
79 $site->setGlobalId( 'ertrywuutr' );
80 $site->setLanguageCode( 'en' );
81 $sites[] = $site;
83 $site = new MediaWikiSite();
84 $site->setGlobalId( 'sdfhxujgkfpth' );
85 $site->setLanguageCode( 'nl' );
86 $sites[] = $site;
88 $this->assertTrue( $store->saveSites( $sites ) );
90 $site = $store->getSite( 'ertrywuutr' );
91 $this->assertInstanceOf( 'Site', $site );
92 $this->assertEquals( 'en', $site->getLanguageCode() );
93 $this->assertTrue( is_integer( $site->getInternalId() ) );
94 $this->assertTrue( $site->getInternalId() >= 0 );
96 $site = $store->getSite( 'sdfhxujgkfpth' );
97 $this->assertInstanceOf( 'Site', $site );
98 $this->assertEquals( 'nl', $site->getLanguageCode() );
99 $this->assertTrue( is_integer( $site->getInternalId() ) );
100 $this->assertTrue( $site->getInternalId() >= 0 );
104 * @covers DBSiteStore::reset
106 public function testReset() {
107 $store1 = $this->newDBSiteStore();
108 $store2 = $this->newDBSiteStore();
110 // initialize internal cache
111 $this->assertGreaterThan( 0, $store1->getSites()->count() );
112 $this->assertGreaterThan( 0, $store2->getSites()->count() );
114 // Clear actual data. Will purge the external cache and reset the internal
115 // cache in $store1, but not the internal cache in store2.
116 $this->assertTrue( $store1->clear() );
118 // sanity check: $store2 should have a stale cache now
119 $this->assertNotNull( $store2->getSite( 'enwiki' ) );
121 // purge cache
122 $store2->reset();
124 // ...now the internal cache of $store2 should be updated and thus empty.
125 $site = $store2->getSite( 'enwiki' );
126 $this->assertNull( $site );
130 * @covers DBSiteStore::clear
132 public function testClear() {
133 $store = $this->newDBSiteStore();
134 $this->assertTrue( $store->clear() );
136 $site = $store->getSite( 'enwiki' );
137 $this->assertNull( $site );
139 $sites = $store->getSites();
140 $this->assertEquals( 0, $sites->count() );
144 * @covers DBSiteStore::getSites
146 public function testGetSitesDefaultOrder() {
147 $store = $this->newDBSiteStore();
148 $siteB = new Site();
149 $siteB->setGlobalId( 'B' );
150 $siteA = new Site();
151 $siteA->setGlobalId( 'A' );
152 $store->saveSites( [ $siteB, $siteA ] );
154 $sites = $store->getSites();
155 $siteIdentifiers = [];
156 /** @var Site $site */
157 foreach ( $sites as $site ) {
158 $siteIdentifiers[] = $site->getGlobalId();
160 $this->assertSame( [ 'A', 'B' ], $siteIdentifiers );
162 // Note: SiteList::getGlobalIdentifiers uses an other internal state. Iteration must be
163 // tested separately.
164 $this->assertSame( [ 'A', 'B' ], $sites->getGlobalIdentifiers() );