Enforce an alphabetic default order for SiteList entries
authorThiemo Mättig <thiemo.maettig@wikimedia.de>
Thu, 16 Jul 2015 16:48:05 +0000 (11:48 -0500)
committerThiemo Mättig <thiemo.maettig@wikimedia.de>
Thu, 6 Aug 2015 14:24:33 +0000 (16:24 +0200)
Bug: T106054
Change-Id: I8bf22d99b8304d7a6f40e384e8de00a4aca9313d

includes/site/DBSiteStore.php
tests/phpunit/includes/site/DBSiteStoreTest.php

index f167584..1193bd6 100644 (file)
@@ -153,7 +153,11 @@ class DBSiteStore implements SiteStore {
        protected function loadSites() {
                $this->sites = new SiteList();
 
-               foreach ( $this->sitesTable->select() as $siteRow ) {
+               $siteRows = $this->sitesTable->select( null, array(), array(
+                       'ORDER BY' => 'site_global_key'
+               ) );
+
+               foreach ( $siteRows as $siteRow ) {
                        $this->sites[] = $this->siteFromRow( $siteRow );
                }
 
index 673ba54..48ef524 100644 (file)
@@ -130,4 +130,28 @@ class DBSiteStoreTest extends MediaWikiTestCase {
                $sites = $store->getSites();
                $this->assertEquals( 0, $sites->count() );
        }
+
+       /**
+        * @covers DBSiteStore::getSites
+        */
+       public function testGetSitesDefaultOrder() {
+               $store = new DBSiteStore();
+               $siteB = new Site();
+               $siteB->setGlobalId( 'B' );
+               $siteA = new Site();
+               $siteA->setGlobalId( 'A' );
+               $store->saveSites( array( $siteB, $siteA ) );
+
+               $sites = $store->getSites();
+               $siteIdentifiers = array();
+               /** @var Site $site */
+               foreach ( $sites as $site ) {
+                       $siteIdentifiers[] = $site->getGlobalId();
+               }
+               $this->assertSame( array( 'A', 'B' ), $siteIdentifiers );
+
+               // Note: SiteList::getGlobalIdentifiers uses an other internal state. Iteration must be
+               // tested separately.
+               $this->assertSame( array( 'A', 'B' ), $sites->getGlobalIdentifiers() );
+       }
 }