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
|
|
|
|
*/
|
2016-12-16 02:47:52 +00:00
|
|
|
|
|
|
|
use Popups\PopupsContext;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group Popups
|
2019-02-01 08:40:34 +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;
|
|
|
|
|
2017-07-21 17:06:08 +00:00
|
|
|
/**
|
|
|
|
* Helper method to quickly build Popups Context
|
|
|
|
* @param ExtensionRegistry|null $registry
|
|
|
|
* @param \Popups\PopupsGadgetsIntegration|null $integration
|
|
|
|
* @param \Popups\EventLogging\EventLogger $eventLogger
|
|
|
|
* @return PopupsContext
|
|
|
|
*/
|
|
|
|
protected function getContext( $registry = null, $integration = null, $eventLogger = null ) {
|
|
|
|
$config = new GlobalVarConfig();
|
|
|
|
$registry = $registry ?: ExtensionRegistry::getInstance();
|
|
|
|
if ( $eventLogger === null ) {
|
|
|
|
$eventLogger = $this->getMockBuilder( \Popups\EventLogging\EventLogger::class )
|
|
|
|
->getMock();
|
|
|
|
}
|
|
|
|
if ( $integration === null ) {
|
|
|
|
$integration = $this->getMockBuilder( \Popups\PopupsGadgetsIntegration::class )
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->setMethods( [ 'conflictsWithNavPopupsGadget' ] )
|
|
|
|
->getMock();
|
|
|
|
$integration->method( 'conflictsWithNavPopupsGadget' )
|
|
|
|
->willReturn( false );
|
|
|
|
}
|
|
|
|
return new PopupsContext( $config, $registry, $integration, $eventLogger );
|
2016-12-20 21:54:47 +00:00
|
|
|
}
|
2019-01-16 14:25:27 +00:00
|
|
|
|
2016-12-20 21:54:47 +00:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
*/
|
2019-01-24 14:44:26 +00:00
|
|
|
public function testShowPreviewsPreferencesPage( array $config, $expected ) {
|
2016-12-16 02:47:52 +00:00
|
|
|
$this->setMwGlobals( $config );
|
2017-07-21 17:06:08 +00:00
|
|
|
$context = $this->getContext();
|
2018-05-08 19:48:17 +00:00
|
|
|
$this->assertEquals( $expected,
|
|
|
|
$context->showPreviewsOptInOnPreferencesPage(),
|
2018-10-05 22:46:27 +00:00
|
|
|
'The previews opt-in is ' . ( $expected ? 'shown.' : 'hidden.' ) );
|
2016-12-16 02:47:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function provideConfigForShowPreviewsInOptIn() {
|
|
|
|
return [
|
|
|
|
[
|
2018-04-26 20:43:05 +00:00
|
|
|
[
|
2016-12-16 02:47:52 +00:00
|
|
|
"wgPopupsHideOptInOnPreferencesPage" => false
|
|
|
|
],
|
2018-04-26 20:43:05 +00:00
|
|
|
true
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[
|
2016-12-16 02:47:52 +00:00
|
|
|
"wgPopupsHideOptInOnPreferencesPage" => true
|
|
|
|
],
|
2018-04-26 20:43:05 +00:00
|
|
|
false
|
2016-12-16 02:47:52 +00:00
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-02-10 00:27:05 +00:00
|
|
|
* @covers ::shouldSendModuleToUser
|
|
|
|
* @dataProvider provideTestDataForShouldSendModuleToUser
|
2016-12-20 21:54:47 +00:00
|
|
|
* @param bool $optIn
|
|
|
|
* @param bool $expected
|
2016-12-16 02:47:52 +00:00
|
|
|
*/
|
2017-02-10 00:27:05 +00:00
|
|
|
public function testShouldSendModuleToUser( $optIn, $expected ) {
|
2017-07-21 17:06:08 +00:00
|
|
|
$context = $this->getContext();
|
2016-12-16 02:47:52 +00:00
|
|
|
$user = $this->getMutableTestUser()->getUser();
|
|
|
|
$user->setOption( PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME, $optIn );
|
2018-05-08 19:48:17 +00:00
|
|
|
$this->assertEquals( $expected,
|
|
|
|
$context->shouldSendModuleToUser( $user ),
|
2018-10-05 22:46:27 +00:00
|
|
|
( $expected ? 'A' : 'No' ) . ' module is sent to the user.' );
|
2016-12-16 02:47:52 +00:00
|
|
|
}
|
|
|
|
|
2017-02-10 00:27:05 +00:00
|
|
|
public function provideTestDataForShouldSendModuleToUser() {
|
2016-12-16 02:47:52 +00:00
|
|
|
return [
|
|
|
|
[
|
|
|
|
"optin" => PopupsContext::PREVIEWS_ENABLED,
|
|
|
|
'expected' => true
|
|
|
|
],
|
|
|
|
[
|
|
|
|
"optin" => PopupsContext::PREVIEWS_DISABLED,
|
|
|
|
'expected' => false
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-07-21 17:06:08 +00:00
|
|
|
* Check tst Page Previews are disabled for anonymous user
|
2017-02-10 00:27:05 +00:00
|
|
|
* @covers ::shouldSendModuleToUser
|
2017-02-08 22:08:12 +00:00
|
|
|
* @dataProvider providerAnonUserHasDisabledPagePreviews
|
2016-12-16 02:47:52 +00:00
|
|
|
*/
|
2018-04-26 20:43:05 +00:00
|
|
|
public function testAnonUserHasDisabledPagePreviews( $expected ) {
|
2016-12-16 02:47:52 +00:00
|
|
|
$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
|
|
|
|
2017-07-21 17:06:08 +00:00
|
|
|
$context = $this->getContext();
|
2018-05-08 19:48:17 +00:00
|
|
|
$this->assertEquals( $expected,
|
|
|
|
$context->shouldSendModuleToUser( $user ),
|
2018-10-05 22:46:27 +00:00
|
|
|
( $expected ? 'A' : 'No' ) . ' module is sent to the user.' );
|
2017-02-08 22:08:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function providerAnonUserHasDisabledPagePreviews() {
|
|
|
|
return [
|
2018-04-26 20:43:05 +00:00
|
|
|
// Anons see this by default
|
|
|
|
[ true ],
|
2016-12-16 02:47:52 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-20 21:54:47 +00:00
|
|
|
* @covers ::areDependenciesMet
|
2018-12-18 16:05:33 +00:00
|
|
|
* @covers ::__construct
|
2016-12-20 21:54:47 +00:00
|
|
|
* @dataProvider provideTestDataForTestAreDependenciesMet
|
|
|
|
* @param bool $textExtracts
|
|
|
|
* @param bool $pageImages
|
|
|
|
* @param bool $expected
|
|
|
|
*/
|
2018-04-26 20:43:05 +00:00
|
|
|
public function testAreDependenciesMet( $textExtracts, $pageImages,
|
|
|
|
$gateway, $expected ) {
|
2016-12-20 21:54:47 +00:00
|
|
|
$this->setMwGlobals( [
|
2018-03-28 22:53:34 +00:00
|
|
|
"wgPopupsGateway" => $gateway,
|
2016-12-20 21:54:47 +00:00
|
|
|
] );
|
2018-04-26 20:43:05 +00:00
|
|
|
$returnValues = [ $textExtracts, $pageImages ];
|
2016-12-20 21:54:47 +00:00
|
|
|
|
|
|
|
$mock = $this->getMock( ExtensionRegistry::class, [ 'isLoaded' ] );
|
|
|
|
$mock->expects( $this->any() )
|
|
|
|
->method( 'isLoaded' )
|
|
|
|
->will( new PHPUnit_Framework_MockObject_Stub_ConsecutiveCalls( $returnValues ) );
|
2017-07-21 17:06:08 +00:00
|
|
|
$context = $this->getContext( $mock );
|
2018-05-08 19:48:17 +00:00
|
|
|
$this->assertEquals( $expected,
|
|
|
|
$context->areDependenciesMet(),
|
2018-10-05 22:46:27 +00:00
|
|
|
'Dependencies are ' . ( $expected ? '' : 'not ' ) . 'met.' );
|
2016-12-20 21:54:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function provideTestDataForTestAreDependenciesMet() {
|
|
|
|
return [
|
2018-04-26 20:43:05 +00:00
|
|
|
// Dependencies are met
|
2017-07-12 20:44:12 +00:00
|
|
|
[
|
2016-12-20 21:54:47 +00:00
|
|
|
"textExtracts" => true,
|
|
|
|
"pageImages" => true,
|
2018-03-28 22:53:34 +00:00
|
|
|
"gateway" => "mwApiPlain",
|
2016-12-20 21:54:47 +00:00
|
|
|
"expected" => true
|
2017-07-12 20:44:12 +00:00
|
|
|
],
|
|
|
|
// textExtracts dep is missing
|
|
|
|
[
|
2016-12-20 21:54:47 +00:00
|
|
|
"textExtracts" => false,
|
|
|
|
"pageImages" => true,
|
2018-03-28 22:53:34 +00:00
|
|
|
"gateway" => "mwApiPlain",
|
2016-12-20 21:54:47 +00:00
|
|
|
"expected" => false
|
2017-07-12 20:44:12 +00:00
|
|
|
],
|
|
|
|
// PageImages dep is missing
|
|
|
|
[
|
2016-12-20 21:54:47 +00:00
|
|
|
"textExtracts" => true,
|
|
|
|
"pageImages" => false,
|
2018-03-28 22:53:34 +00:00
|
|
|
"gateway" => "mwApiPlain",
|
2016-12-20 21:54:47 +00:00
|
|
|
"expected" => false
|
2017-07-12 20:44:12 +00:00
|
|
|
],
|
2018-03-28 22:53:34 +00:00
|
|
|
// when Popups uses gateway!=mwApiPlain we don't require PageImages nor TextExtracts
|
|
|
|
[
|
|
|
|
"textExtracts" => false,
|
|
|
|
"pageImages" => false,
|
|
|
|
"gateway" => "restbaseHTML",
|
|
|
|
"expected" => true
|
|
|
|
],
|
2016-12-20 21:54:47 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-07-28 18:58:13 +00:00
|
|
|
/**
|
|
|
|
* @covers ::isTitleBlacklisted
|
|
|
|
* @dataProvider provideTestIsTitleBLacklisted
|
2019-01-24 14:44:26 +00:00
|
|
|
* @param string[] $blacklist
|
2017-07-28 18:58:13 +00:00
|
|
|
* @param Title $title
|
2018-04-26 20:43:05 +00:00
|
|
|
* @param bool $expected
|
2017-07-28 18:58:13 +00:00
|
|
|
*/
|
2019-01-24 14:44:26 +00:00
|
|
|
public function testIsTitleBlacklisted( array $blacklist, Title $title, $expected ) {
|
2017-07-28 18:58:13 +00:00
|
|
|
$this->setMwGlobals( [ "wgPopupsPageBlacklist" => $blacklist ] );
|
|
|
|
$context = $this->getContext();
|
2018-05-08 19:48:17 +00:00
|
|
|
$this->assertEquals( $expected,
|
|
|
|
$context->isTitleBlacklisted( $title ),
|
2018-10-05 22:46:27 +00:00
|
|
|
'The title is' . ( $expected ? ' ' : ' not ' ) . 'blacklisted.' );
|
2017-07-28 18:58:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function provideTestIsTitleBlacklisted() {
|
2017-08-24 20:51:39 +00:00
|
|
|
$blacklist = [ 'Special:Userlogin', 'Special:CreateAccount', 'User:A' ];
|
2017-07-28 18:58:13 +00:00
|
|
|
return [
|
|
|
|
[ $blacklist, Title::newFromText( 'Main_Page' ), false ],
|
|
|
|
[ $blacklist, Title::newFromText( 'Special:CreateAccount' ), true ],
|
|
|
|
[ $blacklist, Title::newFromText( 'User:A' ), true ],
|
|
|
|
[ $blacklist, Title::newFromText( 'User:A/B' ), true ],
|
|
|
|
[ $blacklist, Title::newFromText( 'User:B' ), false ],
|
|
|
|
[ $blacklist, Title::newFromText( 'User:B/A' ), false ],
|
2017-08-24 20:51:39 +00:00
|
|
|
// test canonical name handling
|
|
|
|
[ $blacklist, Title::newFromText( 'Special:UserLogin' ), true ],
|
2017-07-28 18:58:13 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-08-24 20:51:39 +00:00
|
|
|
/**
|
|
|
|
* Test if special page in different language is blacklisted
|
|
|
|
*
|
|
|
|
* @covers ::isTitleBlacklisted
|
|
|
|
*/
|
|
|
|
public function testIsTranslatedTitleBlacklisted() {
|
|
|
|
$page = 'Specjalna:Preferencje';
|
2018-09-05 19:00:38 +00:00
|
|
|
$blacklist = [ 'Special:Preferences' ];
|
2017-08-24 20:51:39 +00:00
|
|
|
|
|
|
|
$this->setMwGlobals( [
|
2018-05-08 19:48:17 +00:00
|
|
|
'wgPopupsPageBlacklist' => $blacklist,
|
|
|
|
'wgLanguageCode' => 'pl'
|
2017-08-24 20:51:39 +00:00
|
|
|
] );
|
|
|
|
$context = $this->getContext();
|
2018-05-08 19:48:17 +00:00
|
|
|
$this->assertEquals( true,
|
|
|
|
$context->isTitleBlacklisted( Title::newFromText( $page ) ),
|
|
|
|
'The title is blacklisted.' );
|
2017-08-24 20:51:39 +00:00
|
|
|
}
|
|
|
|
|
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-07-21 17:06:08 +00:00
|
|
|
$context = $this->getContext( null, $integrationMock );
|
2018-05-08 19:48:17 +00:00
|
|
|
$this->assertEquals( true,
|
|
|
|
$context->conflictsWithNavPopupsGadget( $user ),
|
|
|
|
'A conflict is identified.' );
|
2017-01-04 16:33:40 +00:00
|
|
|
}
|
2017-01-11 21:52:07 +00:00
|
|
|
|
2017-07-06 18:16:05 +00:00
|
|
|
/**
|
|
|
|
* @covers ::logUserDisabledPagePreviewsEvent
|
|
|
|
*/
|
|
|
|
public function testLogsEvent() {
|
|
|
|
$loggerMock = $this->getMock( \Popups\EventLogging\EventLogger::class );
|
|
|
|
$loggerMock->expects( $this->once() )
|
|
|
|
->method( 'log' );
|
|
|
|
|
2017-07-21 17:06:08 +00:00
|
|
|
$context = $this->getContext( null, null, $loggerMock );
|
2017-07-06 18:16:05 +00:00
|
|
|
$context->logUserDisabledPagePreviewsEvent();
|
|
|
|
}
|
2016-12-16 02:47:52 +00:00
|
|
|
}
|