<?php
namespace Drupal\system\Tests\Common;
use Drupal\simpletest\WebTestBase;
class SimpleTestErrorCollectorTest extends WebTestBase {
public static $modules = array(
'system_test',
'error_test',
);
protected $collectedErrors = array();
public static function getInfo() {
return array(
'name' => 'SimpleTest error collector',
'description' => 'Performs tests on the Simpletest error and exception collector.',
'group' => 'Common',
);
}
function testErrorCollect() {
$this->collectedErrors = array();
$this
->drupalGet('error-test/generate-warnings-with-report');
$this
->assertEqual(count($this->collectedErrors), 3, 'Three errors were collected');
if (count($this->collectedErrors) == 3) {
$this
->assertError($this->collectedErrors[0], 'Notice', 'error_test_generate_warnings()', 'error_test.module', 'Undefined variable: bananas');
$this
->assertError($this->collectedErrors[1], 'Warning', 'error_test_generate_warnings()', 'error_test.module', 'Division by zero');
$this
->assertError($this->collectedErrors[2], 'User warning', 'error_test_generate_warnings()', 'error_test.module', 'Drupal is awesome');
}
else {
foreach ($this->collectedErrors as $error) {
parent::error($error['message'], $error['group'], $error['caller']);
}
}
}
protected function error($message = '', $group = 'Other', array $caller = NULL) {
if ($group == 'User notice') {
parent::error($message, $group, $caller);
}
else {
$this->collectedErrors[] = array(
'message' => $message,
'group' => $group,
'caller' => $caller,
);
}
}
function assertError($error, $group, $function, $file, $message = NULL) {
$this
->assertEqual($error['group'], $group, format_string("Group was %group", array(
'%group' => $group,
)));
$this
->assertEqual($error['caller']['function'], $function, format_string("Function was %function", array(
'%function' => $function,
)));
$this
->assertEqual(drupal_basename($error['caller']['file']), $file, format_string("File was %file", array(
'%file' => $file,
)));
if (isset($message)) {
$this
->assertEqual($error['message'], $message, format_string("Message was %message", array(
'%message' => $message,
)));
}
}
}