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
|
|
|
|
2019-10-06 05:43:20 +00:00
|
|
|
use PHPUnit\Framework\MockObject\Stub\ConsecutiveCalls;
|
2019-10-10 15:15:31 +00:00
|
|
|
use Popups\EventLogging\EventLogger;
|
2016-12-16 02:47:52 +00:00
|
|
|
use Popups\PopupsContext;
|
2019-10-10 15:15:31 +00:00
|
|
|
use Popups\PopupsGadgetsIntegration;
|
2016-12-16 02:47:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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 {
|
2019-02-26 10:55:57 +00:00
|
|
|
|
2016-12-16 02:47:52 +00:00
|
|
|
/**
|
2016-12-20 21:54:47 +00:00
|
|
|
* Anonymous user id
|
|
|
|
* @see MediaWikiTestCase::addCoreDBData()
|
|
|
|
*/
|
2019-10-10 14:53:30 +00:00
|
|
|
private const ANONYMOUS_USER = 0;
|
2016-12-20 21:54:47 +00:00
|
|
|
|
2017-07-21 17:06:08 +00:00
|
|
|
/**
|
|
|
|
* Helper method to quickly build Popups Context
|
|
|
|
* @param ExtensionRegistry|null $registry
|
2019-10-10 15:15:31 +00:00
|
|
|
* @param PopupsGadgetsIntegration|null $integration
|
|
|
|
* @param EventLogger $eventLogger
|
2017-07-21 17:06:08 +00:00
|
|
|
* @return PopupsContext
|
|
|
|
*/
|
|
|
|
protected function getContext( $registry = null, $integration = null, $eventLogger = null ) {
|
|
|
|
$config = new GlobalVarConfig();
|
|
|
|
$registry = $registry ?: ExtensionRegistry::getInstance();
|
|
|
|
if ( $eventLogger === null ) {
|
2019-10-10 15:15:31 +00:00
|
|
|
$eventLogger = $this->createMock( EventLogger::class );
|
2017-07-21 17:06:08 +00:00
|
|
|
}
|
|
|
|
if ( $integration === null ) {
|
2019-10-10 15:15:31 +00:00
|
|
|
$integration = $this->createMock( PopupsGadgetsIntegration::class );
|
2017-07-21 17:06:08 +00:00
|
|
|
$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();
|
2019-10-04 08:31:58 +00:00
|
|
|
$this->assertSame( $expected,
|
2018-05-08 19:48:17 +00:00
|
|
|
$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
|
|
|
[
|
2019-02-25 19:09:35 +00:00
|
|
|
'wgPopupsHideOptInOnPreferencesPage' => false
|
2016-12-16 02:47:52 +00:00
|
|
|
],
|
2018-04-26 20:43:05 +00:00
|
|
|
true
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[
|
2019-02-25 19:09:35 +00:00
|
|
|
'wgPopupsHideOptInOnPreferencesPage' => true
|
2016-12-16 02:47:52 +00:00
|
|
|
],
|
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 );
|
2019-10-04 08:31:58 +00:00
|
|
|
$this->assertSame( $expected,
|
2018-05-08 19:48:17 +00:00
|
|
|
$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 [
|
|
|
|
[
|
2019-02-25 19:09:35 +00:00
|
|
|
'optin' => PopupsContext::PREVIEWS_ENABLED,
|
2016-12-16 02:47:52 +00:00
|
|
|
'expected' => true
|
|
|
|
],
|
|
|
|
[
|
2019-02-25 19:09:35 +00:00
|
|
|
'optin' => PopupsContext::PREVIEWS_DISABLED,
|
2016-12-16 02:47:52 +00:00
|
|
|
'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();
|
2019-10-04 08:31:58 +00:00
|
|
|
$this->assertSame( $expected,
|
2018-05-08 19:48:17 +00:00
|
|
|
$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
|
2019-02-25 19:09:35 +00:00
|
|
|
* @param string $gateway
|
2016-12-20 21:54:47 +00:00
|
|
|
* @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( [
|
2019-02-25 19:09:35 +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
|
|
|
|
2019-10-10 15:15:31 +00:00
|
|
|
$mock = $this->createMock( ExtensionRegistry::class );
|
2016-12-20 21:54:47 +00:00
|
|
|
$mock->expects( $this->any() )
|
|
|
|
->method( 'isLoaded' )
|
2019-10-06 05:43:20 +00:00
|
|
|
->will( new ConsecutiveCalls( $returnValues ) );
|
2017-07-21 17:06:08 +00:00
|
|
|
$context = $this->getContext( $mock );
|
2019-10-04 08:31:58 +00:00
|
|
|
$this->assertSame( $expected,
|
2018-05-08 19:48:17 +00:00
|
|
|
$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
|
|
|
[
|
2019-02-25 19:09:35 +00:00
|
|
|
'textExtracts' => true,
|
|
|
|
'pageImages' => true,
|
|
|
|
'gateway' => 'mwApiPlain',
|
|
|
|
'expected' => true
|
2017-07-12 20:44:12 +00:00
|
|
|
],
|
|
|
|
// textExtracts dep is missing
|
|
|
|
[
|
2019-02-25 19:09:35 +00:00
|
|
|
'textExtracts' => false,
|
|
|
|
'pageImages' => true,
|
|
|
|
'gateway' => 'mwApiPlain',
|
|
|
|
'expected' => false
|
2017-07-12 20:44:12 +00:00
|
|
|
],
|
|
|
|
// PageImages dep is missing
|
|
|
|
[
|
2019-02-25 19:09:35 +00:00
|
|
|
'textExtracts' => true,
|
|
|
|
'pageImages' => false,
|
|
|
|
'gateway' => 'mwApiPlain',
|
|
|
|
'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
|
|
|
|
[
|
2019-02-25 19:09:35 +00:00
|
|
|
'textExtracts' => false,
|
|
|
|
'pageImages' => false,
|
|
|
|
'gateway' => 'restbaseHTML',
|
|
|
|
'expected' => true
|
2018-03-28 22:53:34 +00:00
|
|
|
],
|
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 ) {
|
2019-02-25 19:09:35 +00:00
|
|
|
$this->setMwGlobals( [ 'wgPopupsPageBlacklist' => $blacklist ] );
|
2017-07-28 18:58:13 +00:00
|
|
|
$context = $this->getContext();
|
2019-10-04 08:31:58 +00:00
|
|
|
$this->assertSame( $expected,
|
2018-05-08 19:48:17 +00:00
|
|
|
$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();
|
2019-10-04 08:31:58 +00:00
|
|
|
$this->assertTrue(
|
2018-05-08 19:48:17 +00:00
|
|
|
$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() {
|
2019-10-10 15:15:31 +00:00
|
|
|
$integrationMock = $this->createMock( PopupsGadgetsIntegration::class );
|
2017-01-04 16:33:40 +00:00
|
|
|
|
|
|
|
$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 );
|
2019-10-04 08:31:58 +00:00
|
|
|
$this->assertTrue(
|
2018-05-08 19:48:17 +00:00
|
|
|
$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() {
|
2019-10-10 15:15:31 +00:00
|
|
|
$loggerMock = $this->createMock( EventLogger::class );
|
2017-07-06 18:16:05 +00:00
|
|
|
$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();
|
|
|
|
}
|
2019-02-26 10:55:57 +00:00
|
|
|
|
2016-12-16 02:47:52 +00:00
|
|
|
}
|