MediaWiki  1.30.2
ResourceLoaderModuleTest.php
Go to the documentation of this file.
1 <?php
2 
4 
8  public function testGetVersionHash() {
10 
11  $baseParams = [
12  'scripts' => [ 'foo.js', 'bar.js' ],
13  'dependencies' => [ 'jquery', 'mediawiki' ],
14  'messages' => [ 'hello', 'world' ],
15  ];
16 
17  $module = new ResourceLoaderFileModule( $baseParams );
18  $version = json_encode( $module->getVersionHash( $context ) );
19 
20  // Exactly the same
21  $module = new ResourceLoaderFileModule( $baseParams );
22  $this->assertEquals(
23  $version,
24  json_encode( $module->getVersionHash( $context ) ),
25  'Instance is insignificant'
26  );
27 
28  // Re-order dependencies
29  $module = new ResourceLoaderFileModule( [
30  'dependencies' => [ 'mediawiki', 'jquery' ],
31  ] + $baseParams );
32  $this->assertEquals(
33  $version,
34  json_encode( $module->getVersionHash( $context ) ),
35  'Order of dependencies is insignificant'
36  );
37 
38  // Re-order messages
39  $module = new ResourceLoaderFileModule( [
40  'messages' => [ 'world', 'hello' ],
41  ] + $baseParams );
42  $this->assertEquals(
43  $version,
44  json_encode( $module->getVersionHash( $context ) ),
45  'Order of messages is insignificant'
46  );
47 
48  // Re-order scripts
49  $module = new ResourceLoaderFileModule( [
50  'scripts' => [ 'bar.js', 'foo.js' ],
51  ] + $baseParams );
52  $this->assertNotEquals(
53  $version,
54  json_encode( $module->getVersionHash( $context ) ),
55  'Order of scripts is significant'
56  );
57 
58  // Subclass
59  $module = new ResourceLoaderFileModuleTestModule( $baseParams );
60  $this->assertNotEquals(
61  $version,
62  json_encode( $module->getVersionHash( $context ) ),
63  'Class is significant'
64  );
65  }
66 
70  public function testValidateScriptFile() {
71  $this->setMwGlobals( 'wgResourceLoaderValidateJS', true );
72 
74 
75  $module = new ResourceLoaderTestModule( [
76  'script' => "var a = 'this is';\n {\ninvalid"
77  ] );
78  $this->assertEquals(
79  'mw.log.error(' .
80  '"JavaScript parse error: Parse error: Unexpected token; ' .
81  'token } expected in file \'input\' on line 3"' .
82  ');',
83  $module->getScript( $context ),
84  'Replace invalid syntax with error logging'
85  );
86 
87  $module = new ResourceLoaderTestModule( [
88  'script' => "\n'valid';"
89  ] );
90  $this->assertEquals(
91  "\n'valid';",
92  $module->getScript( $context ),
93  'Leave valid scripts as-is'
94  );
95  }
96 
97  public static function provideBuildContentScripts() {
98  return [
99  [
100  "mw.foo()",
101  "mw.foo()\n",
102  ],
103  [
104  "mw.foo();",
105  "mw.foo();\n",
106  ],
107  [
108  "mw.foo();\n",
109  "mw.foo();\n",
110  ],
111  [
112  "mw.foo()\n",
113  "mw.foo()\n",
114  ],
115  [
116  "mw.foo()\n// mw.bar();",
117  "mw.foo()\n// mw.bar();\n",
118  ],
119  [
120  "mw.foo()\n// mw.bar()",
121  "mw.foo()\n// mw.bar()\n",
122  ],
123  [
124  "mw.foo()// mw.bar();",
125  "mw.foo()// mw.bar();\n",
126  ],
127  ];
128  }
129 
134  public function testBuildContentScripts( $raw, $build, $message = null ) {
136  $module = new ResourceLoaderTestModule( [
137  'script' => $raw
138  ] );
139  $this->assertEquals( $raw, $module->getScript( $context ), 'Raw script' );
140  $this->assertEquals(
141  [ 'scripts' => $build ],
142  $module->getModuleContent( $context ),
143  $message
144  );
145  }
146 
151  public function testPlaceholderize() {
152  $getRelativePaths = new ReflectionMethod( 'ResourceLoaderModule', 'getRelativePaths' );
153  $getRelativePaths->setAccessible( true );
154  $expandRelativePaths = new ReflectionMethod( 'ResourceLoaderModule', 'expandRelativePaths' );
155  $expandRelativePaths->setAccessible( true );
156 
157  $this->setMwGlobals( [
158  'IP' => '/srv/example/mediawiki/core',
159  ] );
160  $raw = [
161  '/srv/example/mediawiki/core/resources/foo.js',
162  '/srv/example/mediawiki/core/extensions/Example/modules/bar.js',
163  '/srv/example/mediawiki/skins/Example/baz.css',
164  '/srv/example/mediawiki/skins/Example/images/quux.png',
165  ];
166  $canonical = [
167  'resources/foo.js',
168  'extensions/Example/modules/bar.js',
169  '../skins/Example/baz.css',
170  '../skins/Example/images/quux.png',
171  ];
172  $this->assertEquals(
173  $canonical,
174  $getRelativePaths->invoke( null, $raw ),
175  'Insert placeholders'
176  );
177  $this->assertEquals(
178  $raw,
179  $expandRelativePaths->invoke( null, $canonical ),
180  'Substitute placeholders'
181  );
182  }
183 
188  public function testGetHeaders() {
190 
191  $module = new ResourceLoaderTestModule();
192  $this->assertSame( [], $module->getHeaders( $context ), 'Default' );
193 
194  $module = $this->getMockBuilder( ResourceLoaderTestModule::class )
195  ->setMethods( [ 'getPreloadLinks' ] )->getMock();
196  $module->method( 'getPreloadLinks' )->willReturn( [
197  'https://example.org/script.js' => [ 'as' => 'script' ],
198  ] );
199  $this->assertSame(
200  [
201  'Link: <https://example.org/script.js>;rel=preload;as=script'
202  ],
203  $module->getHeaders( $context ),
204  'Preload one resource'
205  );
206 
207  $module = $this->getMockBuilder( ResourceLoaderTestModule::class )
208  ->setMethods( [ 'getPreloadLinks' ] )->getMock();
209  $module->method( 'getPreloadLinks' )->willReturn( [
210  'https://example.org/script.js' => [ 'as' => 'script' ],
211  '/example.png' => [ 'as' => 'image' ],
212  ] );
213  $this->assertSame(
214  [
215  'Link: <https://example.org/script.js>;rel=preload;as=script,' .
216  '</example.png>;rel=preload;as=image'
217  ],
218  $module->getHeaders( $context ),
219  'Preload two resources'
220  );
221  }
222 }
$context
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext $context
Definition: hooks.txt:2581
ResourceLoaderFileModuleTestModule
Definition: ResourceLoaderTestCase.php:173
ResourceLoaderModuleTest\provideBuildContentScripts
static provideBuildContentScripts()
Definition: ResourceLoaderModuleTest.php:97
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
ResourceLoaderModuleTest
Definition: ResourceLoaderModuleTest.php:3
ResourceLoaderFileModule
ResourceLoader module based on local JavaScript/CSS files.
Definition: ResourceLoaderFileModule.php:28
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:672
ResourceLoaderModuleTest\testGetVersionHash
testGetVersionHash()
ResourceLoaderModule::getVersionHash.
Definition: ResourceLoaderModuleTest.php:8
ResourceLoaderModuleTest\testValidateScriptFile
testValidateScriptFile()
ResourceLoaderModule::validateScriptFile.
Definition: ResourceLoaderModuleTest.php:70
ResourceLoaderTestModule
Definition: ResourceLoaderTestCase.php:85
ResourceLoaderModuleTest\testGetHeaders
testGetHeaders()
ResourceLoaderModule::getHeaders ResourceLoaderModule::getPreloadLinks.
Definition: ResourceLoaderModuleTest.php:188
ResourceLoaderModuleTest\testPlaceholderize
testPlaceholderize()
ResourceLoaderModule::getRelativePaths ResourceLoaderModule::expandRelativePaths.
Definition: ResourceLoaderModuleTest.php:151
ResourceLoaderModuleTest\testBuildContentScripts
testBuildContentScripts( $raw, $build, $message=null)
provideBuildContentScripts ResourceLoaderModule::buildContent
Definition: ResourceLoaderModuleTest.php:134
ResourceLoaderTestCase\getResourceLoaderContext
getResourceLoaderContext( $options=[], ResourceLoader $rl=null)
Definition: ResourceLoaderTestCase.php:23
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
ResourceLoaderTestCase
Definition: ResourceLoaderTestCase.php:7