Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #3011 from tractorcow/pulls/absurl

Docs / Tests for Director::absoluteURL
  • Loading branch information...
commit 5b0a96979484fad12e11ce69aef98feda57b321f 2 parents 9230d41 + 15c6b67
@hafriedlander hafriedlander authored
View
7 control/Director.php
@@ -355,7 +355,12 @@ public static function set_current_page($page) {
/**
* Turns the given URL into an absolute URL.
- * @todo Document how relativeToSiteBase works
+ * By default non-site root relative urls will be evaluated relative to the current request.
+ *
+ * @param string $url URL To transform to absolute
+ * @param bool $relativeToSiteBase Flag indicating if non-site root relative urls should be
+ * evaluated relative to the site BaseURL instead of the current url.
+ * @return string The fully qualified URL
*/
public static function absoluteURL($url, $relativeToSiteBase = false) {
if(!isset($_SERVER['REQUEST_URI'])) return false;
View
2  control/HTTPResponse.php
@@ -213,7 +213,7 @@ public function output() {
}
if(in_array($this->statusCode, self::$redirect_codes) && headers_sent($file, $line)) {
- $url = (string)$this->headers['Location'];
+ $url = Director::absoluteURL($this->headers['Location'], true);
$urlATT = Convert::raw2htmlatt($url);
$urlJS = Convert::raw2js($url);
echo
View
37 tests/control/DirectorTest.php
@@ -29,6 +29,9 @@ public function setUp() {
public function tearDown() {
// TODO Remove director rule, currently API doesnt allow this
+ // Remove base URL override (setting to false reverts to default behaviour)
+ Director::setBaseURL(false);
+
// Reinstate the original REQUEST_URI after it was modified by some tests
$_SERVER['REQUEST_URI'] = self::$originalRequestURI;
@@ -54,6 +57,40 @@ public function testFileExists() {
unlink($tempFilePath);
}
+
+ public function testAbsoluteURL() {
+
+ $rootURL = Director::protocolAndHost();
+ $_SERVER['REQUEST_URI'] = "$rootURL/mysite/sub-page/";
+ Director::setBaseURL('/mysite/');
+
+ // Test already absolute url
+ $this->assertEquals($rootURL, Director::absoluteURL($rootURL));
+ $this->assertEquals($rootURL, Director::absoluteURL($rootURL, true));
+ $this->assertEquals('http://www.mytest.com', Director::absoluteURL('http://www.mytest.com'));
+ $this->assertEquals('http://www.mytest.com', Director::absoluteURL('http://www.mytest.com', true));
+ $this->assertEquals("$rootURL/test", Director::absoluteURL("$rootURL/test"));
+ $this->assertEquals("$rootURL/test", Director::absoluteURL("$rootURL/test", true));
+
+ // Test relative to base
+ $this->assertEquals("$rootURL/mysite/test", Director::absoluteURL("test", true));
+ $this->assertEquals("$rootURL/mysite/test/url", Director::absoluteURL("test/url", true));
+ $this->assertEquals("$rootURL/root", Director::absoluteURL("/root", true));
+ $this->assertEquals("$rootURL/root/url", Director::absoluteURL("/root/url", true));
+
+ // Test relative to requested page
+ $this->assertEquals("$rootURL/mysite/sub-page/test", Director::absoluteURL("test"));
+ // Legacy behaviour resolves this to $rootURL/mysite/test/url
+ //$this->assertEquals("$rootURL/mysite/sub-page/test/url", Director::absoluteURL("test/url"));
+ $this->assertEquals("$rootURL/root", Director::absoluteURL("/root"));
+ $this->assertEquals("$rootURL/root/url", Director::absoluteURL("/root/url"));
+
+ // Test that javascript links are not left intact
+ $this->assertStringStartsNotWith('javascript', Director::absoluteURL('javascript:alert("attack")'));
+ $this->assertStringStartsNotWith('alert', Director::absoluteURL('javascript:alert("attack")'));
+ $this->assertStringStartsNotWith('javascript', Director::absoluteURL('alert("attack")'));
+ $this->assertStringStartsNotWith('alert', Director::absoluteURL('alert("attack")'));
+ }
public function testAlternativeBaseURL() {
// relative base URLs - you should end them in a /
Please sign in to comment.
Something went wrong with that request. Please try again.