Move Wvui Search A/B Logic to FeatureManager

FeatureManager allows the logic to be centralized and allows clients to
ask about its state. For instance, SkinVector will make use of it in
I70277c1082a504fbd5f6023e9873e8071de7e35d.

Also:

* Adds WvuiSearchTreatmentRequirementTest to test A/B logic

WvuiSearchTreatmentRequirement/Test logic are adapted from
I878239a85ffbecb5e78d73aed5568c56dbd7d659.

Bug: T270202
Change-Id: Ia02349a7b41c7caf26fbd728e0be7d47488b97e5
This commit is contained in:
Nicholas Ray 2021-01-26 16:42:00 -07:00
parent 2789e27a29
commit 8c76a17e43
5 changed files with 248 additions and 12 deletions

View File

@ -128,6 +128,21 @@ final class Constants {
*/
public const QUERY_PARAM_SKIN_VERSION = 'useskinversion';
/**
* @var string
*/
public const FEATURE_USE_WVUI_SEARCH = 'UseWvuiSearch';
/**
* @var string
*/
public const CONFIG_KEY_USE_WVUI_SEARCH = 'VectorUseWvuiSearch';
/**
* @var string
*/
public const REQUIREMENT_USE_WVUI_SEARCH = 'VectorUseWvuiSearch';
/**
* The `mediawiki.searchSuggest` protocol piece of the SearchSatisfaction instrumention reads
* the value of an element with the "data-search-loc" attribute and set the event's

View File

@ -0,0 +1,84 @@
<?php
/**
* 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
* @since 1.36
*/
namespace Vector\FeatureManagement\Requirements;
use Config;
use Vector\Constants;
use Vector\FeatureManagement\Requirement;
/**
* Checks whether or not WVUI search should be used.
*
* @unstable
*
* @package Vector\FeatureManagement\Requirements
* @internal
*/
final class WvuiSearchTreatmentRequirement implements Requirement {
/**
* @var Config
*/
private $config;
/**
* @var \User
*/
private $user;
/**
* This constructor accepts all dependencies needed to determine
* whether wvui search is enabled for current user and config.
*
* @param \Config $config
* @param \User $user
*/
public function __construct( \Config $config, \User $user ) {
$this->config = $config;
$this->user = $user;
}
/**
* @inheritDoc
*/
public function getName() : string {
return Constants::REQUIREMENT_USE_WVUI_SEARCH;
}
/**
* If A/B test is enabled check whether the user is logged in and bucketed.
* Fallback to `VectorUseWvuiSearch` config value.
*
* @inheritDoc
* @throws \ConfigException
*/
public function isMet() : bool {
// Determine the search widget treatment to send to the user
$shouldUseWvuiSearch = (bool)$this->config->get( Constants::CONFIG_KEY_USE_WVUI_SEARCH );
if ( (bool)$this->config->get( Constants::CONFIG_SEARCH_TREATMENT_AB_TEST ) && $this->user->isRegistered() ) {
$shouldUseWvuiSearch = $this->user->getID() % 2 === 0;
}
return $shouldUseWvuiSearch;
}
}

View File

@ -274,25 +274,16 @@ class Hooks {
// list.
if ( self::isSkinVersionLegacy() ) {
$bodyAttrs['class'] .= ' skin-vector-legacy';
return;
}
// Determine the search widget treatment to send to the user
$config = $sk->getConfig();
$user = $sk->getUser();
$shouldUseWvuiSearch = $config->get( 'VectorUseWvuiSearch' );
if ( $config->get( 'VectorSearchTreatmentABTest' ) && $user->isRegistered() ) {
$shouldUseWvuiSearch = $user->getID() % 2 === 0;
}
if ( $shouldUseWvuiSearch ) {
if ( VectorServices::getFeatureManager()->isFeatureEnabled( Constants::FEATURE_USE_WVUI_SEARCH ) ) {
$bodyAttrs['class'] .= ' skin-vector-search-vue';
}
$config = $sk->getConfig();
// Should we disable the max-width styling?
if ( $sk->getTitle() && self::shouldDisableMaxWidth(
if ( !self::isSkinVersionLegacy() && $sk->getTitle() && self::shouldDisableMaxWidth(
$config->get( 'VectorMaxWidthOptions' ),
$sk->getTitle(),
$out->getRequest()->getValues()

View File

@ -27,6 +27,7 @@ use Vector\Constants;
use Vector\FeatureManagement\FeatureManager;
use Vector\FeatureManagement\Requirements\DynamicConfigRequirement;
use Vector\FeatureManagement\Requirements\LatestSkinVersionRequirement;
use Vector\FeatureManagement\Requirements\WvuiSearchTreatmentRequirement;
use Vector\SkinVersionLookup;
return [
@ -85,6 +86,24 @@ return [
]
);
// Feature: Use Wvui Search
// ================================
$featureManager->registerRequirement(
new WvuiSearchTreatmentRequirement(
$services->getMainConfig(),
$context->getUser()
)
);
$featureManager->registerFeature(
Constants::FEATURE_USE_WVUI_SEARCH,
[
Constants::REQUIREMENT_FULLY_INITIALISED,
Constants::REQUIREMENT_LATEST_SKIN_VERSION,
Constants::REQUIREMENT_USE_WVUI_SEARCH
]
);
return $featureManager;
}
];

View File

@ -0,0 +1,127 @@
<?php
/**
* 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
* @since 1.36
*/
use Vector\Constants;
use Vector\FeatureManagement\Requirements\WvuiSearchTreatmentRequirement;
/**
* @group Vector
* @coversDefaultClass \Vector\FeatureManagement\Requirements\WvuiSearchTreatmentRequirement
*/
class WvuiSearchTreatmentRequirementTest extends \MediaWikiTestCase {
public function providerWvuiSearchTreatmentRequirement() {
return [
[
// Is wvui search enabled
false,
// is A-B test enabled
false,
// note 0 = anon user
0,
false,
'If nothing enabled nobody gets wvui search'
],
[
// Is wvui search enabled
true,
// is A-B test enabled
false,
// note 0 = anon user
0,
true,
'Anon users should get wvui search if enabled when A/B test disabled'
],
[
// Is wvui search enabled
true,
// is A-B test enabled
false,
2,
true,
'Logged in users should get wvui search if enabled when A/B test disabled'
],
[
// Is wvui search enabled
true,
// is A-B test enabled
false,
1,
true,
'All odd logged in users should get wvui search when A/B test disabled'
],
[
// Is wvui search enabled
true,
// is A-B test enabled
true,
// note 0 = anon user
0,
true,
'Anon users with a/b test enabled should see wvui search when search config enabled'
],
[
// Is wvui search enabled
true,
// is A-B test enabled
true,
2,
true,
'Even logged in users get wvui search when A/B test enabled'
],
[
// Is wvui search enabled
true,
// is A-B test enabled
true,
1,
false,
'Odd logged in users do not wvui search when A/B test enabled'
],
];
}
/**
* @covers ::isMet
* @dataProvider providerWvuiSearchTreatmentRequirement
* @param bool $wvuiSearchConfigValue
* @param bool $abValue
* @param int $userId
* @param bool $expected
* @param string $msg
*/
public function testWvuiSearchTreatmentRequirement(
$wvuiSearchConfigValue, $abValue, $userId, $expected, $msg
) {
$config = new HashConfig( [
Constants::CONFIG_KEY_USE_WVUI_SEARCH => $wvuiSearchConfigValue,
Constants::CONFIG_SEARCH_TREATMENT_AB_TEST => $abValue,
] );
$user = $this->getTestUser()->getUser();
$user->setId( $userId );
$requirement = new WvuiSearchTreatmentRequirement(
$config, $user
);
$this->assertSame( $requirement->isMet(), $expected, $msg );
}
}