You are here

public function MonitoringUITest::testSensorDetailPage in Monitoring 8

Tests the sensor detail page.

File

tests/src/Functional/MonitoringUITest.php, line 224

Class

MonitoringUITest
Tests for the Monitoring UI.

Namespace

Drupal\Tests\monitoring\Functional

Code

public function testSensorDetailPage() {
  $account = $this
    ->drupalCreateUser(array(
    'monitoring reports',
    'monitoring verbose',
    'monitoring force run',
  ), 'integrity_test_user', TRUE);
  $this
    ->drupalLogin($account);
  $page = $this
    ->getSession()
    ->getPage();
  $assert_session = $this
    ->assertSession();
  $this
    ->drupalCreateNode(array(
    'promote' => NodeInterface::PROMOTED,
  ));
  $sensor_config = SensorConfig::load('entity_aggregate_test');
  $this
    ->drupalGet('admin/reports/monitoring/sensors/entity_aggregate_test');
  $this
    ->assertTitle(t('@label (@category) | Drupal', array(
    '@label' => $sensor_config
      ->getLabel(),
    '@category' => $sensor_config
      ->getCategory(),
  )));

  // Make sure that all relevant information is displayed.
  // @todo: Assert position/order.
  // Cannot use $this->runSensor() as the cache needs to remain.
  $result = monitoring_sensor_run('entity_aggregate_test');
  $this
    ->assertText(t('Description'));
  $this
    ->assertText($sensor_config
    ->getDescription());
  $this
    ->assertText(t('Status'));
  $this
    ->assertText('Warning');
  $this
    ->assertText(t('Message'));
  $this
    ->assertText('1 druplicons in 1 day, falls below 2');
  $this
    ->assertText(t('Execution time'));

  // The sensor is cached, so we have the same cached execution time.
  $this
    ->assertText($result
    ->getExecutionTime() . 'ms');
  $this
    ->assertText(t('Cache information'));
  $this
    ->assertText('Executed now, valid for 1 hour');
  $this
    ->assertRaw(t('Run again'));
  $this
    ->assertText(t('Verbose'));
  $this
    ->assertText(t('Settings'));

  // @todo Add asserts about displayed settings once we display them in a
  //   better way.
  $this
    ->assertText(t('Log'));
  $rows = $this
    ->getSession()
    ->getPage()
    ->findAll('css', '#edit-sensor-log tbody tr');
  $this
    ->assertEquals(1, count($rows));
  $this
    ->assertEquals('WARNING', $rows[0]
    ->find('css', 'td:nth-child(2)')
    ->getText());
  $this
    ->assertEquals('1 druplicons in 1 day, falls below 2', $rows[0]
    ->find('css', 'td:nth-child(3)')
    ->getText());

  // Create another node and run again.
  $node = $this
    ->drupalCreateNode(array(
    'promote' => '1',
  ));
  $this
    ->drupalPostForm(NULL, array(), t('Run again'));
  $this
    ->assertText('OK');
  $this
    ->assertText('2 druplicons in 1 day');
  $rows = $this
    ->getSession()
    ->getPage()
    ->findAll('css', '#edit-sensor-log tbody tr');
  $this
    ->assertEquals(2, count($rows));
  $this
    ->assertEquals('OK', $rows[0]
    ->find('css', 'td:nth-child(2)')
    ->getText());
  $this
    ->assertSession()
    ->elementTextContains('css', '#edit-sensor-log tbody tr.monitoring-ok td:nth-child(2)', 'OK');
  $this
    ->assertEquals('WARNING', $rows[1]
    ->find('css', 'td:nth-child(2)')
    ->getText());
  $this
    ->assertSession()
    ->elementTextContains('css', '#edit-sensor-log tbody tr.monitoring-warning td:nth-child(2)', 'WARNING');

  // Refresh the page, this not run the sensor again.
  $this
    ->drupalGet('admin/reports/monitoring/sensors/entity_aggregate_test');
  $this
    ->assertText('OK');
  $this
    ->assertText('2 druplicons in 1 day');
  $this
    ->assertText(t('Verbose output is not available for cached sensor results. Click force run to see verbose output.'));
  $rows = $this
    ->getSession()
    ->getPage()
    ->findAll('css', '#edit-sensor-log tbody tr');
  $this
    ->assertEquals(2, count($rows));

  // Test the verbose output.
  $this
    ->drupalPostForm(NULL, array(), t('Run now'));

  // Check that the verbose output is displayed.
  $this
    ->assertText('Verbose');
  $this
    ->assertText('id');
  $this
    ->assertText('label');
  $this
    ->assertText($node
    ->getTitle());

  // Check the if the sensor message includes value type.
  $this
    ->drupalGet('admin/reports/monitoring/sensors/core_cron_safe_threshold');
  $this
    ->assertText('FALSE');

  // Test that accessing a disabled or nisot-existing sensor results in an
  // access denied and a page not found response.
  monitoring_sensor_manager()
    ->disableSensor('test_sensor');
  $this
    ->drupalGet('admin/reports/monitoring/sensors/test_sensor');
  $this
    ->assertResponse(403);
  $this
    ->drupalGet('admin/reports/monitoring/sensors/non_existing_sensor');
  $this
    ->assertResponse(404);

  // Test user integrity sensor detail page.

  /** @var User $account */
  $account = User::load($account
    ->id());
  $this
    ->drupalGet('admin/reports/monitoring/sensors/user_integrity');
  $this
    ->assertText('1 privileged user(s)');

  // Check that is not showing the query or the query arguments.
  $this
    ->assertNoText(t('Query'));
  $this
    ->assertNoText(t('Arguments'));

  // Test the timestamp is formatted correctly.
  $expected_time = \Drupal::service('date.formatter')
    ->format($account
    ->getCreatedTime(), 'short');
  $assert_session
    ->elementContains('css', '#all_users_with_privileged_access tbody td:nth-child(3)', $expected_time);
  $expected_time = \Drupal::service('date.formatter')
    ->format($account
    ->getLastAccessedTime(), 'short');
  $assert_session
    ->elementContains('css', '#all_users_with_privileged_access tbody td:nth-child(4)', $expected_time);

  // Assert None output when we don't have restricted roles with permissions.
  $this
    ->assertText('List of roles with restricted permissions');
  $this
    ->assertText('None');
  $test_user = $this
    ->drupalCreateUser(array(
    'administer monitoring',
  ), 'test_user');
  $test_user
    ->save();
  $this
    ->drupalLogin($test_user);
  $this
    ->runSensor('user_integrity');
  $this
    ->drupalGet('admin/reports/monitoring/sensors/user_integrity');
  $this
    ->assertText('2 privileged user(s), 1 new user(s)');

  // Grant restricted permission to authenticated users.
  user_role_grant_permissions('authenticated', array(
    'administer account settings',
  ));

  // Run the sensor to check verbose output.
  $this
    ->drupalPostForm(NULL, array(), t('Run now'));

  // Check restricted permissions of Authenticated users.
  $this
    ->assertText('List of roles with restricted permissions');
  $this
    ->assertText('Authenticated user: administer account settings');

  // Check table of users with privileged access.
  $expected_header = [
    'User',
    'Roles',
    'Created',
    'Last accessed',
  ];
  $this
    ->assertText('All users with privileged access');
  $rows = $page
    ->findAll('css', '#all_users_with_privileged_access tbody tr');
  $this
    ->assertEquals(3, count($rows));
  $assert_session
    ->elementContains('css', '#all_users_with_privileged_access thead th:nth-child(1)', $expected_header[0]);
  $assert_session
    ->elementContains('css', '#all_users_with_privileged_access thead th:nth-child(2)', $expected_header[1]);
  $assert_session
    ->elementContains('css', '#all_users_with_privileged_access thead th:nth-child(3)', $expected_header[2]);
  $assert_session
    ->elementContains('css', '#all_users_with_privileged_access thead th:nth-child(4)', $expected_header[3]);

  // Assert roles are listed on the table.
  $assert_session
    ->elementContains('css', '#all_users_with_privileged_access tbody tr:nth-child(1) td:nth-child(2)', implode(', ', $test_user
    ->getRoles()));
  $assert_session
    ->elementContains('css', '#all_users_with_privileged_access tbody tr:nth-child(2) td:nth-child(2)', implode(', ', $account
    ->getRoles()));

  // Check the new user name in verbose output.
  $this
    ->assertText('test_user');

  // Reset the user data and run the sensor again.
  $this
    ->drupalPostForm('admin/config/system/monitoring/sensors/user_integrity', array(), t('Reset user data'));
  $this
    ->runSensor('user_integrity');
  $this
    ->drupalGet('admin/reports/monitoring/sensors/user_integrity');
  $this
    ->assertText('2 privileged user(s)');

  // Change user data and run sensor.
  $test_user
    ->setUsername('changed_name');
  $test_user
    ->save();
  $this
    ->runSensor('user_integrity');
  $this
    ->drupalGet('admin/reports/monitoring/sensors/user_integrity');
  $this
    ->assertText('3 privileged user(s), 1 changed user(s)');

  // Reset user data again and check sensor message.
  $this
    ->drupalPostForm('admin/config/system/monitoring/sensors/user_integrity', array(), t('Reset user data'));
  $this
    ->runSensor('user_integrity');
  $this
    ->drupalGet('admin/reports/monitoring/sensors/user_integrity');
  $this
    ->assertText('2 privileged user(s)');

  // Check the list of deleted users.
  $account
    ->delete();
  $this
    ->drupalPostForm('admin/reports/monitoring/sensors/user_integrity', array(), t('Run now'));
  $this
    ->assertText('Deleted users with privileged access');

  // Assert the deleted user is listed.
  $assert_session
    ->elementContains('css', '#deleted_users_with_privileged_access tbody tr:nth-child(1) td:nth-child(1)', 'integrity_test_user');

  // Test enabled sensor link works after save.
  $this
    ->drupalPostForm('admin/config/system/monitoring/sensors/user_integrity', array(), 'Save');
  $this
    ->clickLink('Privileged user integrity');
  $this
    ->assertResponse(200);
  $this
    ->assertUrl('admin/reports/monitoring/sensors/user_integrity');

  // Test disabled sensor link works and redirect to edit page.
  monitoring_sensor_manager()
    ->disableSensor('user_integrity');
  $this
    ->drupalPostForm('admin/config/system/monitoring/sensors/user_integrity', array(), 'Save');
  $this
    ->clickLink('Privileged user integrity');
  $this
    ->assertResponse(200);
  $this
    ->assertUrl('admin/config/system/monitoring/sensors/user_integrity');
}