Fix for assertWatchdog()

  1.   function tearDown() {
  2.     // Capture any (remaining) watchdog messages.
  3.     $this->assertMollomWatchdogMessages();
  4.     parent::tearDown();
  5.   }
  6.  
  7.   /**
  8.    * Assert any watchdog messages based on their severity.
  9.    *
  10.    * This function can be (repeatedly) invoked to assert new watchdog messages.
  11.    * All watchdog messages with a higher severity than WATCHDOG_NOTICE are
  12.    * considered as fails.
  13.    *
  14.    * @param $no_fail_expected
  15.    *   (optional) Boolean whether a failing watchdog message is expected.
  16.    *   Defaults to TRUE (no fail expected). If FALSE is passed, the logic for
  17.    *   assertion mesages is flipped.
  18.    */
  19.   protected function assertMollomWatchdogMessages($no_fail_expected = TRUE) {
  20.     static $seen_ids = array();
  21.  
  22.     if (!module_exists('dblog')) {
  23.       return;
  24.     }
  25.     module_load_include('inc', 'dblog', 'dblog.admin');
  26.  
  27.     $where = ($seen_ids ? 'AND wid NOT IN (' . db_placeholders($seen_ids) . ')' : '');
  28.     $result = db_query("SELECT * FROM {watchdog} WHERE type = 'mollom' $where ORDER BY timestamp ASC", $seen_ids);
  29.     while ($row = db_fetch_object($result)) {
  30.       if ($no_fail_expected ? $row->severity >= WATCHDOG_NOTICE : $row->severity < WATCHDOG_NOTICE) {
  31.         $this->pass(_dblog_format_message($row), t('Watchdog'));
  32.       }
  33.       else {
  34.         $this->fail(_dblog_format_message($row), t('Watchdog'));
  35.       }
  36.       $seen_ids[] = $row->wid;
  37.     }
  38.   }

Submit Fix

Any tags you'd like to associate with your code, delimitered by commas (example: Views, CCK, Module, etc).
Select the syntax highlighting mode to use.