You are here

public function ConfigurationHandlerBaseTestCase::testCheckModifications in Configuration Management 7.2

Import a configuration. Make modifications on it, and then revert it.

This function will check that the configuration is 'In Sync' after is imported the first time. Then is overridden after is modified, and is again 'In Sync' after revert it.

File

tests/handlers/ConfigurationHandlerBaseTestCase.test, line 118
Tests for Configuration Management: Base Class

Class

ConfigurationHandlerBaseTestCase
Base class for functional tests for configuration management.

Code

public function testCheckModifications() {
  $verbose = '';

  // Change the path from where the configurations are loaded.
  $source = drupal_get_path('module', 'configuration') . '/tests/test_configs/';
  $configToModify = $this
    ->configToModify();
  $verbose .= 'Configs to modify: ' . implode($configToModify, ', ') . "\n";
  $results = ConfigurationManagement::importToActiveStore($configToModify, $this
    ->modifyDependencies(), $this
    ->modifyOptionals(), TRUE, $source);
  $original_hash = array();

  // Check that all the configurations were tracked and are In Sync.
  foreach ($configToModify as $config) {
    list($component, $identifier) = explode('.', $config, 2);
    $object = db_select('configuration_tracked', 'ct')
      ->fields('ct')
      ->condition('component', $component)
      ->condition('identifier', $identifier)
      ->execute()
      ->fetchObject();
    $this
      ->assertTrue(!empty($object), "Config {$config} was successfully tracked.");
    $verbose .= "This is the content of the tracked.inc file: \n";
    $tracking_file = ConfigurationManagement::readTrackingFile();
    foreach ($tracking_file as $config_id => $tracked_hash) {
      $verbose .= "\$tracked_hash[{$config_id}] = " . var_export($tracked_hash, TRUE) . "\n";
    }
    $verbose .= "\n\n\n";
    $handler = ConfigurationManagement::createConfigurationInstance($config);
    $original_hash[$config] = $handler
      ->loadFromActiveStore()
      ->getHash();
    $verbose .= 'The content of ' . $config . ' is ' . $handler
      ->raw() . "\n\n";
    $this
      ->assertTrue($handler
      ->getStatus() == 'In Sync', "{$config} is In Sync");
    $verbose .= "\n\n\n";
    unset($handler);
  }
  $verbose .= "Original Hashes after import: \n";
  foreach ($original_hash as $config_id => $hash) {
    $verbose .= "\$original_hash[{$config_id}] = {$hash} \n";
  }

  // Now modify the configurations
  $this
    ->modifyConfiguration();
  $verbose .= "The configurations were modified \n";
  foreach ($configToModify as $config) {
    if ($this
      ->checkModification($config)) {
      $this
        ->assertTrue($this
        ->isModified($config), "{$config} was modified in the active store.");
      $handler = ConfigurationManagement::createConfigurationInstance($config);
      $current_hashes[$config] = $current_hash = $handler
        ->loadFromActiveStore()
        ->getHash();

      //$this->verbose('<pre>' . $handler->raw() . '</pre>', 'Modified');
      $verbose .= 'The content of ' . $config . ' is ' . $handler
        ->raw() . "\n\n";
      $this
        ->assertTrue($current_hash != $original_hash[$config], "The hash for {$config} is not the same after the modification");
      $verbose .= "\n\n\n";
      unset($handler);
    }
  }
  $verbose .= "Hashes after modify configs: \n";
  foreach ($current_hashes as $config_id => $hash) {
    $verbose .= "\$current_hashes[{$config_id}] = {$hash} \n";
  }
  $verbose .= "\n\n\n";

  // Finally revert the changes
  $results = ConfigurationManagement::importToActiveStore($configToModify, $this
    ->modifyDependencies(), $this
    ->modifyOptionals(), TRUE);
  foreach ($configToModify as $config) {
    if ($this
      ->checkModification($config)) {
      $this
        ->assertFalse($this
        ->isModified($config), "{$config} is not modified in the active store.");
    }
    $handler = ConfigurationManagement::createConfigurationInstance($config);
    $final_hashes[$config] = $current_hash = $handler
      ->loadFromActiveStore()
      ->getHash();
    $status = $handler
      ->getStatus();
    $this
      ->assertTrue($status == 'In Sync', "{$config} is In Sync after revert (Status {$status})");
    $this
      ->assertTrue($current_hash == $original_hash[$config], "The hash for {$config} is the same after than the original after the revert");
  }
  $verbose .= "Hashes after revert configs: \n";
  foreach ($current_hashes as $config_id => $hash) {
    $verbose .= "\$final_hashes[{$config_id}] = {$hash} \n";
  }
  $verbose .= "\n\n\n";
  foreach ($final_hashes as $config => $hash) {
    if ($this
      ->checkModification($config)) {
      $verbose .= 'Original: ' . $original_hash[$config] . '   Modified: ' . $current_hashes[$config] . '  Final: ' . $final_hashes[$config] . "\n\n";
    }
  }
  $this
    ->verbose('<pre>' . $verbose . '</pre>');
  $this
    ->extraChecksOnModify();
}