mediawiki-extensions-Popups/tests/phpunit/PopupsContextTest.php
Piotr Miazga 211f1d1658 Disable Page Previews preferences when NavPopups are enabled
Changes:
 - introduced PopupsGadgetsIntegration class handles all checks
 - show help message on Preferences page when NavPopups is enabled
 - unit test everything

Bug: T151058
Change-Id: Ia474b1b30378efe84dedf3ad47c1f833e88d69b5
2017-01-11 01:06:48 +01:00

304 lines
8.3 KiB
PHP

<?php
/*
* This file is part of the MediaWiki extension Popups.
*
* Popups 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.
*
* Popups 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 Popups. If not, see <http://www.gnu.org/licenses/>.
*
* @file
* @ingroup extensions
*/
require_once ( 'PopupsContextTestWrapper.php' );
use Popups\PopupsContext;
/**
* Popups module tests
*
* @group Popups
* @coversDefaultClass Popups\PopupsContext
*/
class PopupsContextTest extends MediaWikiTestCase {
/**
* Anonymous user id
* @see MediaWikiTestCase::addCoreDBData()
*/
const ANONYMOUS_USER = 0;
public function tearDown() {
PopupsContextTestWrapper::resetTestInstance();
parent::tearDown();
}
/**
* @covers ::showPreviewsOptInOnPreferencesPage
* @dataProvider provideConfigForShowPreviewsInOptIn
* @param array $config
* @param bool $expected
*/
public function testShowPreviewsPreferencesPage( $config, $expected ) {
$this->setMwGlobals( $config );
$context = PopupsContext::getInstance();
$this->assertEquals( $expected, $context->showPreviewsOptInOnPreferencesPage() );
}
/**
* @covers ::__construct
* @covers ::getConfig
*/
public function testContextAndConfigInitialization() {
$configMock = $this->getMock( Config::class );
$configFactoryMock = $this->getMock( ConfigFactory::class, [ 'makeConfig' ] );
$configFactoryMock->expects( $this->once() )
->method( 'makeConfig' )
->with( PopupsContext::EXTENSION_NAME )
->will( $this->returnValue( $configMock ) );
$mwServices = $this->overrideMwServices();
$mwServices->redefineService( 'ConfigFactory', function() use ( $configFactoryMock ) {
return $configFactoryMock;
} );
$context = PopupsContext::getInstance();
$this->assertSame( $context->getConfig(), $configMock );
}
/**
* @return array
*/
public function provideConfigForShowPreviewsInOptIn() {
return [
[
"options" => [
"wgPopupsBetaFeature" => false,
"wgPopupsHideOptInOnPreferencesPage" => false
],
"expected" => true
], [
"options" => [
"wgPopupsBetaFeature" => true,
"wgPopupsHideOptInOnPreferencesPage" => false
],
"expected" => false
], [
"options" => [
"wgPopupsBetaFeature" => false,
"wgPopupsHideOptInOnPreferencesPage" => true
],
"expected" => false
]
];
}
/**
* @covers ::isEnabledByUser
* @covers ::isBetaFeatureEnabled
* @dataProvider provideTestDataForIsEnabledByUser
* @param bool $optIn
* @param bool $expected
*/
public function testIsEnabledByUser( $optIn, $expected ) {
$this->setMwGlobals( [
"wgPopupsBetaFeature" => false
] );
$context = PopupsContext::getInstance();
$user = $this->getMutableTestUser()->getUser();
$user->setOption( PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME, $optIn );
$this->assertEquals( $context->isEnabledByUser( $user ), $expected );
}
/**
* @return array/
*/
public function provideTestDataForIsEnabledByUser() {
return [
[
"optin" => PopupsContext::PREVIEWS_ENABLED,
'expected' => true
],
[
"optin" => PopupsContext::PREVIEWS_DISABLED,
'expected' => false
]
];
}
/**
* @covers ::isEnabledByUser
* @covers ::isBetaFeatureEnabled
* @dataProvider provideTestDataForIsEnabledByUserWhenBetaEnabled
* @param bool $optIn
* @param bool $expected
*/
public function testIsEnabledByUserWhenBetaEnabled( $optIn, $expected ) {
if ( !class_exists( 'BetaFeatures' ) ) {
$this->markTestSkipped( 'Skipped as BetaFeatures is not available' );
}
$this->setMwGlobals( [
"wgPopupsBetaFeature" => true
] );
$context = PopupsContext::getInstance();
$user = $this->getMutableTestUser()->getUser();
$user->setOption( PopupsContext::PREVIEWS_BETA_PREFERENCE_NAME, $optIn );
$this->assertEquals( $context->isEnabledByUser( $user ), $expected );
}
/**
* Check that Page Previews are disabled for anonymous user
* @covers ::isEnabledByUser
* @covers ::isBetaFeatureEnabled
*/
public function testAnonUserHasDisabledPagePreviews() {
$user = $this->getMutableTestUser()->getUser();
$user->setId( self::ANONYMOUS_USER );
$user->setOption( PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME,
PopupsContext::PREVIEWS_DISABLED );
$this->setMwGlobals( [
"wgPopupsBetaFeature" => false
] );
$context = PopupsContext::getInstance();
$this->assertEquals( true, $context->isEnabledByUser( $user ) );
}
/**
* @return array/
*/
public function provideTestDataForIsEnabledByUserWhenBetaEnabled() {
return [
[
"optin" => PopupsContext::PREVIEWS_ENABLED,
'expected' => true
], [
"optin" => PopupsContext::PREVIEWS_DISABLED,
'expected' => false
]
];
}
/**
* @covers ::areDependenciesMet
* @dataProvider provideTestDataForTestAreDependenciesMet
* @param bool $betaOn
* @param bool $textExtracts
* @param bool $pageImages
* @param bool $betaFeatures
* @param bool $expected
*/
public function testAreDependenciesMet( $betaOn, $textExtracts, $pageImages,
$betaFeatures, $expected ) {
$this->setMwGlobals( [
"wgPopupsBetaFeature" => $betaOn
] );
$returnValues = [ $textExtracts, $pageImages, $betaFeatures ];
$mock = $this->getMock( ExtensionRegistry::class, [ 'isLoaded' ] );
$mock->expects( $this->any() )
->method( 'isLoaded' )
->will( new PHPUnit_Framework_MockObject_Stub_ConsecutiveCalls( $returnValues ) );
$context = new PopupsContextTestWrapper( $mock );
$this->assertEquals( $expected, $context->areDependenciesMet() );
}
/**
* @return array/
*/
public function provideTestDataForTestAreDependenciesMet() {
return [
[ // Beta is off, dependencies are met even BetaFeatures ext is not available
"betaOn" => false,
"textExtracts" => true,
"pageImages" => true,
"betaFeatures" => false,
"expected" => true
], [ // textExtracts dep is missing
"betaOn" => false,
"textExtracts" => false,
"pageImages" => true,
"betaFeatures" => false,
"expected" => false
], [ // PageImages dep is missing
"betaOn" => false,
"textExtracts" => true,
"pageImages" => false,
"betaFeatures" => false,
"expected" => false
], [ // Beta is on but BetaFeatures dep is missing
"betaOn" => true,
"textExtracts" => true,
"pageImages" => true,
"betaFeatures" => false,
"expected" => false
], [ // beta is on and all deps are available
"betaOn" => true,
"textExtracts" => true,
"pageImages" => true,
"betaFeatures" => true,
"expected" => true
]
];
}
/**
* @covers ::getLogger
*/
public function testGetLogger() {
$loggerMock = $this->getMock( \Psr\Log\LoggerInterface::class );
$this->setLogger( PopupsContext::LOGGER_CHANNEL, $loggerMock );
$context = PopupsContext::getInstance();
$this->assertSame( $loggerMock, $context->getLogger() );
}
/**
* @covers ::getInstance
*/
public function testGetInstanceReturnsSameObjectEveryTime() {
$first = PopupsContext::getInstance();
$second = PopupsContext::getInstance();
$this->assertSame( $first, $second );
$this->assertInstanceOf( PopupsContext::class, $first );
}
/**
* @covers ::getDefaultIsEnabledState
*/
public function testGetDefaultIsEnabledState() {
$this->setMwGlobals( [
'wgPopupsOptInDefaultState' => "2"
] );
$this->assertEquals( "2", PopupsContext::getInstance()->getDefaultIsEnabledState() );
}
/**
* @covers ::conflictsWithNavPopupsGadget
*/
public function testConflictsWithNavPopupsGadget() {
$integrationMock = $this->getMockBuilder( \Popups\PopupsGadgetsIntegration::class )
->disableOriginalConstructor()
->setMethods( [ 'conflictsWithNavPopupsGadget' ] )
->getMock();
$user = $this->getTestUser()->getUser();
$integrationMock->expects( $this->once() )
->method( 'conflictsWithNavPopupsGadget' )
->with( $user )
->willReturn( true );
$context = new PopupsContextTestWrapper( ExtensionRegistry::getInstance(), $integrationMock );
$this->assertEquals( true, $context->conflictsWithNavPopupsGadget( $user ) );
}
}