Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / tests / phpunit / includes / resourceloader / ResourceLoaderModuleTest.php
blob6751f4dc2a609b71db0752db7b742ec18a27a251
1 <?php
3 class ResourceLoaderModuleTest extends ResourceLoaderTestCase {
5 /**
6 * @covers ResourceLoaderModule::getVersionHash
7 */
8 public function testGetVersionHash() {
9 $context = $this->getResourceLoaderContext();
11 $baseParams = [
12 'scripts' => [ 'foo.js', 'bar.js' ],
13 'dependencies' => [ 'jquery', 'mediawiki' ],
14 'messages' => [ 'hello', 'world' ],
17 $module = new ResourceLoaderFileModule( $baseParams );
18 $version = json_encode( $module->getVersionHash( $context ) );
20 // Exactly the same
21 $module = new ResourceLoaderFileModule( $baseParams );
22 $this->assertEquals(
23 $version,
24 json_encode( $module->getVersionHash( $context ) ),
25 'Instance is insignificant'
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'
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'
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'
58 // Subclass
59 $module = new ResourceLoaderFileModuleTestModule( $baseParams );
60 $this->assertNotEquals(
61 $version,
62 json_encode( $module->getVersionHash( $context ) ),
63 'Class is significant'
67 /**
68 * @covers ResourceLoaderModule::validateScriptFile
70 public function testValidateScriptFile() {
71 $this->setMwGlobals( 'wgResourceLoaderValidateJS', true );
73 $context = $this->getResourceLoaderContext();
75 $module = new ResourceLoaderTestModule( [
76 'script' => "var a = 'this is';\n {\ninvalid"
77 ] );
78 $this->assertEquals(
79 $module->getScript( $context ),
80 'mw.log.error(' .
81 '"JavaScript parse error: Parse error: Unexpected token; ' .
82 'token } expected in file \'input\' on line 3"' .
83 ');',
84 'Replace invalid syntax with error logging'
87 $module = new ResourceLoaderTestModule( [
88 'script' => "\n'valid';"
89 ] );
90 $this->assertEquals(
91 $module->getScript( $context ),
92 "\n'valid';",
93 'Leave valid scripts as-is'
97 /**
98 * @covers ResourceLoaderModule::getRelativePaths
99 * @covers ResourceLoaderModule::expandRelativePaths
101 public function testPlaceholderize() {
102 $getRelativePaths = new ReflectionMethod( 'ResourceLoaderModule', 'getRelativePaths' );
103 $getRelativePaths->setAccessible( true );
104 $expandRelativePaths = new ReflectionMethod( 'ResourceLoaderModule', 'expandRelativePaths' );
105 $expandRelativePaths->setAccessible( true );
107 $this->setMwGlobals( [
108 'IP' => '/srv/example/mediawiki/core',
109 ] );
110 $raw = [
111 '/srv/example/mediawiki/core/resources/foo.js',
112 '/srv/example/mediawiki/core/extensions/Example/modules/bar.js',
113 '/srv/example/mediawiki/skins/Example/baz.css',
114 '/srv/example/mediawiki/skins/Example/images/quux.png',
116 $canonical = [
117 'resources/foo.js',
118 'extensions/Example/modules/bar.js',
119 '../skins/Example/baz.css',
120 '../skins/Example/images/quux.png',
122 $this->assertEquals(
123 $getRelativePaths->invoke( null, $raw ),
124 $canonical,
125 'Insert placeholders'
127 $this->assertEquals(
128 $expandRelativePaths->invoke( null, $canonical ),
129 $raw,
130 'Substitute placeholders'