summaryrefslogtreecommitdiff
path: root/tests/phpunit/ResourceLoaderTestCase.php
blob: 325b20eec1b85ecbf26e12cb1d10cf4e36b70005 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php

abstract class ResourceLoaderTestCase extends MediaWikiTestCase {
	/**
	 * @param string $lang
	 * @param string $dir
	 * @return ResourceLoaderContext
	 */
	protected function getResourceLoaderContext( $lang = 'en', $dir = 'ltr' ) {
		$resourceLoader = new ResourceLoader();
		$request = new FauxRequest( array(
				'lang' => $lang,
				'modules' => 'startup',
				'only' => 'scripts',
				'skin' => 'vector',
				'target' => 'test',
		) );
		$ctx = $this->getMockBuilder( 'ResourceLoaderContext' )
			->setConstructorArgs( array( $resourceLoader, $request ) )
			->setMethods( array( 'getDirection' ) )
			->getMock();
		$ctx->expects( $this->any() )->method( 'getDirection' )->will(
			$this->returnValue( $dir )
		);
		return $ctx;
	}

	public static function getSettings() {
		return array(
			// For ResourceLoader::inDebugMode since it doesn't have context
			'ResourceLoaderDebug' => true,

			// Avoid influence from wgInvalidateCacheOnLocalSettingsChange
			'CacheEpoch' => '20140101000000',

			// For ResourceLoader::__construct()
			'ResourceLoaderSources' => array(),

			// For wfScript()
			'ScriptPath' => '/w',
			'ScriptExtension' => '.php',
			'Script' => '/w/index.php',
			'LoadScript' => '/w/load.php',
		);
	}

	protected function setUp() {
		parent::setUp();

		ResourceLoader::clearCache();

		$globals = array();
		foreach ( self::getSettings() as $key => $value ) {
			$globals['wg' . $key] = $value;
		}
		$this->setMwGlobals( $globals );
	}
}

/* Stubs */

class ResourceLoaderTestModule extends ResourceLoaderModule {
	protected $dependencies = array();
	protected $group = null;
	protected $source = 'local';
	protected $script = '';
	protected $styles = '';
	protected $skipFunction = null;
	protected $isRaw = false;
	protected $targets = array( 'test' );

	public function __construct( $options = array() ) {
		foreach ( $options as $key => $value ) {
			$this->$key = $value;
		}
	}

	public function getScript( ResourceLoaderContext $context ) {
		return $this->validateScriptFile( 'input', $this->script );
	}

	public function getStyles( ResourceLoaderContext $context ) {
		return array( '' => $this->styles );
	}

	public function getDependencies( ResourceLoaderContext $context = null ) {
		return $this->dependencies;
	}

	public function getGroup() {
		return $this->group;
	}

	public function getSource() {
		return $this->source;
	}

	public function getSkipFunction() {
		return $this->skipFunction;
	}

	public function isRaw() {
		return $this->isRaw;
	}

	public function enableModuleContentVersion() {
		return true;
	}
}

class ResourceLoaderFileModuleTestModule extends ResourceLoaderFileModule {
}