Refactor: Separate A/B test configuration from site configuration

Making it easier to add configuration variables to JavaScript
in future. This will be used for the pointer indicator.

Change-Id: I65396a3867e7e92d7385ebaa573fb48105ecb9fd
This commit is contained in:
Jon Robson 2023-04-10 11:56:19 -07:00 committed by Jdlrobson
parent dd146468ff
commit 28ada2dc78
7 changed files with 45 additions and 60 deletions

View file

@ -46,10 +46,14 @@ class Hooks implements
}
/**
* @param RL\Context $context
* @param Config $config
* @return array
*/
private static function getActiveABTest( $config ) {
public static function getActiveABTest(
RL\Context $context,
Config $config
) {
$ab = $config->get(
Constants::CONFIG_WEB_AB_TEST_ENROLLMENT
);
@ -80,22 +84,6 @@ class Hooks implements
return $ab;
}
/**
* Passes config variables to Vector (modern) ResourceLoader module.
* @param RL\Context $context
* @param Config $config
* @return array
*/
public static function getVectorResourceLoaderConfig(
RL\Context $context,
Config $config
) {
return [
'wgVectorSearchApiUrl' => $config->get( 'VectorSearchApiUrl' ),
'wgVectorWebABTestEnrollment' => self::getActiveABTest( $config ),
];
}
/**
* Generates config variables for skins.vector.search Resource Loader module (defined in
* skin.json).
@ -140,8 +128,6 @@ class Hooks implements
// and from its point of view they are the same thing.
// Please see the modules `skins.vector.js` and `skins.vector.legacy.js`
// for the wire up of search.
// The related method self::getVectorResourceLoaderConfig handles which
// search to load.
$config['search'] = false;
}

View file

@ -1,4 +1,4 @@
/** See Vector\Hooks::getVectorResourceLoaderConfig */
/** For virtual configuration of resources/skins.vector.js/config.json */
interface VectorResourceLoaderVirtualConfig {
wgVectorSearchApiUrl: string;
VectorSearchApiUrl: string;
}

View file

@ -0,0 +1,3 @@
{
"@doc": "This file describes the shape of the AB config. It exists to support jest"
}

View file

@ -11,7 +11,7 @@ const
popupNotification = require( './popupNotification.js' ),
features = require( './features.js' ),
deferUntilFrame = require( './deferUntilFrame.js' ),
ABTestConfig = require( /** @type {string} */ ( './config.json' ) ).wgVectorWebABTestEnrollment || {},
ABTestConfig = require( /** @type {string} */ ( './activeABTest.json' ) ),
STICKY_HEADER_VISIBLE_CLASS = 'vector-sticky-header-visible',
TOC_ID = 'vector-toc',
BODY_CONTENT_ID = 'bodyContent',

View file

@ -141,8 +141,8 @@ function initSearchLoader( document ) {
var searchBoxes = document.querySelectorAll( '.vector-search-box' );
// Allow developers to defined $wgVectorSearchApiUrl in LocalSettings to target different APIs
if ( config.wgVectorSearchApiUrl ) {
mw.config.set( 'wgVectorSearchApiUrl', config.wgVectorSearchApiUrl );
if ( config.VectorSearchApiUrl ) {
mw.config.set( 'wgVectorSearchApiUrl', config.VectorSearchApiUrl );
}
if ( !searchBoxes.length ) {

View file

@ -353,9 +353,13 @@
"resources/skins.vector.es6/features.js",
"resources/skins.vector.es6/limitedWidthToggle.js",
"resources/skins.vector.es6/popupNotification.js",
{
"name": "resources/skins.vector.es6/activeABTest.json",
"callback": "MediaWiki\\Skins\\Vector\\Hooks::getActiveABTest"
},
{
"name": "resources/skins.vector.es6/config.json",
"callback": "MediaWiki\\Skins\\Vector\\Hooks::getVectorResourceLoaderConfig"
"config": [ "VectorSearchApiUrl" ]
},
{
"name": "resources/skins.vector.es6/tableOfContentsConfig.json",
@ -401,7 +405,7 @@
"resources/skins.vector.js/skin.js",
{
"name": "resources/skins.vector.js/config.json",
"callback": "MediaWiki\\Skins\\Vector\\Hooks::getVectorResourceLoaderConfig"
"config": [ "VectorSearchApiUrl" ]
},
"resources/skins.vector.js/watchstar.js",
"resources/skins.vector.js/dropdownMenus.js",

View file

@ -53,17 +53,13 @@ class VectorHooksTest extends MediaWikiIntegrationTestCase {
];
}
public function provideGetVectorResourceLoaderConfig() {
public function provideGetActiveABTest() {
return [
[
[
'VectorWebABTestEnrollment' => [],
'VectorSearchApiUrl' => 'https://en.wikipedia.org/w/rest.php'
],
[
'wgVectorSearchApiUrl' => 'https://en.wikipedia.org/w/rest.php',
'wgVectorWebABTestEnrollment' => [],
]
[]
],
[
[
@ -85,34 +81,30 @@ class VectorHooksTest extends MediaWikiIntegrationTestCase {
],
],
],
'VectorSearchApiUrl' => 'https://en.wikipedia.org/w/rest.php'
],
[
'wgVectorSearchApiUrl' => 'https://en.wikipedia.org/w/rest.php',
'wgVectorWebABTestEnrollment' => [
'name' => 'vector.sticky_header',
'enabled' => true,
'buckets' => [
'unsampled' => [
'samplingRate' => 1,
],
'control' => [
'samplingRate' => 0
],
'stickyHeaderEnabled' => [
'samplingRate' => 0
],
'stickyHeaderDisabled' => [
'samplingRate' => 0
],
],
'name' => 'vector.sticky_header',
'enabled' => true,
'buckets' => [
'unsampled' => [
'samplingRate' => 1,
],
'control' => [
'samplingRate' => 0
],
'stickyHeaderEnabled' => [
'samplingRate' => 0
],
'stickyHeaderDisabled' => [
'samplingRate' => 0
],
],
]
],
];
}
public function provideGetVectorResourceLoaderConfigWithExceptions() {
public function provideGetActiveABTestWithExceptions() {
return [
# Bad experiment (no buckets)
[
@ -343,12 +335,12 @@ class VectorHooksTest extends MediaWikiIntegrationTestCase {
}
/**
* @covers ::getVectorResourceLoaderConfig
* @dataProvider provideGetVectorResourceLoaderConfig
* @covers ::getActiveABTest
* @dataProvider provideGetActiveABTest
*/
public function testGetVectorResourceLoaderConfig( $configData, $expected ) {
public function testGetActiveABTest( $configData, $expected ) {
$config = new HashConfig( $configData );
$vectorConfig = Hooks::getVectorResourceLoaderConfig(
$vectorConfig = Hooks::getActiveABTest(
$this->createMock( ResourceLoaderContext::class ),
$config
);
@ -360,13 +352,13 @@ class VectorHooksTest extends MediaWikiIntegrationTestCase {
}
/**
* @covers ::getVectorResourceLoaderConfig
* @dataProvider provideGetVectorResourceLoaderConfigWithExceptions
* @covers ::getActiveABTest
* @dataProvider provideGetActiveABTestWithExceptions
*/
public function testGetVectorResourceLoaderConfigWithExceptions( $configData ) {
public function testGetActiveABTestWithExceptions( $configData ) {
$config = new HashConfig( $configData );
$this->expectException( RuntimeException::class );
Hooks::getVectorResourceLoaderConfig(
Hooks::getActiveABTest(
$this->createMock( ResourceLoaderContext::class ),
$config
);