Merge "Add support for PHP7 random_bytes in favor of mcrypt_create_iv"
[lhc/web/wiklou.git] / tests / phpunit / ResourceLoaderTestCase.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4 use Psr\Log\LoggerInterface;
5 use Psr\Log\NullLogger;
6
7 abstract class ResourceLoaderTestCase extends MediaWikiTestCase {
8 // Version hash for a blank file module.
9 // Result of ResourceLoader::makeHash(), ResourceLoaderTestModule
10 // and ResourceLoaderFileModule::getDefinitionSummary().
11 const BLANK_VERSION = '09p30q0';
12
13 /**
14 * @param array|string $options Language code or options array
15 * - string 'lang' Language code
16 * - string 'dir' Language direction (ltr or rtl)
17 * - string 'modules' Pipe-separated list of module names
18 * - string|null 'only' "scripts" (unwrapped script), "styles" (stylesheet), or null
19 * (mw.loader.implement).
20 * @return ResourceLoaderContext
21 */
22 protected function getResourceLoaderContext( $options = [], ResourceLoader $rl = null ) {
23 if ( is_string( $options ) ) {
24 // Back-compat for extension tests
25 $options = [ 'lang' => $options ];
26 }
27 $options += [
28 'lang' => 'en',
29 'dir' => 'ltr',
30 'modules' => 'startup',
31 'only' => 'scripts',
32 ];
33 $resourceLoader = $rl ?: new ResourceLoader();
34 $request = new FauxRequest( [
35 'lang' => $options['lang'],
36 'modules' => $options['modules'],
37 'only' => $options['only'],
38 'skin' => 'vector',
39 'target' => 'phpunit',
40 ] );
41 $ctx = $this->getMockBuilder( 'ResourceLoaderContext' )
42 ->setConstructorArgs( [ $resourceLoader, $request ] )
43 ->setMethods( [ 'getDirection' ] )
44 ->getMock();
45 $ctx->method( 'getDirection' )->willReturn( $options['dir'] );
46 return $ctx;
47 }
48
49 public static function getSettings() {
50 return [
51 // For ResourceLoader::inDebugMode since it doesn't have context
52 'ResourceLoaderDebug' => true,
53
54 // Avoid influence from wgInvalidateCacheOnLocalSettingsChange
55 'CacheEpoch' => '20140101000000',
56
57 // For ResourceLoader::__construct()
58 'ResourceLoaderSources' => [],
59
60 // For wfScript()
61 'ScriptPath' => '/w',
62 'ScriptExtension' => '.php',
63 'Script' => '/w/index.php',
64 'LoadScript' => '/w/load.php',
65 ];
66 }
67
68 protected function setUp() {
69 parent::setUp();
70
71 ResourceLoader::clearCache();
72
73 $globals = [];
74 foreach ( self::getSettings() as $key => $value ) {
75 $globals['wg' . $key] = $value;
76 }
77 $this->setMwGlobals( $globals );
78 }
79 }
80
81 /* Stubs */
82
83 class ResourceLoaderTestModule extends ResourceLoaderModule {
84 protected $messages = [];
85 protected $dependencies = [];
86 protected $group = null;
87 protected $source = 'local';
88 protected $position = 'bottom';
89 protected $script = '';
90 protected $styles = '';
91 protected $skipFunction = null;
92 protected $isRaw = false;
93 protected $isKnownEmpty = false;
94 protected $type = ResourceLoaderModule::LOAD_GENERAL;
95 protected $targets = [ 'phpunit' ];
96
97 public function __construct( $options = [] ) {
98 foreach ( $options as $key => $value ) {
99 $this->$key = $value;
100 }
101 }
102
103 public function getScript( ResourceLoaderContext $context ) {
104 return $this->validateScriptFile( 'input', $this->script );
105 }
106
107 public function getStyles( ResourceLoaderContext $context ) {
108 return [ '' => $this->styles ];
109 }
110
111 public function getMessages() {
112 return $this->messages;
113 }
114
115 public function getDependencies( ResourceLoaderContext $context = null ) {
116 return $this->dependencies;
117 }
118
119 public function getGroup() {
120 return $this->group;
121 }
122
123 public function getSource() {
124 return $this->source;
125 }
126 public function getPosition() {
127 return $this->position;
128 }
129
130 public function getType() {
131 return $this->type;
132 }
133
134 public function getSkipFunction() {
135 return $this->skipFunction;
136 }
137
138 public function isRaw() {
139 return $this->isRaw;
140 }
141 public function isKnownEmpty( ResourceLoaderContext $context ) {
142 return $this->isKnownEmpty;
143 }
144
145 public function enableModuleContentVersion() {
146 return true;
147 }
148 }
149
150 class ResourceLoaderFileModuleTestModule extends ResourceLoaderFileModule {
151 }
152
153 class EmptyResourceLoader extends ResourceLoader {
154 // TODO: This won't be needed once ResourceLoader is empty by default
155 // and default registrations are done from ServiceWiring instead.
156 public function __construct( Config $config = null, LoggerInterface $logger = null ) {
157 $this->setLogger( $logger ?: new NullLogger() );
158 $this->config = $config ?: MediaWikiServices::getInstance()->getMainConfig();
159 // Source "local" is required by StartupModule
160 $this->addSource( 'local', $this->config->get( 'LoadScript' ) );
161 $this->setMessageBlobStore( new MessageBlobStore( $this, $this->getLogger() ) );
162 }
163
164 public function getErrors() {
165 return $this->errors;
166 }
167 }