2015-03-06 23:37:06 +01:00
|
|
|
<?php
|
2014-08-06 06:29:52 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Dev\Tests;
|
|
|
|
|
|
|
|
use SilverStripe\Dev\DevelopmentAdmin;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\FunctionalTest;
|
|
|
|
use SilverStripe\Control\Director;
|
2016-10-14 03:30:05 +02:00
|
|
|
use Exception;
|
|
|
|
use SilverStripe\Dev\Tests\DevAdminControllerTest\Controller1;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2014-08-06 06:29:52 +02:00
|
|
|
/**
|
|
|
|
* Note: the running of this test is handled by the thing it's testing (DevelopmentAdmin controller).
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
class DevAdminControllerTest extends FunctionalTest
|
|
|
|
{
|
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
protected function setUp(): void
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
DevelopmentAdmin::config()->update(
|
|
|
|
'registered_controllers',
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
'x1' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'controller' => Controller1::class,
|
2020-04-20 19:58:09 +02:00
|
|
|
'links' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'x1' => 'x1 link description',
|
|
|
|
'x1/y1' => 'x1/y1 link description'
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
|
|
|
],
|
|
|
|
'x2' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'controller' => 'DevAdminControllerTest_Controller2', // intentionally not a class that exists
|
2020-04-20 19:58:09 +02:00
|
|
|
'links' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'x2' => 'x2 link description'
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testGoodRegisteredControllerOutput()
|
|
|
|
{
|
|
|
|
// Check for the controller running from the registered url above
|
2017-01-30 16:35:43 +01:00
|
|
|
// (we use contains rather than equals because sometimes you get a warning)
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString(Controller1::OK_MSG, $this->getCapture('/dev/x1'));
|
|
|
|
$this->assertStringContainsString(Controller1::OK_MSG, $this->getCapture('/dev/x1/y1'));
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGoodRegisteredControllerStatus()
|
|
|
|
{
|
|
|
|
// Check response code is 200/OK
|
|
|
|
$this->assertEquals(false, $this->getAndCheckForError('/dev/x1'));
|
|
|
|
$this->assertEquals(false, $this->getAndCheckForError('/dev/x1/y1'));
|
|
|
|
|
|
|
|
// Check response code is 500/ some sort of error
|
|
|
|
$this->assertEquals(true, $this->getAndCheckForError('/dev/x2'));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected function getCapture($url)
|
|
|
|
{
|
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$this->get($url);
|
|
|
|
$r = ob_get_contents();
|
|
|
|
ob_end_clean();
|
|
|
|
|
|
|
|
return $r;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getAndCheckForError($url)
|
|
|
|
{
|
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
|
|
|
|
if (Director::is_cli()) {
|
|
|
|
// when in CLI the admin controller throws exceptions
|
|
|
|
ob_start();
|
|
|
|
try {
|
|
|
|
$this->get($url);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
ob_end_clean();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ob_end_clean();
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
// when in http the admin controller sets a response header
|
|
|
|
ob_start();
|
|
|
|
$resp = $this->get($url);
|
|
|
|
ob_end_clean();
|
|
|
|
return $resp->isError();
|
|
|
|
}
|
|
|
|
}
|
2014-08-06 06:29:52 +02:00
|
|
|
}
|