You are here

public function DbLogTest::testDBLogAddAndClear in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/dblog/tests/src/Functional/DbLogTest.php \Drupal\Tests\dblog\Functional\DbLogTest::testDBLogAddAndClear()
  2. 9 core/modules/dblog/tests/src/Functional/DbLogTest.php \Drupal\Tests\dblog\Functional\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/tests/src/Functional/DbLogTest.php, line 630

Class

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

Namespace

Drupal\Tests\dblog\Functional

Code

public function testDBLogAddAndClear() {
  global $base_root;
  $connection = Database::getConnection();

  // Get a count of how many watchdog entries already exist.
  $count = $connection
    ->select('watchdog')
    ->countQuery()
    ->execute()
    ->fetchField();
  $log = [
    'channel' => 'system',
    'message' => 'Log entry added to test the doClearTest clear down.',
    'variables' => [],
    'severity' => RfcLogLevel::NOTICE,
    'link' => NULL,
    '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
    ->assertEquals($count + 1, (int) $connection
    ->select('watchdog')
    ->countQuery()
    ->execute()
    ->fetchField(), new FormattableMarkup('\\Drupal\\dblog\\Logger\\DbLog->log() added an entry to the dblog :count', [
    ':count' => $count,
  ]));

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

  // Post in order to clear the database table.
  $this
    ->clearLogsEntries();

  // Confirm that the logs should be cleared.
  $this
    ->submitForm([], 'Confirm');

  // Count the rows in watchdog that previously related to the deleted user.
  $count = $connection
    ->select('watchdog')
    ->countQuery()
    ->execute()
    ->fetchField();
  $this
    ->assertEquals(0, $count, new FormattableMarkup('DBLog contains :count records after a clear.', [
    ':count' => $count,
  ]));
}