You are here

public function MonitoringCoreKernelTest::testDblog404SensorPlugin in Monitoring 8

Tests dblog 404 errors sensor.

Logged through watchdog.

See also

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

File

tests/src/Kernel/MonitoringCoreKernelTest.php, line 221

Class

MonitoringCoreKernelTest
Kernel tests for the core pieces of monitoring.

Namespace

Drupal\Tests\monitoring\Kernel

Code

public function testDblog404SensorPlugin() {

  // Fake some not found errors.
  \Drupal::service('logger.dblog')
    ->log(RfcLogLevel::NOTICE, '@uri', [
    'request_uri' => 'not/found',
    'uid' => 0,
    'channel' => 'page not found',
    'link' => '',
    'referer' => '',
    'ip' => '127.0.0.1',
    'timestamp' => \Drupal::time()
      ->getRequestTime(),
  ]);

  // Run sensor and test the output.
  $result = $this
    ->runSensor('dblog_404');
  $this
    ->assertTrue($result
    ->isOk());
  $this
    ->assertEqual($result
    ->getMessage(), '1 watchdog events in 1 day, not/found');
  $this
    ->assertEqual($result
    ->getValue(), 1);

  // Fake more 404s.
  for ($i = 1; $i <= 20; $i++) {
    \Drupal::service('logger.dblog')
      ->log(RfcLogLevel::NOTICE, '@uri', [
      'request_uri' => 'not/found',
      'uid' => 0,
      'channel' => 'page not found',
      'link' => '',
      'referer' => '',
      'ip' => '127.0.0.1',
      'timestamp' => \Drupal::time()
        ->getRequestTime(),
    ]);
  }

  // Run sensor and check the aggregate value.
  $result = $this
    ->runSensor('dblog_404');
  $this
    ->assertEqual($result
    ->getValue(), 21);
  $this
    ->assertTrue($result
    ->isWarning());

  // Fake more 404s.
  for ($i = 0; $i <= 100; $i++) {
    \Drupal::service('logger.dblog')
      ->log(RfcLogLevel::NOTICE, '@uri', [
      'request_uri' => 'not/found/another',
      'uid' => 0,
      'channel' => 'page not found',
      'link' => '',
      'referer' => '',
      'ip' => '127.0.0.1',
      'timestamp' => \Drupal::time()
        ->getRequestTime(),
    ]);
  }

  // Run sensor and check the aggregate value.
  $result = $this
    ->runSensor('dblog_404');
  $this
    ->assertEqual($result
    ->getValue(), 101);
  $this
    ->assertTrue($result
    ->isCritical());
}