wiki.techinc.nl/tests/qunit/data/testloader.php
lupo 5d7ae11c57 (bug 35240) Fix mw.loader state machine.
Main changes:

* handlePending() correctly handles "missing" and "error" states
and propagates error states up the dependency tree.

* handlePending() is called whenever a module enters one of the
states "ready", "error", or "missing" (in execute() and
mw.loader.state()).

* load() filters out not only undefined modules, but -- by the logic of
the comment there -- also modules in state "error" or "missing".

Minor changes:

* recurse() renamed to sortDependencies(), also uses a hash for
unresolved now instead of an array.

* execute() was never called with the second parameter "callback", hence
I've removed it.

* simplified the "are all dependencies 'ready'?" test and moved it to
its own function.

The change comes with additional QUnit tests for mw.loader. If I run
these tests against the current mediawiki.js, several of them fail. In
particular test #86 ("mw.loader real missing dependency") is
instructive: if the server returns "missing" for a module, dependent
modules never progress beyond "loaded", and if there are jobs (from
mw.loader.using()) depending on such a missing module (directly or
indirectly), neither ready() nor error() is ever called.

Running the tests against the changed mediawiki.js in this change, they
all succeed for me.

Patchset 2: whitespace changes, $_GET instead of $_REQUEST in
testloader.

Patchset 3: XML::encodeJsVar() in testloader, deepEqual() in Qunit
tests.

Patchset 4: rebase

Patchset 5: Amend commit message only (typo)

Change-Id: Ia67edfc07fc9237def04ed13bb2cee16e519d7af
2012-05-09 11:01:19 +02:00

100 lines
3.1 KiB
PHP

<?php
/**
* ResourceLoader stub working with pre-defined test modules.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @package MediaWiki
* @author Lupo
* @since 1.20
*/
header( 'Content-Type: text/javascript; charset=utf-8' );
require_once "../../../includes/Xml.php";
$modules = array (
'test.use_missing' => array (
'src' => 'mw.loader.implement("test.use_missing", function () {start(); ok(false, "Module test.use_missing should not run.");}, {}, {});',
'deps' => array ( 'test.missing' )
),
'test.use_missing2' => array (
'src' => 'mw.loader.implement("test.use_missing2", function () {start(); ok(false, "Module test.use_missing2 should not run.");}, {}, {});',
'deps' => array ( 'test.missing2' )
)
);
// Copied from ResourceLoaderContext
function expandModuleNames( $modules ) {
$retval = array();
// For backwards compatibility with an earlier hack, replace ! with .
$modules = str_replace( '!', '.', $modules );
$exploded = explode( '|', $modules );
foreach ( $exploded as $group ) {
if ( strpos( $group, ',' ) === false ) {
// This is not a set of modules in foo.bar,baz notation
// but a single module
$retval[] = $group;
} else {
// This is a set of modules in foo.bar,baz notation
$pos = strrpos( $group, '.' );
if ( $pos === false ) {
// Prefixless modules, i.e. without dots
$retval = explode( ',', $group );
} else {
// We have a prefix and a bunch of suffixes
$prefix = substr( $group, 0, $pos ); // 'foo'
$suffixes = explode( ',', substr( $group, $pos + 1 ) ); // array( 'bar', 'baz' )
foreach ( $suffixes as $suffix ) {
$retval[] = "$prefix.$suffix";
}
}
}
}
return $retval;
}
function addModule ( $moduleName, &$gotten ) {
global $modules;
$result = "";
if ( isset( $gotten[$moduleName] ) ) {
return $result;
}
$gotten[$moduleName] = true;
if ( isset( $modules[$moduleName] ) ) {
$deps = $modules[$moduleName]['deps'];
foreach ( $deps as $depName ) {
$result .= addModule( $depName, $gotten ) . "\n";
}
$result .= $modules[$moduleName]['src'] . "\n";
} else {
$result .= 'mw.loader.state( ' . Xml::encodeJsVar( $moduleName ) . ', "missing" );' . "\n";
}
return $result . "\n";
}
$result = "";
if ( isset( $_GET['modules'] ) ) {
$toGet = expandModuleNames( $_GET['modules'] );
$gotten = array();
foreach ( $toGet as $moduleName ) {
$result .= addModule( $moduleName, $gotten );
}
}
echo $result;