You are here

public function MonitoringCoreWebTest::testSensorDisappearedSensors in Monitoring 8

Tests for disappearing sensors.

We provide a separate test method for the DisappearedSensorsSensorPlugin as we need to install and uninstall additional modules.

See also

\Drupal\monitoring\Plugin\monitoring\SensorPlugin\DisappearedSensorsSensorPlugin

File

tests/src/Functional/MonitoringCoreWebTest.php, line 456

Class

MonitoringCoreWebTest
Integration tests for the core pieces of monitoring.

Namespace

Drupal\Tests\monitoring\Functional

Code

public function testSensorDisappearedSensors() {

  // Install the media module.
  $this
    ->installModules(array(
    'media',
  ));

  // Run the disappeared sensor - it should not report any problems.
  $result = $this
    ->runSensor('monitoring_disappeared_sensors');
  $this
    ->assertTrue($result
    ->isOk());
  $log = $this
    ->loadWatchdog();
  $this
    ->assertEqual(count($log), 1, 'There should be one log entry: all sensors enabled by default added.');
  $sensor_config_all = monitoring_sensor_manager()
    ->getAllSensorConfig();
  $this
    ->assertEqual(new FormattableMarkup($log[0]->message, unserialize($log[0]->variables)), new FormattableMarkup('@count new sensor/s added: @names', array(
    '@count' => count($sensor_config_all),
    '@names' => implode(', ', array_keys($sensor_config_all)),
  )));

  // Uninstall the media module so that the media requirements sensor goes
  // away.
  $this
    ->uninstallModules(array(
    'media',
  ));

  // The comment_new sensor has gone away and therefore we should have the
  // critical status.
  $result = $this
    ->runSensor('monitoring_disappeared_sensors');
  $this
    ->assertTrue($result
    ->isCritical());
  $this
    ->assertEqual($result
    ->getMessage(), 'Missing sensor core_requirements_media');

  // There should be no new logs.
  $this
    ->assertEqual(count($this
    ->loadWatchdog()), 1);

  // Install the comment module to test the correct procedure of removing
  // sensors.
  $this
    ->installModules(array(
    'media',
  ));
  monitoring_sensor_manager()
    ->enableSensor('core_requirements_media');

  // Now we should be back to normal.
  $result = $this
    ->runSensor('monitoring_disappeared_sensors');
  $this
    ->assertTrue($result
    ->isOk());
  $this
    ->assertEqual(count($this
    ->loadWatchdog()), 1);

  // Do the correct procedure to remove a sensor - first disable thes sensors
  // and then uninstall the comment module.
  monitoring_sensor_manager()
    ->disableSensor('core_requirements_media');
  $this
    ->uninstallModules(array(
    'media',
  ));

  // The sensor should not report any problem this time.
  $result = $this
    ->runSensor('monitoring_disappeared_sensors');
  $this
    ->assertTrue($result
    ->isOk());
  $log = $this
    ->loadWatchdog();
  $this
    ->assertEqual(count($log), 2, 'Removal of core_requirements_media sensor should be logged.');
  $this
    ->assertEqual(new FormattableMarkup($log[1]->message, unserialize($log[1]->variables)), '1 new sensor/s removed: core_requirements_media');
}