2017-05-11 07:38:29 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\Logging\Tests;
|
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2017-05-11 07:38:29 +02:00
|
|
|
use SilverStripe\Control\Email\Email;
|
2018-10-20 14:27:57 +02:00
|
|
|
use SilverStripe\Control\HTTPRequest;
|
|
|
|
use SilverStripe\Core\Injector\Injector;
|
2017-05-11 07:38:29 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Logging\DebugViewFriendlyErrorFormatter;
|
|
|
|
|
|
|
|
class DebugViewFriendlyErrorFormatterTest extends SapphireTest
|
|
|
|
{
|
2021-10-27 04:39:47 +02:00
|
|
|
protected function setUp(): void
|
2017-05-11 07:38:29 +02:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
Email::config()->set('admin_email', 'testy@mctest.face');
|
|
|
|
}
|
|
|
|
|
2018-10-20 14:27:57 +02:00
|
|
|
public function testFormatPassesRecordCodeToOutput()
|
|
|
|
{
|
2021-10-27 04:39:47 +02:00
|
|
|
/** @var DebugViewFriendlyErrorFormatter|MockObject $mock */
|
2018-10-20 14:27:57 +02:00
|
|
|
$mock = $this->getMockBuilder(DebugViewFriendlyErrorFormatter::class)
|
2024-09-18 03:53:44 +02:00
|
|
|
->onlyMethods(['output'])
|
2018-10-20 14:27:57 +02:00
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$mock->expects($this->once())->method('output')->with(403)->willReturn('foo');
|
|
|
|
$this->assertSame('foo', $mock->format(['code' => 403]));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFormatPassesInstanceStatusCodeToOutputWhenNotProvidedByRecord()
|
|
|
|
{
|
2021-10-27 04:39:47 +02:00
|
|
|
/** @var DebugViewFriendlyErrorFormatter|MockObject $mock */
|
2018-10-20 14:27:57 +02:00
|
|
|
$mock = $this->getMockBuilder(DebugViewFriendlyErrorFormatter::class)
|
2024-09-18 03:53:44 +02:00
|
|
|
->onlyMethods(['output'])
|
2018-10-20 14:27:57 +02:00
|
|
|
->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'],
|
|
|
|
];
|
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
/** @var DebugViewFriendlyErrorFormatter|MockObject $mock */
|
2018-10-20 14:27:57 +02:00
|
|
|
$mock = $this->getMockBuilder(DebugViewFriendlyErrorFormatter::class)
|
2024-09-18 03:53:44 +02:00
|
|
|
->onlyMethods(['format'])
|
2018-10-20 14:27:57 +02:00
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$mock->expects($this->exactly(3))
|
|
|
|
->method('format')
|
|
|
|
->willReturn('foo');
|
|
|
|
|
|
|
|
$this->assertSame('foofoofoo', $mock->formatBatch($records));
|
|
|
|
}
|
|
|
|
|
2017-05-11 07:38:29 +02:00
|
|
|
public function testOutput()
|
|
|
|
{
|
|
|
|
$formatter = new DebugViewFriendlyErrorFormatter();
|
|
|
|
$formatter->setTitle("There has been an error");
|
|
|
|
$formatter->setBody("The website server has not been able to respond to your request");
|
|
|
|
|
|
|
|
$expected = <<<TEXT
|
|
|
|
WEBSITE ERROR
|
|
|
|
There has been an error
|
|
|
|
-----------------------
|
|
|
|
The website server has not been able to respond to your request
|
|
|
|
|
|
|
|
Contact an administrator: testy [at] mctest [dot] face
|
|
|
|
|
|
|
|
|
|
|
|
TEXT
|
|
|
|
;
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $formatter->output(404));
|
|
|
|
}
|
2018-10-20 14:27:57 +02:00
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
2019-10-17 23:57:35 +02:00
|
|
|
|
2019-10-03 00:31:43 +02:00
|
|
|
public function testAdminEmailWithName()
|
|
|
|
{
|
|
|
|
Email::config()->set('admin_email', ['testy@mctest.face' => 'The ad&min']);
|
|
|
|
|
|
|
|
$formatter = new DebugViewFriendlyErrorFormatter();
|
|
|
|
$formatter->setTitle("There has been an error");
|
|
|
|
$formatter->setBody("The website server has not been able to respond to your request");
|
|
|
|
|
|
|
|
$expected = <<<TEXT
|
|
|
|
WEBSITE ERROR
|
|
|
|
There has been an error
|
|
|
|
-----------------------
|
|
|
|
The website server has not been able to respond to your request
|
|
|
|
|
|
|
|
Contact The ad&min: testy [at] mctest [dot] face
|
|
|
|
|
|
|
|
|
2017-05-11 07:38:29 +02:00
|
|
|
TEXT
|
|
|
|
;
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $formatter->output(404));
|
|
|
|
}
|
|
|
|
}
|