Add more unit tests for DebugViewFriendlyErrorFormatter, tidy up Director::is_ajax() return

This commit is contained in:
Robbie Averill 2018-10-20 14:27:57 +02:00
parent f40da0d552
commit e211e27470
3 changed files with 69 additions and 9 deletions

View File

@ -1004,13 +1004,13 @@ class Director implements TemplateGlobalProvider
$request = self::currentRequest($request);
if ($request) {
return $request->isAjax();
} else {
}
return (
isset($_REQUEST['ajax']) ||
(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == "XMLHttpRequest")
);
}
}
/**
* Returns true if this script is being run from the command line rather than the web server.

View File

@ -97,7 +97,7 @@ class DebugViewFriendlyErrorFormatter implements FormatterInterface
public function format(array $record)
{
// Get error code
$code = empty($record['code']) ? $this->statusCode : $record['code'];
$code = empty($record['code']) ? $this->getStatusCode() : $record['code'];
return $this->output($code);
}
@ -127,8 +127,9 @@ class DebugViewFriendlyErrorFormatter implements FormatterInterface
$output = $renderer->renderHeader();
$output .= $renderer->renderInfo("Website Error", $this->getTitle(), $this->getBody());
if (Email::config()->admin_email) {
$mailto = Email::obfuscate(Email::config()->admin_email);
$adminEmail = Email::config()->get('admin_email');
if ($adminEmail) {
$mailto = Email::obfuscate($adminEmail);
$output .= $renderer->renderParagraph('Contact an administrator: ' . $mailto . '');
}

View File

@ -2,18 +2,66 @@
namespace SilverStripe\Logging\Tests;
use PHPUnit_Framework_MockObject_MockObject;
use SilverStripe\Control\Email\Email;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\DebugView;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Logging\DebugViewFriendlyErrorFormatter;
class DebugViewFriendlyErrorFormatterTest extends SapphireTest
{
public function setUp()
protected function setUp()
{
parent::setUp();
Email::config()->set('admin_email', 'testy@mctest.face');
}
public function testFormatPassesRecordCodeToOutput()
{
/** @var DebugViewFriendlyErrorFormatter|PHPUnit_Framework_MockObject_MockObject $mock */
$mock = $this->getMockBuilder(DebugViewFriendlyErrorFormatter::class)
->setMethods(['output'])
->getMock();
$mock->expects($this->once())->method('output')->with(403)->willReturn('foo');
$this->assertSame('foo', $mock->format(['code' => 403]));
}
public function testFormatPassesInstanceStatusCodeToOutputWhenNotProvidedByRecord()
{
/** @var DebugViewFriendlyErrorFormatter|PHPUnit_Framework_MockObject_MockObject $mock */
$mock = $this->getMockBuilder(DebugViewFriendlyErrorFormatter::class)
->setMethods(['output'])
->getMock();
$mock->setStatusCode(404);
$mock->expects($this->once())->method('output')->with(404)->willReturn('foo');
$this->assertSame('foo', $mock->format(['notacode' => 'bar']));
}
public function testFormatBatch()
{
$records = [
['message' => 'bar'],
['open' => 'sausage'],
['horse' => 'caballo'],
];
/** @var DebugViewFriendlyErrorFormatter|PHPUnit_Framework_MockObject_MockObject $mock */
$mock = $this->getMockBuilder(DebugViewFriendlyErrorFormatter::class)
->setMethods(['format'])
->getMock();
$mock->expects($this->exactly(3))
->method('format')
->willReturn('foo');
$this->assertSame('foofoofoo', $mock->formatBatch($records));
}
public function testOutput()
{
$formatter = new DebugViewFriendlyErrorFormatter();
@ -34,4 +82,15 @@ TEXT
$this->assertEquals($expected, $formatter->output(404));
}
public function testOutputReturnsTitleWhenRequestIsAjax()
{
// Mock an AJAX request
Injector::inst()->registerService(new HTTPRequest('GET', '', ['ajax' => true]));
$formatter = new DebugViewFriendlyErrorFormatter();
$formatter->setTitle('The Diary of Anne Frank');
$this->assertSame('The Diary of Anne Frank', $formatter->output(200));
}
}