Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / tests / phpunit / includes / site / DBSiteStoreTest.php
blob48ef5243b9ef06a5a894b78b52dbd99946bd1e21
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 * @covers DBSiteStore::getSites
37 public function testGetSites() {
38 $expectedSites = TestSites::getSites();
39 TestSites::insertIntoDb();
41 $store = new DBSiteStore();
43 $sites = $store->getSites();
45 $this->assertInstanceOf( 'SiteList', $sites );
47 /**
48 * @var Site $site
50 foreach ( $sites as $site ) {
51 $this->assertInstanceOf( 'Site', $site );
54 foreach ( $expectedSites as $site ) {
55 if ( $site->getGlobalId() !== null ) {
56 $this->assertTrue( $sites->hasSite( $site->getGlobalId() ) );
61 /**
62 * @covers DBSiteStore::saveSites
64 public function testSaveSites() {
65 $store = new DBSiteStore();
67 $sites = array();
69 $site = new Site();
70 $site->setGlobalId( 'ertrywuutr' );
71 $site->setLanguageCode( 'en' );
72 $sites[] = $site;
74 $site = new MediaWikiSite();
75 $site->setGlobalId( 'sdfhxujgkfpth' );
76 $site->setLanguageCode( 'nl' );
77 $sites[] = $site;
79 $this->assertTrue( $store->saveSites( $sites ) );
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 );
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 );
94 /**
95 * @covers DBSiteStore::reset
97 public function testReset() {
98 $store1 = new DBSiteStore();
99 $store2 = new DBSiteStore();
101 // initialize internal cache
102 $this->assertGreaterThan( 0, $store1->getSites()->count() );
103 $this->assertGreaterThan( 0, $store2->getSites()->count() );
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() );
109 // sanity check: $store2 should have a stale cache now
110 $this->assertNotNull( $store2->getSite( 'enwiki' ) );
112 // purge cache
113 $store2->reset();
115 // ...now the internal cache of $store2 should be updated and thus empty.
116 $site = $store2->getSite( 'enwiki' );
117 $this->assertNull( $site );
121 * @covers DBSiteStore::clear
123 public function testClear() {
124 $store = new DBSiteStore();
125 $this->assertTrue( $store->clear() );
127 $site = $store->getSite( 'enwiki' );
128 $this->assertNull( $site );
130 $sites = $store->getSites();
131 $this->assertEquals( 0, $sites->count() );
135 * @covers DBSiteStore::getSites
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 ) );
145 $sites = $store->getSites();
146 $siteIdentifiers = array();
147 /** @var Site $site */
148 foreach ( $sites as $site ) {
149 $siteIdentifiers[] = $site->getGlobalId();
151 $this->assertSame( array( 'A', 'B' ), $siteIdentifiers );
153 // Note: SiteList::getGlobalIdentifiers uses an other internal state. Iteration must be
154 // tested separately.
155 $this->assertSame( array( 'A', 'B' ), $sites->getGlobalIdentifiers() );