Merge "resourceloader: Make various CSSMin performance optimizations and cleanups"
[lhc/web/wiklou.git] / tests / phpunit / includes / site / DBSiteStoreTest.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4
5 /**
6 * Tests for the DBSiteStore class.
7 *
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.
12 *
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.
17 *
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
22 *
23 * @file
24 * @since 1.21
25 *
26 * @ingroup Site
27 * @ingroup Test
28 *
29 * @group Site
30 * @group Database
31 *
32 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
33 */
34 class DBSiteStoreTest extends MediaWikiTestCase {
35
36 /**
37 * @return DBSiteStore
38 */
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 );
44 }
45
46 /**
47 * @covers DBSiteStore::getSites
48 */
49 public function testGetSites() {
50 $expectedSites = TestSites::getSites();
51 TestSites::insertIntoDb();
52
53 $store = $this->newDBSiteStore();
54
55 $sites = $store->getSites();
56
57 $this->assertInstanceOf( SiteList::class, $sites );
58
59 /**
60 * @var Site $site
61 */
62 foreach ( $sites as $site ) {
63 $this->assertInstanceOf( Site::class, $site );
64 }
65
66 foreach ( $expectedSites as $site ) {
67 if ( $site->getGlobalId() !== null ) {
68 $this->assertTrue( $sites->hasSite( $site->getGlobalId() ) );
69 }
70 }
71 }
72
73 /**
74 * @covers DBSiteStore::saveSites
75 */
76 public function testSaveSites() {
77 $store = $this->newDBSiteStore();
78
79 $sites = [];
80
81 $site = new Site();
82 $site->setGlobalId( 'ertrywuutr' );
83 $site->setLanguageCode( 'en' );
84 $sites[] = $site;
85
86 $site = new MediaWikiSite();
87 $site->setGlobalId( 'sdfhxujgkfpth' );
88 $site->setLanguageCode( 'nl' );
89 $sites[] = $site;
90
91 $this->assertTrue( $store->saveSites( $sites ) );
92
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 );
98
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 );
104 }
105
106 /**
107 * @covers DBSiteStore::reset
108 */
109 public function testReset() {
110 $store1 = $this->newDBSiteStore();
111 $store2 = $this->newDBSiteStore();
112
113 // initialize internal cache
114 $this->assertGreaterThan( 0, $store1->getSites()->count() );
115 $this->assertGreaterThan( 0, $store2->getSites()->count() );
116
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() );
120
121 // sanity check: $store2 should have a stale cache now
122 $this->assertNotNull( $store2->getSite( 'enwiki' ) );
123
124 // purge cache
125 $store2->reset();
126
127 // ...now the internal cache of $store2 should be updated and thus empty.
128 $site = $store2->getSite( 'enwiki' );
129 $this->assertNull( $site );
130 }
131
132 /**
133 * @covers DBSiteStore::clear
134 */
135 public function testClear() {
136 $store = $this->newDBSiteStore();
137 $this->assertTrue( $store->clear() );
138
139 $site = $store->getSite( 'enwiki' );
140 $this->assertNull( $site );
141
142 $sites = $store->getSites();
143 $this->assertEquals( 0, $sites->count() );
144 }
145
146 /**
147 * @covers DBSiteStore::getSites
148 */
149 public function testGetSitesDefaultOrder() {
150 $store = $this->newDBSiteStore();
151 $siteB = new Site();
152 $siteB->setGlobalId( 'B' );
153 $siteA = new Site();
154 $siteA->setGlobalId( 'A' );
155 $store->saveSites( [ $siteB, $siteA ] );
156
157 $sites = $store->getSites();
158 $siteIdentifiers = [];
159 /** @var Site $site */
160 foreach ( $sites as $site ) {
161 $siteIdentifiers[] = $site->getGlobalId();
162 }
163 $this->assertSame( [ 'A', 'B' ], $siteIdentifiers );
164
165 // Note: SiteList::getGlobalIdentifiers uses an other internal state. Iteration must be
166 // tested separately.
167 $this->assertSame( [ 'A', 'B' ], $sites->getGlobalIdentifiers() );
168 }
169 }