9af773b86cda026f7f2ef66680376f39ed282dd3
[yaffs-website] / web / core / modules / system / src / Tests / Common / SimpleTestErrorCollectorTest.php
1 <?php
2
3 namespace Drupal\system\Tests\Common;
4
5 use Drupal\simpletest\WebTestBase;
6
7 /**
8  * Tests SimpleTest error and exception collector.
9  *
10  * @group Common
11  */
12 class SimpleTestErrorCollectorTest extends WebTestBase {
13
14   /**
15    * Modules to enable.
16    *
17    * @var array
18    */
19   public static $modules = ['system_test', 'error_test'];
20
21   /**
22    * Errors triggered during the test.
23    *
24    * Errors are intercepted by the overridden implementation
25    * of Drupal\simpletest\WebTestBase::error() below.
26    *
27    * @var Array
28    */
29   protected $collectedErrors = [];
30
31   /**
32    * Tests that simpletest collects errors from the tested site.
33    */
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');
38
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 &amp; awesome');
43     }
44     else {
45       // Give back the errors to the log report.
46       foreach ($this->collectedErrors as $error) {
47         parent::error($error['message'], $error['group'], $error['caller']);
48       }
49     }
50   }
51
52   /**
53    * Stores errors into an array.
54    *
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.
61    */
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);
71     }
72     // Everything else should be collected but not propagated.
73     else {
74       $this->collectedErrors[] = [
75         'message' => $message,
76         'group' => $group,
77         'caller' => $caller
78       ];
79     }
80   }
81
82   /**
83    * Asserts that a collected error matches what we are expecting.
84    */
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]));
91     }
92   }
93
94 }