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