mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/CookieWarning
synced 2024-11-23 22:03:41 +00:00
fed15fd7a5
This cleans up the hook file a lot and movesthe logic to seperate services provided by MediaWikiServices. This also removes some setters and passed- around variables and stuff. Also fixes the unit tests by not querying external services anymore. Change-Id: I0c3c0b7f2f5bd68aaad624e8e2dcad9bdcf97770
178 lines
4.7 KiB
PHP
178 lines
4.7 KiB
PHP
<?php
|
|
/**
|
|
* @covers CookieWarningHooks
|
|
* @group Database
|
|
*/
|
|
class CookieWarningHooksTest extends MediaWikiLangTestCase {
|
|
protected function setUp() {
|
|
parent::setUp();
|
|
MessageCache::singleton()->enable();
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerOnSkinTemplateOutputPageBeforeExec
|
|
*/
|
|
public function testOnSkinTemplateOutputPageBeforeExec( $enabled, $morelinkConfig,
|
|
$morelinkCookieWarningMsg, $morelinkCookiePolicyMsg, $expectedLink
|
|
) {
|
|
$this->setMwGlobals( [
|
|
'wgCookieWarningEnabled' => $enabled,
|
|
'wgCookieWarningMoreUrl' => $morelinkConfig,
|
|
'wgCookieWarningForCountryCodes' => false,
|
|
] );
|
|
if ( $morelinkCookieWarningMsg ) {
|
|
$title = Title::newFromText( 'cookiewarning-more-link', NS_MEDIAWIKI );
|
|
$wikiPage = WikiPage::factory( $title );
|
|
$wikiPage->doEditContent( new WikitextContent( $morelinkCookieWarningMsg ),
|
|
"CookieWarning test" );
|
|
}
|
|
if ( $morelinkCookiePolicyMsg ) {
|
|
$title = Title::newFromText( 'cookie-policy-link', NS_MEDIAWIKI );
|
|
$wikiPage = WikiPage::factory( $title );
|
|
$wikiPage->doEditContent( new WikitextContent( $morelinkCookiePolicyMsg ),
|
|
"CookieWarning test" );
|
|
}
|
|
$sk = new SkinTemplate();
|
|
$tpl = new CookieWarningTestTemplate();
|
|
CookieWarningHooks::onSkinTemplateOutputPageBeforeExec( $sk, $tpl );
|
|
$headElement = '';
|
|
if ( isset( $tpl->data['headelement'] ) ) {
|
|
$headElement = $tpl->data['headelement'];
|
|
}
|
|
if ( $expectedLink === false ) {
|
|
$expected = '';
|
|
} else {
|
|
// @codingStandardsIgnoreStart Generic.Files.LineLength
|
|
$expected =
|
|
str_replace( '$1', $expectedLink,
|
|
'<div class="mw-cookiewarning-container banner-container"><div class="mw-cookiewarning-text"><span>Cookies help us deliver our services. By using our services, you agree to our use of cookies.</span>$1<form method="POST"><input name="disablecookiewarning" class="mw-cookiewarning-dismiss" type="submit" value="OK"/></form></div></div>' );
|
|
// @codingStandardsIgnoreEnd
|
|
}
|
|
$this->assertEquals( $expected, $headElement );
|
|
}
|
|
|
|
public function providerOnSkinTemplateOutputPageBeforeExec() {
|
|
return [
|
|
[
|
|
// $wgCookieWarningEnabled
|
|
true,
|
|
// $wgCookieWarningMoreUrl
|
|
'',
|
|
// MediaWiki:Cookiewarning-more-link
|
|
false,
|
|
// MediaWiki:Cookie-policy-link
|
|
false,
|
|
// expected cookie warning link (when string), nothing if false
|
|
'',
|
|
],
|
|
[
|
|
false,
|
|
'',
|
|
false,
|
|
false,
|
|
false,
|
|
],
|
|
[
|
|
true,
|
|
'http://google.de',
|
|
false,
|
|
false,
|
|
' <a href="http://google.de">More information</a>',
|
|
],
|
|
[
|
|
true,
|
|
'',
|
|
'http://google.de',
|
|
false,
|
|
' <a href="http://google.de">More information</a>',
|
|
],
|
|
[
|
|
true,
|
|
'',
|
|
false,
|
|
'http://google.de',
|
|
' <a href="http://google.de">More information</a>',
|
|
],
|
|
// the config should be the used, if set (no matter if the messages are used or not)
|
|
[
|
|
true,
|
|
'http://google.de',
|
|
false,
|
|
'http://google123.de',
|
|
' <a href="http://google.de">More information</a>',
|
|
],
|
|
[
|
|
true,
|
|
'http://google.de',
|
|
'http://google1234.de',
|
|
'http://google123.de',
|
|
' <a href="http://google.de">More information</a>',
|
|
],
|
|
[
|
|
true,
|
|
'',
|
|
'http://google.de',
|
|
'http://google123.de',
|
|
' <a href="http://google.de">More information</a>',
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerOnSkinTemplateOutputPageBeforeExecGeoLocation
|
|
*/
|
|
public function testOnSkinTemplateOutputPageBeforeExecGeoLocation( $ipAddress, $countryCodes,
|
|
$expected
|
|
) {
|
|
$this->setMwGlobals( [
|
|
'wgCookieWarningEnabled' => true,
|
|
'wgCookieWarningGeoIPLookup' => is_array( $countryCodes ) ? 'php' : 'none',
|
|
'wgCookieWarningForCountryCodes' => $countryCodes,
|
|
] );
|
|
$this->mockGeoLocationService();
|
|
|
|
$request = new FauxRequest();
|
|
$request->setIP( $ipAddress );
|
|
$context = new DerivativeContext( RequestContext::getMain() );
|
|
$context->setRequest( $request );
|
|
$sk = new SkinTemplate();
|
|
$sk->setContext( $context );
|
|
$tpl = new CookieWarningTestTemplate();
|
|
CookieWarningHooks::onSkinTemplateOutputPageBeforeExec( $sk, $tpl );
|
|
|
|
$this->assertEquals(
|
|
$expected,
|
|
isset( $tpl->data['headelement'] ) && (bool)$tpl->data['headelement']
|
|
);
|
|
}
|
|
|
|
public function providerOnSkinTemplateOutputPageBeforeExecGeoLocation() {
|
|
return [
|
|
[
|
|
'8.8.8.8',
|
|
[ 'US' => 'United States of America' ],
|
|
true,
|
|
],
|
|
[
|
|
'8.8.8.8',
|
|
[ 'EU' => 'European Union' ],
|
|
false,
|
|
],
|
|
[
|
|
'8.8.8.8',
|
|
false,
|
|
true,
|
|
],
|
|
];
|
|
}
|
|
|
|
private function mockGeoLocationService() {
|
|
$geoLocation = $this->getMockBuilder( GeoLocation::class )
|
|
->disableOriginalConstructor()
|
|
->getMock();
|
|
$geoLocation->method( 'locate' )->willReturn( true );
|
|
$geoLocation->method( 'getCountryCode' )->willReturn( 'US' );
|
|
$this->setService( 'GeoLocation', $geoLocation );
|
|
}
|
|
}
|