You are here

public function MonitoringApiTest::testLogging in Monitoring 8

Test logging with different settings.

File

tests/src/Kernel/MonitoringApiTest.php, line 312
Contains \Drupal\Tests\monitoring\Kernel\MonitoringApiTest.

Class

MonitoringApiTest
Tests for Monitoring API.

Namespace

Drupal\Tests\monitoring\Kernel

Code

public function testLogging() {

  // First perform tests with the logging strategy in default mode - that is
  // "Log only on request or on status change".
  $test_sensor_result_data = array(
    'sensor_value' => 1,
    'sensor_message' => 'test message',
    'sensor_status' => SensorResultInterface::STATUS_OK,
  );
  \Drupal::state()
    ->set('monitoring_test.sensor_result_data', $test_sensor_result_data);
  $sensor = SensorConfig::load('test_sensor');
  $sensor->settings['result_logging'] = TRUE;
  $sensor
    ->save();
  $this
    ->runSensor('test_sensor');
  $logs = $this
    ->loadSensorLog('test_sensor');
  $this
    ->assertEqual(count($logs), 1);
  $log = array_shift($logs);
  $this
    ->assertEqual($log->sensor_name->value, 'test_sensor');
  $this
    ->assertEqual($log->sensor_status->value, SensorResultInterface::STATUS_OK);
  $this
    ->assertEqual($log->sensor_value->value, 1);
  $this
    ->assertEqual($log->sensor_message->value, 'Value 1, test message');

  // Set log_calls sensor settings to false - that should prevent logging.
  $sensor->settings['result_logging'] = FALSE;
  $sensor
    ->save();

  /** @var \Drupal\monitoring\SensorRunner $runner */
  $runner = \Drupal::service('monitoring.sensor_runner');
  $runner
    ->runSensors(array(
    SensorConfig::load('test_sensor'),
  ));
  $logs = $this
    ->loadSensorLog('test_sensor');
  $this
    ->assertEqual(count($logs), 1);

  // Now change the status - that should result in the call being logged.
  $test_sensor_result_data = array(
    'sensor_status' => SensorResultInterface::STATUS_WARNING,
  );
  \Drupal::state()
    ->set('monitoring_test.sensor_result_data', $test_sensor_result_data);
  $this
    ->runSensor('test_sensor');
  $logs = $this
    ->loadSensorLog('test_sensor');
  $this
    ->assertEqual(count($logs), 2);
  $log = array_pop($logs);
  $this
    ->assertEqual($log->sensor_status->value, SensorResultInterface::STATUS_WARNING);

  // Set the logging strategy to "Log all events".
  $this
    ->config('monitoring.settings')
    ->set('sensor_call_logging', 'all')
    ->save();

  // Running the sensor with 'result_logging' settings FALSE must record the call.
  $sensor->settings['result_logging'] = FALSE;
  $sensor
    ->save();
  $this->container
    ->set('monitoring.sensor_runner', NULL);
  $this
    ->runSensor('test_sensor');
  $logs = $this
    ->loadSensorLog('test_sensor');
  $this
    ->assertEqual(count($logs), 3);

  // Set the logging strategy to "No logging".
  $this
    ->config('monitoring.settings')
    ->set('sensor_call_logging', 'none')
    ->save();

  // Despite log_calls TRUE we should not log any call.
  $sensor->settings['result_logging'] = TRUE;
  $sensor
    ->save();
  $this->container
    ->set('monitoring.sensor_runner', NULL);
  $logs = $this
    ->loadSensorLog('test_sensor');
  $this
    ->runSensor('test_sensor');
  $this
    ->assertEqual(count($logs), 3);
}