ErrorHandlerTest.php 5.56 KB
Newer Older
1 2 3
<?php

/**
4
 * @file
5
 * Definition of Drupal\system\Tests\System\ErrorHandlerTest.
6
 */
7

8
namespace Drupal\system\Tests\System;
9

10 11
use Drupal\simpletest\WebTestBase;

12 13 14 15
/**
 * Tests error and exception handlers.
 */
class ErrorHandlerTest extends WebTestBase {
16 17 18 19 20 21 22 23

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('error_test');

24 25
  public static function getInfo() {
    return array(
26
      'name' => 'Error handlers',
27 28
      'description' => 'Performs tests on the Drupal error and exception handler.',
      'group' => 'System',
29 30 31 32 33 34 35
    );
  }

  /**
   * Test the error handler.
   */
  function testErrorHandler() {
36
    $config = config('system.logging');
37 38
    $error_notice = array(
      '%type' => 'Notice',
39
      '!message' => 'Undefined variable: bananas',
40
      '%function' => 'error_test_generate_warnings()',
41
      '%file' => drupal_get_path('module', 'error_test') . '/error_test.module',
42 43 44
    );
    $error_warning = array(
      '%type' => 'Warning',
45
      '!message' => 'Division by zero',
46
      '%function' => 'error_test_generate_warnings()',
47
      '%file' => drupal_get_path('module', 'error_test') . '/error_test.module',
48 49
    );
    $error_user_notice = array(
50
      '%type' => 'User warning',
51
      '!message' => 'Drupal is awesome',
52
      '%function' => 'error_test_generate_warnings()',
53
      '%file' => drupal_get_path('module', 'error_test') . '/error_test.module',
54 55
    );

56 57 58 59 60 61 62 63 64
    // Set error reporting to display verbose notices.
    config('system.logging')->set('error_level', ERROR_REPORTING_DISPLAY_VERBOSE)->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);
    $this->assertRaw('<pre class="backtrace">', 'Found pre element with backtrace class.');

65
    // Set error reporting to collect notices.
66
    $config->set('error_level', ERROR_REPORTING_DISPLAY_ALL)->save();
67
    $this->drupalGet('error-test/generate-warnings');
68
    $this->assertResponse(200, t('Received expected HTTP status code.'));
69 70 71
    $this->assertErrorMessage($error_notice);
    $this->assertErrorMessage($error_warning);
    $this->assertErrorMessage($error_user_notice);
72
    $this->assertNoRaw('<pre class="backtrace">', 'Did not find pre element with backtrace class.');
73 74

    // Set error reporting to not collect notices.
75
    $config->set('error_level', ERROR_REPORTING_DISPLAY_SOME)->save();
76
    $this->drupalGet('error-test/generate-warnings');
77
    $this->assertResponse(200, t('Received expected HTTP status code.'));
78 79 80
    $this->assertNoErrorMessage($error_notice);
    $this->assertErrorMessage($error_warning);
    $this->assertErrorMessage($error_user_notice);
81
    $this->assertNoRaw('<pre class="backtrace">', 'Did not find pre element with backtrace class.');
82 83

    // Set error reporting to not show any errors.
84
    $config->set('error_level', ERROR_REPORTING_HIDE)->save();
85
    $this->drupalGet('error-test/generate-warnings');
86
    $this->assertResponse(200, t('Received expected HTTP status code.'));
87 88 89
    $this->assertNoErrorMessage($error_notice);
    $this->assertNoErrorMessage($error_warning);
    $this->assertNoErrorMessage($error_user_notice);
90
    $this->assertNoRaw('<pre class="backtrace">', 'Did not find pre element with backtrace class.');
91 92 93 94 95 96 97 98
  }

  /**
   * Test the exception handler.
   */
  function testExceptionHandler() {
    $error_exception = array(
      '%type' => 'Exception',
99
      '!message' => 'Drupal is awesome',
100
      '%function' => 'error_test_trigger_exception()',
101
      '%line' => 56,
102
      '%file' => drupal_get_path('module', 'error_test') . '/error_test.module',
103 104
    );
    $error_pdo_exception = array(
105
      '%type' => 'DatabaseExceptionWrapper',
106
      '!message' => 'SELECT * FROM bananas_are_awesome',
107
      '%function' => 'error_test_trigger_pdo_exception()',
108
      '%line' => 64,
109
      '%file' => drupal_get_path('module', 'error_test') . '/error_test.module',
110 111 112
    );

    $this->drupalGet('error-test/trigger-exception');
113
    $this->assertTrue(strpos($this->drupalGetHeader(':status'), '500 Service unavailable (with message)'), t('Received expected HTTP status line.'));
114 115 116
    $this->assertErrorMessage($error_exception);

    $this->drupalGet('error-test/trigger-pdo-exception');
117
    $this->assertTrue(strpos($this->drupalGetHeader(':status'), '500 Service unavailable (with message)'), t('Received expected HTTP status line.'));
118
    // We cannot use assertErrorMessage() since the extact error reported
119
    // varies from database to database. Check that the SQL string is displayed.
120
    $this->assertText($error_pdo_exception['%type'], t('Found %type in error page.', $error_pdo_exception));
121
    $this->assertText($error_pdo_exception['!message'], t('Found !message in error page.', $error_pdo_exception));
122
    $error_details = t('in %function (line ', $error_pdo_exception);
123
    $this->assertRaw($error_details, t("Found '!message' in error page.", array('!message' => $error_details)));
124 125 126 127 128 129
  }

  /**
   * Helper function: assert that the error message is found.
   */
  function assertErrorMessage(array $error) {
130 131
    $message = t('%type: !message in %function (line ', $error);
    $this->assertRaw($message, t('Found error message: !message.', array('!message' => $message)));
132 133 134 135 136 137
  }

  /**
   * Helper function: assert that the error message is not found.
   */
  function assertNoErrorMessage(array $error) {
138 139
    $message = t('%type: !message in %function (line ', $error);
    $this->assertNoRaw($message, t('Did not find error message: !message.', array('!message' => $message)));
140 141
  }
}