Merge "Validate the value of VectorWebABTestEnrollment"
This commit is contained in:
commit
8357b2c325
|
@ -7,6 +7,7 @@ use HTMLForm;
|
||||||
use MediaWiki\MediaWikiServices;
|
use MediaWiki\MediaWikiServices;
|
||||||
use OutputPage;
|
use OutputPage;
|
||||||
use ResourceLoaderContext;
|
use ResourceLoaderContext;
|
||||||
|
use RuntimeException;
|
||||||
use Skin;
|
use Skin;
|
||||||
use SkinTemplate;
|
use SkinTemplate;
|
||||||
use SkinVector;
|
use SkinVector;
|
||||||
|
@ -23,6 +24,41 @@ use Vector\HTMLForm\Fields\HTMLLegacySkinVersionField;
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
class Hooks {
|
class Hooks {
|
||||||
|
/**
|
||||||
|
* @param Config $config
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private static function getActiveABTest( $config ) {
|
||||||
|
$ab = $config->get(
|
||||||
|
Constants::CONFIG_STICKY_HEADER_TREATMENT_AB_TEST_ENROLLMENT
|
||||||
|
);
|
||||||
|
if ( count( $ab ) === 0 ) {
|
||||||
|
// If array is empty then no experiment and need to validate.
|
||||||
|
return $ab;
|
||||||
|
}
|
||||||
|
if ( !array_key_exists( 'buckets', $ab ) ) {
|
||||||
|
throw new RuntimeException( 'Invalid VectorWebABTestEnrollment value: Must contain buckets key.' );
|
||||||
|
}
|
||||||
|
if ( !array_key_exists( 'unsampled', $ab['buckets'] ) ) {
|
||||||
|
throw new RuntimeException( 'Invalid VectorWebABTestEnrollment value: Must define an `unsampled` bucket.' );
|
||||||
|
} else {
|
||||||
|
// check bucket values.
|
||||||
|
foreach ( $ab['buckets'] as $bucketName => $bucketDefinition ) {
|
||||||
|
if ( !is_array( $bucketDefinition ) ) {
|
||||||
|
throw new RuntimeException( 'Invalid VectorWebABTestEnrollment value: Buckets should be arrays' );
|
||||||
|
}
|
||||||
|
$samplingRate = $bucketDefinition['samplingRate'];
|
||||||
|
if ( is_string( $samplingRate ) ) {
|
||||||
|
throw new RuntimeException(
|
||||||
|
'Invalid VectorWebABTestEnrollment value: Sampling rate should be number between 0 and 1.'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $ab;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Passes config variables to Vector (modern) ResourceLoader module.
|
* Passes config variables to Vector (modern) ResourceLoader module.
|
||||||
* @param ResourceLoaderContext $context
|
* @param ResourceLoaderContext $context
|
||||||
|
@ -35,9 +71,7 @@ class Hooks {
|
||||||
) {
|
) {
|
||||||
return [
|
return [
|
||||||
'wgVectorSearchHost' => $config->get( 'VectorSearchHost' ),
|
'wgVectorSearchHost' => $config->get( 'VectorSearchHost' ),
|
||||||
'wgVectorWebABTestEnrollment' => $config->get(
|
'wgVectorWebABTestEnrollment' => self::getActiveABTest( $config ),
|
||||||
Constants::CONFIG_STICKY_HEADER_TREATMENT_AB_TEST_ENROLLMENT
|
|
||||||
),
|
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -43,6 +43,123 @@ class VectorHooksTest extends MediaWikiIntegrationTestCase {
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function provideGetVectorResourceLoaderConfig() {
|
||||||
|
return [
|
||||||
|
[
|
||||||
|
[
|
||||||
|
'VectorWebABTestEnrollment' => [],
|
||||||
|
'VectorSearchHost' => 'en.wikipedia.org'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'wgVectorSearchHost' => 'en.wikipedia.org',
|
||||||
|
'wgVectorWebABTestEnrollment' => [],
|
||||||
|
]
|
||||||
|
],
|
||||||
|
[
|
||||||
|
[
|
||||||
|
'VectorWebABTestEnrollment' => [
|
||||||
|
'name' => 'vector.sticky_header',
|
||||||
|
'enabled' => true,
|
||||||
|
'buckets' => [
|
||||||
|
'unsampled' => [
|
||||||
|
'samplingRate' => 1,
|
||||||
|
],
|
||||||
|
'control' => [
|
||||||
|
'samplingRate' => 0
|
||||||
|
],
|
||||||
|
'stickyHeaderEnabled' => [
|
||||||
|
'samplingRate' => 0
|
||||||
|
],
|
||||||
|
'stickyHeaderDisabled' => [
|
||||||
|
'samplingRate' => 0
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'VectorSearchHost' => 'en.wikipedia.org'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'wgVectorSearchHost' => 'en.wikipedia.org',
|
||||||
|
'wgVectorWebABTestEnrollment' => [
|
||||||
|
'name' => 'vector.sticky_header',
|
||||||
|
'enabled' => true,
|
||||||
|
'buckets' => [
|
||||||
|
'unsampled' => [
|
||||||
|
'samplingRate' => 1,
|
||||||
|
],
|
||||||
|
'control' => [
|
||||||
|
'samplingRate' => 0
|
||||||
|
],
|
||||||
|
'stickyHeaderEnabled' => [
|
||||||
|
'samplingRate' => 0
|
||||||
|
],
|
||||||
|
'stickyHeaderDisabled' => [
|
||||||
|
'samplingRate' => 0
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
]
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function provideGetVectorResourceLoaderConfigWithExceptions() {
|
||||||
|
return [
|
||||||
|
# Bad experiment (no buckets)
|
||||||
|
[
|
||||||
|
[
|
||||||
|
'VectorSearchHost' => 'en.wikipedia.org',
|
||||||
|
'VectorWebABTestEnrollment' => [
|
||||||
|
'name' => 'vector.sticky_header',
|
||||||
|
'enabled' => true,
|
||||||
|
],
|
||||||
|
]
|
||||||
|
],
|
||||||
|
# Bad experiment (no unsampled bucket)
|
||||||
|
[
|
||||||
|
[
|
||||||
|
'VectorSearchHost' => 'en.wikipedia.org',
|
||||||
|
'VectorWebABTestEnrollment' => [
|
||||||
|
'name' => 'vector.sticky_header',
|
||||||
|
'enabled' => true,
|
||||||
|
'buckets' => [
|
||||||
|
'a' => [
|
||||||
|
'samplingRate' => 0
|
||||||
|
],
|
||||||
|
]
|
||||||
|
],
|
||||||
|
]
|
||||||
|
],
|
||||||
|
# Bad experiment (wrong format)
|
||||||
|
[
|
||||||
|
[
|
||||||
|
'VectorSearchHost' => 'en.wikipedia.org',
|
||||||
|
'VectorWebABTestEnrollment' => [
|
||||||
|
'name' => 'vector.sticky_header',
|
||||||
|
'enabled' => true,
|
||||||
|
'buckets' => [
|
||||||
|
'unsampled' => 1,
|
||||||
|
]
|
||||||
|
],
|
||||||
|
]
|
||||||
|
],
|
||||||
|
# Bad experiment (samplingRate defined as string)
|
||||||
|
[
|
||||||
|
[
|
||||||
|
'VectorSearchHost' => 'en.wikipedia.org',
|
||||||
|
'VectorWebABTestEnrollment' => [
|
||||||
|
'name' => 'vector.sticky_header',
|
||||||
|
'enabled' => true,
|
||||||
|
'buckets' => [
|
||||||
|
'unsampled' => [
|
||||||
|
'samplingRate' => '1'
|
||||||
|
],
|
||||||
|
]
|
||||||
|
],
|
||||||
|
]
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers ::shouldDisableMaxWidth
|
* @covers ::shouldDisableMaxWidth
|
||||||
*/
|
*/
|
||||||
|
@ -220,6 +337,36 @@ class VectorHooksTest extends MediaWikiIntegrationTestCase {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::getVectorResourceLoaderConfig
|
||||||
|
* @dataProvider provideGetVectorResourceLoaderConfig
|
||||||
|
*/
|
||||||
|
public function testGetVectorResourceLoaderConfig( $configData, $expected ) {
|
||||||
|
$config = new HashConfig( $configData );
|
||||||
|
$vectorConfig = Hooks::getVectorResourceLoaderConfig(
|
||||||
|
$this->createMock( ResourceLoaderContext::class ),
|
||||||
|
$config
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertSame(
|
||||||
|
$vectorConfig,
|
||||||
|
$expected
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::getVectorResourceLoaderConfig
|
||||||
|
* @dataProvider provideGetVectorResourceLoaderConfigWithExceptions
|
||||||
|
*/
|
||||||
|
public function testGetVectorResourceLoaderConfigWithExceptions( $configData ) {
|
||||||
|
$config = new HashConfig( $configData );
|
||||||
|
$this->expectException( RuntimeException::class );
|
||||||
|
$vectorConfig = Hooks::getVectorResourceLoaderConfig(
|
||||||
|
$this->createMock( ResourceLoaderContext::class ),
|
||||||
|
$config
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers ::onGetPreferences
|
* @covers ::onGetPreferences
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue