From 243e0f5d6ced10dbd5c2a82884db4ccb5787c03f Mon Sep 17 00:00:00 2001 From: Aryeh Gregor Date: Mon, 23 Jul 2018 21:26:32 +0300 Subject: [PATCH] Improve test coverage for OutputPage Raises coverage locally from 16.10% to 26.81%. Change-Id: Ib564624c644ee6620ac06872f5684831acaaaadb Depends-On: I79c4e37092958c63a693194b27a9eafae70cb2f8 --- includes/OutputPage.php | 2 +- tests/phpunit/includes/OutputPageTest.php | 1405 +++++++++++++++------ 2 files changed, 996 insertions(+), 411 deletions(-) diff --git a/includes/OutputPage.php b/includes/OutputPage.php index 2e091f1062..a1c59192c8 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -780,7 +780,7 @@ class OutputPage extends ContextSource { # this breaks strtotime(). $clientHeader = preg_replace( '/;.*$/', '', $clientHeader ); - Wikimedia\suppressWarnings(); // E_STRICT system time bitching + Wikimedia\suppressWarnings(); // E_STRICT system time warnings $clientHeaderTime = strtotime( $clientHeader ); Wikimedia\restoreWarnings(); if ( !$clientHeaderTime ) { diff --git a/tests/phpunit/includes/OutputPageTest.php b/tests/phpunit/includes/OutputPageTest.php index 73447c95c8..d3c7af515c 100644 --- a/tests/phpunit/includes/OutputPageTest.php +++ b/tests/phpunit/includes/OutputPageTest.php @@ -3,29 +3,71 @@ use Wikimedia\TestingAccessWrapper; /** - * * @author Matthew Flaschen * * @group Database * @group Output - * - * @todo factor tests in this class into providers and test methods */ class OutputPageTest extends MediaWikiTestCase { const SCREEN_MEDIA_QUERY = 'screen and (min-width: 982px)'; const SCREEN_ONLY_MEDIA_QUERY = 'only screen and (min-width: 982px)'; + /** + * @dataProvider provideRedirect + * + * @covers OutputPage::__construct + * @covers OutputPage::redirect + * @covers OutputPage::getRedirect + */ + public function testRedirect( $url, $code = null ) { + $op = $this->newInstance(); + if ( isset( $code ) ) { + $op->redirect( $url, $code ); + } else { + $op->redirect( $url ); + } + $expectedUrl = str_replace( "\n", '', $url ); + $this->assertSame( $expectedUrl, $op->getRedirect() ); + $this->assertSame( $expectedUrl, $op->mRedirect ); + $this->assertSame( $code ?? '302', $op->mRedirectCode ); + } + + public function provideRedirect() { + return [ + [ 'http://example.com' ], + [ 'http://example.com', '400' ], + [ 'http://example.com', 'squirrels!!!' ], + [ "a\nb" ], + ]; + } + + /** + * @covers OutputPage::setCopyrightUrl + * @covers OutputPage::getHeadLinksArray + */ + public function testSetCopyrightUrl() { + $op = $this->newInstance(); + $op->setCopyrightUrl( 'http://example.com' ); + + $this->assertSame( + Html::element( 'link', [ 'rel' => 'license', 'href' => 'http://example.com' ] ), + $op->getHeadLinksArray()['copyright'] + ); + } + + // @todo How to test setStatusCode? + /** * @covers OutputPage::addMeta * @covers OutputPage::getMetaTags * @covers OutputPage::getHeadLinksArray */ public function testMetaTags() { - $outputPage = $this->newInstance(); - $outputPage->addMeta( 'http:expires', '0' ); - $outputPage->addMeta( 'keywords', 'first' ); - $outputPage->addMeta( 'keywords', 'second' ); - $outputPage->addMeta( 'og:title', 'Ta-duh' ); + $op = $this->newInstance(); + $op->addMeta( 'http:expires', '0' ); + $op->addMeta( 'keywords', 'first' ); + $op->addMeta( 'keywords', 'second' ); + $op->addMeta( 'og:title', 'Ta-duh' ); $expected = [ [ 'http:expires', '0' ], @@ -33,9 +75,9 @@ class OutputPageTest extends MediaWikiTestCase { [ 'keywords', 'second' ], [ 'og:title', 'Ta-duh' ], ]; - $this->assertSame( $expected, $outputPage->getMetaTags() ); + $this->assertSame( $expected, $op->getMetaTags() ); - $links = $outputPage->getHeadLinksArray(); + $links = $op->getHeadLinksArray(); $this->assertContains( '', $links ); $this->assertContains( '', $links ); $this->assertContains( '', $links ); @@ -44,84 +86,317 @@ class OutputPageTest extends MediaWikiTestCase { } /** - * @covers OutputPage::setIndexPolicy - * @covers OutputPage::setFollowPolicy + * @covers OutputPage::addLink + * @covers OutputPage::getLinkTags * @covers OutputPage::getHeadLinksArray */ - public function testRobotsPolicies() { - $outputPage = $this->newInstance(); - $outputPage->setIndexPolicy( 'noindex' ); - $outputPage->setFollowPolicy( 'nofollow' ); + public function testAddLink() { + $op = $this->newInstance(); - $links = $outputPage->getHeadLinksArray(); - $this->assertContains( '', $links ); + $links = [ + [], + [ 'rel' => 'foo', 'href' => 'http://example.com' ], + ]; + + foreach ( $links as $link ) { + $op->addLink( $link ); + } + + $this->assertSame( $links, $op->getLinkTags() ); + + $result = $op->getHeadLinksArray(); + + foreach ( $links as $link ) { + $this->assertContains( Html::element( 'link', $link ), $result ); + } } /** - * Tests a particular case of transformCssMedia, using the given input, globals, - * expected return, and message + * @covers OutputPage::setCanonicalUrl + * @covers OutputPage::getCanonicalUrl + * @covers OutputPage::getHeadLinksArray + */ + public function testSetCanonicalUrl() { + $op = $this->newInstance(); + $op->setCanonicalUrl( 'http://example.comm' ); + $op->setCanonicalUrl( 'http://example.com' ); + + $this->assertSame( 'http://example.com', $op->getCanonicalUrl() ); + + $headLinks = $op->getHeadLinksArray(); + + $this->assertContains( Html::element( 'link', [ + 'rel' => 'canonical', 'href' => 'http://example.com' + ] ), $headLinks ); + + $this->assertNotContains( Html::element( 'link', [ + 'rel' => 'canonical', 'href' => 'http://example.comm' + ] ), $headLinks ); + } + + /** + * @covers OutputPage::addScript + */ + public function testAddScript() { + $op = $this->newInstance(); + $op->addScript( 'some random string' ); + + $this->assertContains( "\nsome random string\n", "\n" . $op->getBottomScripts() . "\n" ); + } + + /** + * @covers OutputPage::addScriptFile + */ + public function testAddScriptFile() { + $op = $this->newInstance(); + $op->addScriptFile( '/somescript.js' ); + $op->addScriptFile( '//example.com/somescript.js' ); + + $this->assertContains( + "\n" . Html::linkedScript( '/somescript.js', $op->getCSPNonce() ) . + Html::linkedScript( '//example.com/somescript.js', $op->getCSPNonce() ) . "\n", + "\n" . $op->getBottomScripts() . "\n" + ); + } + + /** + * Test that addScriptFile() throws due to deprecation. * - * Asserts that $expectedReturn is returned. + * @covers OutputPage::addScriptFile + */ + public function testAddDeprecatedScriptFileWarning() { + $this->setExpectedException( PHPUnit_Framework_Error_Deprecated::class, + 'Use of OutputPage::addScriptFile was deprecated in MediaWiki 1.24.' ); + + $op = $this->newInstance(); + $op->addScriptFile( 'ignored-script.js' ); + } + + /** + * Test the actual behavior of the method (in the case where it doesn't throw, e.g., in + * production). Since it threw an exception once in this file, it won't when we call it again. * - * options['printableQuery'] - value of query string for printable, or omitted for none - * options['handheldQuery'] - value of query string for handheld, or omitted for none - * options['media'] - passed into the method under the same name - * options['expectedReturn'] - expected return value - * options['message'] - PHPUnit message for assertion + * @covers OutputPage::addScriptFile + */ + public function testAddDeprecatedScriptFileNoOp() { + $op = $this->newInstance(); + $op->addScriptFile( 'ignored-script.js' ); + + $this->assertNotContains( 'ignored-script.js', '' . $op->getBottomScripts() ); + } + + /** + * @covers OutputPage::addInlineScript + */ + public function testAddInlineScript() { + $op = $this->newInstance(); + $op->addInlineScript( 'let foo = "bar";' ); + $op->addInlineScript( 'alert( foo );' ); + + $this->assertContains( + "\n" . Html::inlineScript( "\nlet foo = \"bar\";\n", $op->getCSPNonce() ) . "\n" . + Html::inlineScript( "\nalert( foo );\n", $op->getCSPNonce() ) . "\n", + "\n" . $op->getBottomScripts() . "\n" + ); + } + + // @todo How to test filterModules(), warnModuleTargetFilter(), getModules(), etc.? + + /** + * @covers OutputPage::getTarget + * @covers OutputPage::setTarget + */ + public function testSetTarget() { + $op = $this->newInstance(); + $op->setTarget( 'foo' ); + + $this->assertSame( 'foo', $op->getTarget() ); + // @todo What else? Test some actual effect? + } + + // @todo How to test addContentOverride(Callback)? + + /** + * @covers OutputPage::getHeadItemsArray + * @covers OutputPage::addHeadItem + * @covers OutputPage::addHeadItems + * @covers OutputPage::hasHeadItem + */ + public function testHeadItems() { + $op = $this->newInstance(); + $op->addHeadItem( 'a', 'b' ); + $op->addHeadItems( [ 'c' => '&', 'e' => 'f', 'a' => 'q' ] ); + $op->addHeadItem( 'e', 'g' ); + $op->addHeadItems( 'x' ); + + $this->assertSame( [ 'a' => 'q', 'c' => '&', 'e' => 'g', 'x' ], + $op->getHeadItemsArray() ); + + $this->assertTrue( $op->hasHeadItem( 'a' ) ); + $this->assertTrue( $op->hasHeadItem( 'c' ) ); + $this->assertTrue( $op->hasHeadItem( 'e' ) ); + $this->assertTrue( $op->hasHeadItem( '0' ) ); + + $this->assertContains( "\nq\n&\ng\nx\n", + '' . $op->headElement( $op->getContext()->getSkin() ) ); + } + + /** + * @covers OutputPage::addBodyClasses + */ + public function testAddBodyClasses() { + $op = $this->newInstance(); + $op->addBodyClasses( 'a' ); + $op->addBodyClasses( 'mediawiki' ); + $op->addBodyClasses( 'b c' ); + $op->addBodyClasses( [ 'd', 'e' ] ); + $op->addBodyClasses( 'a' ); + + $this->assertContains( '"a mediawiki b c d e ltr', + '' . $op->headElement( $op->getContext()->getSkin() ) ); + } + + /** + * @covers OutputPage::setArticleBodyOnly + * @covers OutputPage::getArticleBodyOnly + */ + public function testArticleBodyOnly() { + $op = $this->newInstance(); + $this->assertFalse( $op->getArticleBodyOnly() ); + + $op->setArticleBodyOnly( true ); + $this->assertTrue( $op->getArticleBodyOnly() ); + + $op->addHTML( 'a' ); + + $this->assertSame( 'a', $op->output( true ) ); + } + + /** + * @covers OutputPage::setProperty + * @covers OutputPage::getProperty + */ + public function testProperties() { + $op = $this->newInstance(); + + $this->assertNull( $op->getProperty( 'foo' ) ); + + $op->setProperty( 'foo', 'bar' ); + $op->setProperty( 'baz', 'quz' ); + + $this->assertSame( 'bar', $op->getProperty( 'foo' ) ); + $this->assertSame( 'quz', $op->getProperty( 'baz' ) ); + } + + /** + * @dataProvider provideCheckLastModified * - * @param array $args Key-value array of arguments as shown above + * @covers OutputPage::checkLastModified + * @covers OutputPage::getCdnCacheEpoch */ - protected function assertTransformCssMediaCase( $args ) { - $queryData = []; - if ( isset( $args['printableQuery'] ) ) { - $queryData['printable'] = $args['printableQuery']; + public function testCheckLastModified( + $timestamp, $ifModifiedSince, $expected, $config = [], $callback = null + ) { + $request = new FauxRequest(); + if ( $ifModifiedSince ) { + if ( is_numeric( $ifModifiedSince ) ) { + // Unix timestamp + $ifModifiedSince = date( 'D, d M Y H:i:s', $ifModifiedSince ) . ' GMT'; + } + $request->setHeader( 'If-Modified-Since', $ifModifiedSince ); } - if ( isset( $args['handheldQuery'] ) ) { - $queryData['handheld'] = $args['handheldQuery']; + if ( !isset( $config['CacheEpoch'] ) ) { + // Make sure it's not too recent + $config['CacheEpoch'] = '20000101000000'; } - $fauxRequest = new FauxRequest( $queryData, false ); - $this->setMwGlobals( [ - 'wgRequest' => $fauxRequest, - ] ); + $op = $this->newInstance( $config, $request ); - $actualReturn = OutputPage::transformCssMedia( $args['media'] ); - $this->assertSame( $args['expectedReturn'], $actualReturn, $args['message'] ); + if ( $callback ) { + $callback( $op, $this ); + } + + // Avoid a complaint about not being able to disable compression + Wikimedia\suppressWarnings(); + try { + $this->assertEquals( $expected, $op->checkLastModified( $timestamp ) ); + } finally { + Wikimedia\restoreWarnings(); + } + } + + public function provideCheckLastModified() { + $lastModified = time() - 3600; + return [ + 'Timestamp 0' => + [ '0', $lastModified, false ], + 'Timestamp Unix epoch' => + [ '19700101000000', $lastModified, false ], + 'Timestamp same as If-Modified-Since' => + [ $lastModified, $lastModified, true ], + 'Timestamp one second after If-Modified-Since' => + [ $lastModified + 1, $lastModified, false ], + 'No If-Modified-Since' => + [ $lastModified + 1, null, false ], + 'Malformed If-Modified-Since' => + [ $lastModified + 1, 'GIBBERING WOMBATS !!!', false ], + 'Non-standard IE-style If-Modified-Since' => + [ $lastModified, date( 'D, d M Y H:i:s', $lastModified ) . ' GMT; length=5202', + true ], + // @todo Should we fix this behavior to match the spec? Probably no reason to. + 'If-Modified-Since not per spec but we accept it anyway because strtotime does' => + [ $lastModified, "@$lastModified", true ], + '$wgCachePages = false' => + [ $lastModified, $lastModified, false, [ 'CachePages' => false ] ], + '$wgCacheEpoch' => + [ $lastModified, $lastModified, false, + [ 'CacheEpoch' => wfTimestamp( TS_MW, $lastModified + 1 ) ] ], + 'Recently-touched user' => + [ $lastModified, $lastModified, false, [], + function ( $op ) { + $op->getContext()->setUser( $this->getTestUser()->getUser() ); + } ], + 'After Squid expiry' => + [ $lastModified, $lastModified, false, + [ 'UseSquid' => true, 'SquidMaxage' => 3599 ] ], + 'Hook allows cache use' => + [ $lastModified + 1, $lastModified, true, [], + function ( $op, $that ) { + $that->setTemporaryHook( 'OutputPageCheckLastModified', + function ( &$modifiedTimes ) { + $modifiedTimes = [ 1 ]; + } + ); + } ], + 'Hooks prohibits cache use' => + [ $lastModified, $lastModified, false, [], + function ( $op, $that ) { + $that->setTemporaryHook( 'OutputPageCheckLastModified', + function ( &$modifiedTimes ) { + $modifiedTimes = [ max( $modifiedTimes ) + 1 ]; + } + ); + } ], + ]; } /** - * Tests print requests - * @covers OutputPage::transformCssMedia + * @dataProvider provideCdnCacheEpoch + * + * @covers OutputPage::getCdnCacheEpoch */ - public function testPrintRequests() { - $this->assertTransformCssMediaCase( [ - 'printableQuery' => '1', - 'media' => 'screen', - 'expectedReturn' => null, - 'message' => 'On printable request, screen returns null' - ] ); - - $this->assertTransformCssMediaCase( [ - 'printableQuery' => '1', - 'media' => self::SCREEN_MEDIA_QUERY, - 'expectedReturn' => null, - 'message' => 'On printable request, screen media query returns null' - ] ); - - $this->assertTransformCssMediaCase( [ - 'printableQuery' => '1', - 'media' => self::SCREEN_ONLY_MEDIA_QUERY, - 'expectedReturn' => null, - 'message' => 'On printable request, screen media query with only returns null' - ] ); + public function testCdnCacheEpoch( $params ) { + $out = TestingAccessWrapper::newFromObject( $this->newInstance() ); + $reqTime = strtotime( $params['reqTime'] ); + $pageTime = strtotime( $params['pageTime'] ); + $actual = max( $pageTime, $out->getCdnCacheEpoch( $reqTime, $params['maxAge'] ) ); - $this->assertTransformCssMediaCase( [ - 'printableQuery' => '1', - 'media' => 'print', - 'expectedReturn' => '', - 'message' => 'On printable request, media print returns empty string' - ] ); + $this->assertEquals( + $params['expect'], + gmdate( DateTime::ATOM, $actual ), + 'cdn epoch' + ); } public static function provideCdnCacheEpoch() { @@ -149,229 +424,418 @@ class OutputPageTest extends MediaWikiTestCase { ]; } + // @todo How to test setLastModified? + /** - * @dataProvider provideCdnCacheEpoch - * @covers OutputPage::getCdnCacheEpoch + * @covers OutputPage::setRobotPolicy + * @covers OutputPage::getHeadLinksArray */ - public function testCdnCacheEpoch( $params ) { - $out = TestingAccessWrapper::newFromObject( $this->newInstance() ); - $reqTime = strtotime( $params['reqTime'] ); - $pageTime = strtotime( $params['pageTime'] ); - $actual = max( $pageTime, $out->getCdnCacheEpoch( $reqTime, $params['maxAge'] ) ); + public function testSetRobotPolicy() { + $op = $this->newInstance(); + $op->setRobotPolicy( 'noindex, nofollow' ); - $this->assertEquals( - $params['expect'], - gmdate( DateTime::ATOM, $actual ), - 'cdn epoch' + $links = $op->getHeadLinksArray(); + $this->assertContains( '', $links ); + } + + /** + * @covers OutputPage::setIndexPolicy + * @covers OutputPage::setFollowPolicy + * @covers OutputPage::getHeadLinksArray + */ + public function testSetIndexFollowPolicies() { + $op = $this->newInstance(); + $op->setIndexPolicy( 'noindex' ); + $op->setFollowPolicy( 'nofollow' ); + + $links = $op->getHeadLinksArray(); + $this->assertContains( '', $links ); + } + + // @todo mPageTitleActionText has done nothing and has no callers for a long time: + // + // * e4d21170 inadvertently made it do nothing (Apr 2009) + // * 10ecfcb0/cadc951d removed the dead code that would have at least indicated what it was + // supposed to do (Nov 2010) + // * 9e230f30/2d045fa1 removed from history pages because it did nothing (Oct/Aug 2011) + // * e275ea28 removed from articles (Oct 2011) + // * ae45908c removed from EditPage (Oct 2011) + // + // Nice if we had had tests so these things couldn't happen by mistake, right?! + // + // https://phabricator.wikimedia.org/T200643 + + private function extractHTMLTitle( OutputPage $op ) { + $html = $op->headElement( $op->getContext()->getSkin() ); + + // OutputPage should always output the title in a nice format such that regexes will work + // fine. If it doesn't, we'll fail the tests. + preg_match_all( '!(.*?)!', $html, $matches ); + + $this->assertLessThanOrEqual( 1, count( $matches[1] ), 'More than one !' ); + + if ( !count( $matches[1] ) ) { + return null; + } + + return $matches[1][0]; + } + + /** + * Shorthand for getting the text of a message, in content language. + */ + private static function getMsgText( $op, ...$msgParams ) { + return $op->msg( ...$msgParams )->inContentLanguage()->text(); + } + + /** + * @covers OutputPage::setHTMLTitle + * @covers OutputPage::getHTMLTitle + */ + public function testHTMLTitle() { + $op = $this->newInstance(); + + // Default + $this->assertSame( '', $op->getHTMLTitle() ); + $this->assertSame( '', $op->getPageTitle() ); + $this->assertSame( + $this->getMsgText( $op, 'pagetitle', '' ), + $this->extractHTMLTitle( $op ) ); + + // Set to string + $op->setHTMLTitle( 'Potatoes will eat me' ); + + $this->assertSame( 'Potatoes will eat me', $op->getHTMLTitle() ); + $this->assertSame( 'Potatoes will eat me', $this->extractHTMLTitle( $op ) ); + // Shouldn't have changed the page title + $this->assertSame( '', $op->getPageTitle() ); + + // Set to message + $msg = $op->msg( 'mainpage' ); + + $op->setHTMLTitle( $msg ); + $this->assertSame( $msg->text(), $op->getHTMLTitle() ); + $this->assertSame( $msg->text(), $this->extractHTMLTitle( $op ) ); + $this->assertSame( '', $op->getPageTitle() ); } /** - * Tests screen requests, without either query parameter set - * @covers OutputPage::transformCssMedia + * @covers OutputPage::setRedirectedFrom */ - public function testScreenRequests() { - $this->assertTransformCssMediaCase( [ - 'media' => 'screen', - 'expectedReturn' => 'screen', - 'message' => 'On screen request, screen media type is preserved' - ] ); + public function testSetRedirectedFrom() { + $op = $this->newInstance(); - $this->assertTransformCssMediaCase( [ - 'media' => 'handheld', - 'expectedReturn' => 'handheld', - 'message' => 'On screen request, handheld media type is preserved' - ] ); + $op->setRedirectedFrom( Title::newFromText( 'Talk:Some page' ) ); + $this->assertSame( 'Talk:Some_page', $op->getJSVars()['wgRedirectedFrom'] ); + } - $this->assertTransformCssMediaCase( [ - 'media' => self::SCREEN_MEDIA_QUERY, - 'expectedReturn' => self::SCREEN_MEDIA_QUERY, - 'message' => 'On screen request, screen media query is preserved.' - ] ); + /** + * @covers OutputPage::setPageTitle + * @covers OutputPage::getPageTitle + */ + public function testPageTitle() { + // We don't test the actual HTML output anywhere, because that's up to the skin. + $op = $this->newInstance(); - $this->assertTransformCssMediaCase( [ - 'media' => self::SCREEN_ONLY_MEDIA_QUERY, - 'expectedReturn' => self::SCREEN_ONLY_MEDIA_QUERY, - 'message' => 'On screen request, screen media query with only is preserved.' - ] ); + // Test default + $this->assertSame( '', $op->getPageTitle() ); + $this->assertSame( '', $op->getHTMLTitle() ); - $this->assertTransformCssMediaCase( [ - 'media' => 'print', - 'expectedReturn' => 'print', - 'message' => 'On screen request, print media type is preserved' - ] ); + // Test set to plain text + $op->setPageTitle( 'foobar' ); + + $this->assertSame( 'foobar', $op->getPageTitle() ); + // HTML title should change as well + $this->assertSame( $this->getMsgText( $op, 'pagetitle', 'foobar' ), $op->getHTMLTitle() ); + + // Test set to text with good and bad HTML. We don't try to be comprehensive here, that + // belongs in Sanitizer tests. + $op->setPageTitle( '<script>a</script>&<i>b</i>' ); + + $this->assertSame( '<script>a</script>&<i>b</i>', $op->getPageTitle() ); + $this->assertSame( + $this->getMsgText( $op, 'pagetitle', '<script>a</script>&b' ), + $op->getHTMLTitle() + ); + + // Test set to message + $text = $this->getMsgText( $op, 'mainpage' ); + + $op->setPageTitle( $op->msg( 'mainpage' )->inContentLanguage() ); + $this->assertSame( $text, $op->getPageTitle() ); + $this->assertSame( $this->getMsgText( $op, 'pagetitle', $text ), $op->getHTMLTitle() ); } /** - * Tests handheld behavior - * @covers OutputPage::transformCssMedia + * @covers OutputPage::setTitle */ - public function testHandheld() { - $this->assertTransformCssMediaCase( [ - 'handheldQuery' => '1', - 'media' => 'handheld', - 'expectedReturn' => '', - 'message' => 'On request with handheld querystring and media is handheld, returns empty string' + public function testSetTitle() { + $op = $this->newInstance(); + + $this->assertSame( 'My test page', $op->getTitle()->getPrefixedText() ); + + $op->setTitle( Title::newFromText( 'Another test page' ) ); + + $this->assertSame( 'Another test page', $op->getTitle()->getPrefixedText() ); + } + + /** + * @covers OutputPage::setSubtitle + * @covers OutputPage::clearSubtitle + * @covers OutputPage::addSubtitle + * @covers OutputPage::getSubtitle + */ + public function testSubtitle() { + $op = $this->newInstance(); + + $this->assertSame( '', $op->getSubtitle() ); + + $op->addSubtitle( '<b>foo</b>' ); + + $this->assertSame( '<b>foo</b>', $op->getSubtitle() ); + + $op->addSubtitle( $op->msg( 'mainpage' )->inContentLanguage() ); + + $this->assertSame( + "<b>foo</b><br />\n\t\t\t\t" . $this->getMsgText( $op, 'mainpage' ), + $op->getSubtitle() + ); + + $op->setSubtitle( 'There can be only one' ); + + $this->assertSame( 'There can be only one', $op->getSubtitle() ); + + $op->clearSubtitle(); + + $this->assertSame( '', $op->getSubtitle() ); + } + + /** + * @dataProvider provideBacklinkSubtitle + * + * @covers OutputPage::buildBacklinkSubtitle + */ + public function testBuildBacklinkSubtitle( Title $title, $query, $contains, $notContains ) { + $this->editPage( 'Page 1', '' ); + $this->editPage( 'Page 2', '#REDIRECT [[Page 1]]' ); + + $str = OutputPage::buildBacklinkSubtitle( $title, $query )->text(); + + foreach ( $contains as $substr ) { + $this->assertContains( $substr, $str ); + } + + foreach ( $notContains as $substr ) { + $this->assertNotContains( $substr, $str ); + } + } + + /** + * @dataProvider provideBacklinkSubtitle + * + * @covers OutputPage::addBacklinkSubtitle + * @covers OutputPage::getSubtitle + */ + public function testAddBacklinkSubtitle( Title $title, $query, $contains, $notContains ) { + $this->editPage( 'Page 1', '' ); + $this->editPage( 'Page 2', '#REDIRECT [[Page 1]]' ); + + $op = $this->newInstance(); + $op->addBacklinkSubtitle( $title, $query ); + + $str = $op->getSubtitle(); + + foreach ( $contains as $substr ) { + $this->assertContains( $substr, $str ); + } + + foreach ( $notContains as $substr ) { + $this->assertNotContains( $substr, $str ); + } + } + + public function provideBacklinkSubtitle() { + $page1 = Title::newFromText( 'Page 1' ); + $page2 = Title::newFromText( 'Page 2' ); + + return [ + [ $page1, [], [ 'Page 1' ], [ 'redirect', 'Page 2' ] ], + [ $page2, [], [ 'redirect=no' ], [ 'Page 1' ] ], + [ $page1, [ 'action' => 'edit' ], [ 'action=edit' ], [] ], + // @todo Anything else to test? + ]; + } + + /** + * @covers OutputPage::addCategoryLinks + * @covers OutputPage::getCategories + */ + public function testGetCategories() { + $fakeResultWrapper = new FakeResultWrapper( [ + (object)[ + 'pp_value' => 1, + 'page_title' => 'Test' + ], + (object)[ + 'page_title' => 'Test2' + ] ] ); + $op = $this->getMockBuilder( OutputPage::class ) + ->setConstructorArgs( [ new RequestContext() ] ) + ->setMethods( [ 'addCategoryLinksToLBAndGetResult' ] ) + ->getMock(); + $op->expects( $this->any() ) + ->method( 'addCategoryLinksToLBAndGetResult' ) + ->will( $this->returnValue( $fakeResultWrapper ) ); - $this->assertTransformCssMediaCase( [ - 'handheldQuery' => '1', - 'media' => 'screen', - 'expectedReturn' => null, - 'message' => 'On request with handheld querystring and media is screen, returns null' + $op->addCategoryLinks( [ + 'Test' => 'Test', + 'Test2' => 'Test2', ] ); + $this->assertEquals( [ 0 => 'Test', '1' => 'Test2' ], $op->getCategories() ); + $this->assertEquals( [ 0 => 'Test2' ], $op->getCategories( 'normal' ) ); + $this->assertEquals( [ 0 => 'Test' ], $op->getCategories( 'hidden' ) ); } - public static function provideTransformFilePath() { - $baseDir = dirname( __DIR__ ) . '/data/media'; + /** + * @covers OutputPage::haveCacheVaryCookies + */ + public function testHaveCacheVaryCookies() { + $request = new FauxRequest(); + $context = new RequestContext(); + $context->setRequest( $request ); + $op = new OutputPage( $context ); + + // No cookies are set. + $this->assertFalse( $op->haveCacheVaryCookies() ); + + // 'Token' is present but empty, so it shouldn't count. + $request->setCookie( 'Token', '' ); + $this->assertFalse( $op->haveCacheVaryCookies() ); + + // 'Token' present and nonempty. + $request->setCookie( 'Token', '123' ); + $this->assertTrue( $op->haveCacheVaryCookies() ); + } + + /** + * @dataProvider provideVaryHeaders + * + * @covers OutputPage::addVaryHeader + * @covers OutputPage::getVaryHeader + * @covers OutputPage::getKeyHeader + */ + public function testVaryHeaders( $calls, $vary, $key ) { + // get rid of default Vary fields + $op = $this->getMockBuilder( OutputPage::class ) + ->setConstructorArgs( [ new RequestContext() ] ) + ->setMethods( [ 'getCacheVaryCookies' ] ) + ->getMock(); + $op->expects( $this->any() ) + ->method( 'getCacheVaryCookies' ) + ->will( $this->returnValue( [] ) ); + TestingAccessWrapper::newFromObject( $op )->mVaryHeader = []; + + foreach ( $calls as $call ) { + call_user_func_array( [ $op, 'addVaryHeader' ], $call ); + } + $this->assertEquals( $vary, $op->getVaryHeader(), 'Vary:' ); + $this->assertEquals( $key, $op->getKeyHeader(), 'Key:' ); + } + + public function provideVaryHeaders() { + // note: getKeyHeader() automatically adds Vary: Cookie return [ - // File that matches basePath, and exists. Hash found and appended. - [ - 'baseDir' => $baseDir, 'basePath' => '/w', - '/w/test.jpg', - '/w/test.jpg?edcf2' - ], - // File that matches basePath, but not found on disk. Empty query. - [ - 'baseDir' => $baseDir, 'basePath' => '/w', - '/w/unknown.png', - '/w/unknown.png?' - ], - // File not matching basePath. Ignored. - [ - 'baseDir' => $baseDir, 'basePath' => '/w', - '/files/test.jpg' - ], - // Empty string. Ignored. - [ - 'baseDir' => $baseDir, 'basePath' => '/w', - '', - '' - ], - // Similar path, but with domain component. Ignored. - [ - 'baseDir' => $baseDir, 'basePath' => '/w', - '//example.org/w/test.jpg' - ], - [ - 'baseDir' => $baseDir, 'basePath' => '/w', - 'https://example.org/w/test.jpg' - ], - // Unrelated path with domain component. Ignored. - [ - 'baseDir' => $baseDir, 'basePath' => '/w', - 'https://example.org/files/test.jpg' + [ // single header + [ + [ 'Cookie' ], + ], + 'Vary: Cookie', + 'Key: Cookie', ], - [ - 'baseDir' => $baseDir, 'basePath' => '/w', - '//example.org/files/test.jpg' + [ // non-unique headers + [ + [ 'Cookie' ], + [ 'Accept-Language' ], + [ 'Cookie' ], + ], + 'Vary: Cookie, Accept-Language', + 'Key: Cookie,Accept-Language', ], - // Unrelated path with domain, and empty base path (root mw install). Ignored. - [ - 'baseDir' => $baseDir, 'basePath' => '', - 'https://example.org/files/test.jpg' + [ // two headers with single options + [ + [ 'Cookie', [ 'param=phpsessid' ] ], + [ 'Accept-Language', [ 'substr=en' ] ], + ], + 'Vary: Cookie, Accept-Language', + 'Key: Cookie;param=phpsessid,Accept-Language;substr=en', ], - [ - 'baseDir' => $baseDir, 'basePath' => '', - // T155310 - '//example.org/files/test.jpg' + [ // one header with multiple options + [ + [ 'Cookie', [ 'param=phpsessid', 'param=userId' ] ], + ], + 'Vary: Cookie', + 'Key: Cookie;param=phpsessid;param=userId', ], - // Check UploadPath before ResourceBasePath (T155146) - [ - 'baseDir' => dirname( $baseDir ), 'basePath' => '', - 'uploadDir' => $baseDir, 'uploadPath' => '/images', - '/images/test.jpg', - '/images/test.jpg?edcf2' + [ // Duplicate option + [ + [ 'Cookie', [ 'param=phpsessid' ] ], + [ 'Cookie', [ 'param=phpsessid' ] ], + [ 'Accept-Language', [ 'substr=en', 'substr=en' ] ], + ], + 'Vary: Cookie, Accept-Language', + 'Key: Cookie;param=phpsessid,Accept-Language;substr=en', + ], + [ // Same header, different options + [ + [ 'Cookie', [ 'param=phpsessid' ] ], + [ 'Cookie', [ 'param=userId' ] ], + ], + 'Vary: Cookie', + 'Key: Cookie;param=phpsessid;param=userId', ], ]; } /** - * @dataProvider provideTransformFilePath - * @covers OutputPage::transformFilePath - * @covers OutputPage::transformResourcePath + * @dataProvider provideLinkHeaders + * + * @covers OutputPage::addLinkHeader + * @covers OutputPage::getLinkHeader */ - public function testTransformResourcePath( $baseDir, $basePath, $uploadDir = null, - $uploadPath = null, $path = null, $expected = null - ) { - if ( $path === null ) { - // Skip optional $uploadDir and $uploadPath - $path = $uploadDir; - $expected = $uploadPath; - $uploadDir = "$baseDir/images"; - $uploadPath = "$basePath/images"; - } - $this->setMwGlobals( 'IP', $baseDir ); - $conf = new HashConfig( [ - 'ResourceBasePath' => $basePath, - 'UploadDirectory' => $uploadDir, - 'UploadPath' => $uploadPath, - ] ); + public function testLinkHeaders( $headers, $result ) { + $op = $this->newInstance(); - Wikimedia\suppressWarnings(); - $actual = OutputPage::transformResourcePath( $conf, $path ); - Wikimedia\restoreWarnings(); + foreach ( $headers as $header ) { + $op->addLinkHeader( $header ); + } - $this->assertEquals( $expected ?: $path, $actual ); + $this->assertEquals( $result, $op->getLinkHeader() ); } - public static function provideMakeResourceLoaderLink() { - // phpcs:disable Generic.Files.LineLength + public function provideLinkHeaders() { return [ - // Single only=scripts load - [ - [ 'test.foo', ResourceLoaderModule::TYPE_SCRIPTS ], - "<script nonce=\"secret\">(window.RLQ=window.RLQ||[]).push(function(){" - . 'mw.loader.load("http://127.0.0.1:8080/w/load.php?debug=false\u0026lang=en\u0026modules=test.foo\u0026only=scripts\u0026skin=fallback");' - . "});</script>" - ], - // Multiple only=styles load - [ - [ [ 'test.baz', 'test.foo', 'test.bar' ], ResourceLoaderModule::TYPE_STYLES ], - - '<link rel="stylesheet" href="http://127.0.0.1:8080/w/load.php?debug=false&lang=en&modules=test.bar%2Cbaz%2Cfoo&only=styles&skin=fallback"/>' - ], - // Private embed (only=scripts) [ - [ 'test.quux', ResourceLoaderModule::TYPE_SCRIPTS ], - "<script nonce=\"secret\">(window.RLQ=window.RLQ||[]).push(function(){" - . "mw.test.baz({token:123});\nmw.loader.state({\"test.quux\":\"ready\"});" - . "});</script>" - ], - // Load private module (combined) - [ - [ 'test.quux', ResourceLoaderModule::TYPE_COMBINED ], - "<script nonce=\"secret\">(window.RLQ=window.RLQ||[]).push(function(){" - . "mw.loader.implement(\"test.quux@1ev0ijv\",function($,jQuery,require,module){" - . "mw.test.baz({token:123});},{\"css\":[\".mw-icon{transition:none}" - . "\"]});});</script>" - ], - // Load no modules - [ - [ [], ResourceLoaderModule::TYPE_COMBINED ], - '', + [], + false ], - // noscript group [ - [ 'test.noscript', ResourceLoaderModule::TYPE_STYLES ], - '<noscript><link rel="stylesheet" href="http://127.0.0.1:8080/w/load.php?debug=false&lang=en&modules=test.noscript&only=styles&skin=fallback"/></noscript>' + [ '<https://foo/bar.jpg>;rel=preload;as=image' ], + 'Link: <https://foo/bar.jpg>;rel=preload;as=image', ], - // Load two modules in separate groups [ - [ [ 'test.group.foo', 'test.group.bar' ], ResourceLoaderModule::TYPE_COMBINED ], - "<script nonce=\"secret\">(window.RLQ=window.RLQ||[]).push(function(){" - . 'mw.loader.load("http://127.0.0.1:8080/w/load.php?debug=false\u0026lang=en\u0026modules=test.group.bar\u0026skin=fallback");' - . 'mw.loader.load("http://127.0.0.1:8080/w/load.php?debug=false\u0026lang=en\u0026modules=test.group.foo\u0026skin=fallback");' - . "});</script>" + [ '<https://foo/bar.jpg>;rel=preload;as=image','<https://foo/baz.jpg>;rel=preload;as=image' ], + 'Link: <https://foo/bar.jpg>;rel=preload;as=image,<https://foo/baz.jpg>;rel=preload;as=image', ], ]; - // phpcs:enable } /** * See ResourceLoaderClientHtmlTest for full coverage. * * @dataProvider provideMakeResourceLoaderLink + * * @covers OutputPage::makeResourceLoaderLink */ public function testMakeResourceLoaderLink( $args, $expectedHtml ) { @@ -428,38 +892,54 @@ class OutputPageTest extends MediaWikiTestCase { $this->assertEquals( $expectedHtml, $actualHtml ); } - public static function provideBuildExemptModules() { + public static function provideMakeResourceLoaderLink() { // phpcs:disable Generic.Files.LineLength return [ - 'empty' => [ - 'exemptStyleModules' => [], - '<meta name="ResourceLoaderDynamicStyles" content=""/>', + // Single only=scripts load + [ + [ 'test.foo', ResourceLoaderModule::TYPE_SCRIPTS ], + "<script nonce=\"secret\">(window.RLQ=window.RLQ||[]).push(function(){" + . 'mw.loader.load("http://127.0.0.1:8080/w/load.php?debug=false\u0026lang=en\u0026modules=test.foo\u0026only=scripts\u0026skin=fallback");' + . "});</script>" ], - 'empty sets' => [ - 'exemptStyleModules' => [ 'site' => [], 'noscript' => [], 'private' => [], 'user' => [] ], - '<meta name="ResourceLoaderDynamicStyles" content=""/>', + // Multiple only=styles load + [ + [ [ 'test.baz', 'test.foo', 'test.bar' ], ResourceLoaderModule::TYPE_STYLES ], + + '<link rel="stylesheet" href="http://127.0.0.1:8080/w/load.php?debug=false&lang=en&modules=test.bar%2Cbaz%2Cfoo&only=styles&skin=fallback"/>' ], - 'default logged-out' => [ - 'exemptStyleModules' => [ 'site' => [ 'site.styles' ] ], - '<meta name="ResourceLoaderDynamicStyles" content=""/>' . "\n" . - '<link rel="stylesheet" href="/w/load.php?debug=false&lang=en&modules=site.styles&only=styles&skin=fallback"/>', + // Private embed (only=scripts) + [ + [ 'test.quux', ResourceLoaderModule::TYPE_SCRIPTS ], + "<script nonce=\"secret\">(window.RLQ=window.RLQ||[]).push(function(){" + . "mw.test.baz({token:123});\nmw.loader.state({\"test.quux\":\"ready\"});" + . "});</script>" ], - 'default logged-in' => [ - 'exemptStyleModules' => [ 'site' => [ 'site.styles' ], 'user' => [ 'user.styles' ] ], - '<meta name="ResourceLoaderDynamicStyles" content=""/>' . "\n" . - '<link rel="stylesheet" href="/w/load.php?debug=false&lang=en&modules=site.styles&only=styles&skin=fallback"/>' . "\n" . - '<link rel="stylesheet" href="/w/load.php?debug=false&lang=en&modules=user.styles&only=styles&skin=fallback&version=1e9z0ox"/>', + // Load private module (combined) + [ + [ 'test.quux', ResourceLoaderModule::TYPE_COMBINED ], + "<script nonce=\"secret\">(window.RLQ=window.RLQ||[]).push(function(){" + . "mw.loader.implement(\"test.quux@1ev0ijv\",function($,jQuery,require,module){" + . "mw.test.baz({token:123});},{\"css\":[\".mw-icon{transition:none}" + . "\"]});});</script>" ], - 'custom modules' => [ - 'exemptStyleModules' => [ - 'site' => [ 'site.styles', 'example.site.a', 'example.site.b' ], - 'user' => [ 'user.styles', 'example.user' ], - ], - '<meta name="ResourceLoaderDynamicStyles" content=""/>' . "\n" . - '<link rel="stylesheet" href="/w/load.php?debug=false&lang=en&modules=example.site.a%2Cb&only=styles&skin=fallback"/>' . "\n" . - '<link rel="stylesheet" href="/w/load.php?debug=false&lang=en&modules=site.styles&only=styles&skin=fallback"/>' . "\n" . - '<link rel="stylesheet" href="/w/load.php?debug=false&lang=en&modules=example.user&only=styles&skin=fallback&version=0a56zyi"/>' . "\n" . - '<link rel="stylesheet" href="/w/load.php?debug=false&lang=en&modules=user.styles&only=styles&skin=fallback&version=1e9z0ox"/>', + // Load no modules + [ + [ [], ResourceLoaderModule::TYPE_COMBINED ], + '', + ], + // noscript group + [ + [ 'test.noscript', ResourceLoaderModule::TYPE_STYLES ], + '<noscript><link rel="stylesheet" href="http://127.0.0.1:8080/w/load.php?debug=false&lang=en&modules=test.noscript&only=styles&skin=fallback"/></noscript>' + ], + // Load two modules in separate groups + [ + [ [ 'test.group.foo', 'test.group.bar' ], ResourceLoaderModule::TYPE_COMBINED ], + "<script nonce=\"secret\">(window.RLQ=window.RLQ||[]).push(function(){" + . 'mw.loader.load("http://127.0.0.1:8080/w/load.php?debug=false\u0026lang=en\u0026modules=test.group.bar\u0026skin=fallback");' + . 'mw.loader.load("http://127.0.0.1:8080/w/load.php?debug=false\u0026lang=en\u0026modules=test.group.foo\u0026skin=fallback");' + . "});</script>" ], ]; // phpcs:enable @@ -467,6 +947,7 @@ class OutputPageTest extends MediaWikiTestCase { /** * @dataProvider provideBuildExemptModules + * * @covers OutputPage::buildExemptModules */ public function testBuildExemptModules( array $exemptStyleModules, $expect ) { @@ -482,14 +963,14 @@ class OutputPageTest extends MediaWikiTestCase { $ctx = new RequestContext(); $ctx->setSkin( SkinFactory::getDefaultInstance()->makeSkin( 'fallback' ) ); $ctx->setLanguage( 'en' ); - $outputPage = $this->getMockBuilder( OutputPage::class ) + $op = $this->getMockBuilder( OutputPage::class ) ->setConstructorArgs( [ $ctx ] ) ->setMethods( [ 'buildCssLinksArray' ] ) ->getMock(); - $outputPage->expects( $this->any() ) + $op->expects( $this->any() ) ->method( 'buildCssLinksArray' ) ->willReturn( [] ); - $rl = $outputPage->getResourceLoader(); + $rl = $op->getResourceLoader(); $rl->setMessageBlobStore( new NullMessageBlobStore() ); // Register custom modules @@ -499,174 +980,269 @@ class OutputPageTest extends MediaWikiTestCase { 'example.user' => new ResourceLoaderTestModule( [ 'group' => 'user' ] ), ] ); - $outputPage = TestingAccessWrapper::newFromObject( $outputPage ); - $outputPage->rlExemptStyleModules = $exemptStyleModules; + $op = TestingAccessWrapper::newFromObject( $op ); + $op->rlExemptStyleModules = $exemptStyleModules; $this->assertEquals( $expect, - strval( $outputPage->buildExemptModules() ) + strval( $op->buildExemptModules() ) ); } + public static function provideBuildExemptModules() { + // phpcs:disable Generic.Files.LineLength + return [ + 'empty' => [ + 'exemptStyleModules' => [], + '<meta name="ResourceLoaderDynamicStyles" content=""/>', + ], + 'empty sets' => [ + 'exemptStyleModules' => [ 'site' => [], 'noscript' => [], 'private' => [], 'user' => [] ], + '<meta name="ResourceLoaderDynamicStyles" content=""/>', + ], + 'default logged-out' => [ + 'exemptStyleModules' => [ 'site' => [ 'site.styles' ] ], + '<meta name="ResourceLoaderDynamicStyles" content=""/>' . "\n" . + '<link rel="stylesheet" href="/w/load.php?debug=false&lang=en&modules=site.styles&only=styles&skin=fallback"/>', + ], + 'default logged-in' => [ + 'exemptStyleModules' => [ 'site' => [ 'site.styles' ], 'user' => [ 'user.styles' ] ], + '<meta name="ResourceLoaderDynamicStyles" content=""/>' . "\n" . + '<link rel="stylesheet" href="/w/load.php?debug=false&lang=en&modules=site.styles&only=styles&skin=fallback"/>' . "\n" . + '<link rel="stylesheet" href="/w/load.php?debug=false&lang=en&modules=user.styles&only=styles&skin=fallback&version=1e9z0ox"/>', + ], + 'custom modules' => [ + 'exemptStyleModules' => [ + 'site' => [ 'site.styles', 'example.site.a', 'example.site.b' ], + 'user' => [ 'user.styles', 'example.user' ], + ], + '<meta name="ResourceLoaderDynamicStyles" content=""/>' . "\n" . + '<link rel="stylesheet" href="/w/load.php?debug=false&lang=en&modules=example.site.a%2Cb&only=styles&skin=fallback"/>' . "\n" . + '<link rel="stylesheet" href="/w/load.php?debug=false&lang=en&modules=site.styles&only=styles&skin=fallback"/>' . "\n" . + '<link rel="stylesheet" href="/w/load.php?debug=false&lang=en&modules=example.user&only=styles&skin=fallback&version=0a56zyi"/>' . "\n" . + '<link rel="stylesheet" href="/w/load.php?debug=false&lang=en&modules=user.styles&only=styles&skin=fallback&version=1e9z0ox"/>', + ], + ]; + // phpcs:enable + } + /** - * @dataProvider provideVaryHeaders - * @covers OutputPage::addVaryHeader - * @covers OutputPage::getVaryHeader - * @covers OutputPage::getKeyHeader + * @dataProvider provideTransformFilePath + * @covers OutputPage::transformFilePath + * @covers OutputPage::transformResourcePath */ - public function testVaryHeaders( $calls, $vary, $key ) { - // get rid of default Vary fields - $outputPage = $this->getMockBuilder( OutputPage::class ) - ->setConstructorArgs( [ new RequestContext() ] ) - ->setMethods( [ 'getCacheVaryCookies' ] ) - ->getMock(); - $outputPage->expects( $this->any() ) - ->method( 'getCacheVaryCookies' ) - ->will( $this->returnValue( [] ) ); - TestingAccessWrapper::newFromObject( $outputPage )->mVaryHeader = []; - - foreach ( $calls as $call ) { - call_user_func_array( [ $outputPage, 'addVaryHeader' ], $call ); + public function testTransformResourcePath( $baseDir, $basePath, $uploadDir = null, + $uploadPath = null, $path = null, $expected = null + ) { + if ( $path === null ) { + // Skip optional $uploadDir and $uploadPath + $path = $uploadDir; + $expected = $uploadPath; + $uploadDir = "$baseDir/images"; + $uploadPath = "$basePath/images"; } - $this->assertEquals( $vary, $outputPage->getVaryHeader(), 'Vary:' ); - $this->assertEquals( $key, $outputPage->getKeyHeader(), 'Key:' ); + $this->setMwGlobals( 'IP', $baseDir ); + $conf = new HashConfig( [ + 'ResourceBasePath' => $basePath, + 'UploadDirectory' => $uploadDir, + 'UploadPath' => $uploadPath, + ] ); + + // Some of these paths don't exist and will cause warnings + Wikimedia\suppressWarnings(); + $actual = OutputPage::transformResourcePath( $conf, $path ); + Wikimedia\restoreWarnings(); + + $this->assertEquals( $expected ?: $path, $actual ); } - public function provideVaryHeaders() { - // note: getKeyHeader() automatically adds Vary: Cookie + public static function provideTransformFilePath() { + $baseDir = dirname( __DIR__ ) . '/data/media'; return [ - [ // single header - [ - [ 'Cookie' ], - ], - 'Vary: Cookie', - 'Key: Cookie', + // File that matches basePath, and exists. Hash found and appended. + [ + 'baseDir' => $baseDir, 'basePath' => '/w', + '/w/test.jpg', + '/w/test.jpg?edcf2' ], - [ // non-unique headers - [ - [ 'Cookie' ], - [ 'Accept-Language' ], - [ 'Cookie' ], - ], - 'Vary: Cookie, Accept-Language', - 'Key: Cookie,Accept-Language', + // File that matches basePath, but not found on disk. Empty query. + [ + 'baseDir' => $baseDir, 'basePath' => '/w', + '/w/unknown.png', + '/w/unknown.png?' ], - [ // two headers with single options - [ - [ 'Cookie', [ 'param=phpsessid' ] ], - [ 'Accept-Language', [ 'substr=en' ] ], - ], - 'Vary: Cookie, Accept-Language', - 'Key: Cookie;param=phpsessid,Accept-Language;substr=en', + // File not matching basePath. Ignored. + [ + 'baseDir' => $baseDir, 'basePath' => '/w', + '/files/test.jpg' ], - [ // one header with multiple options - [ - [ 'Cookie', [ 'param=phpsessid', 'param=userId' ] ], - ], - 'Vary: Cookie', - 'Key: Cookie;param=phpsessid;param=userId', + // Empty string. Ignored. + [ + 'baseDir' => $baseDir, 'basePath' => '/w', + '', + '' ], - [ // Duplicate option - [ - [ 'Cookie', [ 'param=phpsessid' ] ], - [ 'Cookie', [ 'param=phpsessid' ] ], - [ 'Accept-Language', [ 'substr=en', 'substr=en' ] ], - ], - 'Vary: Cookie, Accept-Language', - 'Key: Cookie;param=phpsessid,Accept-Language;substr=en', + // Similar path, but with domain component. Ignored. + [ + 'baseDir' => $baseDir, 'basePath' => '/w', + '//example.org/w/test.jpg' ], - [ // Same header, different options - [ - [ 'Cookie', [ 'param=phpsessid' ] ], - [ 'Cookie', [ 'param=userId' ] ], - ], - 'Vary: Cookie', - 'Key: Cookie;param=phpsessid;param=userId', + [ + 'baseDir' => $baseDir, 'basePath' => '/w', + 'https://example.org/w/test.jpg' + ], + // Unrelated path with domain component. Ignored. + [ + 'baseDir' => $baseDir, 'basePath' => '/w', + 'https://example.org/files/test.jpg' + ], + [ + 'baseDir' => $baseDir, 'basePath' => '/w', + '//example.org/files/test.jpg' + ], + // Unrelated path with domain, and empty base path (root mw install). Ignored. + [ + 'baseDir' => $baseDir, 'basePath' => '', + 'https://example.org/files/test.jpg' + ], + [ + 'baseDir' => $baseDir, 'basePath' => '', + // T155310 + '//example.org/files/test.jpg' + ], + // Check UploadPath before ResourceBasePath (T155146) + [ + 'baseDir' => dirname( $baseDir ), 'basePath' => '', + 'uploadDir' => $baseDir, 'uploadPath' => '/images', + '/images/test.jpg', + '/images/test.jpg?edcf2' ], ]; } /** - * @covers OutputPage::haveCacheVaryCookies + * Tests a particular case of transformCssMedia, using the given input, globals, + * expected return, and message + * + * Asserts that $expectedReturn is returned. + * + * options['printableQuery'] - value of query string for printable, or omitted for none + * options['handheldQuery'] - value of query string for handheld, or omitted for none + * options['media'] - passed into the method under the same name + * options['expectedReturn'] - expected return value + * options['message'] - PHPUnit message for assertion + * + * @param array $args Key-value array of arguments as shown above */ - public function testHaveCacheVaryCookies() { - $request = new FauxRequest(); - $context = new RequestContext(); - $context->setRequest( $request ); - $outputPage = new OutputPage( $context ); + protected function assertTransformCssMediaCase( $args ) { + $queryData = []; + if ( isset( $args['printableQuery'] ) ) { + $queryData['printable'] = $args['printableQuery']; + } - // No cookies are set. - $this->assertFalse( $outputPage->haveCacheVaryCookies() ); + if ( isset( $args['handheldQuery'] ) ) { + $queryData['handheld'] = $args['handheldQuery']; + } - // 'Token' is present but empty, so it shouldn't count. - $request->setCookie( 'Token', '' ); - $this->assertFalse( $outputPage->haveCacheVaryCookies() ); + $fauxRequest = new FauxRequest( $queryData, false ); + $this->setMwGlobals( [ + 'wgRequest' => $fauxRequest, + ] ); - // 'Token' present and nonempty. - $request->setCookie( 'Token', '123' ); - $this->assertTrue( $outputPage->haveCacheVaryCookies() ); + $actualReturn = OutputPage::transformCssMedia( $args['media'] ); + $this->assertSame( $args['expectedReturn'], $actualReturn, $args['message'] ); } /** - * @covers OutputPage::addCategoryLinks - * @covers OutputPage::getCategories + * Tests print requests + * + * @covers OutputPage::transformCssMedia */ - public function testGetCategories() { - $fakeResultWrapper = new FakeResultWrapper( [ - (object)[ - 'pp_value' => 1, - 'page_title' => 'Test' - ], - (object)[ - 'page_title' => 'Test2' - ] + public function testPrintRequests() { + $this->assertTransformCssMediaCase( [ + 'printableQuery' => '1', + 'media' => 'screen', + 'expectedReturn' => null, + 'message' => 'On printable request, screen returns null' ] ); - $outputPage = $this->getMockBuilder( OutputPage::class ) - ->setConstructorArgs( [ new RequestContext() ] ) - ->setMethods( [ 'addCategoryLinksToLBAndGetResult' ] ) - ->getMock(); - $outputPage->expects( $this->any() ) - ->method( 'addCategoryLinksToLBAndGetResult' ) - ->will( $this->returnValue( $fakeResultWrapper ) ); - $outputPage->addCategoryLinks( [ - 'Test' => 'Test', - 'Test2' => 'Test2', + $this->assertTransformCssMediaCase( [ + 'printableQuery' => '1', + 'media' => self::SCREEN_MEDIA_QUERY, + 'expectedReturn' => null, + 'message' => 'On printable request, screen media query returns null' + ] ); + + $this->assertTransformCssMediaCase( [ + 'printableQuery' => '1', + 'media' => self::SCREEN_ONLY_MEDIA_QUERY, + 'expectedReturn' => null, + 'message' => 'On printable request, screen media query with only returns null' + ] ); + + $this->assertTransformCssMediaCase( [ + 'printableQuery' => '1', + 'media' => 'print', + 'expectedReturn' => '', + 'message' => 'On printable request, media print returns empty string' ] ); - $this->assertEquals( [ 0 => 'Test', '1' => 'Test2' ], $outputPage->getCategories() ); - $this->assertEquals( [ 0 => 'Test2' ], $outputPage->getCategories( 'normal' ) ); - $this->assertEquals( [ 0 => 'Test' ], $outputPage->getCategories( 'hidden' ) ); } /** - * @dataProvider provideLinkHeaders - * @covers OutputPage::addLinkHeader - * @covers OutputPage::getLinkHeader + * Tests screen requests, without either query parameter set + * + * @covers OutputPage::transformCssMedia */ - public function testLinkHeaders( $headers, $result ) { - $outputPage = $this->newInstance(); + public function testScreenRequests() { + $this->assertTransformCssMediaCase( [ + 'media' => 'screen', + 'expectedReturn' => 'screen', + 'message' => 'On screen request, screen media type is preserved' + ] ); - foreach ( $headers as $header ) { - $outputPage->addLinkHeader( $header ); - } + $this->assertTransformCssMediaCase( [ + 'media' => 'handheld', + 'expectedReturn' => 'handheld', + 'message' => 'On screen request, handheld media type is preserved' + ] ); + + $this->assertTransformCssMediaCase( [ + 'media' => self::SCREEN_MEDIA_QUERY, + 'expectedReturn' => self::SCREEN_MEDIA_QUERY, + 'message' => 'On screen request, screen media query is preserved.' + ] ); + + $this->assertTransformCssMediaCase( [ + 'media' => self::SCREEN_ONLY_MEDIA_QUERY, + 'expectedReturn' => self::SCREEN_ONLY_MEDIA_QUERY, + 'message' => 'On screen request, screen media query with only is preserved.' + ] ); - $this->assertEquals( $result, $outputPage->getLinkHeader() ); + $this->assertTransformCssMediaCase( [ + 'media' => 'print', + 'expectedReturn' => 'print', + 'message' => 'On screen request, print media type is preserved' + ] ); } - public function provideLinkHeaders() { - return [ - [ - [], - false - ], - [ - [ '<https://foo/bar.jpg>;rel=preload;as=image' ], - 'Link: <https://foo/bar.jpg>;rel=preload;as=image', - ], - [ - [ '<https://foo/bar.jpg>;rel=preload;as=image','<https://foo/baz.jpg>;rel=preload;as=image' ], - 'Link: <https://foo/bar.jpg>;rel=preload;as=image,<https://foo/baz.jpg>;rel=preload;as=image', - ], - ]; + /** + * Tests handheld behavior + * + * @covers OutputPage::transformCssMedia + */ + public function testHandheld() { + $this->assertTransformCssMediaCase( [ + 'handheldQuery' => '1', + 'media' => 'handheld', + 'expectedReturn' => '', + 'message' => 'On request with handheld querystring and media is handheld, returns empty string' + ] ); + + $this->assertTransformCssMediaCase( [ + 'handheldQuery' => '1', + 'media' => 'screen', + 'expectedReturn' => null, + 'message' => 'On request with handheld querystring and media is screen, returns null' + ] ); } /** @@ -748,22 +1324,31 @@ class OutputPageTest extends MediaWikiTestCase { /** * @return OutputPage */ - private function newInstance( $config = [] ) { + private function newInstance( $config = [], WebRequest $request = null ) { $context = new RequestContext(); - $context->setConfig( new HashConfig( $config + [ - 'AppleTouchIcon' => false, - 'DisableLangConversion' => true, - 'EnableCanonicalServerLink' => false, - 'Favicon' => false, - 'Feed' => false, - 'LanguageCode' => false, - 'ReferrerPolicy' => false, - 'RightsPage' => false, - 'RightsUrl' => false, - 'UniversalEditButton' => false, + $context->setConfig( new MultiConfig( [ + new HashConfig( $config + [ + 'AppleTouchIcon' => false, + 'DisableLangConversion' => true, + 'EnableCanonicalServerLink' => false, + 'Favicon' => false, + 'Feed' => false, + 'LanguageCode' => false, + 'ReferrerPolicy' => false, + 'RightsPage' => false, + 'RightsUrl' => false, + 'UniversalEditButton' => false, + ] ), + $context->getConfig() ] ) ); + $context->setTitle( Title::newFromText( 'My test page' ) ); + + if ( $request ) { + $context->setRequest( $request ); + } + return new OutputPage( $context ); } } -- 2.20.1