<?php
/**
- * @group Database
* @group ResourceLoader
*/
class ResourceLoaderFileModuleTest extends ResourceLoaderTestCase {
}
);
$this->setService( 'SkinFactory', $skinFactory );
+
+ // This test is not expected to query any database
+ MediaWiki\MediaWikiServices::disableStorageBackend();
}
private static function getModules() {
$base = [
- 'localBasePath' => realpath( __DIR__ ),
+ 'localBasePath' => __DIR__,
];
return [
*/
public function testMixedCssAnnotations() {
$basePath = __DIR__ . '/../../data/css';
- $testModule = new ResourceLoaderFileModule( [
+ $testModule = new ResourceLoaderFileTestModule( [
'localBasePath' => $basePath,
'styles' => [ 'test.css' ],
] );
$testModule->setName( 'testing' );
- $expectedModule = new ResourceLoaderFileModule( [
+ $expectedModule = new ResourceLoaderFileTestModule( [
'localBasePath' => $basePath,
'styles' => [ 'expected.css' ],
] );
);
}
+ /**
+ * Test reading files from elsewhere than localBasePath using ResourceLoaderFilePath.
+ *
+ * This mimics modules modified by skins using 'ResourceModuleSkinStyles' and 'OOUIThemePaths'
+ * skin attributes.
+ *
+ * @covers ResourceLoaderFilePath::getLocalBasePath
+ * @covers ResourceLoaderFilePath::getRemoteBasePath
+ */
+ public function testResourceLoaderFilePath() {
+ $basePath = __DIR__ . '/../../data/blahblah';
+ $filePath = __DIR__ . '/../../data/rlfilepath';
+ $testModule = new ResourceLoaderFileModule( [
+ 'localBasePath' => $basePath,
+ 'remoteBasePath' => 'blahblah',
+ 'styles' => new ResourceLoaderFilePath( 'style.css', $filePath, 'rlfilepath' ),
+ 'skinStyles' => [
+ 'vector' => new ResourceLoaderFilePath( 'skinStyle.css', $filePath, 'rlfilepath' ),
+ ],
+ 'scripts' => new ResourceLoaderFilePath( 'script.js', $filePath, 'rlfilepath' ),
+ 'templates' => new ResourceLoaderFilePath( 'template.html', $filePath, 'rlfilepath' ),
+ ] );
+ $expectedModule = new ResourceLoaderFileModule( [
+ 'localBasePath' => $filePath,
+ 'remoteBasePath' => 'rlfilepath',
+ 'styles' => 'style.css',
+ 'skinStyles' => [
+ 'vector' => 'skinStyle.css',
+ ],
+ 'scripts' => 'script.js',
+ 'templates' => 'template.html',
+ ] );
+
+ $context = $this->getResourceLoaderContext();
+ $this->assertEquals(
+ $expectedModule->getModuleContent( $context ),
+ $testModule->getModuleContent( $context ),
+ "Using ResourceLoaderFilePath works correctly"
+ );
+ }
+
public static function providerGetTemplates() {
$modules = self::getModules();
*/
public function testBomConcatenation() {
$basePath = __DIR__ . '/../../data/css';
- $testModule = new ResourceLoaderFileModule( [
+ $testModule = new ResourceLoaderFileTestModule( [
'localBasePath' => $basePath,
'styles' => [ 'bom.css' ],
] );
$module = new ResourceLoaderFileTestModule( [
'localBasePath' => $basePath,
'styles' => [ 'styles.less' ],
- ], [
'lessVars' => [ 'foo' => '2px', 'Foo' => '#eeeeee' ]
] );
$module->setName( 'test.less' );
$this->assertStringEqualsFile( $basePath . '/styles.css', $styles['all'] );
}
+ public 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' => function () {
+ return [ 'aaa' ];
+ } ] ]
+ ];
+ $b = [
+ 'packageFiles' => [ [ 'name' => 'data.json', 'callback' => function () {
+ return [ 'bbb' ];
+ } ] ]
+ ];
+ yield 'packageFiles with different callback' => [ $a, $b, false ];
+
+ $a = [
+ 'packageFiles' => [ [ 'name' => 'aaa.json', 'callback' => function () {
+ return [ 'x' ];
+ } ] ]
+ ];
+ $b = [
+ 'packageFiles' => [ [ 'name' => 'bbb.json', 'callback' => function () {
+ return [ 'x' ];
+ } ] ]
+ ];
+ yield 'packageFiles with different file name and a callback' => [ $a, $b, false ];
+
+ $a = [
+ 'packageFiles' => [ [ 'name' => 'data.json', 'versionCallback' => function () {
+ return [ 'A-version' ];
+ }, 'callback' => function () {
+ throw new Exception( 'Unexpected computation' );
+ } ] ]
+ ];
+ $b = [
+ 'packageFiles' => [ [ 'name' => 'data.json', 'versionCallback' => function () {
+ return [ 'B-version' ];
+ }, 'callback' => function () {
+ throw new Exception( 'Unexpected computation' );
+ } ] ]
+ ];
+ yield 'packageFiles with different versionCallback' => [ $a, $b, false ];
+
+ $a = [
+ 'packageFiles' => [ [ 'name' => 'aaa.json',
+ 'versionCallback' => function () {
+ return [ 'X-version' ];
+ },
+ 'callback' => function () {
+ throw new Exception( 'Unexpected computation' );
+ }
+ ] ]
+ ];
+ $b = [
+ 'packageFiles' => [ [ 'name' => 'bbb.json',
+ 'versionCallback' => function () {
+ return [ 'X-version' ];
+ },
+ 'callback' => function () {
+ throw new Exception( 'Unexpected computation' );
+ }
+ ] ]
+ ];
+ yield 'packageFiles with different file name and a versionCallback' => [ $a, $b, false ];
+ }
+
/**
+ * @dataProvider provideGetVersionHash
* @covers ResourceLoaderFileModule::getDefinitionSummary
* @covers ResourceLoaderFileModule::getFileHashes
*/
- public function testGetVersionHash() {
+ public function testGetVersionHash( $a, $b, $isEqual ) {
$context = $this->getResourceLoaderContext();
- // Less variables
- $module = new ResourceLoaderFileTestModule();
- $version = $module->getVersionHash( $context );
- $module = new ResourceLoaderFileTestModule( [], [
- 'lessVars' => [ 'key' => 'value' ],
- ] );
- $this->assertNotEquals(
- $version,
- $module->getVersionHash( $context ),
- 'Using less variables is significant'
+ $moduleA = new ResourceLoaderFileTestModule( $a );
+ $versionA = $moduleA->getVersionHash( $context );
+ $moduleB = new ResourceLoaderFileTestModule( $b );
+ $versionB = $moduleB->getVersionHash( $context );
+
+ $this->assertSame(
+ $isEqual,
+ ( $versionA === $versionB ),
+ 'Whether versions hashes are equal'
);
}
- public function providerGetScriptPackageFiles() {
+ public function provideGetScriptPackageFiles() {
$basePath = __DIR__ . '/../../data/resourceloader';
$base = [ 'localBasePath' => $basePath ];
$commentScript = file_get_contents( "$basePath/script-comment.js" );
'main' => 'init.js'
]
],
- [
+ 'package file with callback' => [
$base + [
'packageFiles' => [
[ 'name' => 'foo.json', 'content' => [ 'Hello' => 'world' ] ],
'lang' => 'fy'
]
],
+ 'package file with callback and versionCallback' => [
+ $base + [
+ 'packageFiles' => [
+ [ 'name' => 'bar.js', 'content' => "console.log('Hello');" ],
+ [ 'name' => 'data.json', 'versionCallback' => function ( $context ) {
+ return $context->getLanguage();
+ }, 'callback' => function ( $context ) {
+ return [ 'langCode' => $context->getLanguage() ];
+ } ],
+ ]
+ ],
+ [
+ 'files' => [
+ 'bar.js' => [
+ 'type' => 'script',
+ 'content' => "console.log('Hello');",
+ ],
+ 'data.json' => [
+ 'type' => 'data',
+ 'content' => [ 'langCode' => 'fy' ]
+ ],
+ ],
+ 'main' => 'bar.js'
+ ],
+ [
+ 'lang' => 'fy'
+ ]
+ ],
[
$base + [
'packageFiles' => [
],
false
],
- [
+ 'package file with invalid callback' => [
$base + [
'packageFiles' => [
[ 'name' => 'foo.json', 'callback' => 'functionThatDoesNotExist142857' ]
}
/**
- * @dataProvider providerGetScriptPackageFiles
+ * @dataProvider provideGetScriptPackageFiles
* @covers ResourceLoaderFileModule::getScript
* @covers ResourceLoaderFileModule::getPackageFiles
* @covers ResourceLoaderFileModule::expandPackageFiles