Only add Popups code to output page where needed

If the Popups code is enabled in beta feature mode only then
only add it to the page if the beta feature is enabled.

isEnabledByUser now returns false if the user is anonymous
and Popups is restricted to beta mode.

Bug: T156290
Change-Id: If152d2a67a079050173c6d642e0960b59730bc6d
This commit is contained in:
jdlrobson 2017-02-08 14:08:12 -08:00
parent d7b5f56665
commit 4e2ce1ce39
4 changed files with 49 additions and 13 deletions

View file

@ -86,6 +86,7 @@ class PopupsHooks {
public static function onBeforePageDisplay( OutputPage &$out, Skin &$skin ) { public static function onBeforePageDisplay( OutputPage &$out, Skin &$skin ) {
$module = PopupsContext::getInstance(); $module = PopupsContext::getInstance();
$user = $out->getUser();
if ( !$module->areDependenciesMet() ) { if ( !$module->areDependenciesMet() ) {
$logger = $module->getLogger(); $logger = $module->getLogger();
@ -94,7 +95,9 @@ class PopupsHooks {
return true; return true;
} }
$out->addModules( [ 'ext.popups' ] ); if ( !$module->isBetaFeatureEnabled() || $module->isEnabledByUser( $user ) ) {
$out->addModules( [ 'ext.popups' ] );
}
return true; return true;
} }

View file

@ -148,13 +148,12 @@ class PopupsContext {
* @return bool * @return bool
*/ */
public function isEnabledByUser( \User $user ) { public function isEnabledByUser( \User $user ) {
if ( $user->isAnon() ) {
return true;
}
if ( $this->isBetaFeatureEnabled() ) { if ( $this->isBetaFeatureEnabled() ) {
return \BetaFeatures::isFeatureEnabled( $user, self::PREVIEWS_BETA_PREFERENCE_NAME ); return $user->isAnon() ? false :
}; \BetaFeatures::isFeatureEnabled( $user, self::PREVIEWS_BETA_PREFERENCE_NAME );
return $user->getOption( self::PREVIEWS_OPTIN_PREFERENCE_NAME ) === self::PREVIEWS_ENABLED; }
return $user->isAnon() ? true :
$user->getOption( self::PREVIEWS_OPTIN_PREFERENCE_NAME ) === self::PREVIEWS_ENABLED;
} }
/** /**

View file

@ -158,18 +158,28 @@ class PopupsContextTest extends MediaWikiTestCase {
* Check that Page Previews are disabled for anonymous user * Check that Page Previews are disabled for anonymous user
* @covers ::isEnabledByUser * @covers ::isEnabledByUser
* @covers ::isBetaFeatureEnabled * @covers ::isBetaFeatureEnabled
* @dataProvider providerAnonUserHasDisabledPagePreviews
*/ */
public function testAnonUserHasDisabledPagePreviews() { public function testAnonUserHasDisabledPagePreviews( $betaFeatureEnabled, $expected ) {
$user = $this->getMutableTestUser()->getUser(); $user = $this->getMutableTestUser()->getUser();
$user->setId( self::ANONYMOUS_USER ); $user->setId( self::ANONYMOUS_USER );
$user->setOption( PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME, $user->setOption( PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME,
PopupsContext::PREVIEWS_DISABLED ); PopupsContext::PREVIEWS_DISABLED );
$this->setMwGlobals( [ $this->setMwGlobals( [
"wgPopupsBetaFeature" => false "wgPopupsBetaFeature" => $betaFeatureEnabled,
] ); ] );
$context = PopupsContext::getInstance(); $context = PopupsContext::getInstance();
$this->assertEquals( true, $context->isEnabledByUser( $user ) ); $this->assertEquals( $expected, $context->isEnabledByUser( $user ) );
}
public static function providerAnonUserHasDisabledPagePreviews() {
return [
// If beta feature is enabled we can assume it's opt in only.
[ true, false ],
// If beta feature is disabled we can assume it's rolled out to everyone.
[ false, true ],
];
} }
/** /**
* @return array/ * @return array/

View file

@ -257,10 +257,24 @@ class PopupsHooksTest extends MediaWikiTestCase {
PopupsHooks::onBeforePageDisplay( $outPageMock, $skinMock ); PopupsHooks::onBeforePageDisplay( $outPageMock, $skinMock );
} }
public function providerOnBeforePageDisplay() {
return [
[ false, true, false ],
[ true, true, true ],
// if the user doesnt have the feature but the beta feature is disabled
// we can assume the user has it (as its rolled out to everyone)
[ false, false, true ],
// If the user has enabled it and the beta feature is disabled
// we can assume the code will be loaded.
[ true, false, true ]
];
}
/** /**
* @covers ::onBeforePageDisplay * @covers ::onBeforePageDisplay
* @dataProvider providerOnBeforePageDisplay
*/ */
public function testOnBeforePageDisplay() { public function testOnBeforePageDisplay( $isEnabledByUser, $isBetaFeatureEnabled, $isCodeLoaded ) {
$skinMock = $this->getMock( Skin::class ); $skinMock = $this->getMock( Skin::class );
$outPageMock = $this->getMock( $outPageMock = $this->getMock(
@ -270,18 +284,28 @@ class PopupsHooksTest extends MediaWikiTestCase {
'', '',
false false
); );
$outPageMock->expects( $this->once() )
$outPageMock->expects( $isCodeLoaded ? $this->once() : $this->never() )
->method( 'addModules' ) ->method( 'addModules' )
->with( [ 'ext.popups' ] ); ->with( [ 'ext.popups' ] );
$contextMock = $this->getMockBuilder( PopupsContextTestWrapper::class ) $contextMock = $this->getMockBuilder( PopupsContextTestWrapper::class )
->setMethods( [ 'areDependenciesMet' ] ) ->setMethods( [ 'areDependenciesMet', 'isBetaFeatureEnabled', 'isEnabledByUser' ] )
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$contextMock->expects( $this->once() ) $contextMock->expects( $this->once() )
->method( 'areDependenciesMet' ) ->method( 'areDependenciesMet' )
->will( $this->returnValue( true ) ); ->will( $this->returnValue( true ) );
$contextMock->expects( $this->any() )
->method( 'isBetaFeatureEnabled' )
->will( $this->returnValue( $isBetaFeatureEnabled ) );
$contextMock->expects( $this->any() )
->method( 'isEnabledByUser' )
->will( $this->returnValue( $isEnabledByUser ) );
PopupsContextTestWrapper::injectTestInstance( $contextMock ); PopupsContextTestWrapper::injectTestInstance( $contextMock );
PopupsHooks::onBeforePageDisplay( $outPageMock, $skinMock ); PopupsHooks::onBeforePageDisplay( $outPageMock, $skinMock );
} }