function DbLogTest::testDBLogAddAndClear

Same name and namespace in other branches
  1. 9 core/modules/dblog/tests/src/Functional/DbLogTest.php \Drupal\Tests\dblog\Functional\DbLogTest::testDBLogAddAndClear()
  2. 8.9.x core/modules/dblog/tests/src/Functional/DbLogTest.php \Drupal\Tests\dblog\Functional\DbLogTest::testDBLogAddAndClear()
  3. 10 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 620

Class

DbLogTest
Verifies log entries and user access based on permissions.

Namespace

Drupal\Tests\dblog\Functional

Code

public function testDBLogAddAndClear() : void {
    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' => \Drupal::time()->getRequestTime(),
    ];
    // 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(), '\\Drupal\\dblog\\Logger\\DbLog->log() added an entry to the dblog ' . $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, "DBLog contains {$count} records after a clear.");
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.