summaryrefslogtreecommitdiff
path: root/www/wiki/tests/phpunit/ResourceLoaderTestCase.php
blob: d5c14a2558d34cc063240cd3ca8d00c7fd988a4c (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php

use MediaWiki\MediaWikiServices;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

abstract class ResourceLoaderTestCase extends MediaWikiTestCase {
	// Version hash for a blank file module.
	// Result of ResourceLoader::makeHash(), ResourceLoaderTestModule
	// and ResourceLoaderFileModule::getDefinitionSummary().
	const BLANK_VERSION = '09p30q0';

	/**
	 * @param array|string $options Language code or options array
	 * - string 'lang' Language code
	 * - string 'dir' Language direction (ltr or rtl)
	 * - string 'modules' Pipe-separated list of module names
	 * - string|null 'only' "scripts" (unwrapped script), "styles" (stylesheet), or null
	 *    (mw.loader.implement).
	 * @param ResourceLoader|null $rl
	 * @return ResourceLoaderContext
	 */
	protected function getResourceLoaderContext( $options = [], ResourceLoader $rl = null ) {
		if ( is_string( $options ) ) {
			// Back-compat for extension tests
			$options = [ 'lang' => $options ];
		}
		$options += [
			'lang' => 'en',
			'dir' => 'ltr',
			'skin' => 'vector',
			'modules' => 'startup',
			'only' => 'scripts',
		];
		$resourceLoader = $rl ?: new ResourceLoader();
		$request = new FauxRequest( [
				'lang' => $options['lang'],
				'modules' => $options['modules'],
				'only' => $options['only'],
				'skin' => $options['skin'],
				'target' => 'phpunit',
		] );
		$ctx = $this->getMockBuilder( ResourceLoaderContext::class )
			->setConstructorArgs( [ $resourceLoader, $request ] )
			->setMethods( [ 'getDirection' ] )
			->getMock();
		$ctx->method( 'getDirection' )->willReturn( $options['dir'] );
		return $ctx;
	}

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

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

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

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

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

		ResourceLoader::clearCache();

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

/* Stubs */

class ResourceLoaderTestModule extends ResourceLoaderModule {
	protected $messages = [];
	protected $dependencies = [];
	protected $group = null;
	protected $source = 'local';
	protected $script = '';
	protected $styles = '';
	protected $skipFunction = null;
	protected $isRaw = false;
	protected $isKnownEmpty = false;
	protected $type = ResourceLoaderModule::LOAD_GENERAL;
	protected $targets = [ 'phpunit' ];
	protected $shouldEmbed = null;

	public function __construct( $options = [] ) {
		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 [ '' => $this->styles ];
	}

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

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

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

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

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

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

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

	public function shouldEmbedModule( ResourceLoaderContext $context ) {
		return $this->shouldEmbed !== null ? $this->shouldEmbed : parent::shouldEmbedModule( $context );
	}

	public function enableModuleContentVersion() {
		return true;
	}
}

class ResourceLoaderFileTestModule extends ResourceLoaderFileModule {
	protected $lessVars = [];

	public function __construct( $options = [], $test = [] ) {
		parent::__construct( $options );

		foreach ( $test as $key => $value ) {
			$this->$key = $value;
		}
	}

	public function getLessVars( ResourceLoaderContext $context ) {
		return $this->lessVars;
	}
}

class ResourceLoaderFileModuleTestModule extends ResourceLoaderFileModule {
}

class EmptyResourceLoader extends ResourceLoader {
	// TODO: This won't be needed once ResourceLoader is empty by default
	// and default registrations are done from ServiceWiring instead.
	public function __construct( Config $config = null, LoggerInterface $logger = null ) {
		$this->setLogger( $logger ?: new NullLogger() );
		$this->config = $config ?: MediaWikiServices::getInstance()->getMainConfig();
		// Source "local" is required by StartupModule
		$this->addSource( 'local', $this->config->get( 'LoadScript' ) );
		$this->setMessageBlobStore( new MessageBlobStore( $this, $this->getLogger() ) );
	}

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