Fix RepoGroup::hasForeignRepos() and RepoGroup::forEachForeignRepo()
authorBrian Wolff <bawolff+wn@gmail.com>
Thu, 3 Jul 2014 06:12:27 +0000 (03:12 -0300)
committerBrian Wolff <bawolff+wn@gmail.com>
Thu, 3 Jul 2014 06:12:27 +0000 (03:12 -0300)
Also add unit tests for those methods.

Change-Id: Ic72be60d13820492efc90b5925bb5cfc1813d28a

includes/filerepo/RepoGroup.php
tests/phpunit/includes/filerepo/RepoGroupTest.php [new file with mode: 0644]

index 103e78e..65637df 100644 (file)
@@ -367,6 +367,9 @@ class RepoGroup {
         * @return bool
         */
        function forEachForeignRepo( $callback, $params = array() ) {
+               if ( !$this->reposInitialised ) {
+                       $this->initialiseRepos();
+               }
                foreach ( $this->foreignRepos as $repo ) {
                        $args = array_merge( array( $repo ), $params );
                        if ( call_user_func_array( $callback, $args ) ) {
@@ -382,6 +385,9 @@ class RepoGroup {
         * @return bool
         */
        function hasForeignRepos() {
+               if ( !$this->reposInitialised ) {
+                       $this->initialiseRepos();
+               }
                return (bool)$this->foreignRepos;
        }
 
diff --git a/tests/phpunit/includes/filerepo/RepoGroupTest.php b/tests/phpunit/includes/filerepo/RepoGroupTest.php
new file mode 100644 (file)
index 0000000..2c52338
--- /dev/null
@@ -0,0 +1,57 @@
+<?php
+class RepoGroupTest extends MediaWikiTestCase {
+
+       function testHasForeignRepoNegative() {
+               $this->setMWGlobals( 'wgForeignFileRepos', array() );
+               RepoGroup::destroySingleton();
+               FileBackendGroup::destroySingleton();
+               $this->assertFalse( RepoGroup::singleton()->hasForeignRepos() );
+       }
+
+       function testHasForeignRepoPositive() {
+               $this->setUpForeignRepo();
+               $this->assertTrue( RepoGroup::singleton()->hasForeignRepos() );
+       }
+
+       function testForEachForeignRepo() {
+               $this->setUpForeignRepo();
+               $fakeCallback = $this->getMock( 'RepoGroupTestHelper' );
+               $fakeCallback->expects( $this->once() )->method( 'callback' );
+               RepoGroup::singleton()->forEachForeignRepo( array( $fakeCallback, 'callback' ), array( array() ) );
+       }
+
+       function testForEachForeignRepoNone() {
+               $this->setMWGlobals( 'wgForeignFileRepos', array() );
+               RepoGroup::destroySingleton();
+               FileBackendGroup::destroySingleton();
+               $fakeCallback = $this->getMock( 'RepoGroupTestHelper' );
+               $fakeCallback->expects( $this->never() )->method( 'callback' );
+               RepoGroup::singleton()->forEachForeignRepo( array( $fakeCallback, 'callback' ), array( array() ) );
+       }
+
+       private function setUpForeignRepo() {
+               global $wgUploadDirectory;
+               $this->setMWGlobals( 'wgForeignFileRepos', array( array(
+                       'class' => 'ForeignAPIRepo',
+                       'name' => 'wikimediacommons',
+                       'backend' => 'wikimediacommons-backend',
+                       'apibase' => 'https://commons.wikimedia.org/w/api.php',
+                       'hashLevels' => 2,
+                       'fetchDescription' => true,
+                       'descriptionCacheExpiry' => 43200,
+                       'apiThumbCacheExpiry' => 86400,
+                       'directory' => $wgUploadDirectory
+               ) ) );
+               RepoGroup::destroySingleton();
+               FileBackendGroup::destroySingleton();
+       }
+}
+
+/**
+ * Quick helper class to use as a mock callback for RepoGroup::singleton()->forEachForeignRepo.
+ */
+class RepoGroupTestHelper {
+       function callback( FileRepo $repo, array $foo ) {
+               return true;
+       }
+}