You are here

public function AcquiaConnectorSpiTest::testAcquiaSpiSetVariablesTests in Acquia Connector 3.x

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/AcquiaConnectorSpiTest.php \Drupal\Tests\acquia_connector\Functional\AcquiaConnectorSpiTest::testAcquiaSpiSetVariablesTests()
  2. 8 tests/src/Functional/AcquiaConnectorSpiTest.php \Drupal\Tests\acquia_connector\Functional\AcquiaConnectorSpiTest::testAcquiaSpiSetVariablesTests()

Test Acquia SPI set variables.

File

tests/src/Functional/AcquiaConnectorSpiTest.php, line 552

Class

AcquiaConnectorSpiTest
Tests the functionality of the Acquia SPI module.

Namespace

Drupal\Tests\acquia_connector\Functional

Code

public function testAcquiaSpiSetVariablesTests() {

  // Connect site on non-error key and id.
  $this
    ->connectSite();
  $edit_fields = [
    'name' => $this->acqtestName,
    'machine_name' => $this->acqtestMachineName,
  ];
  $submit_button = 'Save configuration';
  $this
    ->drupalPostForm($this->settingsPath, $edit_fields, $submit_button);
  $spi = new SpiController(\Drupal::service('acquia_connector.client'), \Drupal::service('config.factory'), \Drupal::service('path_alias.manager'));
  $spi_data = $spi
    ->get();
  $vars = Json::decode($spi_data['system_vars']);
  $this
    ->assertEmpty($vars['acquia_spi_saved_variables']['variables'], 'Have not saved any variables');

  // Set error reporting so variable is saved.
  $edit = [
    'error_level' => 'verbose',
  ];
  $this
    ->drupalPostForm('admin/config/development/logging', $edit, 'Save configuration');

  // Turn off error reporting.
  $set_variables = [
    'error_level' => 'hide',
  ];
  $variables = new VariablesController();
  $variables
    ->setVariables($set_variables);
  $new = \Drupal::config('system.logging')
    ->get('error_level');
  $this
    ->assertTrue($new === 'hide', 'Set error reporting to log only');
  $vars = Json::decode($variables
    ->getVariablesData());
  $this
    ->assertContains('error_level', $vars['acquia_spi_saved_variables']['variables'], 'SPI data reports error level was saved');
  $this
    ->assertArrayHasKey('time', $vars['acquia_spi_saved_variables'], 'Set time for saved variables');

  // Attemp to set variable that is not whitelisted.
  $current = \Drupal::config('system.site')
    ->get('name');
  $set_variables = [
    'site_name' => 0,
  ];
  $variables
    ->setVariables($set_variables);
  $after = \Drupal::config('system.site')
    ->get('name');
  $this
    ->assertIdentical($current, $after, 'Non-whitelisted variable cannot be automatically set');
  $vars = Json::decode($variables
    ->getVariablesData());
  $this
    ->assertNotContains('site_name', $vars['acquia_spi_saved_variables']['variables'], 'SPI data does not include anything about trying to save clean url');

  // Test override of approved variable list.
  \Drupal::configFactory()
    ->getEditable('acquia_connector.settings')
    ->set('spi.set_variables_override', FALSE)
    ->save();

  // Variables controller stores old config.
  $variables = new VariablesController();
  $set_variables = [
    'acquia_spi_set_variables_automatic' => 'test_variable',
  ];
  $variables
    ->setVariables($set_variables);
  $vars = Json::decode($variables
    ->getVariablesData());
  $this
    ->assertArrayNotHasKey('test_variable', $vars, 'Using default list of approved list of variables');
  \Drupal::configFactory()
    ->getEditable('acquia_connector.settings')
    ->set('spi.set_variables_override', TRUE)
    ->save();

  // Variables controller stores old config.
  $variables = new VariablesController();
  $set_variables = [
    'acquia_spi_set_variables_automatic' => 'test_variable',
  ];
  $variables
    ->setVariables($set_variables);
  $vars = Json::decode($variables
    ->getVariablesData());
  $this
    ->assertIdentical($vars['acquia_spi_set_variables_automatic'], 'test_variable', 'Altered approved list of variables that can be set');
}