Merge "Add DROP INDEX support to DatabaseSqlite::replaceVars method"
[lhc/web/wiklou.git] / tests / phpunit / includes / site / SiteSQLStoreTest.php
1 <?php
2
3 /**
4 * Tests for the SiteSQLStore 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 * @licence GNU GPL v2+
31 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
32 */
33 class SiteSQLStoreTest extends MediaWikiTestCase {
34
35 /**
36 * @covers SiteSQLStore::getSites
37 */
38 public function testGetSites() {
39 $expectedSites = TestSites::getSites();
40 TestSites::insertIntoDb();
41
42 $store = SiteSQLStore::newInstance();
43
44 $sites = $store->getSites();
45
46 $this->assertInstanceOf( 'SiteList', $sites );
47
48 /**
49 * @var Site $site
50 */
51 foreach ( $sites as $site ) {
52 $this->assertInstanceOf( 'Site', $site );
53 }
54
55 foreach ( $expectedSites as $site ) {
56 if ( $site->getGlobalId() !== null ) {
57 $this->assertTrue( $sites->hasSite( $site->getGlobalId() ) );
58 }
59 }
60 }
61
62 /**
63 * @covers SiteSQLStore::saveSites
64 */
65 public function testSaveSites() {
66 $store = SiteSQLStore::newInstance();
67
68 $sites = array();
69
70 $site = new Site();
71 $site->setGlobalId( 'ertrywuutr' );
72 $site->setLanguageCode( 'en' );
73 $sites[] = $site;
74
75 $site = new MediaWikiSite();
76 $site->setGlobalId( 'sdfhxujgkfpth' );
77 $site->setLanguageCode( 'nl' );
78 $sites[] = $site;
79
80 $this->assertTrue( $store->saveSites( $sites ) );
81
82 $site = $store->getSite( 'ertrywuutr' );
83 $this->assertInstanceOf( 'Site', $site );
84 $this->assertEquals( 'en', $site->getLanguageCode() );
85 $this->assertTrue( is_integer( $site->getInternalId() ) );
86 $this->assertTrue( $site->getInternalId() >= 0 );
87
88 $site = $store->getSite( 'sdfhxujgkfpth' );
89 $this->assertInstanceOf( 'Site', $site );
90 $this->assertEquals( 'nl', $site->getLanguageCode() );
91 $this->assertTrue( is_integer( $site->getInternalId() ) );
92 $this->assertTrue( $site->getInternalId() >= 0 );
93 }
94
95 /**
96 * @covers SiteSQLStore::reset
97 */
98 public function testReset() {
99 $store1 = SiteSQLStore::newInstance();
100 $store2 = SiteSQLStore::newInstance();
101
102 // initialize internal cache
103 $this->assertGreaterThan( 0, $store1->getSites()->count() );
104 $this->assertGreaterThan( 0, $store2->getSites()->count() );
105
106 // Clear actual data. Will purge the external cache and reset the internal
107 // cache in $store1, but not the internal cache in store2.
108 $this->assertTrue( $store1->clear() );
109
110 // sanity check: $store2 should have a stale cache now
111 $this->assertNotNull( $store2->getSite( 'enwiki' ) );
112
113 // purge cache
114 $store2->reset();
115
116 // ...now the internal cache of $store2 should be updated and thus empty.
117 $site = $store2->getSite( 'enwiki' );
118 $this->assertNull( $site );
119 }
120
121 /**
122 * @covers SiteSQLStore::clear
123 */
124 public function testClear() {
125 $store = SiteSQLStore::newInstance();
126 $this->assertTrue( $store->clear() );
127
128 $site = $store->getSite( 'enwiki' );
129 $this->assertNull( $site );
130
131 $sites = $store->getSites();
132 $this->assertEquals( 0, $sites->count() );
133 }
134 }