You are here

public function MonitoringCoreWebTest::testPageNotFoundErrors in Monitoring 8

Tests the page not found errors.

See also

\Drupal\monitoring\Plugin\monitoring\SensorPlugin\Dblog404SensorPlugin

File

tests/src/Functional/MonitoringCoreWebTest.php, line 735

Class

MonitoringCoreWebTest
Integration tests for the core pieces of monitoring.

Namespace

Drupal\Tests\monitoring\Functional

Code

public function testPageNotFoundErrors() {
  $test_user = $this
    ->drupalCreateUser([
    'administer monitoring',
    'monitoring reports',
    'monitoring verbose',
  ]);
  $this
    ->drupalLogin($test_user);
  $event_time = \Drupal::time()
    ->getRequestTime();

  // Insert three page not found events.
  Database::getConnection('default')
    ->insert('watchdog')
    ->fields([
    'type' => 'page not found',
    'message' => '@uri',
    'variables' => serialize([
      '%ip' => '127.0.0.1',
    ]),
    'location' => 'http://example.com/non_existing_page',
    'timestamp' => $event_time - 10,
  ])
    ->execute();
  Database::getConnection('default')
    ->insert('watchdog')
    ->fields([
    'type' => 'page not found',
    'message' => '@uri',
    'variables' => serialize([
      '%ip' => '127.0.0.1',
    ]),
    'location' => 'http://example.com/non_existing_page',
    'timestamp' => $event_time,
  ])
    ->execute();
  Database::getConnection('default')
    ->insert('watchdog')
    ->fields([
    'type' => 'page not found',
    'message' => '@uri',
    'variables' => serialize([
      '%ip' => '127.0.0.1',
    ]),
    'location' => 'http://example.com/another_non_existing_page',
    'timestamp' => $event_time - 10,
  ])
    ->execute();
  $this
    ->drupalGet('admin/reports/monitoring/sensors/dblog_404');
  $rows = $this
    ->getSession()
    ->getPage()
    ->findAll('css', '#unaggregated_result tbody tr');
  $this
    ->assertEquals(2, count($rows), 'Two rows found.');
  $this
    ->assertEquals('2', $rows[0]
    ->find('css', 'td:nth-child(2)')
    ->getText(), 'Two access to "/non_existing_page"');

  // Test the timestamp is the last one and that is formatted correctly.
  $login_time = $rows[0]
    ->find('css', 'td:nth-child(3)')
    ->getText();
  $expected_time = \Drupal::service('date.formatter')
    ->format($event_time, 'short');
  $this
    ->assertEquals($expected_time, $login_time);
}