From: Timo Tijhof Date: Fri, 19 Apr 2019 23:35:15 +0000 (+0100) Subject: FauxRequest: Remove influence from $wgRequest on getFullRequestURL() X-Git-Tag: 1.34.0-rc.0~1760^2 X-Git-Url: http://git.cyclocoop.org/%22%20.%20generer_url_aide%28?a=commitdiff_plain;h=9d225ee6039c56dae3275ceb96d4a1d9e3c6fe08;p=lhc%2Fweb%2Fwiklou.git FauxRequest: Remove influence from $wgRequest on getFullRequestURL() It inherited the method from WebRequest, which in turn uses PROTO_CURRENT, which indirectly makes it then look up $wgRequest->protocol to decide how to expand the url. This global state isn't expected from FauxRequest and makes its result less than predictable. Change-Id: Ia616e0bfa00c35f78d27db55f26b336a7d0c7606 --- diff --git a/includes/FauxRequest.php b/includes/FauxRequest.php index ecbc6e3373..906e5e242d 100644 --- a/includes/FauxRequest.php +++ b/includes/FauxRequest.php @@ -170,6 +170,17 @@ class FauxRequest extends WebRequest { return $this->requestUrl; } + public function getFullRequestURL() { + // Pass an explicit PROTO constant instead of PROTO_CURRENT so that we + // do not rely on state from the global $wgRequest object (which it would, + // via wfGetServerUrl/wfExpandUrl/$wgRequest->protocol). + if ( $this->protocol === 'http' ) { + return wfGetServerUrl( PROTO_HTTP ) . $this->getRequestURL(); + } else { + return wfGetServerUrl( PROTO_HTTPS ) . $this->getRequestURL(); + } + } + public function getProtocol() { return $this->protocol; } diff --git a/tests/phpunit/includes/FauxRequestTest.php b/tests/phpunit/includes/FauxRequestTest.php index 9e7d680268..c054caa06f 100644 --- a/tests/phpunit/includes/FauxRequestTest.php +++ b/tests/phpunit/includes/FauxRequestTest.php @@ -7,6 +7,16 @@ class FauxRequestTest extends PHPUnit\Framework\TestCase { use MediaWikiCoversValidator; use PHPUnit4And6Compat; + public function setUp() { + parent::setUp(); + $this->orgWgServer = $GLOBALS['wgServer']; + } + + public function tearDown() { + $GLOBALS['wgServer'] = $this->orgWgServer; + parent::tearDown(); + } + /** * @covers FauxRequest::__construct */ @@ -148,7 +158,7 @@ class FauxRequestTest extends PHPUnit\Framework\TestCase { /** * @covers FauxRequest::getRequestURL */ - public function testGetRequestURL() { + public function testGetRequestURL_disallowed() { $req = new FauxRequest(); $this->setExpectedException( MWException::class ); $req->getRequestURL(); @@ -164,6 +174,45 @@ class FauxRequestTest extends PHPUnit\Framework\TestCase { $this->assertEquals( 'https://example.org', $req->getRequestURL() ); } + /** + * @covers FauxRequest::getFullRequestURL + */ + public function testGetFullRequestURL_disallowed() { + $GLOBALS['wgServer'] = '//wiki.test'; + $req = new FauxRequest(); + + $this->setExpectedException( MWException::class ); + $req->getFullRequestURL(); + } + + /** + * @covers FauxRequest::getFullRequestURL + */ + public function testGetFullRequestURL_http() { + $GLOBALS['wgServer'] = '//wiki.test'; + $req = new FauxRequest(); + $req->setRequestURL( '/path' ); + + $this->assertSame( + 'http://wiki.test/path', + $req->getFullRequestURL() + ); + } + + /** + * @covers FauxRequest::getFullRequestURL + */ + public function testGetFullRequestURL_https() { + $GLOBALS['wgServer'] = '//wiki.test'; + $req = new FauxRequest( [], false, null, 'https' ); + $req->setRequestURL( '/path' ); + + $this->assertSame( + 'https://wiki.test/path', + $req->getFullRequestURL() + ); + } + /** * @covers FauxRequest::__construct * @covers FauxRequest::getProtocol