Skip to content
Snippets Groups Projects
Select Git revision
  • 04a2523997d0363012f37c4a66e8cd924fbd9f8e
  • 11.x default protected
  • 11.2.x protected
  • 10.6.x protected
  • 10.5.x protected
  • 11.1.x protected
  • 10.4.x protected
  • 11.0.x protected
  • 10.3.x protected
  • 7.x protected
  • 10.2.x protected
  • 10.1.x protected
  • 9.5.x protected
  • 10.0.x protected
  • 9.4.x protected
  • 9.3.x protected
  • 9.2.x protected
  • 9.1.x protected
  • 8.9.x protected
  • 9.0.x protected
  • 8.8.x protected
  • 10.5.1 protected
  • 11.2.2 protected
  • 11.2.1 protected
  • 11.2.0 protected
  • 10.5.0 protected
  • 11.2.0-rc2 protected
  • 10.5.0-rc1 protected
  • 11.2.0-rc1 protected
  • 10.4.8 protected
  • 11.1.8 protected
  • 10.5.0-beta1 protected
  • 11.2.0-beta1 protected
  • 11.2.0-alpha1 protected
  • 10.4.7 protected
  • 11.1.7 protected
  • 10.4.6 protected
  • 11.1.6 protected
  • 10.3.14 protected
  • 10.4.5 protected
  • 11.0.13 protected
41 results

ErrorHandlerTest.php

Blame
  • webchick's avatar
    Issue #1493108 by marcingy, Mike Wacker, sun, kbasarab, Berdir, Rok Žlender,...
    Angie Byron authored
    Issue #1493108 by marcingy, Mike Wacker, sun, kbasarab, Berdir, Rok Žlender, cosmicdreams, alexpott: Convert logging and error settings to configuration system.
    04a25239
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    ErrorHandlerTest.php 4.72 KiB
    <?php
    
    /**
     * @file
     * Definition of Drupal\system\Tests\System\ErrorHandlerTest.
     */
    
    namespace Drupal\system\Tests\System;
    
    use Drupal\simpletest\WebTestBase;
    
    /**
     * Tests error and exception handlers.
     */
    class ErrorHandlerTest extends WebTestBase {
      public static function getInfo() {
        return array(
          'name' => 'Error handlers',
          'description' => 'Performs tests on the Drupal error and exception handler.',
          'group' => 'System',
        );
      }
    
      function setUp() {
        parent::setUp('error_test');
      }
    
      /**
       * Test the error handler.
       */
      function testErrorHandler() {
        $config = config('system.logging');
        $error_notice = array(
          '%type' => 'Notice',
          '!message' => 'Undefined variable: bananas',
          '%function' => 'error_test_generate_warnings()',
          '%file' => drupal_get_path('module', 'error_test') . '/error_test.module',
        );
        $error_warning = array(
          '%type' => 'Warning',
          '!message' => 'Division by zero',
          '%function' => 'error_test_generate_warnings()',
          '%file' => drupal_get_path('module', 'error_test') . '/error_test.module',
        );
        $error_user_notice = array(
          '%type' => 'User warning',
          '!message' => 'Drupal is awesome',
          '%function' => 'error_test_generate_warnings()',
          '%file' => drupal_get_path('module', 'error_test') . '/error_test.module',
        );
    
        // Set error reporting to collect notices.
        $config->set('error_level', ERROR_REPORTING_DISPLAY_ALL)->save();
        $this->drupalGet('error-test/generate-warnings');
        $this->assertResponse(200, t('Received expected HTTP status code.'));
        $this->assertErrorMessage($error_notice);
        $this->assertErrorMessage($error_warning);
        $this->assertErrorMessage($error_user_notice);
    
        // Set error reporting to not collect notices.
        $config->set('error_level', ERROR_REPORTING_DISPLAY_SOME)->save();
        $this->drupalGet('error-test/generate-warnings');
        $this->assertResponse(200, t('Received expected HTTP status code.'));
        $this->assertNoErrorMessage($error_notice);
        $this->assertErrorMessage($error_warning);
        $this->assertErrorMessage($error_user_notice);
    
        // Set error reporting to not show any errors.
        $config->set('error_level', ERROR_REPORTING_HIDE)->save();
        $this->drupalGet('error-test/generate-warnings');
        $this->assertResponse(200, t('Received expected HTTP status code.'));
        $this->assertNoErrorMessage($error_notice);
        $this->assertNoErrorMessage($error_warning);
        $this->assertNoErrorMessage($error_user_notice);
      }
    
      /**
       * Test the exception handler.
       */
      function testExceptionHandler() {
        $error_exception = array(
          '%type' => 'Exception',
          '!message' => 'Drupal is awesome',
          '%function' => 'error_test_trigger_exception()',
          '%line' => 56,
          '%file' => drupal_get_path('module', 'error_test') . '/error_test.module',
        );
        $error_pdo_exception = array(
          '%type' => 'DatabaseExceptionWrapper',
          '!message' => 'SELECT * FROM bananas_are_awesome',
          '%function' => 'error_test_trigger_pdo_exception()',
          '%line' => 64,
          '%file' => drupal_get_path('module', 'error_test') . '/error_test.module',
        );
    
        $this->drupalGet('error-test/trigger-exception');
        $this->assertTrue(strpos($this->drupalGetHeader(':status'), '500 Service unavailable (with message)'), t('Received expected HTTP status line.'));
        $this->assertErrorMessage($error_exception);
    
        $this->drupalGet('error-test/trigger-pdo-exception');
        $this->assertTrue(strpos($this->drupalGetHeader(':status'), '500 Service unavailable (with message)'), t('Received expected HTTP status line.'));
        // We cannot use assertErrorMessage() since the extact error reported
        // varies from database to database. Check that the SQL string is displayed.
        $this->assertText($error_pdo_exception['%type'], t('Found %type in error page.', $error_pdo_exception));
        $this->assertText($error_pdo_exception['!message'], t('Found !message in error page.', $error_pdo_exception));
        $error_details = t('in %function (line ', $error_pdo_exception);
        $this->assertRaw($error_details, t("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, t('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, t('Did not find error message: !message.', array('!message' => $message)));
      }
    }