mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Add more unit tests for DebugViewFriendlyErrorFormatter, tidy up Director::is_ajax() return
This commit is contained in:
parent
f40da0d552
commit
e211e27470
@ -1004,12 +1004,12 @@ 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")
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
isset($_REQUEST['ajax']) ||
|
||||
(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == "XMLHttpRequest")
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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 . '');
|
||||
}
|
||||
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user