You are here

protected function MonitoringUITest::doTestExceedsThresholdSettings in Monitoring 8

Tests exceeds threshold settings UI and validation.

1 call to MonitoringUITest::doTestExceedsThresholdSettings()
MonitoringUITest::testSensorSettingsUI in tests/src/Functional/MonitoringUITest.php
Test the sensor settings UI.

File

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

Class

MonitoringUITest
Tests for the Monitoring UI.

Namespace

Drupal\Tests\monitoring\Functional

Code

protected function doTestExceedsThresholdSettings() {

  // Test with valid values.
  $thresholds = array(
    'critical' => 11,
    'warning' => 6,
  );
  $this
    ->submitThresholdSettings('test_sensor_exceeds', $thresholds);
  $this
    ->assertText(new FormattableMarkup('Sensor @label saved.', array(
    '@label' => 'Test sensor exceeds',
  )));
  $this
    ->assertThresholdSettingsUIDefaults('test_sensor_exceeds', $thresholds);

  // Make sure that it is possible to save empty thresholds.
  $thresholds = array(
    'critical' => '',
    'warning' => '',
  );
  $this
    ->submitThresholdSettings('test_sensor_exceeds', $thresholds);
  $this
    ->assertText(new FormattableMarkup('Sensor @label saved.', array(
    '@label' => 'Test sensor exceeds',
  )));
  $this
    ->assertThresholdSettingsUIDefaults('test_sensor_exceeds', $thresholds);
  monitoring_sensor_manager()
    ->resetCache();
  \Drupal::service('monitoring.sensor_runner')
    ->resetCache();
  $sensor_result = $this
    ->runSensor('test_sensor_exceeds');
  $this
    ->assertTrue($sensor_result
    ->isOk());

  // Test validation.
  $thresholds = array(
    'critical' => 5,
    'warning' => 10,
  );
  $this
    ->submitThresholdSettings('test_sensor_exceeds', $thresholds);
  $this
    ->assertText('Warning must be lower than critical or empty.');
  $thresholds = array(
    'critical' => 5,
    'warning' => 5,
  );
  $this
    ->submitThresholdSettings('test_sensor_exceeds', $thresholds);
  $this
    ->assertText('Warning must be lower than critical or empty.');
  $thresholds = array(
    'critical' => 'alphanumeric',
    'warning' => 'alphanumeric',
  );
  $this
    ->submitThresholdSettings('test_sensor_exceeds', $thresholds);
  $this
    ->assertText('Warning must be a number.');
  $this
    ->assertText('Critical must be a number.');

  // Test threshold exceeds with zero values for critical.
  $thresholds = [
    'critical' => 0,
    'warning' => '',
  ];
  $this
    ->submitThresholdSettings('test_sensor_exceeds', $thresholds);
  $test_sensor_result_data = [
    'sensor_value' => 7,
  ];
  \Drupal::state()
    ->set('monitoring_test.sensor_result_data', $test_sensor_result_data);
  $result = $this
    ->runSensor('test_sensor_exceeds');
  $this
    ->assertTrue($result
    ->isCritical());
  $test_sensor_result_data = [
    'sensor_value' => 0,
  ];
  \Drupal::state()
    ->set('monitoring_test.sensor_result_data', $test_sensor_result_data);
  $result = $this
    ->runSensor('test_sensor_exceeds');
  $this
    ->assertTrue($result
    ->isOk());

  // Test threshold exceeds with zero values for warning.
  $thresholds = [
    'critical' => '',
    'warning' => 0,
  ];
  $this
    ->submitThresholdSettings('test_sensor_exceeds', $thresholds);
  $test_sensor_result_data = [
    'sensor_value' => 7,
  ];
  \Drupal::state()
    ->set('monitoring_test.sensor_result_data', $test_sensor_result_data);
  $result = $this
    ->runSensor('test_sensor_exceeds');
  $this
    ->assertTrue($result
    ->isWarning());
  $test_sensor_result_data = [
    'sensor_value' => 0,
  ];
  \Drupal::state()
    ->set('monitoring_test.sensor_result_data', $test_sensor_result_data);
  $result = $this
    ->runSensor('test_sensor_exceeds');
  $this
    ->assertTrue($result
    ->isOk());
  return $thresholds;
}