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
|
|
|
|
*/
|
2017-01-04 16:33:40 +00:00
|
|
|
use Popups\PopupsContext;
|
2017-02-14 19:22:55 +00:00
|
|
|
use Popups\PopupsHooks;
|
2019-10-10 15:15:31 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
2016-12-16 02:47:52 +00:00
|
|
|
|
|
|
|
/**
|
2018-03-20 20:20:43 +00:00
|
|
|
* Integration tests for popups hooks
|
2016-12-16 02:47:52 +00:00
|
|
|
*
|
|
|
|
* @group Popups
|
2017-12-27 19:53:07 +00:00
|
|
|
* @coversDefaultClass \Popups\PopupsHooks
|
2016-12-16 02:47:52 +00:00
|
|
|
*/
|
|
|
|
class PopupsHooksTest extends MediaWikiTestCase {
|
|
|
|
|
2019-10-15 23:12:03 +00:00
|
|
|
protected function tearDown() : void {
|
2016-12-16 02:47:52 +00:00
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-20 21:54:47 +00:00
|
|
|
* @covers ::onGetPreferences
|
2016-12-16 02:47:52 +00:00
|
|
|
*/
|
|
|
|
public function testOnGetPreferencesPreviewsDisabled() {
|
2019-10-10 15:15:31 +00:00
|
|
|
$contextMock = $this->createMock( PopupsContext::class );
|
2016-12-16 02:47:52 +00:00
|
|
|
$contextMock->expects( $this->once() )
|
|
|
|
->method( 'showPreviewsOptInOnPreferencesPage' )
|
|
|
|
->will( $this->returnValue( false ) );
|
|
|
|
|
2017-07-21 17:06:08 +00:00
|
|
|
$this->setService( 'Popups.Context', $contextMock );
|
2016-12-16 02:47:52 +00:00
|
|
|
$prefs = [ 'someNotEmptyValue' => 'notEmpty' ];
|
|
|
|
|
|
|
|
PopupsHooks::onGetPreferences( $this->getTestUser()->getUser(), $prefs );
|
2018-05-08 19:48:17 +00:00
|
|
|
$this->assertCount( 1, $prefs, 'No preferences are retrieved.' );
|
2019-10-04 08:31:58 +00:00
|
|
|
$this->assertSame( 'notEmpty',
|
2018-05-08 19:48:17 +00:00
|
|
|
$prefs[ 'someNotEmptyValue'],
|
|
|
|
'No preferences are changed.' );
|
2016-12-16 02:47:52 +00:00
|
|
|
}
|
|
|
|
|
2017-01-04 16:33:40 +00:00
|
|
|
/**
|
|
|
|
* @covers ::onGetPreferences
|
|
|
|
*/
|
|
|
|
public function testOnGetPreferencesNavPopupGadgetIsOn() {
|
|
|
|
$userMock = $this->getTestUser()->getUser();
|
|
|
|
|
2019-10-10 15:15:31 +00:00
|
|
|
$contextMock = $this->createMock( PopupsContext::class );
|
2017-01-04 16:33:40 +00:00
|
|
|
$contextMock->expects( $this->once() )
|
|
|
|
->method( 'showPreviewsOptInOnPreferencesPage' )
|
|
|
|
->will( $this->returnValue( true ) );
|
|
|
|
$contextMock->expects( $this->once() )
|
|
|
|
->method( 'conflictsWithNavPopupsGadget' )
|
|
|
|
->with( $userMock )
|
|
|
|
->will( $this->returnValue( true ) );
|
|
|
|
|
2017-07-21 17:06:08 +00:00
|
|
|
$this->setService( 'Popups.Context', $contextMock );
|
2017-01-04 16:33:40 +00:00
|
|
|
$prefs = [];
|
|
|
|
|
|
|
|
PopupsHooks::onGetPreferences( $this->getTestUser()->getUser(), $prefs );
|
2018-05-08 19:48:17 +00:00
|
|
|
$this->assertArrayHasKey( PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME,
|
|
|
|
$prefs,
|
|
|
|
'The opt-in preference is retrieved.' );
|
2017-01-04 16:33:40 +00:00
|
|
|
$this->assertArrayHasKey( 'disabled',
|
2018-05-08 19:48:17 +00:00
|
|
|
$prefs[ PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME ],
|
|
|
|
'The opt-in preference has a status.' );
|
2019-10-04 08:31:58 +00:00
|
|
|
$this->assertTrue(
|
2018-05-08 19:48:17 +00:00
|
|
|
$prefs[ PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME]['disabled'],
|
2019-03-01 10:28:20 +00:00
|
|
|
'The opt-in preference\'s status is disabled.' );
|
2018-05-08 19:48:17 +00:00
|
|
|
$this->assertNotEmpty( $prefs[ PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME]['help-message'],
|
|
|
|
'The opt-in preference has a help message.' );
|
2017-01-04 16:33:40 +00:00
|
|
|
}
|
|
|
|
|
2016-12-16 02:47:52 +00:00
|
|
|
/**
|
2016-12-20 21:54:47 +00:00
|
|
|
* @covers ::onGetPreferences
|
2016-12-16 02:47:52 +00:00
|
|
|
*/
|
|
|
|
public function testOnGetPreferencesPreviewsEnabled() {
|
2019-10-10 15:15:31 +00:00
|
|
|
$contextMock = $this->createMock( PopupsContext::class );
|
2016-12-16 02:47:52 +00:00
|
|
|
$contextMock->expects( $this->once() )
|
|
|
|
->method( 'showPreviewsOptInOnPreferencesPage' )
|
|
|
|
->will( $this->returnValue( true ) );
|
2017-01-11 21:52:07 +00:00
|
|
|
$contextMock->expects( $this->once() )
|
|
|
|
->method( 'conflictsWithNavPopupsGadget' )
|
|
|
|
->will( $this->returnValue( false ) );
|
2016-12-16 02:47:52 +00:00
|
|
|
|
2017-07-21 17:06:08 +00:00
|
|
|
$this->setService( 'Popups.Context', $contextMock );
|
2016-12-20 22:49:01 +00:00
|
|
|
$prefs = [
|
|
|
|
'skin' => 'skin stuff',
|
|
|
|
'someNotEmptyValue' => 'notEmpty',
|
|
|
|
'other' => 'notEmpty'
|
|
|
|
];
|
2016-12-16 02:47:52 +00:00
|
|
|
|
|
|
|
PopupsHooks::onGetPreferences( $this->getTestUser()->getUser(), $prefs );
|
2019-10-10 14:24:51 +00:00
|
|
|
$this->assertGreaterThan( 3, count( $prefs ), 'A preference is retrieved.' );
|
2019-10-04 08:31:58 +00:00
|
|
|
$this->assertSame( 'notEmpty',
|
2018-05-08 19:48:17 +00:00
|
|
|
$prefs[ 'someNotEmptyValue'],
|
|
|
|
'Unretrieved preferences are unchanged.' );
|
|
|
|
$this->assertArrayHasKey( PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME,
|
|
|
|
$prefs,
|
|
|
|
'The opt-in preference is retrieved.' );
|
2019-10-04 08:31:58 +00:00
|
|
|
$this->assertSame( 1,
|
2019-10-10 15:15:31 +00:00
|
|
|
array_search( PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME,
|
2018-05-08 19:48:17 +00:00
|
|
|
array_keys( $prefs ) ),
|
|
|
|
'The opt-in preference is injected after Skin select.' );
|
2016-12-20 22:49:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::onGetPreferences
|
|
|
|
*/
|
|
|
|
public function testOnGetPreferencesPreviewsEnabledWhenSkinIsNotAvailable() {
|
2019-10-10 15:15:31 +00:00
|
|
|
$contextMock = $this->createMock( PopupsContext::class );
|
2016-12-20 22:49:01 +00:00
|
|
|
$contextMock->expects( $this->once() )
|
|
|
|
->method( 'showPreviewsOptInOnPreferencesPage' )
|
|
|
|
->will( $this->returnValue( true ) );
|
2017-01-11 21:52:07 +00:00
|
|
|
$contextMock->expects( $this->once() )
|
|
|
|
->method( 'conflictsWithNavPopupsGadget' )
|
|
|
|
->will( $this->returnValue( false ) );
|
2016-12-20 22:49:01 +00:00
|
|
|
|
2017-07-21 17:06:08 +00:00
|
|
|
$this->setService( 'Popups.Context', $contextMock );
|
2016-12-20 22:49:01 +00:00
|
|
|
$prefs = [
|
|
|
|
'someNotEmptyValue' => 'notEmpty',
|
|
|
|
'other' => 'notEmpty'
|
|
|
|
];
|
|
|
|
|
|
|
|
PopupsHooks::onGetPreferences( $this->getTestUser()->getUser(), $prefs );
|
2019-10-10 14:24:51 +00:00
|
|
|
$this->assertGreaterThan( 2, count( $prefs ), 'A preference is retrieved.' );
|
2018-05-08 19:48:17 +00:00
|
|
|
$this->assertArrayHasKey( PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME,
|
|
|
|
$prefs,
|
|
|
|
'The opt-in preference is retrieved.' );
|
2019-10-04 08:31:58 +00:00
|
|
|
$this->assertSame( 2,
|
2018-05-08 19:48:17 +00:00
|
|
|
array_search( PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME,
|
|
|
|
array_keys( $prefs ) ),
|
|
|
|
'The opt-in preference is appended.' );
|
2016-12-16 02:47:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-20 21:54:47 +00:00
|
|
|
* @covers ::onResourceLoaderGetConfigVars
|
2016-12-16 02:47:52 +00:00
|
|
|
*/
|
|
|
|
public function testOnResourceLoaderGetConfigVars() {
|
|
|
|
$vars = [ 'something' => 'notEmpty' ];
|
2017-01-15 18:59:37 +00:00
|
|
|
$config = [
|
2017-08-10 17:55:26 +00:00
|
|
|
'wgPopupsEventLogging' => false,
|
2018-03-07 20:15:30 +00:00
|
|
|
'wgPopupsRestGatewayEndpoint' => '/api',
|
2018-02-08 22:11:44 +00:00
|
|
|
'wgPopupsVirtualPageViews' => true,
|
2017-06-08 13:29:57 +00:00
|
|
|
'wgPopupsGateway' => 'mwApiPlain',
|
2017-03-07 00:27:38 +00:00
|
|
|
'wgPopupsStatsvSamplingRate' => 0
|
2017-01-15 18:59:37 +00:00
|
|
|
];
|
|
|
|
$this->setMwGlobals( $config );
|
2019-02-26 11:15:54 +00:00
|
|
|
PopupsHooks::onResourceLoaderGetConfigVars( $vars, '' );
|
2019-02-20 16:43:15 +00:00
|
|
|
$this->assertCount( 6, $vars, 'A configuration is retrieved.' );
|
2017-01-15 18:59:37 +00:00
|
|
|
|
|
|
|
foreach ( $config as $key => $value ) {
|
2019-10-04 08:31:58 +00:00
|
|
|
$this->assertSame(
|
2017-01-15 18:59:37 +00:00
|
|
|
$value,
|
|
|
|
$vars[ $key ],
|
|
|
|
"It forwards the \"{$key}\" config variable to the client."
|
|
|
|
);
|
|
|
|
}
|
2016-12-16 02:47:52 +00:00
|
|
|
}
|
|
|
|
|
2016-12-20 21:54:47 +00:00
|
|
|
/**
|
|
|
|
* @covers ::onBeforePageDisplay
|
|
|
|
*/
|
|
|
|
public function testOnBeforePageDisplayWhenDependenciesAreNotMet() {
|
2019-10-06 05:43:20 +00:00
|
|
|
$skinMock = $this->createMock( Skin::class );
|
2019-10-10 15:15:31 +00:00
|
|
|
$outPageMock = $this->createMock( OutputPage::class );
|
2016-12-20 21:54:47 +00:00
|
|
|
$outPageMock->expects( $this->never() )
|
|
|
|
->method( 'addModules' );
|
2019-10-10 15:15:31 +00:00
|
|
|
$loggerMock = $this->createMock( LoggerInterface::class );
|
2016-12-20 21:54:47 +00:00
|
|
|
$loggerMock->expects( $this->once() )
|
|
|
|
->method( 'error' );
|
|
|
|
|
2019-10-10 15:15:31 +00:00
|
|
|
$contextMock = $this->createMock( PopupsContext::class );
|
2016-12-20 21:54:47 +00:00
|
|
|
$contextMock->expects( $this->once() )
|
|
|
|
->method( 'areDependenciesMet' )
|
|
|
|
->will( $this->returnValue( false ) );
|
2017-07-28 18:58:13 +00:00
|
|
|
$contextMock->expects( $this->once() )
|
|
|
|
->method( 'isTitleBlacklisted' )
|
|
|
|
->will( $this->returnValue( false ) );
|
2016-12-20 21:54:47 +00:00
|
|
|
$contextMock->expects( $this->once() )
|
|
|
|
->method( 'getLogger' )
|
|
|
|
->will( $this->returnValue( $loggerMock ) );
|
|
|
|
|
2017-07-21 17:06:08 +00:00
|
|
|
$this->setService( 'Popups.Context', $contextMock );
|
2016-12-20 21:54:47 +00:00
|
|
|
PopupsHooks::onBeforePageDisplay( $outPageMock, $skinMock );
|
|
|
|
}
|
|
|
|
|
2017-02-08 22:08:12 +00:00
|
|
|
public function providerOnBeforePageDisplay() {
|
|
|
|
return [
|
2018-04-26 20:43:05 +00:00
|
|
|
[ false, false, false ],
|
|
|
|
[ true, true, false ],
|
|
|
|
// Code not sent if title blacklisted
|
|
|
|
[ true, false, true ],
|
|
|
|
// Code not sent if title blacklisted
|
|
|
|
[ false, false, true ]
|
2017-02-08 22:08:12 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-12-20 21:54:47 +00:00
|
|
|
/**
|
|
|
|
* @covers ::onBeforePageDisplay
|
2017-02-08 22:08:12 +00:00
|
|
|
* @dataProvider providerOnBeforePageDisplay
|
2016-12-20 21:54:47 +00:00
|
|
|
*/
|
2017-05-16 17:59:29 +00:00
|
|
|
public function testOnBeforePageDisplay( $shouldSendModuleToUser,
|
2018-04-26 20:43:05 +00:00
|
|
|
$isCodeLoaded, $isTitleBlacklisted ) {
|
2019-10-06 05:43:20 +00:00
|
|
|
$skinMock = $this->createMock( Skin::class );
|
|
|
|
|
2019-10-10 15:15:31 +00:00
|
|
|
$outPageMock = $this->createMock( OutputPage::class );
|
2017-02-08 22:08:12 +00:00
|
|
|
$outPageMock->expects( $isCodeLoaded ? $this->once() : $this->never() )
|
2016-12-21 12:35:35 +00:00
|
|
|
->method( 'addModules' )
|
|
|
|
->with( [ 'ext.popups' ] );
|
2019-10-10 15:15:31 +00:00
|
|
|
$outPageMock->method( 'getUser' )
|
|
|
|
->willReturn( User::newFromId( 0 ) );
|
2016-12-20 21:54:47 +00:00
|
|
|
|
2019-10-10 15:15:31 +00:00
|
|
|
$contextMock = $this->createMock( PopupsContext::class );
|
2017-02-08 22:08:12 +00:00
|
|
|
|
2017-07-28 18:58:13 +00:00
|
|
|
if ( !$isTitleBlacklisted ) {
|
|
|
|
$contextMock->expects( $this->once() )
|
|
|
|
->method( 'areDependenciesMet' )
|
|
|
|
->will( $this->returnValue( true ) );
|
|
|
|
}
|
2016-12-20 21:54:47 +00:00
|
|
|
|
2017-02-08 22:08:12 +00:00
|
|
|
$contextMock->expects( $this->any() )
|
2017-02-10 00:27:05 +00:00
|
|
|
->method( 'shouldSendModuleToUser' )
|
|
|
|
->will( $this->returnValue( $shouldSendModuleToUser ) );
|
2017-02-08 22:08:12 +00:00
|
|
|
|
2017-07-28 18:58:13 +00:00
|
|
|
$contextMock->expects( $this->once() )
|
|
|
|
->method( 'isTitleBlacklisted' )
|
|
|
|
->will( $this->returnValue( $isTitleBlacklisted ) );
|
|
|
|
|
2017-07-21 17:06:08 +00:00
|
|
|
$this->setService( 'Popups.Context', $contextMock );
|
2016-12-20 21:54:47 +00:00
|
|
|
PopupsHooks::onBeforePageDisplay( $outPageMock, $skinMock );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-27 19:53:07 +00:00
|
|
|
* @covers ::onMakeGlobalVariablesScript
|
2016-12-20 21:54:47 +00:00
|
|
|
*/
|
2016-12-21 12:35:35 +00:00
|
|
|
public function testOnMakeGlobalVariablesScript() {
|
|
|
|
$user = User::newFromId( 0 );
|
2016-12-20 21:54:47 +00:00
|
|
|
|
2019-10-10 15:15:31 +00:00
|
|
|
$outputPage = $this->createMock( OutputPage::class );
|
2019-04-09 18:10:23 +00:00
|
|
|
$outputPage->expects( $this->any() )
|
2016-12-21 12:35:35 +00:00
|
|
|
->method( 'getUser' )
|
|
|
|
->willReturn( $user );
|
2016-12-20 21:54:47 +00:00
|
|
|
|
2019-10-10 15:15:31 +00:00
|
|
|
$contextMock = $this->createMock( PopupsContext::class );
|
2019-04-09 18:10:23 +00:00
|
|
|
$contextMock->expects( $this->any() )
|
2017-01-23 22:00:27 +00:00
|
|
|
->method( 'conflictsWithNavPopupsGadget' )
|
|
|
|
->with( $user )
|
|
|
|
->willReturn( false );
|
2019-10-10 14:24:51 +00:00
|
|
|
$contextMock->method( 'isReferencePreviewsEnabled' )
|
|
|
|
->with( $user )
|
|
|
|
->willReturn( true );
|
2016-12-20 21:54:47 +00:00
|
|
|
|
2017-07-21 17:06:08 +00:00
|
|
|
$this->setService( 'Popups.Context', $contextMock );
|
2016-12-21 12:35:35 +00:00
|
|
|
|
|
|
|
$vars = [];
|
|
|
|
PopupsHooks::onMakeGlobalVariablesScript( $vars, $outputPage );
|
|
|
|
|
2019-04-09 18:10:23 +00:00
|
|
|
$this->assertCount( 2, $vars, 'Number of added variables.' );
|
2018-05-08 19:48:17 +00:00
|
|
|
$this->assertFalse( $vars[ 'wgPopupsConflictsWithNavPopupGadget' ],
|
|
|
|
'The PopupsConflictsWithNavPopupGadget global is present and false.' );
|
2016-12-20 21:54:47 +00:00
|
|
|
}
|
2018-05-09 21:21:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::onUserGetDefaultOptions
|
2019-12-17 15:27:26 +00:00
|
|
|
* @dataProvider provideReferencePreviewsBetaFlag
|
2018-05-09 21:21:30 +00:00
|
|
|
*/
|
2019-12-17 15:27:26 +00:00
|
|
|
public function testOnUserGetDefaultOptions( $beta ) {
|
2018-05-09 21:21:30 +00:00
|
|
|
$userOptions = [
|
|
|
|
'test' => 'not_empty'
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->setMwGlobals( [
|
2019-12-17 10:58:36 +00:00
|
|
|
'wgPopupsOptInDefaultState' => '1',
|
|
|
|
'wgPopupsReferencePreviews' => true,
|
2019-12-17 15:27:26 +00:00
|
|
|
'wgPopupsReferencePreviewsBetaFeature' => $beta,
|
2018-05-09 21:21:30 +00:00
|
|
|
] );
|
|
|
|
|
|
|
|
PopupsHooks::onUserGetDefaultOptions( $userOptions );
|
2019-12-17 15:27:26 +00:00
|
|
|
$this->assertCount( 3 - $beta, $userOptions );
|
2019-10-10 15:15:31 +00:00
|
|
|
$this->assertSame( '1', $userOptions[ PopupsContext::PREVIEWS_OPTIN_PREFERENCE_NAME ] );
|
2018-05-09 21:21:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::onUserGetDefaultOptions
|
2019-12-17 15:27:26 +00:00
|
|
|
* @dataProvider provideReferencePreviewsBetaFlag
|
2018-05-09 21:21:30 +00:00
|
|
|
*/
|
2019-12-17 15:27:26 +00:00
|
|
|
public function testOnLocalUserCreatedForNewlyCreatedUser( $beta ) {
|
2018-05-09 21:21:30 +00:00
|
|
|
$expectedState = '1';
|
|
|
|
|
2019-10-10 15:15:31 +00:00
|
|
|
$userMock = $this->createMock( User::class );
|
2019-12-17 15:27:26 +00:00
|
|
|
$userMock->expects( $this->exactly( 2 - $beta ) )
|
2018-05-09 21:21:30 +00:00
|
|
|
->method( 'setOption' )
|
2019-12-17 10:58:36 +00:00
|
|
|
->withConsecutive(
|
|
|
|
[ 'popups', $expectedState ],
|
|
|
|
[ 'popupsreferencepreviews', $expectedState ]
|
|
|
|
);
|
2018-05-09 21:21:30 +00:00
|
|
|
|
|
|
|
$this->setMwGlobals( [
|
2019-12-17 10:58:36 +00:00
|
|
|
'wgPopupsOptInStateForNewAccounts' => $expectedState,
|
|
|
|
'wgPopupsReferencePreviews' => true,
|
2019-12-17 15:27:26 +00:00
|
|
|
'wgPopupsReferencePreviewsBetaFeature' => $beta,
|
2018-05-09 21:21:30 +00:00
|
|
|
] );
|
|
|
|
PopupsHooks::onLocalUserCreated( $userMock, false );
|
|
|
|
}
|
|
|
|
|
2019-12-17 15:27:26 +00:00
|
|
|
public function provideReferencePreviewsBetaFlag() {
|
|
|
|
return [
|
|
|
|
[ false ],
|
|
|
|
[ true ],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-12-16 02:47:52 +00:00
|
|
|
}
|