3 namespace Drupal\system\Tests\Common;
5 use Drupal\simpletest\WebTestBase;
8 * Tests SimpleTest error and exception collector.
12 class SimpleTestErrorCollectorTest extends WebTestBase {
19 public static $modules = ['system_test', 'error_test'];
22 * Errors triggered during the test.
24 * Errors are intercepted by the overridden implementation
25 * of Drupal\simpletest\WebTestBase::error() below.
29 protected $collectedErrors = [];
32 * Tests that simpletest collects errors from the tested site.
34 public function testErrorCollect() {
35 $this->collectedErrors = [];
36 $this->drupalGet('error-test/generate-warnings-with-report');
37 $this->assertEqual(count($this->collectedErrors), 3, 'Three errors were collected');
39 if (count($this->collectedErrors) == 3) {
40 $this->assertError($this->collectedErrors[0], 'Notice', 'Drupal\error_test\Controller\ErrorTestController->generateWarnings()', 'ErrorTestController.php', 'Undefined variable: bananas');
41 $this->assertError($this->collectedErrors[1], 'Warning', 'Drupal\error_test\Controller\ErrorTestController->generateWarnings()', 'ErrorTestController.php', 'Division by zero');
42 $this->assertError($this->collectedErrors[2], 'User warning', 'Drupal\error_test\Controller\ErrorTestController->generateWarnings()', 'ErrorTestController.php', 'Drupal & awesome');
45 // Give back the errors to the log report.
46 foreach ($this->collectedErrors as $error) {
47 parent::error($error['message'], $error['group'], $error['caller']);
53 * Stores errors into an array.
55 * This test class is trying to verify that simpletest correctly sees errors
56 * and warnings. However, it can't generate errors and warnings that
57 * propagate up to the testing framework itself, or these tests would always
58 * fail. So, this special copy of error() doesn't propagate the errors up
59 * the class hierarchy. It just stuffs them into a protected collectedErrors
60 * array for various assertions to inspect.
62 protected function error($message = '', $group = 'Other', array $caller = NULL) {
63 // Due to a WTF elsewhere, simpletest treats debug() and verbose()
64 // messages as if they were an 'error'. But, we don't want to collect
65 // those here. This function just wants to collect the real errors (PHP
66 // notices, PHP fatal errors, etc.), and let all the 'errors' from the
67 // 'User notice' group bubble up to the parent classes to be handled (and
68 // eventually displayed) as normal.
69 if ($group == 'User notice') {
70 parent::error($message, $group, $caller);
72 // Everything else should be collected but not propagated.
74 $this->collectedErrors[] = [
75 'message' => $message,
83 * Asserts that a collected error matches what we are expecting.
85 public function assertError($error, $group, $function, $file, $message = NULL) {
86 $this->assertEqual($error['group'], $group, format_string("Group was %group", ['%group' => $group]));
87 $this->assertEqual($error['caller']['function'], $function, format_string("Function was %function", ['%function' => $function]));
88 $this->assertEqual(drupal_basename($error['caller']['file']), $file, format_string("File was %file", ['%file' => $file]));
89 if (isset($message)) {
90 $this->assertEqual($error['message'], $message, format_string("Message was %message", ['%message' => $message]));