SpecialJavaScriptTest: Bypass ResourceLoader 'target' scope
authorTimo Tijhof <krinklemail@gmail.com>
Thu, 4 Jun 2015 20:12:23 +0000 (21:12 +0100)
committerJdlrobson <jrobson@wikimedia.org>
Tue, 4 Aug 2015 05:37:25 +0000 (05:37 +0000)
To make unit testing easier, allow any module to be loaded within
the unit test suite. Regardless of the intended 'target'.

Targets are meant for restricting front-end scope in production.
Enforcing that in the test suite causes various test suites to
get de-registered at run time client-side.

Otherwise, in order to truly run all unit tests, Jenkins would
have to re-run the entire test suite in all known targets. This
wouldn't make sense because modules have to be globally uniquely
named (no conflicts) and unit tests are atomic. They can all run
in the same suite.

To prevent modules being comitted with incompatible target
dependencies, we already have a Structure test in the PHPUnit
suite to catch those issues in the module registry.

This makes the main 'qunit' build for MobileFrontend more useful,
where currently many modules aren't being tested due to them not
being in the 'desktop' target.

Bug: T103027
Change-Id: I69f735eb56c1362189298d9859d3add576faaadb

includes/resourceloader/ResourceLoaderStartUpModule.php
includes/specials/SpecialJavaScriptTest.php

index 16424a0..cc35266 100644 (file)
@@ -187,6 +187,9 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule {
 
                $resourceLoader = $context->getResourceLoader();
                $target = $context->getRequest()->getVal( 'target', 'desktop' );
+               // Bypass target filter if this request is from a unit test context. To prevent misuse in
+               // production, this is only allowed if testing is enabled server-side.
+               $byPassTargetFilter = $this->getConfig()->get( 'EnableJavaScriptTest' ) && $target === 'test';
 
                $out = '';
                $registryData = array();
@@ -195,7 +198,7 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule {
                foreach ( $resourceLoader->getModuleNames() as $name ) {
                        $module = $resourceLoader->getModule( $name );
                        $moduleTargets = $module->getTargets();
-                       if ( !in_array( $target, $moduleTargets ) ) {
+                       if ( !$byPassTargetFilter && !in_array( $target, $moduleTargets ) ) {
                                continue;
                        }
 
index 442b764..801f977 100644 (file)
@@ -205,6 +205,7 @@ HTML;
                        'lang' => $this->getLanguage()->getCode(),
                        'skin' => $this->getSkin()->getSkinName(),
                        'debug' => ResourceLoader::inDebugMode() ? 'true' : 'false',
+                       'target' => 'test',
                );
                $embedContext = new ResourceLoaderContext( $rl, new FauxRequest( $query ) );
                $query['only'] = 'scripts';