You are here

public function MonitoringUiJavascriptTest::testSensorInstalledModulesUI in Monitoring 8

Tests the UI/settings of the installed modules sensor.

See also

\Drupal\monitoring\Plugin\monitoring\SensorPlugin\EnabledModulesSensorPlugin

File

tests/src/FunctionalJavascript/MonitoringUiJavascriptTest.php, line 363

Class

MonitoringUiJavascriptTest
Tests for the Monitoring UI.

Namespace

Drupal\Tests\monitoring\FunctionalJavascript

Code

public function testSensorInstalledModulesUI() {
  $account = $this
    ->drupalCreateUser([
    'administer monitoring',
  ]);
  $this
    ->drupalLogin($account);
  $page = $this
    ->getSession()
    ->getPage();

  // Visit settings page of the disabled sensor. We run the sensor to check
  // for deltas. This led to fatal errors with a disabled sensor.
  $this
    ->drupalGet('admin/config/system/monitoring/sensors/monitoring_installed_modules');

  // Enable the sensor.
  monitoring_sensor_manager()
    ->enableSensor('monitoring_installed_modules');

  // Test submitting the defaults and enabling the sensor.
  $this
    ->drupalPostForm('admin/config/system/monitoring/sensors/monitoring_installed_modules', [
    'status' => TRUE,
  ], 'Save');

  // Reset the sensor config so that it reflects changes done via POST.
  monitoring_sensor_manager()
    ->resetCache();

  // The sensor should now be OK.
  $result = monitoring_sensor_run('monitoring_installed_modules');
  $this
    ->assertTrue($result
    ->isOk());

  // Expect the contact and book modules to be installed.
  $this
    ->drupalPostForm('admin/config/system/monitoring/sensors/monitoring_installed_modules', [
    'settings[modules][contact]' => TRUE,
    'settings[modules][book]' => TRUE,
  ], 'Save');

  // Reset the sensor config so that it reflects changes done via POST.
  monitoring_sensor_manager()
    ->resetCache();

  // Make sure the extended / hidden_modules form submit cleanup worked and
  // they are not stored as a duplicate in settings.
  $sensor_config = SensorConfig::load('monitoring_installed_modules');
  $this
    ->assertTrue(!array_key_exists('extended', $sensor_config->settings), 'Do not persist extended module hidden selections separately.');

  // The sensor should escalate to CRITICAL.
  $result = $this
    ->runSensor('monitoring_installed_modules');
  $this
    ->assertTrue($result
    ->isCritical());
  $this
    ->assertEquals('2 modules delta, expected 0, Following modules are expected to be installed: Book (book), Contact (contact)', $result
    ->getMessage());
  $this
    ->assertEquals(2, $result
    ->getValue());

  // Reset modules selection with the update selection (ajax) button.
  $this
    ->drupalGet('admin/config/system/monitoring/sensors/monitoring_installed_modules');
  $page
    ->pressButton('Update module selection');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->drupalPostForm(NULL, [], 'Save');
  $result = $this
    ->runSensor('monitoring_installed_modules');
  $this
    ->assertTrue($result
    ->isOk());
  $this
    ->assertEquals('0 modules delta', $result
    ->getMessage());

  // The default setting is not to allow additional modules. Enable comment
  // and the sensor should escalate to CRITICAL.
  $this
    ->installModules([
    'help',
  ]);
  $result = $this
    ->runSensor('monitoring_installed_modules');
  $this
    ->assertTrue($result
    ->isCritical());
  $this
    ->assertEquals('1 modules delta, expected 0, Following modules are NOT expected to be installed: Help (help)', $result
    ->getMessage());
  $this
    ->assertEquals(1, $result
    ->getValue());

  // Allow additional, the sensor should not escalate anymore.
  $this
    ->drupalPostForm('admin/config/system/monitoring/sensors/monitoring_installed_modules', [
    'settings[allow_additional]' => 1,
  ], 'Save');
  $result = $this
    ->runSensor('monitoring_installed_modules');
  $this
    ->assertTrue($result
    ->isOk());
  $this
    ->assertEquals('0 modules delta', $result
    ->getMessage());
}