mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
API Add AbsoluteLink method to RequestHandler (#10749)
This is a method that is commonly implemented on controllers, but it really doesn't need to be.
This commit is contained in:
parent
2c874a1e94
commit
73ef035bd8
@ -551,10 +551,13 @@ class RequestHandler extends ViewableData
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a link to this controller. Overload with your own Link rules if they exist.
|
||||
* Returns a link to this controller.
|
||||
* Returns null if no link could be generated.
|
||||
*
|
||||
* Overload with your own Link rules if they exist.
|
||||
*
|
||||
* @param string $action Optional action
|
||||
* @return string
|
||||
* @return ?string
|
||||
*/
|
||||
public function Link($action = null)
|
||||
{
|
||||
@ -576,6 +579,22 @@ class RequestHandler extends ViewableData
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the absolute URL for this controller, including protocol and host.
|
||||
* Returns null if no link could be generated.
|
||||
*
|
||||
* @param string $action See {@link Link()}
|
||||
* @return ?string
|
||||
*/
|
||||
public function AbsoluteLink($action = '')
|
||||
{
|
||||
$link = $this->Link($action);
|
||||
if ($link === null) {
|
||||
return null;
|
||||
}
|
||||
return Director::absoluteURL((string) $link);
|
||||
}
|
||||
|
||||
/**
|
||||
* Redirect to the given URL.
|
||||
*/
|
||||
|
97
tests/php/Control/RequestHandlerTest.php
Normal file
97
tests/php/Control/RequestHandlerTest.php
Normal file
@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
namespace SilverStripe\Control\Tests;
|
||||
|
||||
use SilverStripe\Control\Controller;
|
||||
use SilverStripe\Control\Director;
|
||||
use SilverStripe\Control\RequestHandler;
|
||||
use SilverStripe\Dev\SapphireTest;
|
||||
|
||||
/**
|
||||
* Tests for the RequestHandler class
|
||||
*/
|
||||
class RequestHandlerTest extends SapphireTest
|
||||
{
|
||||
protected $usesDatabase = false;
|
||||
|
||||
public function provideTestLink(): array
|
||||
{
|
||||
return [
|
||||
// If there's no url segment, there's no link
|
||||
[
|
||||
'urlSegment' => null,
|
||||
'action' => null,
|
||||
'expected' => null,
|
||||
],
|
||||
[
|
||||
'urlSegment' => null,
|
||||
'action' => 'some-action',
|
||||
'expected' => null,
|
||||
],
|
||||
// The action is just addeed on after the url segment
|
||||
[
|
||||
'urlSegment' => 'my-controller',
|
||||
'action' => null,
|
||||
'expected' => 'my-controller',
|
||||
],
|
||||
[
|
||||
'urlSegment' => 'my-controller',
|
||||
'action' => 'some-action',
|
||||
'expected' => 'my-controller/some-action',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideTestLink
|
||||
*/
|
||||
public function testLink(?string $urlSegment, ?string $action, ?string $expected)
|
||||
{
|
||||
if ($urlSegment === null) {
|
||||
$this->expectWarning();
|
||||
$this->expectWarningMessage('Request handler SilverStripe\Control\RequestHandler does not have a url_segment defined. Relying on this link may be an application error');
|
||||
}
|
||||
|
||||
$handler = new RequestHandler();
|
||||
RequestHandler::config()->set('url_segment', $urlSegment);
|
||||
Controller::config()->set('add_trailing_slash', false);
|
||||
|
||||
$this->assertEquals($expected, $handler->Link($action));
|
||||
|
||||
// Validate that trailing slash config is respected
|
||||
Controller::config()->set('add_trailing_slash', true);
|
||||
if (is_string($expected)) {
|
||||
$expected .= '/';
|
||||
}
|
||||
|
||||
$this->assertEquals($expected, $handler->Link($action));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideTestLink
|
||||
*/
|
||||
public function testAbsoluteLink(?string $urlSegment, ?string $action, ?string $expected)
|
||||
{
|
||||
if ($urlSegment === null) {
|
||||
$this->expectWarning();
|
||||
$this->expectWarningMessage('Request handler SilverStripe\Control\RequestHandler does not have a url_segment defined. Relying on this link may be an application error');
|
||||
}
|
||||
|
||||
$handler = new RequestHandler();
|
||||
RequestHandler::config()->set('url_segment', $urlSegment);
|
||||
Controller::config()->set('add_trailing_slash', false);
|
||||
|
||||
if ($expected !== null) {
|
||||
$expected = Director::absoluteURL($expected);
|
||||
}
|
||||
$this->assertEquals($expected, $handler->AbsoluteLink($action));
|
||||
|
||||
// Validate that trailing slash config is respected
|
||||
Controller::config()->set('add_trailing_slash', true);
|
||||
if (is_string($expected)) {
|
||||
$expected = Director::absoluteURL($expected . '/');
|
||||
}
|
||||
|
||||
$this->assertEquals(Director::absoluteURL($expected), $handler->AbsoluteLink($action));
|
||||
}
|
||||
}
|
@ -17,7 +17,7 @@ use SilverStripe\Forms\Form;
|
||||
use SilverStripe\Security\SecurityToken;
|
||||
|
||||
/**
|
||||
* Tests for RequestHandler and HTTPRequest.
|
||||
* Tests for functionality related to handling requests - not unit tests for RequestHandler.
|
||||
* We've set up a simple URL handling model based on
|
||||
*/
|
||||
class RequestHandlingTest extends FunctionalTest
|
||||
|
Loading…
Reference in New Issue
Block a user