Merge pull request #3424 from halkyon/director_test_relative

BUG Fixing Director::test() failing on BASE_URL prefixed URLs
This commit is contained in:
Damian Mooyman 2014-08-22 15:46:54 +12:00
commit 0d169d5c8d
4 changed files with 46 additions and 16 deletions

View File

@ -255,9 +255,12 @@ class Director implements TemplateGlobalProvider {
} else { } else {
$_SERVER['HTTP_HOST'] = $bits['host']; $_SERVER['HTTP_HOST'] = $bits['host'];
} }
$url = Director::makeRelative($url);
} }
// Ensure URL is properly made relative.
// Example: url passed is "/ss31/my-page" (prefixed with BASE_URL), this should be changed to "my-page"
$url = self::makeRelative($url);
$urlWithQuerystring = $url; $urlWithQuerystring = $url;
if(strpos($url, '?') !== false) { if(strpos($url, '?') !== false) {
list($url, $getVarsEncoded) = explode('?', $url, 2); list($url, $getVarsEncoded) = explode('?', $url, 2);

View File

@ -110,6 +110,30 @@ class FunctionalTest extends SapphireTest {
} }
} }
/**
* Run a test while mocking the base url with the provided value
* @param string $url The base URL to use for this test
* @param callable $callback The test to run
*/
protected function withBaseURL($url, $callback) {
$oldBase = Config::inst()->get('Director', 'alternate_base_url');
Config::inst()->update('Director', 'alternate_base_url', $url);
$callback($this);
Config::inst()->update('Director', 'alternate_base_url', $oldBase);
}
/**
* Run a test while mocking the base folder with the provided value
* @param string $folder The base folder to use for this test
* @param callable $callback The test to run
*/
protected function withBaseFolder($folder, $callback) {
$oldFolder = Config::inst()->get('Director', 'alternate_base_folder');
Config::inst()->update('Director', 'alternate_base_folder', $folder);
$callback($this);
Config::inst()->update('Director', 'alternate_base_folder', $oldFolder);
}
/** /**
* Submit a get request * Submit a get request
* @uses Director::test() * @uses Director::test()

View File

@ -5,7 +5,7 @@
* @package framework * @package framework
* @subpackage tests * @subpackage tests
*/ */
class HTTPTest extends SapphireTest { class HTTPTest extends FunctionalTest {
/** /**
* Tests {@link HTTP::getLinksIn()} * Tests {@link HTTP::getLinksIn()}
@ -227,16 +227,5 @@ class HTTPTest extends SapphireTest {
); );
}); });
} }
/**
* Run a test while mocking the base url with the provided value
* @param string $url The base URL to use for this test
* @param callable $callback The test to run
*/
protected function withBaseURL($url, $callback) {
$oldBase = Config::inst()->get('Director', 'alternate_base_url');
Config::inst()->update('Director', 'alternate_base_url', $url);
$callback($this);
Config::inst()->update('Director', 'alternate_base_url', $oldBase);
}
} }

View File

@ -60,10 +60,24 @@ class RequestHandlingTest extends FunctionalTest {
$this->assertEquals("MyField requested", $response->getBody()); $this->assertEquals("MyField requested", $response->getBody());
/* We can also make a POST request on a form field, which could be used for in-place editing, for example. */ /* We can also make a POST request on a form field, which could be used for in-place editing, for example. */
$response = Director::test("testGoodBase1/TestForm/fields/MyField" ,array("MyField" => 5)); $response = Director::test("testGoodBase1/TestForm/fields/MyField", array("MyField" => 5));
$this->assertEquals("MyField posted, update to 5", $response->getBody()); $this->assertEquals("MyField posted, update to 5", $response->getBody());
} }
public function testBaseUrlPrefixed() {
$this->withBaseFolder('/silverstripe', function($test) {
$test->assertEquals(
'MyField requested',
Director::test('/silverstripe/testGoodBase1/TestForm/fields/MyField')->getBody()
);
$test->assertEquals(
'MyField posted, update to 5',
Director::test('/silverstripe/testGoodBase1/TestForm/fields/MyField', array('MyField' => 5))->getBody()
);
});
}
public function testBadBase() { public function testBadBase() {
/* We no longer support using hacky attempting to handle URL parsing with broken rules */ /* We no longer support using hacky attempting to handle URL parsing with broken rules */
$response = Director::test("testBadBase/method/1/2"); $response = Director::test("testBadBase/method/1/2");