diff --git a/src/Controllers/DevCheckController.php b/src/Controllers/DevCheckController.php index 069ebb2..0e7074d 100644 --- a/src/Controllers/DevCheckController.php +++ b/src/Controllers/DevCheckController.php @@ -43,8 +43,11 @@ class DevCheckController extends Controller $suite = $name; } - $checker = new EnvironmentChecker($suite, 'Environment status'); - $checker->setRequest($request); + /** @var EnvironmentChecker */ + $checker = EnvironmentChecker::create($suite, 'Environment status') + ->setRequest($request) + ->setIncludeDetails(true); + $checker->init($this->config()->permission); return $checker; diff --git a/src/Controllers/DevHealthController.php b/src/Controllers/DevHealthController.php index b75d8ad..c9deea0 100644 --- a/src/Controllers/DevHealthController.php +++ b/src/Controllers/DevHealthController.php @@ -28,7 +28,6 @@ class DevHealthController extends Controller public function index() { // health check does not require permission to run - $checker = new EnvironmentChecker('health', 'Site health'); $checker->setErrorCode(500); diff --git a/src/EnvironmentChecker.php b/src/EnvironmentChecker.php index d09faac..b9153f3 100644 --- a/src/EnvironmentChecker.php +++ b/src/EnvironmentChecker.php @@ -40,6 +40,11 @@ class EnvironmentChecker extends RequestHandler */ protected $title; + /** + * @var bool + */ + protected $includeDetails = false; + /** * @var int */ @@ -172,19 +177,31 @@ class EnvironmentChecker extends RequestHandler $response->setStatusCode($this->errorCode); } - $resultText = $result->customise([ + $data = [ 'URL' => Director::absoluteBaseURL(), 'Title' => $this->title, 'Name' => $this->checkSuiteName, - 'ErrorCode' => $this->errorCode, - ])->renderWith(__CLASS__); + 'ErrorCode' => $this->errorCode + ]; + + $emailContent = $result->customise(array_merge($data, [ + 'IncludeDetails' => true + ]))->renderWith(__CLASS__); + + if (!$this->includeDetails) { + $webContent = $result->customise(array_merge($data, [ + 'IncludeDetails' => false + ]))->renderWith(__CLASS__); + } else { + $webContent = $emailContent; + } if ($this->config()->get('email_results') && !$result->ShouldPass()) { $email = new Email( $this->config()->get('from_email_address'), $this->config()->get('to_email_address'), $this->title, - $resultText + $emailContent ); $email->send(); } @@ -213,7 +230,7 @@ class EnvironmentChecker extends RequestHandler return $response; } - $response->setBody($resultText); + $response->setBody($webContent); return $response; } @@ -229,16 +246,36 @@ class EnvironmentChecker extends RequestHandler Injector::inst()->get(LoggerInterface::class)->log($level, $message); } + /** * Set the HTTP status code that should be returned when there's an error. * * @param int $errorCode + * + * @return $this */ public function setErrorCode($errorCode) { $this->errorCode = $errorCode; + + return $this; } + /** + * Set whether to include the full breakdown of services + * + * @param bool $includeDetails + * + * @return $this + */ + public function setIncludeDetails($includeDetails) + { + $this->includeDetails = $includeDetails; + + return $this; + } + + /** * @deprecated * @param string $from diff --git a/templates/SilverStripe/EnvironmentCheck/EnvironmentChecker.ss b/templates/SilverStripe/EnvironmentCheck/EnvironmentChecker.ss index 8704131..dc2bab7 100644 --- a/templates/SilverStripe/EnvironmentCheck/EnvironmentChecker.ss +++ b/templates/SilverStripe/EnvironmentCheck/EnvironmentChecker.ss @@ -64,16 +64,18 @@

$Title: $Status

Site: $URL

+ <% if $IncludeDetails %> <% loop $Details %> <% end_loop %>
Check Status Message
$Check $Status $Message.XML
+ <% end_if %> <% if $ShouldPass %> -

Site is available

-

(you may check for the presence of the text 'Site is available' rather than an HTTP $ErrorCode error on this page, if you prefer.)

+

Site is available

+

(you may check for the presence of the text 'Site is available' rather than an HTTP $ErrorCode error on this page, if you prefer.<% if not $IncludeDetails %> Full details are available for logged in users at dev/check<% end_if %>)

<% else %> <% if $Name == "check" %>

A subsystem of the site is unavailable, but the site remains operational

diff --git a/tests/Controllers/DevCheckControllerTest.php b/tests/Controllers/DevCheckControllerTest.php index bf0798c..c8d3e26 100644 --- a/tests/Controllers/DevCheckControllerTest.php +++ b/tests/Controllers/DevCheckControllerTest.php @@ -28,4 +28,14 @@ class DevCheckControllerTest extends SapphireTest $this->assertInstanceOf(EnvironmentChecker::class, $controller->index($request)); } + + public function testCheckIncludesDetails() + { + $controller = new DevCheckController(); + $request = new HTTPRequest('GET', 'example.com'); + + $response = $controller->index($request)->index(); + + $this->assertStringContainsString('', $response->getBody()); + } } diff --git a/tests/Controllers/DevHealthControllerTest.php b/tests/Controllers/DevHealthControllerTest.php index 5dd2c98..727c949 100644 --- a/tests/Controllers/DevHealthControllerTest.php +++ b/tests/Controllers/DevHealthControllerTest.php @@ -37,4 +37,15 @@ class DevHealthControllerTest extends SapphireTest $this->assertInstanceOf(EnvironmentChecker::class, $controller->index($request)); } + + + public function testHealthDoesNotIncludeDetails() + { + $controller = new DevHealthController(); + $request = new HTTPRequest('GET', 'example.com'); + + $response = $controller->index($request)->index(); + + $this->assertFalse(strpos($response->getBody(), '
')); + } }