You are here

public function MonitoringServicesTest::testSensorResult in Monitoring 8

Test sensor result API calls.

File

tests/src/Functional/MonitoringServicesTest.php, line 110

Class

MonitoringServicesTest
Tests for monitoring services.

Namespace

Drupal\Tests\monitoring\Functional

Code

public function testSensorResult() {
  $this
    ->drupalLogin($this->servicesAccount);

  // Test request for sensor results with expanded sensor config.
  $response_data = $this
    ->doJsonRequest('monitoring-sensor-result', array(
    'expand' => 'sensor',
  ));
  $this
    ->assertResponse(200);
  foreach (monitoring_sensor_manager()
    ->getEnabledSensorConfig() as $sensor_name => $sensor_config) {
    $this
      ->assertTrue(isset($response_data[$sensor_name]['sensor']));
    $this
      ->assertSensorResult($response_data[$sensor_name], $sensor_config);
  }

  // Try a request without expanding the sensor config and check that it is not
  // present.
  $response_data = $this
    ->doJsonRequest('monitoring-sensor-result');
  $this
    ->assertResponse(200);
  $this
    ->assertEqual('UNCACHEABLE', $this
    ->drupalGetHeader(DynamicPageCacheSubscriber::HEADER), 'Render array returned, rendered as HTML response, but uncacheable: Dynamic Page Cache is running, but not caching.');
  $sensor_result = reset($response_data);
  $this
    ->assertTrue(!isset($sensor_result['sensor_info']));

  // Make sure the response contains expected count of results.
  $this
    ->assertEqual(count($response_data), count(monitoring_sensor_manager()
    ->getEnabledSensorConfig()));

  // Test non existing sensor.
  $sensor_name = 'sensor_that_does_not_exist';
  $this
    ->doJsonRequest('monitoring-sensor-result/' . $sensor_name);
  $this
    ->assertResponse(404);

  // Test disabled sensor - note that monitoring_git_dirty_tree is disabled
  // by default.
  $sensor_name = 'monitoring_git_dirty_tree';
  $this
    ->doJsonRequest('monitoring-sensor-result/' . $sensor_name);
  $this
    ->assertResponse(404);
  $sensor_name = 'dblog_event_severity_error';
  $response_data = $this
    ->doJsonRequest('monitoring-sensor-result/' . $sensor_name, array(
    'expand' => 'sensor',
  ));
  $this
    ->assertResponse(200);

  // The response must contain the sensor.
  $this
    ->assertTrue(isset($response_data['sensor']));
  $this
    ->assertSensorResult($response_data, SensorConfig::load($sensor_name));

  // Try a request without expanding the sensor config and check that it is not
  // present.
  $response_data = $this
    ->doJsonRequest('monitoring-sensor-result/' . $sensor_name);
  $this
    ->assertResponse(200);
  $this
    ->assertTrue(!isset($response_data['sensor']));
}