Skip to content

Commit

Permalink
API Add AbsoluteLink method to RequestHandler (#10749)
Browse files Browse the repository at this point in the history
This is a method that is commonly implemented on controllers, but
it really doesn't need to be.
  • Loading branch information
GuySartorelli committed Apr 26, 2023
1 parent 2c874a1 commit 73ef035
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 3 deletions.
23 changes: 21 additions & 2 deletions src/Control/RequestHandler.php
Expand Up @@ -551,10 +551,13 @@ public function setRequest(HTTPRequest $request): static
}

/**
* 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)
{
Expand All @@ -576,6 +579,22 @@ public function Link($action = null)
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.
*/
Expand Down
97 changes: 97 additions & 0 deletions tests/php/Control/RequestHandlerTest.php
@@ -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));
}
}
2 changes: 1 addition & 1 deletion tests/php/Control/RequestHandlingTest.php
Expand Up @@ -17,7 +17,7 @@
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
Expand Down

0 comments on commit 73ef035

Please sign in to comment.