setMwGlobals( [ 'wgCookieWarningEnabled' => $enabled, 'wgCookieWarningMoreUrl' => $morelinkConfig, 'wgCookieWarningForCountryCodes' => false, 'wgUseMediaWikiUIEverywhere' => true, ] ); MediaWikiServices::getInstance()->getMessageCache()->enable(); if ( $morelinkCookieWarningMsg ) { $title = Title::newFromText( 'cookiewarning-more-link', NS_MEDIAWIKI ); $wikiPage = WikiPage::factory( $title ); $pageUpdater = $wikiPage->newPageUpdater( \User::newFromName( 'UTSysop' ) ); $pageUpdater->setContent( SlotRecord::MAIN, new WikitextContent( $morelinkCookieWarningMsg ) ); $pageUpdater->saveRevision( CommentStoreComment::newUnsavedComment( 'CookieWarning test' ) ); } if ( $morelinkCookiePolicyMsg ) { $title = Title::newFromText( 'cookie-policy-link', NS_MEDIAWIKI ); $wikiPage = WikiPage::factory( $title ); $pageUpdater = $wikiPage->newPageUpdater( \User::newFromName( 'UTSysop' ) ); $pageUpdater->setContent( SlotRecord::MAIN, new WikitextContent( $morelinkCookiePolicyMsg ) ); $pageUpdater->saveRevision( CommentStoreComment::newUnsavedComment( 'CookieWarning test' ) ); } $sk = new SkinTemplate(); $data = ''; Hooks::onSiteNoticeAfter( $data, $sk ); if ( $expectedLink === false ) { $expected = ''; } else { // @codingStandardsIgnoreStart Generic.Files.LineLength $expected = str_replace( '$1', $expectedLink, '' ); // @codingStandardsIgnoreEnd } $this->assertEquals( $expected, $data ); } public function providerOnSiteNoticeAfter() { 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, "\u{00A0}More information", ], [ true, '', 'http://google.de', false, "\u{00A0}More information", ], [ true, '', false, 'http://google.de', "\u{00A0}More information", ], // 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', "\u{00A0}More information", ], [ true, 'http://google.de', 'http://google1234.de', 'http://google123.de', "\u{00A0}More information", ], [ true, '', 'http://google.de', 'http://google123.de', "\u{00A0}More information", ], ]; } /** * @dataProvider providerOnSiteNoticeAfterGeoLocation * @throws \MWException * @throws \ConfigException */ public function testOnSiteNoticeAfterGeoLocation( $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 ); $data = ''; Hooks::onSiteNoticeAfter( $data, $sk ); $this->assertEquals( $expected, (bool)$data ); } public function providerOnSiteNoticeAfterGeoLocation() { 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( 'US' ); $this->setService( 'GeoLocation', $geoLocation ); } }