Skip to content

Commit 9e82a30

Browse files
authored
Test for si --existing-config (#3665)
* Test for si --existing-config * CS * --existin-config is only applicable to Drupal 8.6+ * Better comparison.
1 parent f646fb7 commit 9e82a30

File tree

3 files changed

+49
-22
lines changed

3 files changed

+49
-22
lines changed

src/Commands/core/SiteInstallCommands.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ public function install(array $profile, $options = ['db-url' => self::REQ, 'db-p
8080
$db_spec = $sql->getDbSpec();
8181

8282
$account_pass = $options['account-pass'] ?: StringUtils::generatePassword();
83+
84+
// Was giving error during validate() so its here for now.
85+
if ($options['existing-config']) {
86+
$existing_config_dir = config_get_config_directory(CONFIG_SYNC_DIRECTORY);
87+
if (!is_dir($existing_config_dir)) {
88+
throw new \Exception(dt('Existing config directory @dir not found', ['@dir' => $existing_config_dir]));
89+
}
90+
$this->logger()->info(dt('Installing from existing config at @dir', ['@dir' => $existing_config_dir]));
91+
}
92+
8393
$settings = [
8494
'parameters' => [
8595
'profile' => $profile,

tests/ConfigTest.php

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Unish;
44

5+
use Composer\Semver\Comparator;
6+
57
/**
68
* Tests for Configuration Management commands for D8+.
79
* @group commands
@@ -14,24 +16,25 @@ public function setUp()
1416
{
1517
if (!$this->getSites()) {
1618
$this->setUpDrupal(1, true);
17-
$this->drush('pm-enable', ['config']);
19+
// Field module is needed for now for --existing-config. It is not actually
20+
// enabled after testing profile is installed. Its required by file and update though.
21+
$this->drush('pm:enable', ['config, field']);
1822
}
1923
}
2024

2125
public function testConfigGetSet()
2226
{
23-
$this->drush('config-set', ['system.site', 'name', 'config_test']);
24-
$this->drush('config-get', ['system.site', 'name']);
27+
$this->drush('config:set', ['system.site', 'name', 'config_test']);
28+
$this->drush('config:get', ['system.site', 'name']);
2529
$this->assertEquals("'system.site:name': config_test", $this->getOutput(), 'Config was successfully set and get.');
2630
}
2731

28-
public function testConfigExportImportStatus()
32+
public function testConfigExportImportStatusExistingConfig()
2933
{
3034
// Get path to sync dir.
31-
$this->drush('core-status', [], ['format' => 'json', 'fields' => 'config-sync']);
35+
$this->drush('core:status', [], ['format' => 'json', 'fields' => 'config-sync']);
3236
$sync = $this->webroot() . '/' . $this->getOutputFromJSON('config-sync');
3337
$system_site_yml = $sync . '/system.site.yml';
34-
$core_extension_yml = $sync . '/core.extension.yml';
3538

3639
// Test export.
3740
$this->drush('config-export');
@@ -40,12 +43,12 @@ public function testConfigExportImportStatus()
4043
// Test import and status by finishing the round trip.
4144
$contents = file_get_contents($system_site_yml);
4245
$contents = preg_replace('/front: .*/', 'front: unish', $contents);
43-
$contents = file_put_contents($system_site_yml, $contents);
44-
46+
file_put_contents($system_site_yml, $contents);
47+
4548
// Test status of changed configuration.
4649
$this->drush('config:status');
4750
$this->assertContains('system.site', $this->getOutput(), 'config:status correctly reports changes.');
48-
51+
4952
// Test import.
5053
$this->drush('config-import');
5154
$this->drush('config-get', ['system.site', 'page'], ['format' => 'json']);
@@ -55,7 +58,7 @@ public function testConfigExportImportStatus()
5558
// Test status of identical configuration.
5659
$this->drush('config:status', [], ['format' => 'list']);
5760
$this->assertEquals('', $this->getOutput(), 'config:status correctly reports identical config.');
58-
61+
5962
// Similar, but this time via --partial option.
6063
$contents = file_get_contents($system_site_yml);
6164
$contents = preg_replace('/front: .*/', 'front: unish partial', $contents);
@@ -66,5 +69,18 @@ public function testConfigExportImportStatus()
6669
$this->drush('config-get', ['system.site', 'page'], ['format' => 'json']);
6770
$page = $this->getOutputFromJSON('system.site:page');
6871
$this->assertContains('unish partial', $page->front, '--partial was successfully imported.');
72+
73+
// Test the --existing-config option for site:install.
74+
$this->drush('core:status', ['drupal-version'], ['format' => 'string']);
75+
$drupal_version = $this->getOutputRaw();
76+
if (Comparator::greaterThanOrEqualTo($drupal_version, '8.6')) {
77+
$contents = file_get_contents($system_site_yml);
78+
$contents = preg_replace('/front: .*/', 'front: unish existing', $contents);
79+
file_put_contents($system_site_yml, $contents);
80+
$this->setUpDrupal(1, true, ['existing-config' => null]);
81+
$this->drush('config-get', ['system.site', 'page'], ['format' => 'json']);
82+
$page = $this->getOutputFromJSON('system.site:page');
83+
$this->assertContains('unish existing', $page->front, 'Existing config was successfully imported during site:install.');
84+
}
6985
}
7086
}

tests/UnishTestCase.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,11 @@ public function log($message, $type = 'notice')
234234

235235
public function logLevel()
236236
{
237+
$argv = $_SERVER['argv'];
237238
// -d is reserved by `phpunit`
238-
if (in_array('--debug', $_SERVER['argv'])) {
239+
if (in_array(['--debug'], $argv) || in_array('-vvv', $argv)) {
239240
return 'debug';
240-
} elseif (in_array('--verbose', $_SERVER['argv']) || in_array('-v', $_SERVER['argv'])) {
241+
} elseif (in_array('--verbose', $argv) || in_array('-v', $argv)) {
241242
return 'verbose';
242243
}
243244
}
@@ -537,15 +538,15 @@ public function createSettings($subdir)
537538
*
538539
* It is no longer supported to pass alternative versions of Drupal or an alternative install_profile.
539540
*/
540-
public function setUpDrupal($num_sites = 1, $install = false)
541+
public function setUpDrupal($num_sites = 1, $install = false, $options = [])
541542
{
542543
$sites_subdirs_all = ['dev', 'stage', 'prod', 'retired', 'elderly', 'dead', 'dust'];
543544
$sites_subdirs = array_slice($sites_subdirs_all, 0, $num_sites);
544545
$root = $this->webroot();
545546

546547
// Install (if needed).
547548
foreach ($sites_subdirs as $subdir) {
548-
$this->installDrupal($subdir, $install);
549+
$this->installDrupal($subdir, $install, $options);
549550
}
550551

551552
// Write an empty sites.php. Needed for multi-site on D8+.
@@ -590,22 +591,22 @@ public function createAliasFile($sites_subdirs, $aliasGroup = 'unish')
590591
*
591592
* It is no longer supported to pass alternative versions of Drupal or an alternative install_profile.
592593
*/
593-
public function installDrupal($env = 'dev', $install = false)
594+
public function installDrupal($env = 'dev', $install = false, $options = [])
594595
{
595596
$root = $this->webroot();
596597
$uri = $env;
597598
$site = "$root/sites/$uri";
598599

599600
// If specified, install Drupal as a multi-site.
600601
if ($install) {
601-
$options = [
602-
'root' => $root,
603-
'db-url' => $this->dbUrl($env),
604-
'sites-subdir' => $uri,
605-
'yes' => null,
606-
'quiet' => null,
602+
$options += [
603+
'root' => $root,
604+
'db-url' => $this->dbUrl($env),
605+
'sites-subdir' => $uri,
606+
'yes' => null,
607+
'quiet' => null,
607608
];
608-
$this->drush('site-install', ['testing', 'install_configure_form.enable_update_status_emails=NULL'], $options);
609+
$this->drush('site:install', ['testing', 'install_configure_form.enable_update_status_emails=NULL'], $options);
609610
// Give us our write perms back.
610611
chmod($site, 0777);
611612
} else {

0 commit comments

Comments
 (0)