You are here

function MonitoringServicesTest::testSensorResult in Monitoring 7

Test sensor result API calls.

File

test/tests/monitoring.services.test, line 91
Contains \MonitoringServicesTest.

Class

MonitoringServicesTest
Tests for cron sensor.

Code

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

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

  // Try a request without expanding the sensor info and check that it is not
  // present.
  $response_data = $this
    ->doRequest('sensor-result');
  $this
    ->assertResponse(200);
  $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()
    ->getEnabledSensorInfo()));

  // Test non existing sensor.
  $sensor_name = 'sensor_that_does_not_exist';
  $this
    ->doRequest('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
    ->doRequest('sensor-result/' . $sensor_name);
  $this
    ->assertResponse(404);
  $sensor_name = 'dblog_event_severity_error';
  $response_data = $this
    ->doRequest('sensor-result/' . $sensor_name, array(
    'expand' => 'sensor_info',
  ));
  $this
    ->assertResponse(200);

  // The response must contain the sensor_info.
  $this
    ->assertTrue(isset($response_data['sensor_info']));
  $this
    ->assertSensorResult($response_data, monitoring_sensor_manager()
    ->getSensorInfoByName($sensor_name));

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