LocalisationCache: try harder to use LCStoreCDB
[lhc/web/wiklou.git] / tests / phpunit / includes / site / DBSiteStoreTest.php
1 <?php
2
3 /**
4 * Tests for the DBSiteStore class.
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 * @covers DBSiteStore::getSites
36 */
37 public function testGetSites() {
38 $expectedSites = TestSites::getSites();
39 TestSites::insertIntoDb();
40
41 $store = new DBSiteStore();
42
43 $sites = $store->getSites();
44
45 $this->assertInstanceOf( 'SiteList', $sites );
46
47 /**
48 * @var Site $site
49 */
50 foreach ( $sites as $site ) {
51 $this->assertInstanceOf( 'Site', $site );
52 }
53
54 foreach ( $expectedSites as $site ) {
55 if ( $site->getGlobalId() !== null ) {
56 $this->assertTrue( $sites->hasSite( $site->getGlobalId() ) );
57 }
58 }
59 }
60
61 /**
62 * @covers DBSiteStore::saveSites
63 */
64 public function testSaveSites() {
65 $store = new DBSiteStore();
66
67 $sites = array();
68
69 $site = new Site();
70 $site->setGlobalId( 'ertrywuutr' );
71 $site->setLanguageCode( 'en' );
72 $sites[] = $site;
73
74 $site = new MediaWikiSite();
75 $site->setGlobalId( 'sdfhxujgkfpth' );
76 $site->setLanguageCode( 'nl' );
77 $sites[] = $site;
78
79 $this->assertTrue( $store->saveSites( $sites ) );
80
81 $site = $store->getSite( 'ertrywuutr' );
82 $this->assertInstanceOf( 'Site', $site );
83 $this->assertEquals( 'en', $site->getLanguageCode() );
84 $this->assertTrue( is_integer( $site->getInternalId() ) );
85 $this->assertTrue( $site->getInternalId() >= 0 );
86
87 $site = $store->getSite( 'sdfhxujgkfpth' );
88 $this->assertInstanceOf( 'Site', $site );
89 $this->assertEquals( 'nl', $site->getLanguageCode() );
90 $this->assertTrue( is_integer( $site->getInternalId() ) );
91 $this->assertTrue( $site->getInternalId() >= 0 );
92 }
93
94 /**
95 * @covers DBSiteStore::reset
96 */
97 public function testReset() {
98 $store1 = new DBSiteStore();
99 $store2 = new DBSiteStore();
100
101 // initialize internal cache
102 $this->assertGreaterThan( 0, $store1->getSites()->count() );
103 $this->assertGreaterThan( 0, $store2->getSites()->count() );
104
105 // Clear actual data. Will purge the external cache and reset the internal
106 // cache in $store1, but not the internal cache in store2.
107 $this->assertTrue( $store1->clear() );
108
109 // sanity check: $store2 should have a stale cache now
110 $this->assertNotNull( $store2->getSite( 'enwiki' ) );
111
112 // purge cache
113 $store2->reset();
114
115 // ...now the internal cache of $store2 should be updated and thus empty.
116 $site = $store2->getSite( 'enwiki' );
117 $this->assertNull( $site );
118 }
119
120 /**
121 * @covers DBSiteStore::clear
122 */
123 public function testClear() {
124 $store = new DBSiteStore();
125 $this->assertTrue( $store->clear() );
126
127 $site = $store->getSite( 'enwiki' );
128 $this->assertNull( $site );
129
130 $sites = $store->getSites();
131 $this->assertEquals( 0, $sites->count() );
132 }
133
134 /**
135 * @covers DBSiteStore::getSites
136 */
137 public function testGetSitesDefaultOrder() {
138 $store = new DBSiteStore();
139 $siteB = new Site();
140 $siteB->setGlobalId( 'B' );
141 $siteA = new Site();
142 $siteA->setGlobalId( 'A' );
143 $store->saveSites( array( $siteB, $siteA ) );
144
145 $sites = $store->getSites();
146 $siteIdentifiers = array();
147 /** @var Site $site */
148 foreach ( $sites as $site ) {
149 $siteIdentifiers[] = $site->getGlobalId();
150 }
151 $this->assertSame( array( 'A', 'B' ), $siteIdentifiers );
152
153 // Note: SiteList::getGlobalIdentifiers uses an other internal state. Iteration must be
154 // tested separately.
155 $this->assertSame( array( 'A', 'B' ), $sites->getGlobalIdentifiers() );
156 }
157 }