wiki.techinc.nl/tests/phpunit/includes/ResourceLoader/FileModuleTest.php

961 lines
25 KiB
PHP
Raw Normal View History

<?php
namespace MediaWiki\Tests\ResourceLoader;
use Exception;
use HashConfig;
use LogicException;
use MediaWiki\ResourceLoader\FileModule;
use MediaWiki\ResourceLoader\FilePath;
use MediaWiki\ResourceLoader\ResourceLoader;
use MediaWiki\Tests\Unit\DummyServicesTrait;
use ResourceLoaderFileTestModule;
use ResourceLoaderTestCase;
use RuntimeException;
use SkinFactory;
/**
* @group ResourceLoader
* @covers \MediaWiki\ResourceLoader\FileModule
*/
class FileModuleTest extends ResourceLoaderTestCase {
use DummyServicesTrait;
protected function setUp(): void {
parent::setUp();
$skinFactory = new SkinFactory( $this->getDummyObjectFactory(), [] );
// The empty spec shouldn't matter since this test should never call it
$skinFactory->register(
'fakeskin',
'FakeSkin',
[]
);
$this->setService( 'SkinFactory', $skinFactory );
// This test is not expected to query any database
$this->getServiceContainer()->disableStorage();
}
private static function getModules() {
$base = [
'localBasePath' => __DIR__,
];
return [
'noTemplateModule' => [],
'deprecatedModule' => $base + [
'deprecated' => true,
],
'deprecatedTomorrow' => $base + [
'deprecated' => 'Will be removed tomorrow.'
],
'htmlTemplateModule' => $base + [
'templates' => [
'templates/template.html',
'templates/template2.html',
]
],
'htmlTemplateUnknown' => $base + [
'templates' => [
'templates/notfound.html',
]
],
'aliasedHtmlTemplateModule' => $base + [
'templates' => [
'foo.html' => 'templates/template.html',
'bar.html' => 'templates/template2.html',
]
],
'templateModuleHandlebars' => $base + [
'templates' => [
'templates/template_awesome.handlebars',
],
],
'aliasFooFromBar' => $base + [
'templates' => [
'foo.foo' => 'templates/template.bar',
],
],
];
}
public static function providerTemplateDependencies() {
$modules = self::getModules();
return [
[
$modules['noTemplateModule'],
[],
],
[
$modules['htmlTemplateModule'],
[
'mediawiki.template',
],
],
[
$modules['templateModuleHandlebars'],
[
'mediawiki.template',
'mediawiki.template.handlebars',
],
],
[
$modules['aliasFooFromBar'],
[
'mediawiki.template',
'mediawiki.template.foo',
],
],
];
}
/**
* @dataProvider providerTemplateDependencies
*/
public function testTemplateDependencies( $module, $expected ) {
$rl = new FileModule( $module );
$rl->setName( 'testing' );
$this->assertEquals( $expected, $rl->getDependencies() );
}
public static function providerDeprecatedModules() {
return [
[
'deprecatedModule',
'mw.log.warn("This page is using the deprecated ResourceLoader module \"deprecatedModule\".");',
],
[
'deprecatedTomorrow',
'mw.log.warn(' .
'"This page is using the deprecated ResourceLoader module \"deprecatedTomorrow\".\\n' .
"Will be removed tomorrow." .
'");'
]
];
}
/**
* @dataProvider providerDeprecatedModules
*/
public function testDeprecatedModules( $name, $expected ) {
$modules = self::getModules();
$module = new FileModule( $modules[$name] );
$module->setName( $name );
$ctx = $this->getResourceLoaderContext();
$this->assertEquals( $expected, $module->getScript( $ctx ) );
}
public function testGetScript() {
$module = new FileModule( [
'localBasePath' => __DIR__ . '/../../data/resourceloader',
'scripts' => [ 'script-nosemi.js', 'script-comment.js' ],
] );
$module->setName( 'testing' );
$ctx = $this->getResourceLoaderContext();
$this->assertEquals(
"/* eslint-disable */\nmw.foo()\n" .
"/* eslint-disable */\nmw.foo()\n// mw.bar();\n",
$module->getScript( $ctx ),
'scripts with newline at the end are concatenated without a newline'
);
$module = new FileModule( [
'localBasePath' => __DIR__ . '/../../data/resourceloader',
'scripts' => [ 'script-nosemi-nonl.js', 'script-comment-nonl.js' ],
] );
$module->setName( 'testing' );
$ctx = $this->getResourceLoaderContext();
$this->assertEquals(
"/* eslint-disable */\nmw.foo()" .
"\n" .
"/* eslint-disable */\nmw.foo()\n// mw.bar();" .
"\n",
$module->getScript( $ctx ),
'scripts without newline at the end are concatenated with a newline'
);
}
resourceloader: Fix debug mode for RL-to-RL cross-wiki module loads The native "foreign module source" feature, as used by the GlobalCssJs extension, did not work correctly in debug mode as the urls returned by the remote wiki were formatted as "/w/load.php...", which would be interpreted by the browser relative to the host document, instead of relative to the parent script. For example: 1. Page view on en.wikipedia.org. 2. Script call to meta.wikimedia.org/w/load.php?debug=true&modules=ext.globalCssJs.user&user This URL is formatted by getScriptURLsForDebug on en.wikipedia.org, when building the article HTML. It knows the modules is on Meta, and formats it as such. So far so good. 3. meta.wikimedia.org responds with an array of urls for sub resources. That array contained URLs like "/w/load.php...only=scripts". These were formatted by getScriptURLsForDebug running on Meta, no longer with a reason to make it a Meta-Wiki URL as it isn't perceived as cross-wiki. It is indistinguishable from debugging a Meta-Wiki page view from its perspective. This patch affects scenario 3 by always expanding it relative to the current-request's wgServer. We still only do this in debug mode. There is not yet a need to do this in non-debug mode, and if there was we'd likely want to find a way to avoid it in the common case to keep embedded URLs short. The ResourceLoader::expandUrl() method is similar to the one in Wikimedia\Minify\CSSMin. Test Plan: * view-source:http://mw.localhost:8080/w/load.php?debug=1&modules=site For Module base class. Before, the array entries were relative. After, they are full. * view-source:http://mw.localhost:8080/w/load.php?debug=1&modules=jquery For FileModule. Before, the array entries were relative. After, they are full. * view-source:http://mw.localhost:8080/wiki/Main_Page?debug=true Unchanged. * view-source:http://mw.localhost:8080/wiki/Main_Page Unchanged. Bug: T255367 Change-Id: I83919744b2677c7fb52b84089ecc60b89957d32a
2021-08-25 02:36:25 +00:00
/**
* @covers \MediaWiki\ResourceLoader\FileModule
* @covers \MediaWiki\ResourceLoader\Module
* @covers \MediaWiki\ResourceLoader\ResourceLoader
resourceloader: Fix debug mode for RL-to-RL cross-wiki module loads The native "foreign module source" feature, as used by the GlobalCssJs extension, did not work correctly in debug mode as the urls returned by the remote wiki were formatted as "/w/load.php...", which would be interpreted by the browser relative to the host document, instead of relative to the parent script. For example: 1. Page view on en.wikipedia.org. 2. Script call to meta.wikimedia.org/w/load.php?debug=true&modules=ext.globalCssJs.user&user This URL is formatted by getScriptURLsForDebug on en.wikipedia.org, when building the article HTML. It knows the modules is on Meta, and formats it as such. So far so good. 3. meta.wikimedia.org responds with an array of urls for sub resources. That array contained URLs like "/w/load.php...only=scripts". These were formatted by getScriptURLsForDebug running on Meta, no longer with a reason to make it a Meta-Wiki URL as it isn't perceived as cross-wiki. It is indistinguishable from debugging a Meta-Wiki page view from its perspective. This patch affects scenario 3 by always expanding it relative to the current-request's wgServer. We still only do this in debug mode. There is not yet a need to do this in non-debug mode, and if there was we'd likely want to find a way to avoid it in the common case to keep embedded URLs short. The ResourceLoader::expandUrl() method is similar to the one in Wikimedia\Minify\CSSMin. Test Plan: * view-source:http://mw.localhost:8080/w/load.php?debug=1&modules=site For Module base class. Before, the array entries were relative. After, they are full. * view-source:http://mw.localhost:8080/w/load.php?debug=1&modules=jquery For FileModule. Before, the array entries were relative. After, they are full. * view-source:http://mw.localhost:8080/wiki/Main_Page?debug=true Unchanged. * view-source:http://mw.localhost:8080/wiki/Main_Page Unchanged. Bug: T255367 Change-Id: I83919744b2677c7fb52b84089ecc60b89957d32a
2021-08-25 02:36:25 +00:00
*/
public function testGetURLsForDebug() {
resourceloader: Fix debug mode for RL-to-RL cross-wiki module loads The native "foreign module source" feature, as used by the GlobalCssJs extension, did not work correctly in debug mode as the urls returned by the remote wiki were formatted as "/w/load.php...", which would be interpreted by the browser relative to the host document, instead of relative to the parent script. For example: 1. Page view on en.wikipedia.org. 2. Script call to meta.wikimedia.org/w/load.php?debug=true&modules=ext.globalCssJs.user&user This URL is formatted by getScriptURLsForDebug on en.wikipedia.org, when building the article HTML. It knows the modules is on Meta, and formats it as such. So far so good. 3. meta.wikimedia.org responds with an array of urls for sub resources. That array contained URLs like "/w/load.php...only=scripts". These were formatted by getScriptURLsForDebug running on Meta, no longer with a reason to make it a Meta-Wiki URL as it isn't perceived as cross-wiki. It is indistinguishable from debugging a Meta-Wiki page view from its perspective. This patch affects scenario 3 by always expanding it relative to the current-request's wgServer. We still only do this in debug mode. There is not yet a need to do this in non-debug mode, and if there was we'd likely want to find a way to avoid it in the common case to keep embedded URLs short. The ResourceLoader::expandUrl() method is similar to the one in Wikimedia\Minify\CSSMin. Test Plan: * view-source:http://mw.localhost:8080/w/load.php?debug=1&modules=site For Module base class. Before, the array entries were relative. After, they are full. * view-source:http://mw.localhost:8080/w/load.php?debug=1&modules=jquery For FileModule. Before, the array entries were relative. After, they are full. * view-source:http://mw.localhost:8080/wiki/Main_Page?debug=true Unchanged. * view-source:http://mw.localhost:8080/wiki/Main_Page Unchanged. Bug: T255367 Change-Id: I83919744b2677c7fb52b84089ecc60b89957d32a
2021-08-25 02:36:25 +00:00
$ctx = $this->getResourceLoaderContext();
$module = new FileModule( [
resourceloader: Fix debug mode for RL-to-RL cross-wiki module loads The native "foreign module source" feature, as used by the GlobalCssJs extension, did not work correctly in debug mode as the urls returned by the remote wiki were formatted as "/w/load.php...", which would be interpreted by the browser relative to the host document, instead of relative to the parent script. For example: 1. Page view on en.wikipedia.org. 2. Script call to meta.wikimedia.org/w/load.php?debug=true&modules=ext.globalCssJs.user&user This URL is formatted by getScriptURLsForDebug on en.wikipedia.org, when building the article HTML. It knows the modules is on Meta, and formats it as such. So far so good. 3. meta.wikimedia.org responds with an array of urls for sub resources. That array contained URLs like "/w/load.php...only=scripts". These were formatted by getScriptURLsForDebug running on Meta, no longer with a reason to make it a Meta-Wiki URL as it isn't perceived as cross-wiki. It is indistinguishable from debugging a Meta-Wiki page view from its perspective. This patch affects scenario 3 by always expanding it relative to the current-request's wgServer. We still only do this in debug mode. There is not yet a need to do this in non-debug mode, and if there was we'd likely want to find a way to avoid it in the common case to keep embedded URLs short. The ResourceLoader::expandUrl() method is similar to the one in Wikimedia\Minify\CSSMin. Test Plan: * view-source:http://mw.localhost:8080/w/load.php?debug=1&modules=site For Module base class. Before, the array entries were relative. After, they are full. * view-source:http://mw.localhost:8080/w/load.php?debug=1&modules=jquery For FileModule. Before, the array entries were relative. After, they are full. * view-source:http://mw.localhost:8080/wiki/Main_Page?debug=true Unchanged. * view-source:http://mw.localhost:8080/wiki/Main_Page Unchanged. Bug: T255367 Change-Id: I83919744b2677c7fb52b84089ecc60b89957d32a
2021-08-25 02:36:25 +00:00
'localBasePath' => __DIR__ . '/../../data/resourceloader',
'remoteBasePath' => '/w/something',
'styles' => [ 'simple.css' ],
resourceloader: Fix debug mode for RL-to-RL cross-wiki module loads The native "foreign module source" feature, as used by the GlobalCssJs extension, did not work correctly in debug mode as the urls returned by the remote wiki were formatted as "/w/load.php...", which would be interpreted by the browser relative to the host document, instead of relative to the parent script. For example: 1. Page view on en.wikipedia.org. 2. Script call to meta.wikimedia.org/w/load.php?debug=true&modules=ext.globalCssJs.user&user This URL is formatted by getScriptURLsForDebug on en.wikipedia.org, when building the article HTML. It knows the modules is on Meta, and formats it as such. So far so good. 3. meta.wikimedia.org responds with an array of urls for sub resources. That array contained URLs like "/w/load.php...only=scripts". These were formatted by getScriptURLsForDebug running on Meta, no longer with a reason to make it a Meta-Wiki URL as it isn't perceived as cross-wiki. It is indistinguishable from debugging a Meta-Wiki page view from its perspective. This patch affects scenario 3 by always expanding it relative to the current-request's wgServer. We still only do this in debug mode. There is not yet a need to do this in non-debug mode, and if there was we'd likely want to find a way to avoid it in the common case to keep embedded URLs short. The ResourceLoader::expandUrl() method is similar to the one in Wikimedia\Minify\CSSMin. Test Plan: * view-source:http://mw.localhost:8080/w/load.php?debug=1&modules=site For Module base class. Before, the array entries were relative. After, they are full. * view-source:http://mw.localhost:8080/w/load.php?debug=1&modules=jquery For FileModule. Before, the array entries were relative. After, they are full. * view-source:http://mw.localhost:8080/wiki/Main_Page?debug=true Unchanged. * view-source:http://mw.localhost:8080/wiki/Main_Page Unchanged. Bug: T255367 Change-Id: I83919744b2677c7fb52b84089ecc60b89957d32a
2021-08-25 02:36:25 +00:00
'scripts' => [ 'script-comment.js' ],
] );
$module->setName( 'testing' );
$module->setConfig( $ctx->getResourceLoader()->getConfig() );
$this->assertEquals(
[
'https://example.org/w/something/script-comment.js'
],
$module->getScriptURLsForDebug( $ctx ),
'script urls'
);
$this->assertEquals(
[ 'all' => [
'/w/something/simple.css'
] ],
$module->getStyleURLsForDebug( $ctx ),
'style urls'
resourceloader: Fix debug mode for RL-to-RL cross-wiki module loads The native "foreign module source" feature, as used by the GlobalCssJs extension, did not work correctly in debug mode as the urls returned by the remote wiki were formatted as "/w/load.php...", which would be interpreted by the browser relative to the host document, instead of relative to the parent script. For example: 1. Page view on en.wikipedia.org. 2. Script call to meta.wikimedia.org/w/load.php?debug=true&modules=ext.globalCssJs.user&user This URL is formatted by getScriptURLsForDebug on en.wikipedia.org, when building the article HTML. It knows the modules is on Meta, and formats it as such. So far so good. 3. meta.wikimedia.org responds with an array of urls for sub resources. That array contained URLs like "/w/load.php...only=scripts". These were formatted by getScriptURLsForDebug running on Meta, no longer with a reason to make it a Meta-Wiki URL as it isn't perceived as cross-wiki. It is indistinguishable from debugging a Meta-Wiki page view from its perspective. This patch affects scenario 3 by always expanding it relative to the current-request's wgServer. We still only do this in debug mode. There is not yet a need to do this in non-debug mode, and if there was we'd likely want to find a way to avoid it in the common case to keep embedded URLs short. The ResourceLoader::expandUrl() method is similar to the one in Wikimedia\Minify\CSSMin. Test Plan: * view-source:http://mw.localhost:8080/w/load.php?debug=1&modules=site For Module base class. Before, the array entries were relative. After, they are full. * view-source:http://mw.localhost:8080/w/load.php?debug=1&modules=jquery For FileModule. Before, the array entries were relative. After, they are full. * view-source:http://mw.localhost:8080/wiki/Main_Page?debug=true Unchanged. * view-source:http://mw.localhost:8080/wiki/Main_Page Unchanged. Bug: T255367 Change-Id: I83919744b2677c7fb52b84089ecc60b89957d32a
2021-08-25 02:36:25 +00:00
);
}
public function testGetAllSkinStyleFiles() {
$baseParams = [
'scripts' => [
'foo.js',
'bar.js',
],
'styles' => [
'foo.css',
'bar.css' => [ 'media' => 'print' ],
'screen.less' => [ 'media' => 'screen' ],
'screen-query.css' => [ 'media' => 'screen and (min-width: 400px)' ],
],
'skinStyles' => [
'default' => 'quux-fallback.less',
'fakeskin' => [
'baz-vector.css',
'quux-vector.less',
],
],
'messages' => [
'hello',
'world',
],
];
$module = new FileModule( $baseParams );
$module->setName( 'testing' );
$this->assertEquals(
[
'foo.css',
'baz-vector.css',
'quux-vector.less',
'quux-fallback.less',
'bar.css',
'screen.less',
'screen-query.css',
],
array_map( 'basename', $module->getAllStyleFiles() )
);
}
/**
* Strip @noflip annotations from CSS code.
* @param string $css
* @return string
*/
private static function stripNoflip( $css ) {
return str_replace( '/*@noflip*/ ', '', $css );
}
/**
* Confirm that 'ResourceModuleSkinStyles' skin attributes get injected
* into the module, and have their file contents read correctly from their
* own (out-of-module) directories.
*
* @covers \MediaWiki\ResourceLoader\FileModule
* @covers \MediaWiki\ResourceLoader\ResourceLoader
*/
public function testInjectSkinStyles() {
$moduleDir = __DIR__ . '/../../data/resourceloader';
$skinDir = __DIR__ . '/../../data/resourceloader/myskin';
$rl = new ResourceLoader( new HashConfig( self::getSettings() ) );
$rl->setModuleSkinStyles( [
'fakeskin' => [
'localBasePath' => $skinDir,
'testing' => [
'override.css',
],
],
] );
$rl->register( 'testing', [
'localBasePath' => $moduleDir,
'styles' => [ 'simple.css' ],
] );
$ctx = $this->getResourceLoaderContext( [ 'skin' => 'fakeskin' ], $rl );
$module = $rl->getModule( 'testing' );
$this->assertInstanceOf( FileModule::class, $module );
$this->assertEquals(
[ 'all' => ".example { color: blue; }\n\n.override { line-height: 2; }\n" ],
$module->getStyles( $ctx )
);
}
/**
* Verify what happens when you mix @embed and @noflip.
*/
public function testMixedCssAnnotations() {
$basePath = __DIR__ . '/../../data/css';
$testModule = new ResourceLoaderFileTestModule( [
'localBasePath' => $basePath,
'styles' => [ 'test.css' ],
] );
$testModule->setName( 'testing' );
$expectedModule = new ResourceLoaderFileTestModule( [
'localBasePath' => $basePath,
'styles' => [ 'expected.css' ],
] );
$expectedModule->setName( 'testing' );
$contextLtr = $this->getResourceLoaderContext( [
'lang' => 'en',
'dir' => 'ltr',
] );
$contextRtl = $this->getResourceLoaderContext( [
'lang' => 'he',
'dir' => 'rtl',
] );
// Since we want to compare the effect of @noflip+@embed against the effect of just @embed, and
// the @noflip annotations are always preserved, we need to strip them first.
$this->assertEquals(
$expectedModule->getStyles( $contextLtr ),
self::stripNoflip( $testModule->getStyles( $contextLtr ) ),
"/*@noflip*/ with /*@embed*/ gives correct results in LTR mode"
);
$this->assertEquals(
$expectedModule->getStyles( $contextLtr ),
self::stripNoflip( $testModule->getStyles( $contextRtl ) ),
"/*@noflip*/ with /*@embed*/ gives correct results in RTL mode"
);
}
public function testCssFlipping() {
$plain = new ResourceLoaderFileTestModule( [
'localBasePath' => __DIR__ . '/../../data/resourceloader',
'styles' => [ 'direction.css' ],
] );
$plain->setName( 'test' );
$context = $this->getResourceLoaderContext( [ 'lang' => 'en', 'dir' => 'ltr' ] );
$this->assertEquals(
[ 'all' => ".example { text-align: left; }\n" ],
$plain->getStyles( $context ),
'Unchanged styles in LTR mode'
);
$context = $this->getResourceLoaderContext( [ 'lang' => 'he', 'dir' => 'rtl' ] );
$this->assertEquals(
[ 'all' => ".example { text-align: right; }\n" ],
$plain->getStyles( $context ),
'Flipped styles in RTL mode'
);
$noflip = new ResourceLoaderFileTestModule( [
'localBasePath' => __DIR__ . '/../../data/resourceloader',
'styles' => [ 'direction.css' ],
'noflip' => true,
] );
$noflip->setName( 'test' );
$this->assertEquals(
[ 'all' => ".example { text-align: right; }\n" ],
$plain->getStyles( $context ),
'Unchanged styles in RTL mode with noflip at module level'
);
}
Allow skins/extensions to define custom OOUI themes This change follows I39cc2a735d9625c87bf4ede6f5fb0ec441d47dcc. docs/extension.schema.v1.json docs/extension.schema.v2.json includes/registration/ExtensionProcessor.php * The new extension attribute 'OOUIThemePaths' can be used to define custom OOUI themes. See I9187a63e509b601b8558ea82850fa828e5c8cc0a for an example usage. includes/resourceloader/ResourceLoaderOOUIModule.php * Add support for 'OOUIThemePaths'. * Defining 'images' is now optional. I figure custom themes are unlikely to have or need them. * Use ResourceLoaderFilePath objects to allow skin-/extension-defined OOUI module files to use skin/extension's base paths. This was previously used to support $wgResourceModuleSkinStyles, but only for 'skinStyles' - now ResourceLoaderFileModule needs to also handle it for 'skinScripts', and ResourceLoaderImageModule for 'images'). includes/resourceloader/ResourceLoaderFilePath.php * Add getters for local/remote base paths, for when we need to construct a new ResourceLoaderFilePath based on existing one. includes/resourceloader/ResourceLoaderFileModule.php includes/resourceloader/ResourceLoaderImageModule.php includes/resourceloader/ResourceLoaderOOUIImageModule.php * Add or improve handling of ResourceLoaderFilePaths: * Replace `(array)` casts with explicit array wrapping, to avoid casting objects into associative arrays. * Use getLocalPath() instead of string concatenation. tests/phpunit/includes/resourceloader/ResourceLoaderFileModuleTest.php tests/phpunit/includes/resourceloader/ResourceLoaderImageModuleTest.php * Some basic checks for the above. Bug: T100896 Change-Id: I74362f0fc215b26f1f104ce7bdbbac1e106736ad
2017-03-17 02:14:05 +00:00
/**
* Test reading files from elsewhere than localBasePath using ResourceLoaderFilePath.
*
* The use of ResourceLoaderFilePath objects resembles the way that ResourceLoader::getModule()
* injects additional files when 'ResourceModuleSkinStyles' or 'OOUIThemePaths' skin attributes
* apply to a given module.
Allow skins/extensions to define custom OOUI themes This change follows I39cc2a735d9625c87bf4ede6f5fb0ec441d47dcc. docs/extension.schema.v1.json docs/extension.schema.v2.json includes/registration/ExtensionProcessor.php * The new extension attribute 'OOUIThemePaths' can be used to define custom OOUI themes. See I9187a63e509b601b8558ea82850fa828e5c8cc0a for an example usage. includes/resourceloader/ResourceLoaderOOUIModule.php * Add support for 'OOUIThemePaths'. * Defining 'images' is now optional. I figure custom themes are unlikely to have or need them. * Use ResourceLoaderFilePath objects to allow skin-/extension-defined OOUI module files to use skin/extension's base paths. This was previously used to support $wgResourceModuleSkinStyles, but only for 'skinStyles' - now ResourceLoaderFileModule needs to also handle it for 'skinScripts', and ResourceLoaderImageModule for 'images'). includes/resourceloader/ResourceLoaderFilePath.php * Add getters for local/remote base paths, for when we need to construct a new ResourceLoaderFilePath based on existing one. includes/resourceloader/ResourceLoaderFileModule.php includes/resourceloader/ResourceLoaderImageModule.php includes/resourceloader/ResourceLoaderOOUIImageModule.php * Add or improve handling of ResourceLoaderFilePaths: * Replace `(array)` casts with explicit array wrapping, to avoid casting objects into associative arrays. * Use getLocalPath() instead of string concatenation. tests/phpunit/includes/resourceloader/ResourceLoaderFileModuleTest.php tests/phpunit/includes/resourceloader/ResourceLoaderImageModuleTest.php * Some basic checks for the above. Bug: T100896 Change-Id: I74362f0fc215b26f1f104ce7bdbbac1e106736ad
2017-03-17 02:14:05 +00:00
*/
public function testResourceLoaderFilePath() {
$basePath = __DIR__ . '/../../data/blahblah';
$filePath = __DIR__ . '/../../data/rlfilepath';
$testModule = new FileModule( [
Allow skins/extensions to define custom OOUI themes This change follows I39cc2a735d9625c87bf4ede6f5fb0ec441d47dcc. docs/extension.schema.v1.json docs/extension.schema.v2.json includes/registration/ExtensionProcessor.php * The new extension attribute 'OOUIThemePaths' can be used to define custom OOUI themes. See I9187a63e509b601b8558ea82850fa828e5c8cc0a for an example usage. includes/resourceloader/ResourceLoaderOOUIModule.php * Add support for 'OOUIThemePaths'. * Defining 'images' is now optional. I figure custom themes are unlikely to have or need them. * Use ResourceLoaderFilePath objects to allow skin-/extension-defined OOUI module files to use skin/extension's base paths. This was previously used to support $wgResourceModuleSkinStyles, but only for 'skinStyles' - now ResourceLoaderFileModule needs to also handle it for 'skinScripts', and ResourceLoaderImageModule for 'images'). includes/resourceloader/ResourceLoaderFilePath.php * Add getters for local/remote base paths, for when we need to construct a new ResourceLoaderFilePath based on existing one. includes/resourceloader/ResourceLoaderFileModule.php includes/resourceloader/ResourceLoaderImageModule.php includes/resourceloader/ResourceLoaderOOUIImageModule.php * Add or improve handling of ResourceLoaderFilePaths: * Replace `(array)` casts with explicit array wrapping, to avoid casting objects into associative arrays. * Use getLocalPath() instead of string concatenation. tests/phpunit/includes/resourceloader/ResourceLoaderFileModuleTest.php tests/phpunit/includes/resourceloader/ResourceLoaderImageModuleTest.php * Some basic checks for the above. Bug: T100896 Change-Id: I74362f0fc215b26f1f104ce7bdbbac1e106736ad
2017-03-17 02:14:05 +00:00
'localBasePath' => $basePath,
'remoteBasePath' => 'blahblah',
'styles' => new FilePath( 'style.css', $filePath, 'rlfilepath' ),
Allow skins/extensions to define custom OOUI themes This change follows I39cc2a735d9625c87bf4ede6f5fb0ec441d47dcc. docs/extension.schema.v1.json docs/extension.schema.v2.json includes/registration/ExtensionProcessor.php * The new extension attribute 'OOUIThemePaths' can be used to define custom OOUI themes. See I9187a63e509b601b8558ea82850fa828e5c8cc0a for an example usage. includes/resourceloader/ResourceLoaderOOUIModule.php * Add support for 'OOUIThemePaths'. * Defining 'images' is now optional. I figure custom themes are unlikely to have or need them. * Use ResourceLoaderFilePath objects to allow skin-/extension-defined OOUI module files to use skin/extension's base paths. This was previously used to support $wgResourceModuleSkinStyles, but only for 'skinStyles' - now ResourceLoaderFileModule needs to also handle it for 'skinScripts', and ResourceLoaderImageModule for 'images'). includes/resourceloader/ResourceLoaderFilePath.php * Add getters for local/remote base paths, for when we need to construct a new ResourceLoaderFilePath based on existing one. includes/resourceloader/ResourceLoaderFileModule.php includes/resourceloader/ResourceLoaderImageModule.php includes/resourceloader/ResourceLoaderOOUIImageModule.php * Add or improve handling of ResourceLoaderFilePaths: * Replace `(array)` casts with explicit array wrapping, to avoid casting objects into associative arrays. * Use getLocalPath() instead of string concatenation. tests/phpunit/includes/resourceloader/ResourceLoaderFileModuleTest.php tests/phpunit/includes/resourceloader/ResourceLoaderImageModuleTest.php * Some basic checks for the above. Bug: T100896 Change-Id: I74362f0fc215b26f1f104ce7bdbbac1e106736ad
2017-03-17 02:14:05 +00:00
'skinStyles' => [
'vector' => new FilePath( 'skinStyle.css', $filePath, 'rlfilepath' ),
Allow skins/extensions to define custom OOUI themes This change follows I39cc2a735d9625c87bf4ede6f5fb0ec441d47dcc. docs/extension.schema.v1.json docs/extension.schema.v2.json includes/registration/ExtensionProcessor.php * The new extension attribute 'OOUIThemePaths' can be used to define custom OOUI themes. See I9187a63e509b601b8558ea82850fa828e5c8cc0a for an example usage. includes/resourceloader/ResourceLoaderOOUIModule.php * Add support for 'OOUIThemePaths'. * Defining 'images' is now optional. I figure custom themes are unlikely to have or need them. * Use ResourceLoaderFilePath objects to allow skin-/extension-defined OOUI module files to use skin/extension's base paths. This was previously used to support $wgResourceModuleSkinStyles, but only for 'skinStyles' - now ResourceLoaderFileModule needs to also handle it for 'skinScripts', and ResourceLoaderImageModule for 'images'). includes/resourceloader/ResourceLoaderFilePath.php * Add getters for local/remote base paths, for when we need to construct a new ResourceLoaderFilePath based on existing one. includes/resourceloader/ResourceLoaderFileModule.php includes/resourceloader/ResourceLoaderImageModule.php includes/resourceloader/ResourceLoaderOOUIImageModule.php * Add or improve handling of ResourceLoaderFilePaths: * Replace `(array)` casts with explicit array wrapping, to avoid casting objects into associative arrays. * Use getLocalPath() instead of string concatenation. tests/phpunit/includes/resourceloader/ResourceLoaderFileModuleTest.php tests/phpunit/includes/resourceloader/ResourceLoaderImageModuleTest.php * Some basic checks for the above. Bug: T100896 Change-Id: I74362f0fc215b26f1f104ce7bdbbac1e106736ad
2017-03-17 02:14:05 +00:00
],
'scripts' => new FilePath( 'script.js', $filePath, 'rlfilepath' ),
'templates' => new FilePath( 'template.html', $filePath, 'rlfilepath' ),
Allow skins/extensions to define custom OOUI themes This change follows I39cc2a735d9625c87bf4ede6f5fb0ec441d47dcc. docs/extension.schema.v1.json docs/extension.schema.v2.json includes/registration/ExtensionProcessor.php * The new extension attribute 'OOUIThemePaths' can be used to define custom OOUI themes. See I9187a63e509b601b8558ea82850fa828e5c8cc0a for an example usage. includes/resourceloader/ResourceLoaderOOUIModule.php * Add support for 'OOUIThemePaths'. * Defining 'images' is now optional. I figure custom themes are unlikely to have or need them. * Use ResourceLoaderFilePath objects to allow skin-/extension-defined OOUI module files to use skin/extension's base paths. This was previously used to support $wgResourceModuleSkinStyles, but only for 'skinStyles' - now ResourceLoaderFileModule needs to also handle it for 'skinScripts', and ResourceLoaderImageModule for 'images'). includes/resourceloader/ResourceLoaderFilePath.php * Add getters for local/remote base paths, for when we need to construct a new ResourceLoaderFilePath based on existing one. includes/resourceloader/ResourceLoaderFileModule.php includes/resourceloader/ResourceLoaderImageModule.php includes/resourceloader/ResourceLoaderOOUIImageModule.php * Add or improve handling of ResourceLoaderFilePaths: * Replace `(array)` casts with explicit array wrapping, to avoid casting objects into associative arrays. * Use getLocalPath() instead of string concatenation. tests/phpunit/includes/resourceloader/ResourceLoaderFileModuleTest.php tests/phpunit/includes/resourceloader/ResourceLoaderImageModuleTest.php * Some basic checks for the above. Bug: T100896 Change-Id: I74362f0fc215b26f1f104ce7bdbbac1e106736ad
2017-03-17 02:14:05 +00:00
] );
$testModule->setName( 'testModule' );
$expectedModule = new FileModule( [
Allow skins/extensions to define custom OOUI themes This change follows I39cc2a735d9625c87bf4ede6f5fb0ec441d47dcc. docs/extension.schema.v1.json docs/extension.schema.v2.json includes/registration/ExtensionProcessor.php * The new extension attribute 'OOUIThemePaths' can be used to define custom OOUI themes. See I9187a63e509b601b8558ea82850fa828e5c8cc0a for an example usage. includes/resourceloader/ResourceLoaderOOUIModule.php * Add support for 'OOUIThemePaths'. * Defining 'images' is now optional. I figure custom themes are unlikely to have or need them. * Use ResourceLoaderFilePath objects to allow skin-/extension-defined OOUI module files to use skin/extension's base paths. This was previously used to support $wgResourceModuleSkinStyles, but only for 'skinStyles' - now ResourceLoaderFileModule needs to also handle it for 'skinScripts', and ResourceLoaderImageModule for 'images'). includes/resourceloader/ResourceLoaderFilePath.php * Add getters for local/remote base paths, for when we need to construct a new ResourceLoaderFilePath based on existing one. includes/resourceloader/ResourceLoaderFileModule.php includes/resourceloader/ResourceLoaderImageModule.php includes/resourceloader/ResourceLoaderOOUIImageModule.php * Add or improve handling of ResourceLoaderFilePaths: * Replace `(array)` casts with explicit array wrapping, to avoid casting objects into associative arrays. * Use getLocalPath() instead of string concatenation. tests/phpunit/includes/resourceloader/ResourceLoaderFileModuleTest.php tests/phpunit/includes/resourceloader/ResourceLoaderImageModuleTest.php * Some basic checks for the above. Bug: T100896 Change-Id: I74362f0fc215b26f1f104ce7bdbbac1e106736ad
2017-03-17 02:14:05 +00:00
'localBasePath' => $filePath,
'remoteBasePath' => 'rlfilepath',
'styles' => 'style.css',
'skinStyles' => [
'vector' => 'skinStyle.css',
],
'scripts' => 'script.js',
'templates' => 'template.html',
] );
$expectedModule->setName( 'expectedModule' );
Allow skins/extensions to define custom OOUI themes This change follows I39cc2a735d9625c87bf4ede6f5fb0ec441d47dcc. docs/extension.schema.v1.json docs/extension.schema.v2.json includes/registration/ExtensionProcessor.php * The new extension attribute 'OOUIThemePaths' can be used to define custom OOUI themes. See I9187a63e509b601b8558ea82850fa828e5c8cc0a for an example usage. includes/resourceloader/ResourceLoaderOOUIModule.php * Add support for 'OOUIThemePaths'. * Defining 'images' is now optional. I figure custom themes are unlikely to have or need them. * Use ResourceLoaderFilePath objects to allow skin-/extension-defined OOUI module files to use skin/extension's base paths. This was previously used to support $wgResourceModuleSkinStyles, but only for 'skinStyles' - now ResourceLoaderFileModule needs to also handle it for 'skinScripts', and ResourceLoaderImageModule for 'images'). includes/resourceloader/ResourceLoaderFilePath.php * Add getters for local/remote base paths, for when we need to construct a new ResourceLoaderFilePath based on existing one. includes/resourceloader/ResourceLoaderFileModule.php includes/resourceloader/ResourceLoaderImageModule.php includes/resourceloader/ResourceLoaderOOUIImageModule.php * Add or improve handling of ResourceLoaderFilePaths: * Replace `(array)` casts with explicit array wrapping, to avoid casting objects into associative arrays. * Use getLocalPath() instead of string concatenation. tests/phpunit/includes/resourceloader/ResourceLoaderFileModuleTest.php tests/phpunit/includes/resourceloader/ResourceLoaderImageModuleTest.php * Some basic checks for the above. Bug: T100896 Change-Id: I74362f0fc215b26f1f104ce7bdbbac1e106736ad
2017-03-17 02:14:05 +00:00
$context = $this->getResourceLoaderContext();
$this->assertEquals(
$expectedModule->getModuleContent( $context ),
$testModule->getModuleContent( $context ),
"Using ResourceLoaderFilePath works correctly"
);
}
public static function providerGetTemplates() {
$modules = self::getModules();
return [
[
$modules['noTemplateModule'],
[],
],
[
$modules['templateModuleHandlebars'],
[
'templates/template_awesome.handlebars' => "wow\n",
],
],
[
$modules['htmlTemplateModule'],
[
'templates/template.html' => "<strong>hello</strong>\n",
'templates/template2.html' => "<div>goodbye</div>\n",
],
],
[
$modules['aliasedHtmlTemplateModule'],
[
'foo.html' => "<strong>hello</strong>\n",
'bar.html' => "<div>goodbye</div>\n",
],
],
[
$modules['htmlTemplateUnknown'],
false,
],
];
}
/**
* @dataProvider providerGetTemplates
*/
public function testGetTemplates( $module, $expected ) {
$rl = new FileModule( $module );
$rl->setName( 'testing' );
if ( $expected === false ) {
$this->expectException( RuntimeException::class );
$rl->getTemplates();
} else {
$this->assertEquals( $expected, $rl->getTemplates() );
}
}
public function testBomConcatenation() {
$basePath = __DIR__ . '/../../data/css';
$testModule = new ResourceLoaderFileTestModule( [
'localBasePath' => $basePath,
'styles' => [ 'bom.css' ],
] );
$testModule->setName( 'testing' );
$this->assertEquals(
"\xef\xbb\xbf.efbbbf",
substr( file_get_contents( "$basePath/bom.css" ), 0, 10 ),
'File has leading BOM'
);
$context = $this->getResourceLoaderContext();
$this->assertEquals(
[ 'all' => ".efbbbf_bom_char_at_start_of_file {}\n" ],
$testModule->getStyles( $context ),
'Leading BOM removed when concatenating files'
);
}
public function testLessFileCompilation() {
$context = $this->getResourceLoaderContext();
$basePath = __DIR__ . '/../../data/less/module';
$module = new ResourceLoaderFileTestModule( [
'localBasePath' => $basePath,
'styles' => [ 'styles.less' ],
'lessVars' => [ 'foo' => '2px', 'Foo' => '#eeeeee' ]
] );
$module->setName( 'test.less' );
$styles = $module->getStyles( $context );
$this->assertStringEqualsFile( $basePath . '/styles.css', $styles['all'] );
}
public static function provideGetVersionHash() {
$a = [];
$b = [
'lessVars' => [ 'key' => 'value' ],
];
yield 'with and without Less variables' => [ $a, $b, false ];
$a = [
'lessVars' => [ 'key' => 'value1' ],
];
$b = [
'lessVars' => [ 'key' => 'value2' ],
];
yield 'different Less variables' => [ $a, $b, false ];
$x = [
'lessVars' => [ 'key' => 'value' ],
];
yield 'identical Less variables' => [ $x, $x, true ];
$a = [
'packageFiles' => [ [ 'name' => 'data.json', 'callback' => static function () {
return [ 'aaa' ];
} ] ]
];
$b = [
'packageFiles' => [ [ 'name' => 'data.json', 'callback' => static function () {
return [ 'bbb' ];
} ] ]
];
yield 'packageFiles with different callback' => [ $a, $b, false ];
$a = [
'packageFiles' => [ [ 'name' => 'aaa.json', 'callback' => static function () {
return [ 'x' ];
} ] ]
];
$b = [
'packageFiles' => [ [ 'name' => 'bbb.json', 'callback' => static function () {
return [ 'x' ];
} ] ]
];
yield 'packageFiles with different file name and a callback' => [ $a, $b, false ];
$a = [
'packageFiles' => [ [ 'name' => 'data.json', 'versionCallback' => static function () {
return [ 'A-version' ];
}, 'callback' => static function () {
throw new Exception( 'Unexpected computation' );
} ] ]
];
$b = [
'packageFiles' => [ [ 'name' => 'data.json', 'versionCallback' => static function () {
return [ 'B-version' ];
}, 'callback' => static function () {
throw new Exception( 'Unexpected computation' );
} ] ]
];
yield 'packageFiles with different versionCallback' => [ $a, $b, false ];
$a = [
'packageFiles' => [ [ 'name' => 'aaa.json',
'versionCallback' => static function () {
return [ 'X-version' ];
},
'callback' => static function () {
throw new Exception( 'Unexpected computation' );
}
] ]
];
$b = [
'packageFiles' => [ [ 'name' => 'bbb.json',
'versionCallback' => static function () {
return [ 'X-version' ];
},
'callback' => static function () {
throw new Exception( 'Unexpected computation' );
}
] ]
];
yield 'packageFiles with different file name and a versionCallback' => [ $a, $b, false ];
}
/**
* @dataProvider provideGetVersionHash
*/
public function testGetVersionHash( $a, $b, $isEqual ) {
resourceloader: Skip version hash calculation in debug mode === Why * More speed In debug mode, the server should regenerate the startup manifest on each page view to ensure immediate effect of changes. But, this also means more version recomputation work on the server. For most modules, this was already quite fast on repeat views because of OS-level file caches, and our file-hash caches and LESS compile caches in php-apcu from ResourceLoader. But, this makes it even faster. * Better integration with browser devtools. Breakpoints stay more consistently across browsers when the URL stays the same even after you have changed the file and reloaded the page. For static files, I believe most browsers ignore query parameters. But for package files that come from load.php, this was harder for browsers to guess correctly which old script URL is logically replaced by a different one on the next page view. === How Change Module::getVersionHash to return empty strings in debug mode. I considered approaching this from StartupModule::getModuleRegistrations instead to make the change apply only to the client-side manifest. I decided against this because we have other calls to getVersionHash on the server-side (such as for E-Tag calculation, and formatting cross-wiki URLs) which would then not match the version queries that mw.loader formats in debug mode. Also, those calls would still be incurring some the avoidable costs. === Notes * The two test cases for verifying the graceful fallback in production if version hash computations throw an exception, were moved to a non-debug test case as no longer happen now during the debug (unminified) test cases. * Avoid "PHP Notice: Undefined offset 0" in testMakeModuleResponseStartupError by adding a fallback to empty string so that if the test fails, it fails in a more useful way instead of aborting with this error before the assertion happens. (Since PHPUnit generally stops on the first error.) * In practice, there are still "version" query parameters and E-Tag headers in debug mode. These are not module versions, but URL "combined versions" crafted by getCombinedVersion() in JS and PHP. These return the constant "ztntf" in debug mode, which is the hash of an empty string. We could alter these methods to special-case when all inputs are and join to a still-empty string, or maybe we just leave them be. I've done the latter for now. Bug: T235672 Bug: T85805 Change-Id: I0e63eef4f85b13089a0aa3806a5b6f821d527a92
2021-08-28 02:53:36 +00:00
$context = $this->getResourceLoaderContext( [ 'debug' => 'false' ] );
$moduleA = new ResourceLoaderFileTestModule( $a );
$moduleA->setConfig( $context->getResourceLoader()->getConfig() );
$versionA = $moduleA->getVersionHash( $context );
$moduleB = new ResourceLoaderFileTestModule( $b );
$moduleB->setConfig( $context->getResourceLoader()->getConfig() );
$versionB = $moduleB->getVersionHash( $context );
$this->assertSame(
$isEqual,
( $versionA === $versionB ),
'Whether versions hashes are equal'
);
}
public static function provideGetScriptPackageFiles() {
$basePath = __DIR__ . '/../../data/resourceloader';
$basePathB = __DIR__ . '/../../data/resourceloader-b';
$base = [ 'localBasePath' => $basePath ];
$commentScript = file_get_contents( "$basePath/script-comment.js" );
$nosemiScript = file_get_contents( "$basePath/script-nosemi.js" );
$nosemiBScript = file_get_contents( "$basePathB/script-nosemi.js" );
$vueComponentDebug = trim( file_get_contents( "$basePath/vue-component-output-debug.js.txt" ) );
$vueComponentNonDebug = trim( file_get_contents( "$basePath/vue-component-output-nondebug.js.txt" ) );
$config = \MediaWiki\MediaWikiServices::getInstance()->getMainConfig();
return [
[
$base + [
'packageFiles' => [
'script-comment.js',
'script-nosemi.js'
]
],
[
'files' => [
'script-comment.js' => [
'type' => 'script',
'content' => $commentScript,
],
'script-nosemi.js' => [
'type' => 'script',
'content' => $nosemiScript
]
],
'main' => 'script-comment.js'
]
],
[
$base + [
'packageFiles' => [
'script-comment.js',
[ 'name' => 'script-nosemi.js', 'main' => true ]
],
'deprecated' => 'Deprecation test',
'name' => 'test-deprecated'
],
[
'files' => [
'script-comment.js' => [
'type' => 'script',
'content' => $commentScript,
],
'script-nosemi.js' => [
'type' => 'script',
'content' => 'mw.log.warn(' .
'"This page is using the deprecated ResourceLoader module \"test-deprecated\".\\n' .
"Deprecation test" .
'");' .
$nosemiScript
]
],
'main' => 'script-nosemi.js'
]
],
[
$base + [
'packageFiles' => [
[ 'name' => 'init.js', 'file' => 'script-comment.js', 'main' => true ],
[ 'name' => 'nosemi.js', 'file' => 'script-nosemi.js' ],
]
],
[
'files' => [
'init.js' => [
'type' => 'script',
'content' => $commentScript,
],
'nosemi.js' => [
'type' => 'script',
'content' => $nosemiScript
]
],
'main' => 'init.js'
]
],
'package file with callback' => [
$base + [
'packageFiles' => [
[ 'name' => 'foo.json', 'content' => [ 'Hello' => 'world' ] ],
'sample.json',
[ 'name' => 'bar.js', 'content' => "console.log('Hello');" ],
[
'name' => 'data.json',
'callback' => static function ( $context, $config, $extra ) {
return [ 'langCode' => $context->getLanguage(), 'extra' => $extra ];
},
'callbackParam' => [ 'a' => 'b' ],
],
[ 'name' => 'config.json', 'config' => [
'Sitename',
'server' => 'ServerName',
] ],
]
],
[
'files' => [
'foo.json' => [
'type' => 'data',
'content' => [ 'Hello' => 'world' ],
],
'sample.json' => [
'type' => 'data',
'content' => (object)[ 'foo' => 'bar', 'answer' => 42 ],
],
'bar.js' => [
'type' => 'script',
'content' => "console.log('Hello');",
],
'data.json' => [
'type' => 'data',
'content' => [ 'langCode' => 'fy', 'extra' => [ 'a' => 'b' ] ],
],
'config.json' => [
'type' => 'data',
'content' => [
'Sitename' => $config->get( 'Sitename' ),
'server' => $config->get( 'ServerName' ),
]
]
],
'main' => 'bar.js'
],
[
'lang' => 'fy'
]
],
'package file with callback and versionCallback' => [
$base + [
'packageFiles' => [
[ 'name' => 'bar.js', 'content' => "console.log('Hello');" ],
[
'name' => 'data.json',
'versionCallback' => static function ( $context ) {
return 'x';
},
'callback' => static function ( $context, $config, $extra ) {
return [ 'langCode' => $context->getLanguage(), 'extra' => $extra ];
},
'callbackParam' => [ 'A', 'B' ]
],
]
],
[
'files' => [
'bar.js' => [
'type' => 'script',
'content' => "console.log('Hello');",
],
'data.json' => [
'type' => 'data',
'content' => [ 'langCode' => 'fy', 'extra' => [ 'A', 'B' ] ],
],
],
'main' => 'bar.js'
],
[
'lang' => 'fy'
]
],
'package file with callback that returns a file (1)' => [
$base + [
'packageFiles' => [
[ 'name' => 'dynamic.js', 'callback' => static function ( $context ) {
$file = $context->getLanguage() === 'fy' ? 'script-comment.js' : 'script-nosemi.js';
return new FilePath( $file );
} ]
]
],
[
'files' => [
'dynamic.js' => [
'type' => 'script',
'content' => $commentScript,
]
],
'main' => 'dynamic.js'
],
[
'lang' => 'fy'
]
],
'package file with callback that returns a file (2)' => [
$base + [
'packageFiles' => [
[ 'name' => 'dynamic.js', 'callback' => static function ( $context ) {
$file = $context->getLanguage() === 'fy' ? 'script-comment.js' : 'script-nosemi.js';
return new FilePath( $file );
} ]
]
],
[
'files' => [
'dynamic.js' => [
'type' => 'script',
'content' => $nosemiScript,
]
],
'main' => 'dynamic.js'
],
[
'lang' => 'nl'
]
],
'package file with callback that returns a file with base path' => [
$base + [
'packageFiles' => [
[ 'name' => 'dynamic.js', 'callback' => static function () use ( $basePathB ) {
return new FilePath( 'script-nosemi.js', $basePathB );
} ]
]
],
[
'files' => [
'dynamic.js' => [
'type' => 'script',
'content' => $nosemiBScript,
]
],
'main' => 'dynamic.js'
]
],
'.vue file in debug mode' => [
$base + [
'packageFiles' => [
'vue-component.vue'
]
],
[
'files' => [
'vue-component.vue' => [
'type' => 'script',
'content' => $vueComponentDebug
]
],
'main' => 'vue-component.vue',
],
[
'debug' => 'true'
]
],
'.vue file in non-debug mode' => [
$base + [
'packageFiles' => [
'vue-component.vue'
],
'name' => 'nondebug',
],
[
'files' => [
'vue-component.vue' => [
'type' => 'script',
'content' => $vueComponentNonDebug
]
],
'main' => 'vue-component.vue'
],
[
'debug' => 'false'
]
],
[
$base + [
'packageFiles' => [
[ 'file' => 'script-comment.js' ]
]
],
LogicException::class
],
'package file with invalid callback' => [
$base + [
'packageFiles' => [
[ 'name' => 'foo.json', 'callback' => 'functionThatDoesNotExist142857' ]
]
],
LogicException::class
],
[
// 'config' not valid for 'script' type
$base + [
'packageFiles' => [
'foo.json' => [ 'type' => 'script', 'config' => [ 'Sitename' ] ]
]
],
LogicException::class
],
[
// 'config' not valid for '*.js' file
$base + [
'packageFiles' => [
[ 'name' => 'foo.js', 'config' => 'Sitename' ]
]
],
LogicException::class
],
[
// missing type/name/file.
$base + [
'packageFiles' => [
'foo.js' => [ 'garbage' => 'data' ]
]
],
LogicException::class
],
[
$base + [
'packageFiles' => [
'filethatdoesnotexist142857.js'
]
],
RuntimeException::class
],
[
// JSON can't be a main file
$base + [
'packageFiles' => [
'script-nosemi.js',
[ 'name' => 'foo.json', 'content' => [ 'Hello' => 'world' ], 'main' => true ]
]
],
LogicException::class
]
];
}
/**
* @dataProvider provideGetScriptPackageFiles
*/
public function testGetScriptPackageFiles( $moduleDefinition, $expected, $contextOptions = [] ) {
$module = new FileModule( $moduleDefinition );
$context = $this->getResourceLoaderContext( $contextOptions );
$module->setConfig( $context->getResourceLoader()->getConfig() );
if ( isset( $moduleDefinition['name'] ) ) {
$module->setName( $moduleDefinition['name'] );
}
if ( is_string( $expected ) ) {
// Class name of expected exception
$this->expectException( $expected );
$module->getScript( $context );
} else {
// Array of expected return value
$this->assertEquals( $expected, $module->getScript( $context ) );
}
}
public function testRequiresES6() {
$module = new FileModule();
ResourceLoader: Raise MW JavaScript startup requirement to ES6 The UA sniffs that overrode the feature tests are no longer needed. * MSIE 10: Fine, rejected by feature checks. * UC Mini "Speed Mode": Redundant, the version that this sniff matched is pre-ES6. Current versions of UC Mini don't appear to support enabling "Speed Mode" on random websites nor does it offer it for Wikipedia specifically. Details at https://phabricator.wikimedia.org/T178356#8740573. * Google Web Light: Redundant, shutdown as of 2022. Any references or extensions that still reach the proxy, get redirected to our online URLs https://googleweblight.com/?lite_url=https://en.m.wikipedia.org/wiki/Banana https://phabricator.wikimedia.org/T152602 https://en.wikipedia.org/wiki/Google_Web_Light * MeeGo: Redundant, discontinued and presumed rejected. Either way, unsupported. * Opera Mini: Fine, rejected by checks. Details at https://phabricator.wikimedia.org/T178356#8740573. * Ovi Browser: Redundant, discontinued and presumed rejected. Either way, unsupported. * Google Glass: Improve UX (since 2013, T58008). * NetFront: Redundant. Old versions are presumed rejected. Current versions are Chromium-based and presumed fine. The exclusion was not UX based, but due to jQuery explicitly not supporting it in 2013. This is no longer the case, so we can let the feature test lead the way here. * PlayStation: Redundant, same story as NetFront. The version that matched the sniff is presumed rejected. Current versions probably fine, but even not, don't match our sniff so are already enabled today. Bug: T178356 Change-Id: Ib6263ce3ffd11af5e501de8857f3e48a248c6210
2023-03-24 12:56:01 +00:00
$this->assertTrue( $module->requiresES6(), 'requiresES6 defaults to true' );
$module = new FileModule( [ 'es6' => false ] );
ResourceLoader: Raise MW JavaScript startup requirement to ES6 The UA sniffs that overrode the feature tests are no longer needed. * MSIE 10: Fine, rejected by feature checks. * UC Mini "Speed Mode": Redundant, the version that this sniff matched is pre-ES6. Current versions of UC Mini don't appear to support enabling "Speed Mode" on random websites nor does it offer it for Wikipedia specifically. Details at https://phabricator.wikimedia.org/T178356#8740573. * Google Web Light: Redundant, shutdown as of 2022. Any references or extensions that still reach the proxy, get redirected to our online URLs https://googleweblight.com/?lite_url=https://en.m.wikipedia.org/wiki/Banana https://phabricator.wikimedia.org/T152602 https://en.wikipedia.org/wiki/Google_Web_Light * MeeGo: Redundant, discontinued and presumed rejected. Either way, unsupported. * Opera Mini: Fine, rejected by checks. Details at https://phabricator.wikimedia.org/T178356#8740573. * Ovi Browser: Redundant, discontinued and presumed rejected. Either way, unsupported. * Google Glass: Improve UX (since 2013, T58008). * NetFront: Redundant. Old versions are presumed rejected. Current versions are Chromium-based and presumed fine. The exclusion was not UX based, but due to jQuery explicitly not supporting it in 2013. This is no longer the case, so we can let the feature test lead the way here. * PlayStation: Redundant, same story as NetFront. The version that matched the sniff is presumed rejected. Current versions probably fine, but even not, don't match our sniff so are already enabled today. Bug: T178356 Change-Id: Ib6263ce3ffd11af5e501de8857f3e48a248c6210
2023-03-24 12:56:01 +00:00
$this->assertTrue( $module->requiresES6(), 'requiresES6 is true even when set to false' );
$module = new FileModule( [ 'es6' => true ] );
$this->assertTrue( $module->requiresES6(), 'requiresES6 is true when set to true' );
}
}