2023-04-26 18:25:01 +12:00
< ? php
namespace SilverStripe\Control\Tests ;
use SilverStripe\Control\Controller ;
use SilverStripe\Control\Director ;
use SilverStripe\Control\RequestHandler ;
2024-09-18 13:53:44 +12:00
use SilverStripe\Control\Tests\RequestHandlerTest\RequestHandlerTestException ;
2023-04-26 18:25:01 +12:00
use SilverStripe\Dev\SapphireTest ;
2024-09-18 13:53:44 +12:00
use PHPUnit\Framework\Attributes\DataProvider ;
use SilverStripe\Dev\Exceptions\ExpectedWarningException ;
2023-04-26 18:25:01 +12:00
/**
* Tests for the RequestHandler class
*/
class RequestHandlerTest extends SapphireTest
{
protected $usesDatabase = false ;
2024-09-18 13:53:44 +12:00
public static function provideTestLink () : array
2023-04-26 18:25:01 +12:00
{
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' ,
],
];
}
2024-09-18 13:53:44 +12:00
#[DataProvider('provideTestLink')]
2023-04-26 18:25:01 +12:00
public function testLink ( ? string $urlSegment , ? string $action , ? string $expected )
{
2024-09-18 13:53:44 +12:00
$this -> enableErrorHandler ();
2023-04-26 18:25:01 +12:00
if ( $urlSegment === null ) {
2024-09-18 13:53:44 +12:00
$this -> expectException ( ExpectedWarningException :: class );
$this -> expectExceptionMessage ( 'Request handler SilverStripe\Control\RequestHandler does not have a url_segment defined. Relying on this link may be an application error' );
2023-04-26 18:25:01 +12:00
}
$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 ));
}
2024-09-18 13:53:44 +12:00
#[DataProvider('provideTestLink')]
2023-04-26 18:25:01 +12:00
public function testAbsoluteLink ( ? string $urlSegment , ? string $action , ? string $expected )
{
2024-09-18 13:53:44 +12:00
$this -> enableErrorHandler ();
2023-04-26 18:25:01 +12:00
if ( $urlSegment === null ) {
2024-09-18 13:53:44 +12:00
$this -> expectException ( ExpectedWarningException :: class );
$this -> expectExceptionMessage ( 'Request handler SilverStripe\Control\RequestHandler does not have a url_segment defined. Relying on this link may be an application error' );
2023-04-26 18:25:01 +12:00
}
$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 ));
}
}