AndyRussG has uploaded a new change for review.
https://gerrit.wikimedia.org/r/178974
Change subject: QUnit: provide fixtures via a RL module
......................................................................
QUnit: provide fixtures via a RL module
Change-Id: Ib54fce824a7d27f63f9aa2fe2f8cbfbaeae7ca6e
---
M CentralNotice.hooks.php
A tests/CNTestFixturesResourceLoaderModule.php
M tests/CentralNoticeTestFixtures.php
M
tests/qunit/ext.centralNotice.bannerController.lib/bannerController.lib.tests.js
4 files changed, 103 insertions(+), 71 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/CentralNotice
refs/changes/74/178974/1
diff --git a/CentralNotice.hooks.php b/CentralNotice.hooks.php
index 55cd063..3217ccc 100644
--- a/CentralNotice.hooks.php
+++ b/CentralNotice.hooks.php
@@ -390,14 +390,24 @@
function efCentralNoticeResourceLoaderTestModules( array &$testModules,
ResourceLoader $resourceLoader
) {
- global $wgResourceModules;
+ global $wgResourceModules, $wgAutoloadClasses;
+
+ // Set up test fixtures module, which is added as a dependency for all
QUnit
+ // tests.
+ $testModules['qunit']['ext.centralNotice.testFixtures'] = array(
+ 'class' => 'CNTestFixturesResourceLoaderModule'
+ );
+
+ // These classes are only used here or in phpunit tests
+ $wgAutoloadClasses['CNTestFixturesResourceLoaderModule'] = __DIR__ .
'/tests/CNTestFixturesResourceLoaderModule.php';
+ // Note: the following setting is repeated in efCentralNoticeUnitTests()
+ $wgAutoloadClasses['CentralNoticeTestFixtures'] = __DIR__ .
'/tests/CentralNoticeTestFixtures.php';
$testModuleBoilerplate = array(
'localBasePath' => __DIR__,
'remoteExtPath' => 'CentralNotice',
);
- // TODO: Something similar should be provided by core.
// find test files for every RL module
$prefix = 'ext.centralNotice';
foreach ( $wgResourceModules as $key => $module ) {
@@ -414,7 +424,7 @@
// if test files exist for given module, create a
corresponding test module
if ( count( $testFiles ) > 0 ) {
$testModules['qunit']["$key.tests"] =
$testModuleBoilerplate + array(
- 'dependencies' => array( $key ),
+ 'dependencies' => array( $key,
'ext.centralNotice.testFixtures' ),
'scripts' => $testFiles,
);
}
diff --git a/tests/CNTestFixturesResourceLoaderModule.php
b/tests/CNTestFixturesResourceLoaderModule.php
new file mode 100644
index 0000000..ca86871
--- /dev/null
+++ b/tests/CNTestFixturesResourceLoaderModule.php
@@ -0,0 +1,20 @@
+<?php
+
+class CNTestFixturesResourceLoaderModule extends ResourceLoaderModule {
+
+ /**
+ * We use the same targets as core's test.mediawiki.qunit.testrunner (in
+ * QUnitTestResources).
+ *
+ * @see ResourceLoaderModule::targets
+ */
+ protected $targets = array( 'desktop', 'mobile' );
+
+ /**
+ * @see ResourceLoaderModule::getScript()
+ */
+ public function getScript( ResourceLoaderContext $context ) {
+ return 'mw.centralNoticeTestFixtures = ' .
+ CentralNoticeTestFixtures::allocationsDataAsJson();
+ }
+}
\ No newline at end of file
diff --git a/tests/CentralNoticeTestFixtures.php
b/tests/CentralNoticeTestFixtures.php
index 39385e6..89eb21b 100644
--- a/tests/CentralNoticeTestFixtures.php
+++ b/tests/CentralNoticeTestFixtures.php
@@ -160,11 +160,18 @@
}
}
- static function allocationsData() {
- $path = __DIR__ . '/' .
CentralNoticeTestFixtures::FIXTURE_RELATIVE_PATH;
- $json = file_get_contents( $path );
+ public static function allocationsData() {
+ $json = CentralNoticeTestFixtures::allocationsDataAsJson();
$data = FormatJson::decode( $json, true );
-
return $data;
}
+
+ /**
+ * Return the raw JSON allocations data (from the file indicated by
+ * CentralNoticeTestFixtures::FIXTURE_RELATIVE_PATH).
+ */
+ public static function allocationsDataAsJson() {
+ $path = __DIR__ . '/' .
CentralNoticeTestFixtures::FIXTURE_RELATIVE_PATH;
+ return file_get_contents( $path );
+ }
}
diff --git
a/tests/qunit/ext.centralNotice.bannerController.lib/bannerController.lib.tests.js
b/tests/qunit/ext.centralNotice.bannerController.lib/bannerController.lib.tests.js
index fa8c343..f1d622f 100644
---
a/tests/qunit/ext.centralNotice.bannerController.lib/bannerController.lib.tests.js
+++
b/tests/qunit/ext.centralNotice.bannerController.lib/bannerController.lib.tests.js
@@ -24,76 +24,71 @@
}
} ) );
- QUnit.asyncTest( 'allocations test cases', function( assert ) {
- $.ajax( {
- url: mw.config.get( 'wgExtensionAssetsPath' )
- +
'/CentralNotice/tests/data/AllocationsFixtures.json'
- } ).done( function( testCases ) {
- // Declare the number of test cases
- assert.ok( testCases.length );
- QUnit.expect( testCases.length + 1 );
+ QUnit.test( 'allocations test cases', function( assert ) {
- $.each( testCases, function( index, testCaseInputs ) {
- var testCase = testCaseInputs[0],
- lib = mw.cnBannerControllerLib,
- choices,
- choice,
- i,
- allocatedBanner;
+ var testFixtures = mw.centralNoticeTestFixtures,
+ lib = mw.cnBannerControllerLib;
- // Flesh out choice data with some default
values
- // BOOM on priority case
- choices = $.map( testCase.choices, function(
campaign, index ) {
- return $.extend(
- { name: index },
- defaultCampaignData,
- campaign,
- {
- banners: $.map(
campaign.banners, function( banner ) {
- return
$.extend( {}, defaultBannerData, banner );
- } )
- } );
- } );
+ assert.ok( testFixtures.length );
+ QUnit.expect( testFixtures.length + 1 );
- // Set per-campaign buckets to 0 for all
campaigns
- // FIXME Allow testing of different buckets
- lib.bucketsByCampaign = {};
- for ( i = 0; i < choices.length; i++ ) {
- choice = choices[i];
- lib.bucketsByCampaign[choice.name] = {
val: 0 };
- }
+ $.each( testFixtures, function( index, testFixturesInputs ) {
+ var testCase = testFixturesInputs[0],
+ choices,
+ choice,
+ i,
+ allocatedBanner;
- // TODO: would like to declare individual tests
here, but I
- // haven't been able to make that work, yet.
- lib.setChoiceData( choices );
- lib.filterChoiceData();
- lib.makePossibleBanners();
- lib.calculateBannerAllocations();
-
- // TODO: the errors will not reveal anything
useful about
- // which case this is, and what happened. So
we throw
- // exceptions manually. The horror!
- try {
- if ( lib.possibleBanners.length !==
Object.keys( testCase.allocations ).length ) {
- throw 'Wrong number of banners
allocated in "' + testCase.title + '".';
- }
- for ( i = 0; i <
lib.possibleBanners.length; i++ ) {
- allocatedBanner =
lib.possibleBanners[i];
- if ( Math.abs(
allocatedBanner.allocation - testCase.allocations[allocatedBanner.name] ) >
0.001 ) {
- throw 'Banner ' +
allocatedBanner.name + ' was misallocated in "' + testCase.title + '".';
- }
- }
- } catch ( error ) {
- assert.ok( false, error
- + " expected: " +
QUnit.jsDump.parse( testCase.allocations )
- + ", actual: " +
QUnit.jsDump.parse( lib.possibleBanners )
- );
- return;
- }
- assert.ok( true, 'Allocations match in "' +
testCase.title + '"' );
+ // Flesh out choice data with some default values
+ // BOOM on priority case
+ choices = $.map( testCase.choices, function( campaign,
index ) {
+ return $.extend(
+ { name: index },
+ defaultCampaignData,
+ campaign,
+ {
+ banners: $.map(
campaign.banners, function( banner ) {
+ return $.extend( {},
defaultBannerData, banner );
+ } )
+ } );
} );
- QUnit.start();
+ // Set per-campaign buckets to 0 for all campaigns
+ // FIXME Allow testing of different buckets
+ lib.bucketsByCampaign = {};
+ for ( i = 0; i < choices.length; i++ ) {
+ choice = choices[i];
+ lib.bucketsByCampaign[choice.name] = { val: 0 };
+ }
+
+ // TODO: would like to declare individual tests here,
but I
+ // haven't been able to make that work, yet.
+ lib.setChoiceData( choices );
+ lib.filterChoiceData();
+ lib.makePossibleBanners();
+ lib.calculateBannerAllocations();
+
+ // TODO: the errors will not reveal anything useful
about
+ // which case this is, and what happened. So we throw
+ // exceptions manually. The horror!
+ try {
+ if ( lib.possibleBanners.length !==
Object.keys( testCase.allocations ).length ) {
+ throw 'Wrong number of banners
allocated in "' + testCase.title + '".';
+ }
+ for ( i = 0; i < lib.possibleBanners.length;
i++ ) {
+ allocatedBanner =
lib.possibleBanners[i];
+ if ( Math.abs(
allocatedBanner.allocation - testCase.allocations[allocatedBanner.name] ) >
0.001 ) {
+ throw 'Banner ' +
allocatedBanner.name + ' was misallocated in "' + testCase.title + '".';
+ }
+ }
+ } catch ( error ) {
+ assert.ok( false, error
+ + " expected: " + QUnit.jsDump.parse(
testCase.allocations )
+ + ", actual: " + QUnit.jsDump.parse(
lib.possibleBanners )
+ );
+ return;
+ }
+ assert.ok( true, 'Allocations match in "' +
testCase.title + '"' );
} );
} );
--
To view, visit https://gerrit.wikimedia.org/r/178974
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib54fce824a7d27f63f9aa2fe2f8cbfbaeae7ca6e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/CentralNotice
Gerrit-Branch: master
Gerrit-Owner: AndyRussG <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits