diff --git a/i18n/en.json b/i18n/en.json index ddbe41c..90aade8 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -6,5 +6,6 @@ }, "cookiewarning-info": "Cookies help us deliver our services. By using our services, you agree to our use of cookies.", "cookiewarning-desc": "Provides a notice at the top of the page about cookies.", - "cookiewarning-moreinfo-label": "More information" + "cookiewarning-moreinfo-label": "More information", + "cookiewarning-more-link": "-" } diff --git a/i18n/qqq.json b/i18n/qqq.json index 691a8c3..5130213 100644 --- a/i18n/qqq.json +++ b/i18n/qqq.json @@ -6,5 +6,6 @@ }, "cookiewarning-info": "Information message for visitors of the wiki, informing them that this wiki uses cookies.", "cookiewarning-desc": "{{desc|name=CookieWarning|url=https://www.mediawiki.org/wiki/Extension:CookieWarning}}", - "cookiewarning-moreinfo-label": "Label for the \"More information\" link in the cookiewarning information bar." + "cookiewarning-moreinfo-label": "Label for the \"More information\" link in the cookiewarning information bar.", + "cookiewarning-more-link": "{{notranslate}}\nUsed as an on-wiki way to edit the target of the more information link." } diff --git a/includes/CookieWarning.hooks.php b/includes/CookieWarning.hooks.php index 6826bd3..feef6ef 100644 --- a/includes/CookieWarning.hooks.php +++ b/includes/CookieWarning.hooks.php @@ -12,15 +12,13 @@ class CookieWarningHooks { public static function onSkinTemplateOutputPageBeforeExec( SkinTemplate &$sk, QuickTemplate &$tpl ) { - // Config instance of CookieWarning - $conf = ConfigFactory::getDefaultInstance()->makeConfig( 'cookiewarning' ); - $moreLink = ''; + $moreLink = self::getMoreLink(); // if a "more information" URL was configured, add a link to it in the cookiewarning // information bar - if ( $conf->get( 'CookieWarningMoreUrl' ) ) { + if ( $moreLink ) { $moreLink = Html::element( 'a', - array( 'href' => $conf->get( 'CookieWarningMoreUrl' ) ), + array( 'href' => $moreLink ), $sk->msg( 'cookiewarning-moreinfo-label' )->text() ); } @@ -51,6 +49,32 @@ class CookieWarningHooks { } } + /** + * Returns the target for the "More information" link of the cookie warning bar, if one is set. + * The link can be set by either (checked in this order): + * - the configuration variable $wgCookieWarningMoreUrl + * - the interface message MediaWiki:Cookiewarning-more-link + * - the interface message MediaWiki:Cookie-policy-link (bc T145781) + * + * @return string|null The url or null if none set + */ + private static function getMoreLink() { + // Config instance of CookieWarning + $conf = ConfigFactory::getDefaultInstance()->makeConfig( 'cookiewarning' ); + if ( $conf->get( 'CookieWarningMoreUrl' ) ) { + return $conf->get( 'CookieWarningMoreUrl' ); + } + $cookieWarningMessage = wfMessage( 'cookiewarning-more-link' ); + if ( $cookieWarningMessage->exists() && !$cookieWarningMessage->isDisabled() ) { + return $cookieWarningMessage->escaped(); + } + $cookiePolicyMessage = wfMessage( 'cookie-policy-link' ); + if ( $cookiePolicyMessage->exists() && !$cookiePolicyMessage->isDisabled() ) { + return $cookiePolicyMessage->escaped(); + } + return null; + } + /** * BeforePageDisplay hook handler. * diff --git a/tests/phpunit/includes/CookieWarning.hooksTest.php b/tests/phpunit/includes/CookieWarning.hooksTest.php new file mode 100644 index 0000000..a5c3095 --- /dev/null +++ b/tests/phpunit/includes/CookieWarning.hooksTest.php @@ -0,0 +1,124 @@ +enable(); + } + + /** + * @dataProvider providerOnSkinTemplateOutputPageBeforeExec + */ + public function testOnSkinTemplateOutputPageBeforeExec( $enabled, $morelinkConfig, + $morelinkCookieWarningMsg, $morelinkCookiePolicyMsg, $expectedLink + ) { + $this->setMwGlobals( array( + 'wgCookieWarningEnabled' => $enabled, + 'wgCookieWarningMoreUrl' => $morelinkConfig, + ) ); + 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, + '
Cookies help us deliver our services. By using our services, you agree to our use of cookies.$1OK
' ); + // @codingStandardsIgnoreEnd + } + $this->assertEquals( $expected, $headElement ); + } + + public function providerOnSkinTemplateOutputPageBeforeExec() { + return array( + array( + // $wgCookieWarningEnabled + true, + // $wgCookieWarningMoreUrl + '', + // MediaWiki:Cookiewarning-more-link + false, + // MediaWiki:Cookie-policy-link + false, + // expected cookie warning link (when string), nothing if false + '', + ), + array( + false, + '', + false, + false, + false, + ), + array( + true, + 'http://google.de', + false, + false, + 'More information', + ), + array( + true, + '', + 'http://google.de', + false, + 'More information', + ), + array( + true, + '', + false, + 'http://google.de', + 'More information', + ), + // the config should be the used, if set (no matter if the messages are used or not) + array( + true, + 'http://google.de', + false, + 'http://google123.de', + 'More information', + ), + array( + true, + 'http://google.de', + 'http://google1234.de', + 'http://google123.de', + 'More information', + ), + array( + true, + '', + 'http://google.de', + 'http://google123.de', + 'More information', + ), + ); + } +} + +class CookieWarningTestTemplate extends BaseTemplate { + public function execute() { + return; + } +} \ No newline at end of file