wiki.techinc.nl/tests/phpunit/includes/resourceloader/ResourceLoaderModuleTest.php
Timo Tijhof 047b60b96d resourceloader: Store relative instead of absolute paths in module_deps
Make paths stored in the module_deps table relative to $IP. This ensures that when
the MediaWiki install path changes and/or if the location of the extension or skins
directory changes, that ResourceLoader's internal model is still accurate.

Previously when these paths change, ResourceLoader would exhibit various bugs.

1. Unable to detect changes in the module (if the directory no longer exists).
2. Point #1 is usually preceeded by one last cache invalidation as the content hash
   of the file path changes (from a valid hash to an empty string).
3. Unnecessary cache invalidation (if both old and new directories exist). This
   happens when a file is both an entry point (in the 'scripts' or 'styles' array)
   and also a file dependency. At first they are de-duplicated by array_unique.
   But after the disk path changes, the next check will result in the old path
   being fetched from module_deps, and the new path from the live configuration.
   This causes two changes that result in needless cache invalidation:
   - The hash list contains one more item (T111481).
   - The hash list contains both the old and new version of a file.
     (or even alternate versions, e.g. when a change is backported to the old
     wmf branch; it also affects wikis on the new branch due to the stale
     file path still in the database).

It seems unusual to move a MediaWiki install, and usually we recommend third
parties to run update.php if site administrators do move their wiki. However
Wikimedia's deployment system implicitly moves the MediaWiki install continously
from e.g. "/srv/mediawiki/php-1.26wmf5" to "/srv/mediawiki/php-1.26wmf6".

This caused virtually all ResourceLoader caching layers to get invalidated every
week when another wmf-branch is deployed, thus breaking these file paths, which
changes the version hash, which then invalidates the cache.

Bug: T111481
Change-Id: I173a9820b3067c4a6598a4c8d77e239797d2659c
2015-09-30 00:25:27 +00:00

131 lines
3.6 KiB
PHP

<?php
class ResourceLoaderModuleTest extends ResourceLoaderTestCase {
/**
* @covers ResourceLoaderModule::getVersionHash
*/
public function testGetVersionHash() {
$context = $this->getResourceLoaderContext();
$baseParams = array(
'scripts' => array( 'foo.js', 'bar.js' ),
'dependencies' => array( 'jquery', 'mediawiki' ),
'messages' => array( 'hello', 'world' ),
);
$module = new ResourceLoaderFileModule( $baseParams );
$version = json_encode( $module->getVersionHash( $context ) );
// Exactly the same
$module = new ResourceLoaderFileModule( $baseParams );
$this->assertEquals(
$version,
json_encode( $module->getVersionHash( $context ) ),
'Instance is insignificant'
);
// Re-order dependencies
$module = new ResourceLoaderFileModule( array(
'dependencies' => array( 'mediawiki', 'jquery' ),
) + $baseParams );
$this->assertEquals(
$version,
json_encode( $module->getVersionHash( $context ) ),
'Order of dependencies is insignificant'
);
// Re-order messages
$module = new ResourceLoaderFileModule( array(
'messages' => array( 'world', 'hello' ),
) + $baseParams );
$this->assertEquals(
$version,
json_encode( $module->getVersionHash( $context ) ),
'Order of messages is insignificant'
);
// Re-order scripts
$module = new ResourceLoaderFileModule( array(
'scripts' => array( 'bar.js', 'foo.js' ),
) + $baseParams );
$this->assertNotEquals(
$version,
json_encode( $module->getVersionHash( $context ) ),
'Order of scripts is significant'
);
// Subclass
$module = new ResourceLoaderFileModuleTestModule( $baseParams );
$this->assertNotEquals(
$version,
json_encode( $module->getVersionHash( $context ) ),
'Class is significant'
);
}
/**
* @covers ResourceLoaderModule::validateScriptFile
*/
public function testValidateScriptFile() {
$context = $this->getResourceLoaderContext();
$module = new ResourceLoaderTestModule( array(
'script' => "var a = 'this is';\n {\ninvalid"
) );
$this->assertEquals(
$module->getScript( $context ),
'mw.log.error(' .
'"JavaScript parse error: Parse error: Unexpected token; ' .
'token } expected in file \'input\' on line 3"' .
');',
'Replace invalid syntax with error logging'
);
$module = new ResourceLoaderTestModule( array(
'script' => "\n'valid';"
) );
$this->assertEquals(
$module->getScript( $context ),
"\n'valid';",
'Leave valid scripts as-is'
);
}
/**
* @covers ResourceLoaderModule::getRelativePaths
* @covers ResourceLoaderModule::expandRelativePaths
*/
public function testPlaceholderize() {
$getRelativePaths = new ReflectionMethod( 'ResourceLoaderModule', 'getRelativePaths' );
$getRelativePaths->setAccessible( true );
$expandRelativePaths = new ReflectionMethod( 'ResourceLoaderModule', 'expandRelativePaths' );
$expandRelativePaths->setAccessible( true );
$this->setMwGlobals( array(
'IP' => '/srv/example/mediawiki/core',
) );
$raw = array(
'/srv/example/mediawiki/core/resources/foo.js',
'/srv/example/mediawiki/core/extensions/Example/modules/bar.js',
'/srv/example/mediawiki/skins/Example/baz.css',
'/srv/example/mediawiki/skins/Example/images/quux.png',
);
$canonical = array(
'resources/foo.js',
'extensions/Example/modules/bar.js',
'../skins/Example/baz.css',
'../skins/Example/images/quux.png',
);
$this->assertEquals(
$getRelativePaths->invoke( null, $raw ),
$canonical,
'Insert placeholders'
);
$this->assertEquals(
$expandRelativePaths->invoke( null, $canonical ),
$raw,
'Substitute placeholders'
);
}
}