From 15adbabc3a1d564241ee09ac8091760b302b6b6f Mon Sep 17 00:00:00 2001 From: Ori Livneh Date: Wed, 7 Oct 2015 21:45:26 -0700 Subject: [PATCH] Fix-up for I949fc289d: handle falsy cookie values correctly Ensure that OutputPage::haveVaryCacheCookies() handles falsey values correctly, and that it requires a cookie to have a nonempty value before it declares a match. Change-Id: I2afe54b62c940187a427498cb4037e1dd0e78dd9 --- includes/OutputPage.php | 2 +- tests/phpunit/includes/OutputPageTest.php | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/includes/OutputPage.php b/includes/OutputPage.php index 755b165a45..f48497fd8b 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -2004,7 +2004,7 @@ class OutputPage extends ContextSource { function haveCacheVaryCookies() { $request = $this->getRequest(); foreach ( $this->getCacheVaryCookies() as $cookieName ) { - if ( $request->getCookie( $cookieName, '' ) ) { + if ( $request->getCookie( $cookieName, '', '' ) !== '' ) { wfDebug( __METHOD__ . ": found $cookieName\n" ); return true; } diff --git a/tests/phpunit/includes/OutputPageTest.php b/tests/phpunit/includes/OutputPageTest.php index aa6655d2a7..5f21e07186 100644 --- a/tests/phpunit/includes/OutputPageTest.php +++ b/tests/phpunit/includes/OutputPageTest.php @@ -339,6 +339,27 @@ class OutputPageTest extends MediaWikiTestCase { ), ); } + + /** + * @covers OutputPage::haveCacheVaryCookies + */ + function testHaveCacheVaryCookies() { + $request = new FauxRequest(); + $context = new RequestContext(); + $context->setRequest( $request ); + $outputPage = new OutputPage( $context ); + + // No cookies are set. + $this->assertFalse( $outputPage->haveCacheVaryCookies() ); + + // 'Token' is present but empty, so it shouldn't count. + $request->setCookie( 'Token', '' ); + $this->assertFalse( $outputPage->haveCacheVaryCookies() ); + + // 'Token' present and nonempty. + $request->setCookie( 'Token', '123' ); + $this->assertTrue( $outputPage->haveCacheVaryCookies() ); + } } /** -- 2.20.1