- <?php
-
- * Tests Backdrop error and exception handlers.
- */
- class BackdropErrorHandlerUnitTest extends BackdropWebTestCase {
- function setUp() {
- parent::setUp('error_test');
- }
-
-
- * Test the error handler.
- */
- function testErrorHandler() {
- $config = config('system.core');
- $error_notice = array(
- '%type' => 'Notice',
- '!message' => 'Object of class stdClass could not be converted to int',
- '%function' => 'error_test_generate_warnings()',
- '%file' => backdrop_realpath('core/modules/simpletest/tests/error_test.module'),
- );
- $error_warning = array(
- '%type' => 'Warning',
- '!message' => \PHP_VERSION_ID < 80000 ? 'Invalid argument supplied for foreach()' : 'foreach() argument must be of type array|object, string given',
- '%function' => 'error_test_generate_warnings()',
- '%file' => backdrop_realpath('core/modules/simpletest/tests/error_test.module'),
- );
- $error_user_notice = array(
- '%type' => 'User warning',
- '!message' => 'Backdrop is awesome',
- '%function' => 'error_test_generate_warnings()',
- '%file' => backdrop_realpath('core/modules/simpletest/tests/error_test.module'),
- );
-
-
- $config->set('error_level', ERROR_REPORTING_DISPLAY_ALL)->save();
- $this->backdropGet('error-test/generate-warnings');
- $this->assertResponse(200, 'Received expected HTTP status code.');
- $this->assertErrorMessage($error_notice);
- $this->assertErrorMessage($error_warning);
- $this->assertErrorMessage($error_user_notice);
-
-
- $config->set('error_level', ERROR_REPORTING_DISPLAY_SOME)->save();
- $this->backdropGet('error-test/generate-warnings');
- $this->assertResponse(200, 'Received expected HTTP status code.');
- $this->assertNoErrorMessage($error_notice);
- $this->assertErrorMessage($error_warning);
- $this->assertErrorMessage($error_user_notice);
-
-
- $config->set('error_level', ERROR_REPORTING_HIDE)->save();
- $this->backdropGet('error-test/generate-warnings');
- $this->assertResponse(200, 'Received expected HTTP status code.');
- $this->assertNoErrorMessage($error_notice);
- $this->assertNoErrorMessage($error_warning);
- $this->assertNoErrorMessage($error_user_notice);
- }
-
-
- * Test the exception handler.
- */
- function testExceptionHandler() {
- $config = config('system.core');
- $error_exception = array(
- '%type' => 'Exception',
- '!message' => 'Backdrop is awesome',
- '%function' => 'error_test_trigger_exception()',
- '%line' => 57,
- '%file' => backdrop_realpath('core/modules/simpletest/tests/error_test.module'),
- );
- $error_pdo_exception = array(
- '%type' => 'PDOException',
- '!message' => 'SELECT * FROM bananas_are_awesome',
- '%function' => 'error_test_trigger_pdo_exception()',
- '%line' => 65,
- '%file' => backdrop_realpath('core/modules/simpletest/tests/error_test.module'),
- );
-
-
- $config->set('error_level', ERROR_REPORTING_DISPLAY_ALL)->save();
-
- $this->backdropGet('error-test/trigger-exception');
- $this->assertTrue(strpos($this->backdropGetHeader(':status'), '500 Service unavailable (with message)'), 'Received expected HTTP status line.');
- $this->assertErrorMessage($error_exception);
-
- $this->backdropGet('error-test/trigger-pdo-exception');
- $this->assertTrue(strpos($this->backdropGetHeader(':status'), '500 Service unavailable (with message)'), 'Received expected HTTP status line.');
-
-
- $this->assertText($error_pdo_exception['%type'], format_string('Found %type in error page.', $error_pdo_exception));
- $this->assertText($error_pdo_exception['!message'], format_string('Found !message in error page.', $error_pdo_exception));
- $error_details = format_string('in %function (line ', $error_pdo_exception);
- $this->assertRaw($error_details, format_string("Found '!message' in error page.", array('!message' => $error_details)));
- }
-
-
- * Helper function: assert that the error message is found.
- */
- function assertErrorMessage(array $error) {
- $message = t('%type: !message in %function (line ', $error);
- $this->assertRaw($message, format_string('Found error message: !message.', array('!message' => $message)));
- }
-
-
- * Helper function: assert that the error message is not found.
- */
- function assertNoErrorMessage(array $error) {
- $message = t('%type: !message in %function (line ', $error);
- $this->assertNoRaw($message, format_string('Did not find error message: !message.', array('!message' => $message)));
- }
- }