function DbLogTest::testDBLogAddAndClear

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

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->assertEqual($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->drupalPostForm(NULL, [], 'Confirm');
    // Count the rows in watchdog that previously related to the deleted user.
    $count = $connection->select('watchdog')
        ->countQuery()
        ->execute()
        ->fetchField();
    $this->assertEqual($count, 0, new FormattableMarkup('DBLog contains :count records after a clear.', [
        ':count' => $count,
    ]));
}

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