mediawiki-skins-Vector/tests/phpunit/unit/FeatureManagement/Requirements/TableOfContentsTreatmentRequirementTest.php
Clare Ming eb597645c3 Refactor TOC A/B test to bucket users on backend
- Include temporary feature requirement for TOC A/B test.
  - Assumes 100% of logged-in users with even/odd user ids
  being assigned to treatment/control buckets respectively.
  - Sampling rates passed in by config are not considered
  during bucketing.
- Update hook for adding needed TOC A/B test body classes.
- Add test for temp feature.

Note: the temporary feature requirement and associated hooks
should be removed once the 2nd TOC A/B test concludes.

Bug: T313435
Change-Id: If9c75235614af289cd50182baab29bec3155eb81
2022-08-08 15:50:28 -06:00

106 lines
2.5 KiB
PHP

<?php
namespace MediaWiki\Skins\Vector\Tests\Unit\FeatureManagement\Requirements;
use CentralIdLookup;
use HashConfig;
use MediaWiki\Skins\Vector\Constants;
use MediaWiki\Skins\Vector\FeatureManagement\Requirements\TableOfContentsTreatmentRequirement;
use User;
/**
* @group Vector
* @group FeatureManagement
* @coversDefaultClass \MediaWiki\Skins\Vector\FeatureManagement\Requirements\TableOfContentsTreatmentRequirement
*/
class TableOfContentsTreatmentRequirementTest extends \MediaWikiUnitTestCase {
public function providerTableOfContentsTreatmentRequirement() {
return [
[
// is A-B test enabled
false,
// logged-in user with even ID
10,
// use central id lookup?
true,
false,
'If nothing enabled, nobody sees new treatment'
],
[
// is A-B test enabled
true,
// note 0 = anon user
0,
// use central id lookup?
false,
false,
'If test enabled, anon does not see new treatment'
],
[
// is A-B test enabled
true,
// logged-in user with even ID
108,
// use central id lookup?
true,
true,
'If test enabled, logged-in user with even ID sees new treatment'
],
[
// is A-B test enabled
true,
// logged-in user with odd ID
7,
// use central id lookup?
true,
false,
'If test enabled, logged-in user with odd ID does not see new treatment'
],
];
}
/**
* @covers ::isMet
* @dataProvider providerTableOfContentsTreatmentRequirement
* @param bool $abValue
* @param int $userId
* @param bool $useCentralIdLookup
* @param bool $expected
* @param string $msg
*/
public function testTableOfContentsTreatmentRequirement(
$abValue, $userId, $useCentralIdLookup, $expected, $msg
) {
$config = new HashConfig( [
Constants::CONFIG_WEB_AB_TEST_ENROLLMENT => [
'name' => 'skin-vector-toc-experiment',
'enabled' => $abValue,
'buckets' => [
'control' => [
'samplingRate' => 0.5,
],
'treatment' => [
'samplingRate' => 0.5,
]
]
],
] );
$user = $this->createMock( User::class );
$user->method( 'isRegistered' )->willReturn( $userId !== 0 );
$user->method( 'getID' )->willReturn( $userId );
$centralIdLookup = $this->createMock( CentralIdLookup::class );
$centralIdLookup->method( 'centralIdFromLocalUser' )->willReturn( $userId );
$requirement = new TableOfContentsTreatmentRequirement(
$config,
$user,
$useCentralIdLookup ? $centralIdLookup : null
);
$this->assertSame( $expected, $requirement->isMet(), $msg );
}
}