You are here

protected function TestBase::assertErrorLogged in SimpleTest 8.3

Asserts that a specific error has been logged to the PHP error log.

Parameters

string $error_message: The expected error message.

Return value

bool TRUE if the assertion succeeded, FALSE otherwise.

See also

\Drupal\simpletest\TestBase::prepareEnvironment()

\Drupal\Core\DrupalKernel::bootConfiguration()

File

src/TestBase.php, line 752

Class

TestBase
Base class for Drupal tests.

Namespace

Drupal\simpletest

Code

protected function assertErrorLogged($error_message) {
  $error_log_filename = DRUPAL_ROOT . '/' . $this->siteDirectory . '/error.log';
  if (!file_exists($error_log_filename)) {
    $this
      ->error('No error logged yet.');
  }
  $content = file_get_contents($error_log_filename);
  $rows = explode(PHP_EOL, $content);

  // We iterate over the rows in order to be able to remove the logged error
  // afterwards.
  $found = FALSE;
  foreach ($rows as $row_index => $row) {
    if (strpos($content, $error_message) !== FALSE) {
      $found = TRUE;
      unset($rows[$row_index]);
    }
  }
  file_put_contents($error_log_filename, implode("\n", $rows));
  return $this
    ->assertTrue($found, sprintf('The %s error message was logged.', $error_message));
}