You are here

protected function MonitoringCoreWebTest::doTestDatabaseAggregatorSensorPluginActiveSessions in Monitoring 8

Tests active session count through db aggregator sensor.

See also

\Drupal\monitoring\Plugin\monitoring\SensorPlugin\DatabaseAggregatorSensorPlugin

1 call to MonitoringCoreWebTest::doTestDatabaseAggregatorSensorPluginActiveSessions()
MonitoringCoreWebTest::testSensors in tests/src/Functional/MonitoringCoreWebTest.php
Tests individual sensors.

File

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

Class

MonitoringCoreWebTest
Integration tests for the core pieces of monitoring.

Namespace

Drupal\Tests\monitoring\Functional

Code

protected function doTestDatabaseAggregatorSensorPluginActiveSessions() {

  // Create and login a user to have data in the sessions table.
  $test_user = $this
    ->drupalCreateUser([
    'monitoring reports',
    'access site reports',
    'monitoring verbose',
  ]);
  $this
    ->drupalLogin($test_user);
  $result = $this
    ->runSensor('user_sessions_authenticated');
  $this
    ->assertEqual($result
    ->getValue(), 1);
  $result = $this
    ->runSensor('user_sessions_all');
  $this
    ->assertEqual($result
    ->getValue(), 1);

  // Logout the user to see if sensors responded to the change.
  $this
    ->drupalLogout();
  $result = $this
    ->runSensor('user_sessions_authenticated');
  $this
    ->assertEqual($result
    ->getValue(), 0);
  $result = $this
    ->runSensor('user_sessions_all');
  $this
    ->assertEqual($result
    ->getValue(), 0);

  // Check verbose output.
  $this
    ->drupalLogin($test_user);

  /** @var User $test_user */
  $test_user = User::load($test_user
    ->id());
  $this
    ->drupalGet('/admin/reports/monitoring/sensors/user_sessions_authenticated');
  if (\version_compare(\Drupal::VERSION, '9', '>=')) {
    $query = 'SELECT "sessions"."uid" AS "uid", "sessions"."hostname" AS "hostname", "sessions"."timestamp" AS "timestamp" FROM "' . $this->databasePrefix . 'sessions" "sessions" WHERE ("uid" != :db_condition_placeholder_0) AND ("timestamp" > :db_condition_placeholder_1) ORDER BY "timestamp" DESC LIMIT 10 OFFSET 0';
  }
  else {
    $query = "SELECT sessions.uid AS uid, sessions.hostname AS hostname, sessions.timestamp AS timestamp FROM {$this->databasePrefix}sessions sessions WHERE (uid != :db_condition_placeholder_0) AND (timestamp > :db_condition_placeholder_1) ORDER BY timestamp DESC LIMIT 10 OFFSET 0";
  }
  $this
    ->assertSession()
    ->elementTextContains('css', '#unaggregated_result details pre', $query);

  // 3 fields are expected to be displayed.
  $columns = $this
    ->getSession()
    ->getPage()
    ->findAll('css', '#unaggregated_result tbody tr:nth-child(1) td');
  $this
    ->assertTrue(count($columns) == 3, '3 fields have been found in the verbose result.');

  // Test DatabaseAggregator history table result.
  $this
    ->assertSession()
    ->elementTextContains('css', '#history tbody tr:nth-child(1) td:nth-child(2)', '1', 'record_count found in History.');

  // Test the timestamp is shown and formatted correctly.
  $expected_time = \Drupal::service('date.formatter')
    ->format(floor($test_user
    ->getLastLoginTime() / 86400) * 86400, 'short');
  $this
    ->assertSession()
    ->elementTextContains('css', '#history tbody tr:nth-child(1) td:nth-child(1)', $expected_time);

  // The username should be replaced in the message.
  $this
    ->drupalGet('/admin/reports/monitoring/sensors/dblog_event_severity_notice');
  $this
    ->assertText('Session opened for ' . $test_user
    ->label());

  // 'No results' text is displayed when the query has 0 results.
  $this
    ->drupalGet('/admin/reports/monitoring/sensors/dblog_event_severity_warning');
  $this
    ->assertText('There are no results for this sensor to display.');
}