summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/filerepo/RepoGroupTest.php
blob: 5bdb7e7fce0bf479fcfd05ccd117c85df1f0e8e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?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;
	}
}