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
|
|
|
|
2021-05-01 17:46:14 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2019-10-06 05:43:20 +00:00
|
|
|
use PHPUnit\Framework\MockObject\Stub\ConsecutiveCalls;
|
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
|
2017-07-21 17:06:08 +00:00
|
|
|
* @return PopupsContext
|
|
|
|
*/
|
2021-03-02 17:08:45 +00:00
|
|
|
protected function getContext( $registry = null, $integration = null ) {
|
2017-07-21 17:06:08 +00:00
|
|
|
$config = new GlobalVarConfig();
|
|
|
|
$registry = $registry ?: ExtensionRegistry::getInstance();
|
|
|
|
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 );
|
|
|
|
}
|
2021-06-06 13:07:08 +00:00
|
|
|
$userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
|
|
|
|
return new PopupsContext(
|
|
|
|
$config,
|
|
|
|
$registry,
|
|
|
|
$integration,
|
|
|
|
$userOptionsLookup
|
|
|
|
);
|
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
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-12-11 19:04:21 +00:00
|
|
|
* @covers ::shouldSendModuleToUser
|
|
|
|
*/
|
|
|
|
public function testShouldSendToAnonUser() {
|
|
|
|
$user = $this->getMutableTestUser()->getUser();
|
|
|
|
$user->setId( self::ANONYMOUS_USER );
|
|
|
|
|
|
|
|
$context = $this->getContext();
|
2020-02-07 07:16:45 +00:00
|
|
|
$this->assertTrue(
|
2019-12-11 19:04:21 +00:00
|
|
|
$context->shouldSendModuleToUser( $user ),
|
|
|
|
'The module is always sent to anonymous users.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests #shouldSendModuleToUser when the user is logged in and the reference previews feature
|
|
|
|
* is disabled.
|
|
|
|
*
|
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 ) {
|
2019-12-11 19:04:21 +00:00
|
|
|
$this->setMwGlobals( [
|
|
|
|
'wgPopupsReferencePreviews' => false,
|
|
|
|
] );
|
|
|
|
|
2017-07-21 17:06:08 +00:00
|
|
|
$context = $this->getContext();
|
2016-12-16 02:47:52 +00:00
|
|
|
$user = $this->getMutableTestUser()->getUser();
|
2021-05-01 17:46:14 +00:00
|
|
|
$userOptionsManager = MediaWikiServices::getInstance()->getUserOptionsManager();
|
|
|
|
$userOptionsManager->setOption( $user, 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
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
/**
|
2020-06-09 06:52:42 +00:00
|
|
|
* @covers ::isTitleExcluded
|
|
|
|
* @dataProvider provideTestIsTitleExcluded
|
|
|
|
* @param string[] $excludedPages
|
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
|
|
|
*/
|
2020-06-09 06:52:42 +00:00
|
|
|
public function testIsTitleExcluded( array $excludedPages, Title $title, $expected ) {
|
2020-07-02 22:09:22 +00:00
|
|
|
$this->setMwGlobals( [ 'wgPopupsPageDisabled' => $excludedPages ] );
|
2017-07-28 18:58:13 +00:00
|
|
|
$context = $this->getContext();
|
2019-10-04 08:31:58 +00:00
|
|
|
$this->assertSame( $expected,
|
2020-06-09 06:52:42 +00:00
|
|
|
$context->isTitleExcluded( $title ),
|
|
|
|
'The title is' . ( $expected ? ' ' : ' not ' ) . 'excluded.' );
|
2017-07-28 18:58:13 +00:00
|
|
|
}
|
|
|
|
|
2020-06-09 06:52:42 +00:00
|
|
|
public function provideTestIsTitleExcluded() {
|
|
|
|
$excludedPages = [ 'Special:Userlogin', 'Special:CreateAccount', 'User:A' ];
|
2017-07-28 18:58:13 +00:00
|
|
|
return [
|
2020-06-09 06:52:42 +00:00
|
|
|
[ $excludedPages, Title::newFromText( 'Main_Page' ), false ],
|
|
|
|
[ $excludedPages, Title::newFromText( 'Special:CreateAccount' ), true ],
|
|
|
|
[ $excludedPages, Title::newFromText( 'User:A' ), true ],
|
|
|
|
[ $excludedPages, Title::newFromText( 'User:A/B' ), true ],
|
|
|
|
[ $excludedPages, Title::newFromText( 'User:B' ), false ],
|
|
|
|
[ $excludedPages, Title::newFromText( 'User:B/A' ), false ],
|
2017-08-24 20:51:39 +00:00
|
|
|
// test canonical name handling
|
2020-06-09 06:52:42 +00:00
|
|
|
[ $excludedPages, Title::newFromText( 'Special:UserLogin' ), true ],
|
2017-07-28 18:58:13 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-08-24 20:51:39 +00:00
|
|
|
/**
|
2020-06-09 06:52:42 +00:00
|
|
|
* Test if special page in different language is excluded
|
2017-08-24 20:51:39 +00:00
|
|
|
*
|
2020-06-09 06:52:42 +00:00
|
|
|
* @covers ::isTitleExcluded
|
2017-08-24 20:51:39 +00:00
|
|
|
*/
|
2020-06-09 06:52:42 +00:00
|
|
|
public function testIsTranslatedTitleExcluded() {
|
2017-08-24 20:51:39 +00:00
|
|
|
$page = 'Specjalna:Preferencje';
|
2020-06-09 06:52:42 +00:00
|
|
|
$excludedPages = [ 'Special:Preferences' ];
|
2017-08-24 20:51:39 +00:00
|
|
|
|
|
|
|
$this->setMwGlobals( [
|
2020-07-02 22:09:22 +00:00
|
|
|
'wgPopupsPageDisabled' => $excludedPages,
|
2018-05-08 19:48:17 +00:00
|
|
|
'wgLanguageCode' => 'pl'
|
2017-08-24 20:51:39 +00:00
|
|
|
] );
|
|
|
|
$context = $this->getContext();
|
2019-10-04 08:31:58 +00:00
|
|
|
$this->assertTrue(
|
2020-06-09 06:52:42 +00:00
|
|
|
$context->isTitleExcluded( Title::newFromText( $page ) ),
|
|
|
|
'The title is excluded.' );
|
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
|
|
|
|
2021-03-11 10:32:30 +00:00
|
|
|
/**
|
|
|
|
* @covers ::getConfigBitmaskFromUser
|
|
|
|
* @dataProvider provideTestGetConfigBitmaskFromUser
|
|
|
|
* @param bool $navPops
|
|
|
|
* @param bool $refTooltips
|
|
|
|
* @param bool $refEnabled
|
|
|
|
* @param bool $refInBeta
|
|
|
|
* @param int $expected
|
|
|
|
*/
|
|
|
|
public function testGetConfigBitmaskFromUser(
|
|
|
|
$navPops,
|
|
|
|
$refTooltips,
|
|
|
|
$refEnabled,
|
|
|
|
$refInBeta,
|
|
|
|
$expected
|
|
|
|
) {
|
|
|
|
$contextMock = $this->createPartialMock(
|
|
|
|
PopupsContext::class,
|
|
|
|
[
|
|
|
|
'conflictsWithNavPopupsGadget',
|
|
|
|
'conflictsWithRefTooltipsGadget',
|
|
|
|
'isReferencePreviewsEnabled',
|
|
|
|
'isReferencePreviewsInBeta',
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$contextMock->method( 'conflictsWithNavPopupsGadget' )
|
|
|
|
->willReturn( $navPops );
|
|
|
|
$contextMock->method( 'conflictsWithRefTooltipsGadget' )
|
|
|
|
->willReturn( $refTooltips );
|
|
|
|
$contextMock->method( 'isReferencePreviewsEnabled' )
|
|
|
|
->willReturn( $refEnabled );
|
|
|
|
$contextMock->method( 'isReferencePreviewsInBeta' )
|
|
|
|
->willReturn( $refInBeta );
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
$expected,
|
|
|
|
$contextMock->getConfigBitmaskFromUser( $this->getTestUser()->getUser() )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideTestGetConfigBitmaskFromUser() {
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
15,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
10,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
5,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
0,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
2016-12-16 02:47:52 +00:00
|
|
|
}
|