/** * Assert any watchdog messages based on their severity. * * This function can be (repeatedly) invoked to assert new watchdog messages. * All watchdog messages with a higher severity than RfcLogLevel::NOTICE are * considered as "severe". * * @param $max_severity * (optional) A maximum watchdog severity level message constant that log * messages must have to pass the assertion. All messages with a higher * severity will fail. Defaults to RfcLogLevel::NOTICE. If a severity level * higher than RfcLogLevel::NOTICE is passed, then at least one severe message * is expected. */ protected function assertMollomWatchdogMessages($max_severity = RfcLogLevel::NOTICE) { // Ensure that all messages have been written before attempting to verify // them. Actions executed within the test class may lead to log messages, // but those get only logged when hook_exit() is triggered. // mollom.module may not be installed by a test and thus not loaded yet. //drupal_load('module', 'mollom'); Logger::writeLog(); $database = \Drupal::database(); module_load_include('inc', 'dblog', 'dblog.admin'); $this->messages = array(); $query = $database->select('watchdog', 'w')->fields('w')->orderBy('w.timestamp', 'ASC'); // The comparison logic applied in this function is a bit confusing, since // the values of watchdog severity level constants defined by RFC 3164 are // negated to their actual "severity level" meaning: // RfcLogLevel::EMERGENCY is 0, RfcLogLevel::NOTICE is 5, RfcLogLevel::DEBUG is 7. $fail_expected = $max_severity < RfcLogLevel::NOTICE; $had_severe_message = FALSE; foreach ($query->execute() as $row) { $this->messages[$row->wid] = $row; // Only messages with a maximum severity of $max_severity or less severe // messages must pass. More severe messages need to fail. See note about // severity level constant values above. $output = $this->formatMessage($row); if ($row->severity >= $max_severity) { // Visually separate debug log messages from other messages. if ($row->severity == RfcLogLevel::DEBUG) { $this->error($output, 'User notice'); } else { $this->pass(Html::escape($row->type) . ' (' . $row->severity . '): ' . $output, t('Watchdog')); } } else { $this->fail(Html::escape($row->type) . ' (' . $row->severity . '): ' . $output, t('Watchdog')); } // In case a severe message is expected, non-severe messages always pass, // since we would trigger a false positive test failure otherwise. // However, in order to actually assert the expectation, there must have // been at least one severe log message. $had_severe_message = $had_severe_message || $row->severity < RfcLogLevel::NOTICE; } // Assert that there was a severe message, in case we expected one. if ($fail_expected && !$had_severe_message) { $this->fail(t('Severe log message was found.'), t('Watchdog')); } // Delete processed watchdog messages. if (!empty($this->messages)) { $seen_ids = array_keys($this->messages); $database->delete('watchdog')->condition('wid', $seen_ids, 'IN')->execute(); } }
/** * Implements after all other processing. */ function onTerminate() { Logger::writeLog(); }