wiki.techinc.nl/tests/phpunit/includes/resourceloader/ResourceLoaderLessVarFileModuleTest.php
Timo Tijhof 8ad97d7c32 resourceloader: Fail gracefully if a LESS message is not found
The message cache is originally meant for mw.messages in JS,
which expects non-existent messages to be cleanly omitted.
There is minimal server-side and client-side handling in place for this.

For LESS, however, we were assuming that the blob is complete,
thus not feeding anything to the LESS compiler, thus leading to
a run-time failure where the LESS file can't be parsed at all
due to an undeclared variable.

This could happen sometimes during development or after upgrading
a wiki with a stale LocalisationCache that is still being updated
at that time.

Bug: T267785
Change-Id: I60ff4eb7dce1fee56470acc177afd29ee14b764f
2021-01-07 15:41:02 -08:00

73 lines
2 KiB
PHP

<?php
/**
* @group ResourceLoader
* @covers ResourceLoaderLessVarFileModule
*/
class ResourceLoaderLessVarFileModuleTest extends ResourceLoaderTestCase {
public static function providerWrapAndEscapeMessage() {
return [
[
"Foo", '"Foo"',
],
[
"Foo bananas", '"Foo bananas"',
],
[
"Who's that test? Who's that test? It's Jess!",
'"Who\\\'s that test? Who\\\'s that test? It\\\'s Jess!"',
],
[
'Hello "he" said',
'"Hello \"he\" said"',
],
[
'boo";-o-link:javascript:alert(1);color:red;content:"',
'"boo\";-o-link:javascript:alert(1);color:red;content:\""',
],
[
'"jon\'s"',
'"\"jon\\\'s\""'
]
];
}
/**
* @dataProvider providerWrapAndEscapeMessage
* @covers ResourceLoaderLessVarFileModule::wrapAndEscapeMessage
*/
public function testEscapeMessage( $msg, $expected ) {
$method = new ReflectionMethod( ResourceLoaderLessVarFileModule::class, 'wrapAndEscapeMessage' );
$method->setAccessible( true );
$this->assertEquals( $expected, $method->invoke( null, $msg ) );
}
public function testLessMessagesFound() {
$context = $this->getResourceLoaderContext( 'qqx' );
$basePath = __DIR__ . '/../../data/less';
$module = new ResourceLoaderLessVarFileModule( [
'localBasePath' => $basePath,
'styles' => [ 'less-messages.less' ],
'lessMessages' => [ 'pieday' ],
] );
$module->setMessageBlob( '{"pieday":"March 14"}', 'qqx' );
$styles = $module->getStyles( $context );
$this->assertStringEqualsFile( $basePath . '/less-messages-exist.css', $styles['all'] );
}
public function testLessMessagesFailGraceful() {
$context = $this->getResourceLoaderContext( 'qqx' );
$basePath = __DIR__ . '/../../data/less';
$module = new ResourceLoaderLessVarFileModule( [
'localBasePath' => $basePath,
'styles' => [ 'less-messages.less' ],
'lessMessages' => [ 'pieday' ],
] );
$module->setMessageBlob( '{"something":"Else"}', 'qqx' );
$styles = $module->getStyles( $context );
$this->assertStringEqualsFile( $basePath . '/less-messages-nonexist.css', $styles['all'] );
}
}