You are here

public function ControllerTest::testFilter in MongoDB 8.2

Test the dblog filter on admin/reports/dblog.

File

modules/mongodb_watchdog/tests/src/Functional/ControllerTest.php, line 566

Class

ControllerTest
Test the MongoDB report controllers.

Namespace

Drupal\Tests\mongodb_watchdog\Functional

Code

public function testFilter() {
  $this
    ->drupalLogin($this->bigUser);

  // Clear log to ensure that only generated entries are found.
  $database = $this->container
    ->get(MongoDb::SERVICE_DB_FACTORY)
    ->get(Logger::DB_LOGGER);
  $database
    ->drop();
  $logger = $this->container
    ->get(Logger::SERVICE_LOGGER);

  // Generate watchdog entries.
  $typeNames = [];
  $types = [];
  for ($i = 0; $i < 3; $i++) {
    $typeNames[] = $typeName = $this
      ->randomMachineName();
    $severity = RfcLogLevel::EMERGENCY;
    for ($j = 0; $j < 3; $j++) {
      $types[] = $type = [
        'count' => mt_rand(1, 5),
        'type' => $typeName,
        'severity' => $severity++,
      ];
      $this
        ->insertLogEntries($logger, $type['count'], $type['type'], $type['severity']);
    }
  }

  // View the dblog.
  $this
    ->drupalGet(self::PATH_OVERVIEW);

  // Confirm all the entries are displayed.
  $this
    ->assertTypeCount($types);

  // Filter by each type and confirm that entries with various severities are
  // displayed.
  foreach ($typeNames as $typeName) {
    $edit = [
      'type[]' => [
        $typeName,
      ],
    ];
    $this
      ->submitForm($edit, 'Filter');

    // Check whether the displayed event templates match our filter.
    $filteredTypes = array_filter($types, function (array $type) use ($typeName) {
      return $type['type'] === $typeName;
    });
    $this
      ->assertTypeCount($filteredTypes);
  }

  // Set filter to match each of the combined filter sets and confirm the
  // entries displayed.
  foreach ($types as $type) {
    $edit = [
      'type[]' => $typeType = $type['type'],
      'severity[]' => $typeSeverity = $type['severity'],
    ];
    $this
      ->submitForm($edit, 'Filter');
    $filteredTypes = array_filter($types, function (array $type) use ($typeType, $typeSeverity) {
      return $type['type'] === $typeType && $type['severity'] == $typeSeverity;
    });
    $this
      ->assertTypeCount($filteredTypes);
  }
}