2016-09-15 21:07:08 +00:00
< ? php
2018-07-19 10:48:17 +00:00
namespace CookieWarning\Tests ;
2019-11-14 14:10:56 +00:00
use CommentStoreComment ;
2018-07-19 10:48:17 +00:00
use CookieWarning\GeoLocation ;
use CookieWarning\Hooks ;
use DerivativeContext ;
use FauxRequest ;
2019-11-14 14:10:56 +00:00
use MediaWiki\MediaWikiServices ;
use MediaWiki\Revision\SlotRecord ;
2018-07-19 10:48:17 +00:00
use MediaWikiLangTestCase ;
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 {
2016-09-15 21:07:08 +00:00
/**
2020-10-03 18:13:17 +00:00
* @ dataProvider providerOnSiteNoticeAfter
2018-07-19 10:48:17 +00:00
* @ throws \MWException
* @ throws \ConfigException
2016-09-15 21:07:08 +00:00
*/
2020-10-03 18:13:17 +00:00
public function testOnSiteNoticeAfter ( $enabled , $morelinkConfig ,
2016-09-15 21:07:08 +00:00
$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
] );
2019-11-14 14:10:56 +00:00
MediaWikiServices :: getInstance () -> getMessageCache () -> enable ();
2016-09-15 21:07:08 +00:00
if ( $morelinkCookieWarningMsg ) {
$title = Title :: newFromText ( 'cookiewarning-more-link' , NS_MEDIAWIKI );
$wikiPage = WikiPage :: factory ( $title );
2019-11-14 14:10:56 +00:00
$pageUpdater = $wikiPage -> newPageUpdater ( \User :: newFromName ( 'UTSysop' ) );
$pageUpdater -> setContent ( SlotRecord :: MAIN , new WikitextContent ( $morelinkCookieWarningMsg ) );
$pageUpdater -> saveRevision ( CommentStoreComment :: newUnsavedComment ( 'CookieWarning test' ) );
2016-09-15 21:07:08 +00:00
}
if ( $morelinkCookiePolicyMsg ) {
$title = Title :: newFromText ( 'cookie-policy-link' , NS_MEDIAWIKI );
$wikiPage = WikiPage :: factory ( $title );
2019-11-14 14:10:56 +00:00
$pageUpdater = $wikiPage -> newPageUpdater ( \User :: newFromName ( 'UTSysop' ) );
$pageUpdater -> setContent ( SlotRecord :: MAIN , new WikitextContent ( $morelinkCookiePolicyMsg ) );
$pageUpdater -> saveRevision ( CommentStoreComment :: newUnsavedComment ( 'CookieWarning test' ) );
2016-09-15 21:07:08 +00:00
}
$sk = new SkinTemplate ();
2020-10-03 18:13:17 +00:00
$data = '' ;
Hooks :: onSiteNoticeAfter ( $data , $sk );
2016-09-15 21:07:08 +00:00
if ( $expectedLink === false ) {
$expected = '' ;
} else {
// @codingStandardsIgnoreStart Generic.Files.LineLength
$expected =
str_replace ( '$1' , $expectedLink ,
2020-10-03 18:13:17 +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</div><form method="POST"><input name="disablecookiewarning" class="mw-cookiewarning-dismiss mw-ui-button" type="submit" value="OK"/></form></div>' );
2016-09-15 21:07:08 +00:00
// @codingStandardsIgnoreEnd
}
2020-10-03 18:13:17 +00:00
$this -> assertEquals ( $expected , $data );
2016-09-15 21:07:08 +00:00
}
2020-10-03 18:13:17 +00:00
public function providerOnSiteNoticeAfter () {
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
/**
2020-10-03 18:13:17 +00:00
* @ dataProvider providerOnSiteNoticeAfterGeoLocation
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
*/
2020-10-03 18:13:17 +00:00
public function testOnSiteNoticeAfterGeoLocation ( $ipAddress , $countryCodes ,
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
$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 );
2020-10-03 18:13:17 +00:00
$data = '' ;
Hooks :: onSiteNoticeAfter ( $data , $sk );
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 ,
2020-10-03 18:13:17 +00:00
( bool ) $data
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
);
}
2020-10-03 18:13:17 +00:00
public function providerOnSiteNoticeAfterGeoLocation () {
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
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 ();
2018-11-04 15:15:42 +00:00
$geoLocation -> method ( 'locate' ) -> willReturn ( 'US' );
2018-07-18 09:50:46 +00:00
$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
}