public function configMergeTest::testConfigMergeSeparateSites

7.x configMergeTest.php public configMergeTest::testConfigMergeSeparateSites()

File

tests/configMergeTest.php, line 82
Test config-merge, that merges configuration changes from one site to another.

Namespace

Unish

Class

configMergeTest
@group slow @group commands

Code

public function testConfigMergeSeparateSites() {
  if (UNISH_DRUPAL_MAJOR_VERSION != 8) {
    $this->markTestSkipped('config-merge only works with Drupal 8.');
    return;
  }

  // Create a dev site; we will copy this site to create our stage site
  $sites = $this->setUpDrupal(1, TRUE);

  $dev_options = array(
    'root' => $sites['dev']['root'],
    'uri' => 'default',
    'yes' => NULL,
  );

  // Export initial configuration for dev site
  $this->drush('config-export', array(), $sites['dev'] + array('yes' => NULL, 'strict' => 0));

  // Copy the dev state to make a stage site
  $sites = $this->setUpStagingWorkflow($sites);

  $dev_options = $sites['dev'] + array(
    'yes' => NULL,
    'strict' => 0,
  );

  $stage_options = $sites['stage'] + array(
    'yes' => NULL,
    'tool' => '0',
    'strict' => '0',
  );

  // Part one:  test config-merge using the git push / pull mechanism
  // We have to test 'git' first, because it requires both sites to stay
  // in sync with the upstream repository.  In contrast, the
  // rsync mechanism presumes that the remote site cannot reach the
  // central repository, so it does not attempt to keep the remote side
  // in sync.  Doing this tests later means that we do not need to clean
  // up the repository.

  // Get the last commit hash
  $this->execute("git log --pretty=format:%h -1", CommandUnishTestCase::EXIT_SUCCESS, $sites['dev']['root']);
  $base = $this->getOutput();

  // Make a configuration change on 'stage' site
  $this->drush('config-set', array('system.site', 'name', 'git'), $stage_options);

  // Run config-merge to copy the configuration change to the 'dev' site
  $this->drush('config-merge', array('@stage'), $dev_options + array('git' => NULL, 'base' => $base));

  // Verify that the configuration change we made on 'stage' now exists on 'dev'
  $this->drush('config-get', array('system.site', 'name'), $dev_options);
  $this->assertEquals("'system.site:name': git", $this->getOutput(), 'Config set, merged and fetched via git.');


  // Make a second configuration change on 'stage' site
  $this->drush('config-set', array('system.site', 'name', 'git-2'), $stage_options);

  // Run config-merge again
  $this->drush('config-merge', array('@stage'), $dev_options + array('git' => NULL, 'base' => $base));

  // Verify that the configuration change we made on 'stage' now exists on 'dev'
  $this->drush('config-get', array('system.site', 'name'), $dev_options);
  $this->assertEquals("'system.site:name': git-2", $this->getOutput(), 'Config set, merged and fetched via git a second time.');

  // Part two:  test config-merge using the rsync mechanism

  // Make a configuration change on 'stage' site
  $this->drush('config-set', array('system.site', 'name', 'config_test'), $stage_options);

  // Run config-merge to copy the configuration change to the 'dev' site
  $this->drush('config-merge', array('@stage'), $dev_options);

  // Verify that the configuration change we made on 'stage' now exists on 'dev'
  $this->drush('config-get', array('system.site', 'name'), $dev_options);
  $this->assertEquals("'system.site:name': config_test", $this->getOutput(), 'Config set, merged and fetched via rsync.');

  $this->execute("git reset --hard", CommandUnishTestCase::EXIT_SUCCESS, $sites['dev']['root']);

  // Make a second configuration change on 'stage' site
  $this->drush('config-set', array('system.site', 'name', 'config_test_2'), $stage_options);

  // Run config-merge to copy the configuration change to the 'dev' site
  $this->drush('config-merge', array('@stage'), $dev_options);

  // Verify that the configuration change we made on 'stage' now exists on 'dev'
  $this->drush('config-get', array('system.site', 'name'), $dev_options);
  $this->assertEquals("'system.site:name': config_test_2", $this->getOutput(), 'Config set, merged and fetched via rsync.');


  // Finally, make sure that we have all of the commits we should
  // in the 'dev' site
  $this->execute("git log", CommandUnishTestCase::EXIT_SUCCESS, $sites['dev']['root']);
  // First, we remove blank lines, and lines that do not begin with a space.
  // This gets rid of all of the header lines, like "commit" and "Date:" that
  // contain variable data.
  $outputList = array_filter($this->getOutputAsList(), function($line) {
    if (empty($line)) {
      return FALSE;
    }
    return ($line[0] == ' ');
  });
  // Next, convert all runs of spaces into a single space, and trim.
  $outputList = array_map(function($line) {
    return trim(preg_replace("/  */", " ", $line));
  }, $outputList);
  // Test to see if the compressed result matches our expectations.
  $this->assertEquals("Merged configuration from @stage in config_test_2
Collection Config Operation
system.site update
Merged configuration from @stage in config_test
Collection Config Operation
system.site update
Exported configuration.
Collection Config Operation
system.site update
Exported configuration.
Collection Config Operation
system.site update
Initial commit.", implode("\n", $outputList));
}