You are here

public function DbLogTest::testDBLogAddAndClear in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/dblog/src/Tests/DbLogTest.php \Drupal\dblog\Tests\DbLogTest::testDBLogAddAndClear()

Tests the addition and clearing of log events through the admin interface.

Logs in the admin user, creates a database log event, and tests the functionality of clearing the database log through the admin interface.

File

core/modules/dblog/src/Tests/DbLogTest.php, line 486
Contains \Drupal\dblog\Tests\DbLogTest.

Class

DbLogTest
Generate events and verify dblog entries; verify user access to log reports based on permissions.

Namespace

Drupal\dblog\Tests

Code

public function testDBLogAddAndClear() {
  global $base_root;

  // Get a count of how many watchdog entries already exist.
  $count = db_query('SELECT COUNT(*) FROM {watchdog}')
    ->fetchField();
  $log = array(
    'channel' => 'system',
    'message' => 'Log entry added to test the doClearTest clear down.',
    'variables' => array(),
    'severity' => RfcLogLevel::NOTICE,
    'link' => NULL,
    'user' => $this->adminUser,
    'uid' => $this->adminUser
      ->id(),
    'request_uri' => $base_root . \Drupal::request()
      ->getRequestUri(),
    'referer' => \Drupal::request()->server
      ->get('HTTP_REFERER'),
    'ip' => '127.0.0.1',
    'timestamp' => REQUEST_TIME,
  );

  // Add a watchdog entry.
  $this->container
    ->get('logger.dblog')
    ->log($log['severity'], $log['message'], $log);

  // Make sure the table count has actually been incremented.
  $this
    ->assertEqual($count + 1, db_query('SELECT COUNT(*) FROM {watchdog}')
    ->fetchField(), format_string('\\Drupal\\dblog\\Logger\\DbLog->log() added an entry to the dblog :count', array(
    ':count' => $count,
  )));

  // Login the admin user.
  $this
    ->drupalLogin($this->adminUser);

  // Post in order to clear the database table.
  $this
    ->drupalPostForm('admin/reports/dblog', array(), t('Clear log messages'));

  // Confirm that the logs should be cleared.
  $this
    ->drupalPostForm(NULL, array(), 'Confirm');

  // Count the rows in watchdog that previously related to the deleted user.
  $count = db_query('SELECT COUNT(*) FROM {watchdog}')
    ->fetchField();
  $this
    ->assertEqual($count, 0, format_string('DBLog contains :count records after a clear.', array(
    ':count' => $count,
  )));
}