summaryrefslogtreecommitdiff
path: root/www/wiki/tests/phpunit/suites/UploadFromUrlTestSuite.php
blob: 556c75412a4b8c56e45c5b411758a2d3b1d6cd4e (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php

require_once dirname( __DIR__ ) . '/includes/upload/UploadFromUrlTest.php';

class UploadFromUrlTestSuite extends PHPUnit_Framework_TestSuite {
	public $savedGlobals = [];

	public static function addTables( &$tables ) {
		$tables[] = 'user_properties';
		$tables[] = 'filearchive';
		$tables[] = 'logging';
		$tables[] = 'updatelog';
		$tables[] = 'iwlinks';

		return true;
	}

	protected function setUp() {
		global $wgParser, $wgParserConf, $IP, $messageMemc, $wgMemc, $wgUser,
			$wgLang, $wgOut, $wgRequest, $wgStyleDirectory,
			$wgParserCacheType, $wgNamespaceAliases, $wgNamespaceProtection;

		$tmpDir = $this->getNewTempDirectory();
		$tmpGlobals = [];

		$tmpGlobals['wgScript'] = '/index.php';
		$tmpGlobals['wgScriptPath'] = '/';
		$tmpGlobals['wgArticlePath'] = '/wiki/$1';
		$tmpGlobals['wgStylePath'] = '/skins';
		$tmpGlobals['wgThumbnailScriptPath'] = false;
		$tmpGlobals['wgLocalFileRepo'] = [
			'class' => LocalRepo::class,
			'name' => 'local',
			'url' => 'http://example.com/images',
			'hashLevels' => 2,
			'transformVia404' => false,
			'backend' => new FSFileBackend( [
				'name' => 'local-backend',
				'wikiId' => wfWikiID(),
				'containerPaths' => [
					'local-public' => "{$tmpDir}/test-repo/public",
					'local-thumb' => "{$tmpDir}/test-repo/thumb",
					'local-temp' => "{$tmpDir}/test-repo/temp",
					'local-deleted' => "{$tmpDir}/test-repo/delete",
				]
			] ),
		];
		foreach ( $tmpGlobals as $var => $val ) {
			if ( array_key_exists( $var, $GLOBALS ) ) {
				$this->savedGlobals[$var] = $GLOBALS[$var];
			}
			$GLOBALS[$var] = $val;
		}

		$wgNamespaceProtection[NS_MEDIAWIKI] = 'editinterface';
		$wgNamespaceAliases['Image'] = NS_FILE;
		$wgNamespaceAliases['Image_talk'] = NS_FILE_TALK;

		$wgParserCacheType = CACHE_NONE;
		DeferredUpdates::clearPendingUpdates();
		$wgMemc = wfGetMainCache();
		$messageMemc = wfGetMessageCacheStorage();

		RequestContext::resetMain();
		$context = RequestContext::getMain();
		$wgUser = new User;
		$wgLang = $context->getLanguage();
		$wgOut = $context->getOutput();
		$wgParser = new StubObject( 'wgParser', $wgParserConf['class'], [ $wgParserConf ] );
		$wgRequest = $context->getRequest();

		if ( $wgStyleDirectory === false ) {
			$wgStyleDirectory = "$IP/skins";
		}

		RepoGroup::destroySingleton();
		FileBackendGroup::destroySingleton();
	}

	protected function tearDown() {
		foreach ( $this->savedGlobals as $var => $val ) {
			$GLOBALS[$var] = $val;
		}
		// Restore backends
		RepoGroup::destroySingleton();
		FileBackendGroup::destroySingleton();

		parent::tearDown();
	}

	public static function suite() {
		// Hack to invoke the autoloader required to get phpunit to recognize
		// the UploadFromUrlTest class
		class_exists( 'UploadFromUrlTest' );
		$suite = new UploadFromUrlTestSuite( 'UploadFromUrlTest' );

		return $suite;
	}
}