FauxRequest: Remove influence from $wgRequest on getFullRequestURL()
authorTimo Tijhof <krinklemail@gmail.com>
Fri, 19 Apr 2019 23:35:15 +0000 (00:35 +0100)
committerTimo Tijhof <krinklemail@gmail.com>
Fri, 19 Apr 2019 23:57:49 +0000 (00:57 +0100)
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

includes/FauxRequest.php
tests/phpunit/includes/FauxRequestTest.php

index ecbc6e3..906e5e2 100644 (file)
@@ -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;
        }
index 9e7d680..c054caa 100644 (file)
@@ -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