You are here

public function MonitoringCoreWebTest::testUserFailedLoginSensorPlugin in Monitoring 8

Tests the user failed login sensor.

See also

\Drupal\monitoring\Plugin\monitoring\SensorPlugin\UserFailedLoginsSensorPlugin

File

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

Class

MonitoringCoreWebTest
Integration tests for the core pieces of monitoring.

Namespace

Drupal\Tests\monitoring\Functional

Code

public function testUserFailedLoginSensorPlugin() {

  // Add a failed attempt for the admin account.
  $this
    ->drupalPostForm('user/login', [
    'name' => 'admin',
    'pass' => '123',
  ], t('Log in'));

  // Check the verbose sensor result.
  $this
    ->drupalLogin($this->rootUser);
  $this
    ->drupalGet('admin/reports/monitoring/sensors/user_failed_logins');
  $rows = $this
    ->getSession()
    ->getPage()
    ->findAll('css', '#unaggregated_result tbody tr');
  $this
    ->assertEquals(1, count($rows), 'Found 1 results in table');
  $this
    ->assertSession()
    ->elementTextContains('css', '#unaggregated_result tbody tr:nth-child(1) td:nth-child(2)', 'Login attempt failed for admin');

  // Test the timestamp is formatted correctly.
  $wid = $rows[0]
    ->find('css', 'td:nth-child(1) a')
    ->getText();
  $query = \Drupal::database()
    ->select('watchdog');
  $query
    ->addField('watchdog', 'timestamp');
  $query
    ->condition('wid', $wid);
  $result = $query
    ->range(0, 10)
    ->execute()
    ->fetchObject();
  $expected_time = \Drupal::service('date.formatter')
    ->format($result->timestamp, 'short');
  $this
    ->assertSession()
    ->elementTextContains('css', '#unaggregated_result tbody tr:nth-child(1) td:nth-child(3)', $expected_time);
}