function DbLogTest::testFilter

Same name and namespace in other branches
  1. 8.9.x core/modules/dblog/tests/src/Functional/DbLogTest.php \Drupal\Tests\dblog\Functional\DbLogTest::testFilter()
  2. 10 core/modules/dblog/tests/src/Functional/DbLogTest.php \Drupal\Tests\dblog\Functional\DbLogTest::testFilter()
  3. 11.x core/modules/dblog/tests/src/Functional/DbLogTest.php \Drupal\Tests\dblog\Functional\DbLogTest::testFilter()

Tests the database log filter functionality at admin/reports/dblog.

File

core/modules/dblog/tests/src/Functional/DbLogTest.php, line 662

Class

DbLogTest
Verifies log entries and user access based on permissions.

Namespace

Drupal\Tests\dblog\Functional

Code

public function testFilter() {
    $this->drupalLogin($this->adminUser);
    // Clear the log to ensure that only generated entries will be found.
    Database::getConnection()->delete('watchdog')
        ->execute();
    // Generate 9 random watchdog entries.
    $type_names = [];
    $types = [];
    for ($i = 0; $i < 3; $i++) {
        $type_names[] = $type_name = $this->randomMachineName();
        $severity = RfcLogLevel::EMERGENCY;
        for ($j = 0; $j < 3; $j++) {
            $types[] = $type = [
                'count' => $j + 1,
                'type' => $type_name,
                'severity' => $severity++,
            ];
            $this->generateLogEntries($type['count'], [
                'channel' => $type['type'],
                'severity' => $type['severity'],
            ]);
        }
    }
    // View the database log page.
    $this->drupalGet('admin/reports/dblog');
    // Confirm that all the entries are displayed.
    $count = $this->getTypeCount($types);
    foreach ($types as $key => $type) {
        $this->assertEquals($type['count'], $count[$key], 'Count matched');
    }
    // Filter by each type and confirm that entries with various severities are
    // displayed.
    foreach ($type_names as $type_name) {
        $this->filterLogsEntries($type_name);
        // Count the number of entries of this type.
        $type_count = 0;
        foreach ($types as $type) {
            if ($type['type'] == $type_name) {
                $type_count += $type['count'];
            }
        }
        $count = $this->getTypeCount($types);
        $this->assertEquals($type_count, array_sum($count), 'Count matched');
    }
    // Set the filter to match each of the two filter-type attributes and
    // confirm the correct number of entries are displayed.
    foreach ($types as $type) {
        $this->filterLogsEntries($type['type'], $type['severity']);
        $count = $this->getTypeCount($types);
        $this->assertEquals($type['count'], array_sum($count), 'Count matched');
    }
    $this->drupalGet('admin/reports/dblog', [
        'query' => [
            'order' => 'Type',
        ],
    ]);
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSession()
        ->pageTextContains('Operations');
    $this->assertSession()
        ->fieldExists('edit-type');
    // Clear all logs and make sure the confirmation message is found.
    $this->clearLogsEntries();
    // Confirm that the logs should be cleared.
    $this->submitForm([], 'Confirm');
    $this->assertSession()
        ->pageTextContains('Database log cleared.');
    $this->assertSession()
        ->fieldNotExists('edit-type');
}

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