2016-09-15 21:07:08 +00:00
< ? php
2018-07-19 10:48:17 +00:00
namespace CookieWarning\Tests ;
use CookieWarning\GeoLocation ;
use CookieWarning\Hooks ;
use DerivativeContext ;
use FauxRequest ;
use MediaWikiLangTestCase ;
use MessageCache ;
use RequestContext ;
use SkinTemplate ;
use Title ;
use WikiPage ;
use WikitextContent ;
2016-09-15 21:07:08 +00:00
/**
2018-07-19 10:48:17 +00:00
* @ covers Hooks
2016-09-15 21:07:08 +00:00
* @ group Database
*/
2018-07-19 10:48:17 +00:00
class HooksTest extends MediaWikiLangTestCase {
/**
* @ throws \MWException
*/
2016-09-15 21:07:08 +00:00
protected function setUp () {
parent :: setUp ();
MessageCache :: singleton () -> enable ();
}
/**
* @ dataProvider providerOnSkinTemplateOutputPageBeforeExec
2018-07-19 10:48:17 +00:00
* @ throws \MWException
* @ throws \ConfigException
2016-09-15 21:07:08 +00:00
*/
public function testOnSkinTemplateOutputPageBeforeExec ( $enabled , $morelinkConfig ,
$morelinkCookieWarningMsg , $morelinkCookiePolicyMsg , $expectedLink
) {
2016-09-14 15:23:11 +00:00
$this -> setMwGlobals ( [
2016-09-15 21:07:08 +00:00
'wgCookieWarningEnabled' => $enabled ,
'wgCookieWarningMoreUrl' => $morelinkConfig ,
Add option to limit the cookie warning to specific regions
With this change, CookieWarning gets two new ways to limit the warning
bar to users of specific (configurable) regions. Based on the geo location
service (which can be configured, too, defaults to freegeoip.net), the
extension will try to get the region of the user (based on the IP address)
and checks it against the configured regions.
With the configuration variable $wgCookieWarningGeoIPLookup, the wiki sysadmin
can configure, if the lookup should be done with PHP (the timeout for the
request is hardcoded to 2 sedonds max), which will save the paylod sent to
the user, or with JavaScript, which will be asynchronous.
Deactivating this feature is easily done by simply setting the configured regions
to false, instead of an object.
Bug: T145780
Change-Id: I4d9371b8608ef270c11c42bdb3a773675977ab3a
2016-09-16 19:18:49 +00:00
'wgCookieWarningForCountryCodes' => false ,
2018-08-06 20:22:32 +00:00
'wgUseMediaWikiUIEverywhere' => true ,
2016-09-14 15:23:11 +00:00
] );
2016-09-15 21:07:08 +00:00
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 ();
2018-07-19 10:48:17 +00:00
$tpl = new \SkinFallbackTemplate ();
Hooks :: onSkinTemplateOutputPageBeforeExec ( $sk , $tpl );
2016-09-15 21:07:08 +00:00
$headElement = '' ;
if ( isset ( $tpl -> data [ 'headelement' ] ) ) {
$headElement = $tpl -> data [ 'headelement' ];
}
if ( $expectedLink === false ) {
$expected = '' ;
} else {
// @codingStandardsIgnoreStart Generic.Files.LineLength
$expected =
str_replace ( '$1' , $expectedLink ,
2018-08-06 20:22:32 +00:00
'<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 mw-ui-button" type="submit" value="OK"/></form></div></div>' );
2016-09-15 21:07:08 +00:00
// @codingStandardsIgnoreEnd
}
$this -> assertEquals ( $expected , $headElement );
}
public function providerOnSkinTemplateOutputPageBeforeExec () {
2016-09-14 15:23:11 +00:00
return [
[
2016-09-15 21:07:08 +00:00
// $wgCookieWarningEnabled
true ,
// $wgCookieWarningMoreUrl
'' ,
// MediaWiki:Cookiewarning-more-link
false ,
// MediaWiki:Cookie-policy-link
false ,
// expected cookie warning link (when string), nothing if false
'' ,
2016-09-14 15:23:11 +00:00
],
[
2016-09-15 21:07:08 +00:00
false ,
'' ,
false ,
false ,
false ,
2016-09-14 15:23:11 +00:00
],
[
2016-09-15 21:07:08 +00:00
true ,
'http://google.de' ,
false ,
false ,
2018-08-10 18:20:42 +00:00
" \ u { 00A0}<a href= \" http://google.de \" >More information</a> " ,
2016-09-14 15:23:11 +00:00
],
[
2016-09-15 21:07:08 +00:00
true ,
'' ,
'http://google.de' ,
false ,
2018-08-10 18:20:42 +00:00
" \ u { 00A0}<a href= \" http://google.de \" >More information</a> " ,
2016-09-14 15:23:11 +00:00
],
[
2016-09-15 21:07:08 +00:00
true ,
'' ,
false ,
'http://google.de' ,
2018-08-10 18:20:42 +00:00
" \ u { 00A0}<a href= \" http://google.de \" >More information</a> " ,
2016-09-14 15:23:11 +00:00
],
2016-09-15 21:07:08 +00:00
// the config should be the used, if set (no matter if the messages are used or not)
2016-09-14 15:23:11 +00:00
[
2016-09-15 21:07:08 +00:00
true ,
'http://google.de' ,
false ,
'http://google123.de' ,
2018-08-10 18:20:42 +00:00
" \ u { 00A0}<a href= \" http://google.de \" >More information</a> " ,
2016-09-14 15:23:11 +00:00
],
[
2016-09-15 21:07:08 +00:00
true ,
'http://google.de' ,
'http://google1234.de' ,
'http://google123.de' ,
2018-08-10 18:20:42 +00:00
" \ u { 00A0}<a href= \" http://google.de \" >More information</a> " ,
2016-09-14 15:23:11 +00:00
],
[
2016-09-15 21:07:08 +00:00
true ,
'' ,
'http://google.de' ,
'http://google123.de' ,
2018-08-10 18:20:42 +00:00
" \ u { 00A0}<a href= \" http://google.de \" >More information</a> " ,
2016-09-14 15:23:11 +00:00
],
];
2016-09-15 21:07:08 +00:00
}
Add option to limit the cookie warning to specific regions
With this change, CookieWarning gets two new ways to limit the warning
bar to users of specific (configurable) regions. Based on the geo location
service (which can be configured, too, defaults to freegeoip.net), the
extension will try to get the region of the user (based on the IP address)
and checks it against the configured regions.
With the configuration variable $wgCookieWarningGeoIPLookup, the wiki sysadmin
can configure, if the lookup should be done with PHP (the timeout for the
request is hardcoded to 2 sedonds max), which will save the paylod sent to
the user, or with JavaScript, which will be asynchronous.
Deactivating this feature is easily done by simply setting the configured regions
to false, instead of an object.
Bug: T145780
Change-Id: I4d9371b8608ef270c11c42bdb3a773675977ab3a
2016-09-16 19:18:49 +00:00
/**
* @ dataProvider providerOnSkinTemplateOutputPageBeforeExecGeoLocation
2018-07-19 10:48:17 +00:00
* @ throws \MWException
* @ throws \ConfigException
Add option to limit the cookie warning to specific regions
With this change, CookieWarning gets two new ways to limit the warning
bar to users of specific (configurable) regions. Based on the geo location
service (which can be configured, too, defaults to freegeoip.net), the
extension will try to get the region of the user (based on the IP address)
and checks it against the configured regions.
With the configuration variable $wgCookieWarningGeoIPLookup, the wiki sysadmin
can configure, if the lookup should be done with PHP (the timeout for the
request is hardcoded to 2 sedonds max), which will save the paylod sent to
the user, or with JavaScript, which will be asynchronous.
Deactivating this feature is easily done by simply setting the configured regions
to false, instead of an object.
Bug: T145780
Change-Id: I4d9371b8608ef270c11c42bdb3a773675977ab3a
2016-09-16 19:18:49 +00:00
*/
public function testOnSkinTemplateOutputPageBeforeExecGeoLocation ( $ipAddress , $countryCodes ,
$expected
) {
$this -> setMwGlobals ( [
'wgCookieWarningEnabled' => true ,
'wgCookieWarningGeoIPLookup' => is_array ( $countryCodes ) ? 'php' : 'none' ,
'wgCookieWarningForCountryCodes' => $countryCodes ,
] );
2018-07-18 09:50:46 +00:00
$this -> mockGeoLocationService ();
Add option to limit the cookie warning to specific regions
With this change, CookieWarning gets two new ways to limit the warning
bar to users of specific (configurable) regions. Based on the geo location
service (which can be configured, too, defaults to freegeoip.net), the
extension will try to get the region of the user (based on the IP address)
and checks it against the configured regions.
With the configuration variable $wgCookieWarningGeoIPLookup, the wiki sysadmin
can configure, if the lookup should be done with PHP (the timeout for the
request is hardcoded to 2 sedonds max), which will save the paylod sent to
the user, or with JavaScript, which will be asynchronous.
Deactivating this feature is easily done by simply setting the configured regions
to false, instead of an object.
Bug: T145780
Change-Id: I4d9371b8608ef270c11c42bdb3a773675977ab3a
2016-09-16 19:18:49 +00:00
$request = new FauxRequest ();
$request -> setIP ( $ipAddress );
$context = new DerivativeContext ( RequestContext :: getMain () );
$context -> setRequest ( $request );
$sk = new SkinTemplate ();
$sk -> setContext ( $context );
2018-07-19 10:48:17 +00:00
$tpl = new \SkinFallbackTemplate ();
Hooks :: onSkinTemplateOutputPageBeforeExec ( $sk , $tpl );
Add option to limit the cookie warning to specific regions
With this change, CookieWarning gets two new ways to limit the warning
bar to users of specific (configurable) regions. Based on the geo location
service (which can be configured, too, defaults to freegeoip.net), the
extension will try to get the region of the user (based on the IP address)
and checks it against the configured regions.
With the configuration variable $wgCookieWarningGeoIPLookup, the wiki sysadmin
can configure, if the lookup should be done with PHP (the timeout for the
request is hardcoded to 2 sedonds max), which will save the paylod sent to
the user, or with JavaScript, which will be asynchronous.
Deactivating this feature is easily done by simply setting the configured regions
to false, instead of an object.
Bug: T145780
Change-Id: I4d9371b8608ef270c11c42bdb3a773675977ab3a
2016-09-16 19:18:49 +00:00
$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 ,
],
];
}
2018-07-18 09:50:46 +00:00
private function mockGeoLocationService () {
$geoLocation = $this -> getMockBuilder ( GeoLocation :: class )
-> disableOriginalConstructor ()
-> getMock ();
$geoLocation -> method ( 'locate' ) -> willReturn ( true );
$geoLocation -> method ( 'getCountryCode' ) -> willReturn ( 'US' );
$this -> setService ( 'GeoLocation' , $geoLocation );
Add option to limit the cookie warning to specific regions
With this change, CookieWarning gets two new ways to limit the warning
bar to users of specific (configurable) regions. Based on the geo location
service (which can be configured, too, defaults to freegeoip.net), the
extension will try to get the region of the user (based on the IP address)
and checks it against the configured regions.
With the configuration variable $wgCookieWarningGeoIPLookup, the wiki sysadmin
can configure, if the lookup should be done with PHP (the timeout for the
request is hardcoded to 2 sedonds max), which will save the paylod sent to
the user, or with JavaScript, which will be asynchronous.
Deactivating this feature is easily done by simply setting the configured regions
to false, instead of an object.
Bug: T145780
Change-Id: I4d9371b8608ef270c11c42bdb3a773675977ab3a
2016-09-16 19:18:49 +00:00
}
2016-09-15 21:07:08 +00:00
}