2016-12-16 02:47:52 +00:00
|
|
|
<?php
|
2016-12-20 21:54:47 +00:00
|
|
|
/*
|
|
|
|
* 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' );
|
2016-12-16 02:47:52 +00:00
|
|
|
|
|
|
|
use Popups\PopupsContext;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Popups module tests
|
|
|
|
*
|
|
|
|
* @group Popups
|
2016-12-20 21:54:47 +00:00
|
|
|
* @coversDefaultClass Popups\PopupsContext
|
2016-12-16 02:47:52 +00:00
|
|
|
*/
|
|
|
|
class PopupsContextTest extends MediaWikiTestCase {
|
|
|
|
/**
|
2016-12-20 21:54:47 +00:00
|
|
|
* Anonymous user id
|
|
|
|
* @see MediaWikiTestCase::addCoreDBData()
|
|
|
|
*/
|
|
|
|
const ANONYMOUS_USER = 0;
|
|
|
|
|
|
|
|
public function tearDown() {
|
|
|
|
PopupsContextTestWrapper::resetTestInstance();
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::showPreviewsOptInOnPreferencesPage
|
2016-12-16 02:47:52 +00:00
|
|
|
* @dataProvider provideConfigForShowPreviewsInOptIn
|
2016-12-20 21:54:47 +00:00
|
|
|
* @param array $config
|
|
|
|
* @param bool $expected
|
2016-12-16 02:47:52 +00:00
|
|
|
*/
|
|
|
|
public function testShowPreviewsPreferencesPage( $config, $expected ) {
|
|
|
|
$this->setMwGlobals( $config );
|
2016-12-20 21:54:47 +00:00
|
|
|
$context = PopupsContext::getInstance();
|
|
|
|
$this->assertEquals( $expected, $context->showPreviewsOptInOnPreferencesPage() );
|
2016-12-16 02:47:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-20 21:54:47 +00:00
|
|
|
* @covers ::__construct
|
|
|
|
* @covers ::getConfig
|
2016-12-16 02:47:52 +00:00
|
|
|
*/
|
|
|
|
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;
|
|
|
|
} );
|
|
|
|
|
2016-12-20 21:54:47 +00:00
|
|
|
$context = PopupsContext::getInstance();
|
|
|
|
$this->assertSame( $context->getConfig(), $configMock );
|
2016-12-16 02:47:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function provideConfigForShowPreviewsInOptIn() {
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
"options" => [
|
|
|
|
"wgPopupsBetaFeature" => false,
|
|
|
|
"wgPopupsHideOptInOnPreferencesPage" => false
|
|
|
|
],
|
|
|
|
"expected" => true
|
2016-12-20 21:54:47 +00:00
|
|
|
], [
|
2016-12-16 02:47:52 +00:00
|
|
|
"options" => [
|
|
|
|
"wgPopupsBetaFeature" => true,
|
|
|
|
"wgPopupsHideOptInOnPreferencesPage" => false
|
|
|
|
],
|
|
|
|
"expected" => false
|
2016-12-20 21:54:47 +00:00
|
|
|
], [
|
2016-12-16 02:47:52 +00:00
|
|
|
"options" => [
|
|
|
|
"wgPopupsBetaFeature" => false,
|
|
|
|
"wgPopupsHideOptInOnPreferencesPage" => true
|
|
|
|
],
|
|
|
|
"expected" => false
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-20 21:54:47 +00:00
|
|
|
* @covers ::isEnabledByUser
|
|
|
|
* @covers ::isBetaFeatureEnabled
|
2016-12-16 02:47:52 +00:00
|
|
|
* @dataProvider provideTestDataForIsEnabledByUser
|
2016-12-20 21:54:47 +00:00
|
|
|
* @param bool $optIn
|
|
|
|
* @param bool $expected
|
2016-12-16 02:47:52 +00:00
|
|
|
*/
|
|
|
|
public function testIsEnabledByUser( $optIn, $expected ) {
|
|
|
|
$this->setMwGlobals( [
|
|
|
|
"wgPopupsBetaFeature" => false
|
|
|
|
] );
|
2016-12-20 21:54:47 +00:00
|
|
|
$context = PopupsContext::getInstance();
|
2016-12-16 02:47:52 +00:00
|
|
|
$user = $this->getMutableTestUser()->getUser();
|
|
|
|
$user->setOption( PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME, $optIn );
|
2016-12-20 21:54:47 +00:00
|
|
|
$this->assertEquals( $context->isEnabledByUser( $user ), $expected );
|
2016-12-16 02:47:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array/
|
|
|
|
*/
|
|
|
|
public function provideTestDataForIsEnabledByUser() {
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
"optin" => PopupsContext::PREVIEWS_ENABLED,
|
|
|
|
'expected' => true
|
|
|
|
],
|
|
|
|
[
|
|
|
|
"optin" => PopupsContext::PREVIEWS_DISABLED,
|
|
|
|
'expected' => false
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-20 21:54:47 +00:00
|
|
|
* @covers ::isEnabledByUser
|
|
|
|
* @covers ::isBetaFeatureEnabled
|
2016-12-16 02:47:52 +00:00
|
|
|
* @dataProvider provideTestDataForIsEnabledByUserWhenBetaEnabled
|
2016-12-20 21:54:47 +00:00
|
|
|
* @param bool $optIn
|
|
|
|
* @param bool $expected
|
2016-12-16 02:47:52 +00:00
|
|
|
*/
|
|
|
|
public function testIsEnabledByUserWhenBetaEnabled( $optIn, $expected ) {
|
|
|
|
if ( !class_exists( 'BetaFeatures' ) ) {
|
|
|
|
$this->markTestSkipped( 'Skipped as BetaFeatures is not available' );
|
|
|
|
}
|
|
|
|
$this->setMwGlobals( [
|
|
|
|
"wgPopupsBetaFeature" => true
|
|
|
|
] );
|
2016-12-20 21:54:47 +00:00
|
|
|
$context = PopupsContext::getInstance();
|
2016-12-16 02:47:52 +00:00
|
|
|
$user = $this->getMutableTestUser()->getUser();
|
|
|
|
$user->setOption( PopupsContext::PREVIEWS_BETA_PREFERENCE_NAME, $optIn );
|
2016-12-20 21:54:47 +00:00
|
|
|
$this->assertEquals( $context->isEnabledByUser( $user ), $expected );
|
2016-12-16 02:47:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check that Page Previews are disabled for anonymous user
|
2016-12-20 21:54:47 +00:00
|
|
|
* @covers ::isEnabledByUser
|
|
|
|
* @covers ::isBetaFeatureEnabled
|
2016-12-16 02:47:52 +00:00
|
|
|
*/
|
|
|
|
public function testAnonUserHasDisabledPagePreviews() {
|
|
|
|
$user = $this->getMutableTestUser()->getUser();
|
2016-12-20 21:54:47 +00:00
|
|
|
$user->setId( self::ANONYMOUS_USER );
|
2016-12-16 02:47:52 +00:00
|
|
|
$user->setOption( PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME,
|
2016-12-20 22:53:59 +00:00
|
|
|
PopupsContext::PREVIEWS_DISABLED );
|
2016-12-16 02:47:52 +00:00
|
|
|
$this->setMwGlobals( [
|
|
|
|
"wgPopupsBetaFeature" => false
|
|
|
|
] );
|
|
|
|
|
2016-12-20 21:54:47 +00:00
|
|
|
$context = PopupsContext::getInstance();
|
2016-12-20 22:53:59 +00:00
|
|
|
$this->assertEquals( true, $context->isEnabledByUser( $user ) );
|
2016-12-16 02:47:52 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return array/
|
|
|
|
*/
|
|
|
|
public function provideTestDataForIsEnabledByUserWhenBetaEnabled() {
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
"optin" => PopupsContext::PREVIEWS_ENABLED,
|
|
|
|
'expected' => true
|
2016-12-20 21:54:47 +00:00
|
|
|
], [
|
2016-12-16 02:47:52 +00:00
|
|
|
"optin" => PopupsContext::PREVIEWS_DISABLED,
|
|
|
|
'expected' => false
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-20 21:54:47 +00:00
|
|
|
* @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 ) );
|
2017-01-11 21:52:07 +00:00
|
|
|
$context = new PopupsContextTestWrapper( new GlobalVarConfig(), $mock );
|
2016-12-20 21:54:47 +00:00
|
|
|
$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
|
2016-12-16 02:47:52 +00:00
|
|
|
*/
|
|
|
|
public function testGetLogger() {
|
|
|
|
$loggerMock = $this->getMock( \Psr\Log\LoggerInterface::class );
|
|
|
|
|
|
|
|
$this->setLogger( PopupsContext::LOGGER_CHANNEL, $loggerMock );
|
2016-12-20 21:54:47 +00:00
|
|
|
$context = PopupsContext::getInstance();
|
2016-12-16 02:47:52 +00:00
|
|
|
$this->assertSame( $loggerMock, $context->getLogger() );
|
|
|
|
}
|
|
|
|
|
2016-12-20 21:54:47 +00:00
|
|
|
/**
|
|
|
|
* @covers ::getInstance
|
|
|
|
*/
|
|
|
|
public function testGetInstanceReturnsSameObjectEveryTime() {
|
|
|
|
$first = PopupsContext::getInstance();
|
|
|
|
$second = PopupsContext::getInstance();
|
|
|
|
|
|
|
|
$this->assertSame( $first, $second );
|
|
|
|
$this->assertInstanceOf( PopupsContext::class, $first );
|
|
|
|
}
|
|
|
|
|
2017-01-10 01:28:57 +00:00
|
|
|
/**
|
|
|
|
* @covers ::getDefaultIsEnabledState
|
|
|
|
*/
|
|
|
|
public function testGetDefaultIsEnabledState() {
|
|
|
|
$this->setMwGlobals( [
|
|
|
|
'wgPopupsOptInDefaultState' => "2"
|
|
|
|
] );
|
|
|
|
$this->assertEquals( "2", PopupsContext::getInstance()->getDefaultIsEnabledState() );
|
|
|
|
}
|
2017-01-04 16:33:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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 );
|
|
|
|
|
2017-01-11 21:52:07 +00:00
|
|
|
$context = new PopupsContextTestWrapper( $this->getConfigMock(),
|
|
|
|
ExtensionRegistry::getInstance(), $integrationMock );
|
2017-01-04 16:33:40 +00:00
|
|
|
$this->assertEquals( true, $context->conflictsWithNavPopupsGadget( $user ) );
|
|
|
|
}
|
2017-01-11 21:52:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return PHPUnit_Framework_MockObject_MockObject|Config
|
|
|
|
*/
|
|
|
|
private function getConfigMock() {
|
|
|
|
$mock = $this->getMockBuilder( 'Config' )
|
|
|
|
->setMethods( [ 'get', 'has' ] )
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
return $mock;
|
|
|
|
}
|
2016-12-16 02:47:52 +00:00
|
|
|
}
|