You are here

public function MonitoringUITest::testSensorOverviewPage in Monitoring 8

Tests the sensor results overview and the global sensor log.

File

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

Class

MonitoringUITest
Tests for the Monitoring UI.

Namespace

Drupal\Tests\monitoring\Functional

Code

public function testSensorOverviewPage() {

  // Check access for the overviews.
  $this
    ->drupalGet('admin/reports/monitoring');
  $this
    ->assertResponse(403);
  $this
    ->drupalGet('admin/reports/monitoring/log');
  $this
    ->assertResponse(403);
  $account = $this
    ->drupalCreateUser(array(
    'monitoring reports',
  ));
  $this
    ->drupalLogin($account);

  // Run the test_sensor and update the timestamp in the cache to make the
  // result the oldest.
  $this
    ->runSensor('test_sensor');
  $cid = 'monitoring_sensor_result:test_sensor';
  $cache = \Drupal::cache('default')
    ->get($cid);
  $cache->data['timestamp'] = $cache->data['timestamp'] - 1000;
  \Drupal::cache('default')
    ->set($cid, $cache->data, \Drupal::time()
    ->getRequestTime() + 3600, array(
    'monitoring_sensor_result',
  ));
  $this
    ->drupalGet('admin/reports/monitoring');

  // Test if the Test sensor is listed as the oldest cached. We do not test
  // for the cached time as such test contains a risk of random fail.
  $this
    ->assertRaw(new FormattableMarkup('Sensor %sensor (%category) cached before', array(
    '%sensor' => 'Test sensor',
    '%category' => 'Test',
  )));

  // Assert if .js & .css are loaded.
  $this
    ->assertRaw('monitoring.js');
  $this
    ->assertRaw('monitoring.css');

  // Test the action buttons are clickable.
  $this
    ->assertLink(t('Details'));
  $this
    ->assertLink(t('Edit'));
  $this
    ->assertLink(t('Details'));

  // Test the overview table.
  $rows = $this
    ->getSession()
    ->getPage()
    ->findAll('css', '#monitoring-sensors-overview tbody tr');
  $i = 0;
  foreach (monitoring_sensor_config_by_categories() as $category => $category_sensor_config) {
    $tr = $rows[$i];
    $this
      ->assertEquals($category, $tr
      ->find('css', 'h3')
      ->getText());
    foreach ($category_sensor_config as $sensor_config) {
      $i++;
      $tr = $rows[$i];
      $this
        ->assertEquals($sensor_config
        ->getLabel(), $tr
        ->find('css', 'td:nth-child(1) span')
        ->getText());
    }
    $i++;
  }

  // Test the global sensor log.
  $this
    ->clickLink(t('Log'));
  $this
    ->assertText('test_sensor');
  $this
    ->assertText(t('OK'));
  $this
    ->assertText(t('No value'));
  $this
    ->assertRaw('class="monitoring-ok"');
  $this
    ->assertRaw('It is highly recommended that you configure this.');
  $this
    ->assertRaw('See Protecting against HTTP HOST Header attacks');
  $this
    ->clickLink('test_sensor');
  $this
    ->assertResponse(200);
  $this
    ->assertUrl(SensorConfig::load('test_sensor')
    ->toUrl('details-form'));
}