Skip to content

Commit

Permalink
Merge pull request #82 from openeuropa/OPENEUROPA-1149
Browse files Browse the repository at this point in the history
OPENEUROPA-1149: Provide alternative method to override settings in settings.php
  • Loading branch information
voidtek authored Oct 1, 2018
2 parents c897ccc + 4fe0890 commit 77372d2
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 20 deletions.
7 changes: 5 additions & 2 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ pipeline:
volumes:
- /cache:/cache
commands:
- composer update --prefer-lowest --ansi
# @todo remove "composer install" step once the following issue is fixed.
# @link https://webgate.ec.europa.eu/CITnet/jira/browse/OPENEUROPA-1234
- composer install --ansi --no-suggest --no-progress
- composer update --prefer-lowest --ansi --no-suggest --no-progress
when:
matrix:
COMPOSER_BOUNDARY: lowest
Expand All @@ -26,7 +29,7 @@ pipeline:
volumes:
- /cache:/cache
commands:
- composer install --ansi
- composer install --ansi --no-suggest --no-progress
when:
matrix:
COMPOSER_BOUNDARY: highest
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"consolidation/robo": "^1.2.2",
"consolidation/robo": "^1.3",
"gitonomy/gitlib": "^1.0",
"nuvoleweb/robo-config": "^0.2.1",
"jakeasmith/http_build_url": "^1.0.1"
Expand Down
5 changes: 5 additions & 0 deletions config/commands/drupal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@ command:
drush-setup:
options:
config-dir: ${drupal.root}/drush
settings-setup:
options:
sites-subdir: ${drupal.site.sites_subdir}
settings-override-file: ${drupal.site.settings_override_file}
force: ${drupal.site.force}
1 change: 1 addition & 0 deletions config/runner.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ drupal:
locale: "en"
sites_subdir: "default"
config_dir: ~
settings_override_file: "settings.override.php"

# Administrator account information.
account:
Expand Down
63 changes: 53 additions & 10 deletions src/Commands/DrupalCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function siteInstall(array $options = [
}

$drush = $this->getConfig()->get('runner.bin_dir').'/drush';
$task = $this->taskDrush($drush)
$task = $this->taskDrush($drush)
->root($options['root'])
->siteName($options['site-name'])
->siteMail($options['site-mail'])
Expand Down Expand Up @@ -255,11 +255,13 @@ public function drushSetup(array $options = [
}

/**
* Setup default Drupal settings file.
* Setup Drupal settings overrides.
*
* This command will append settings specified at "drupal.settings" to the
* current site's "default.settings.php" which, in turn, will be used
* to generate the actual "settings.php" at installation time.
* This command will:
*
* - Copy "default.settings.php" to "settings.php", which will be overridden if existing
* - Append to "settings.php" an include operation for a "settings.override.php" file
* - Write settings specified at "drupal.settings" in "settings.override.php"
*
* Default settings can be customized in your local runner.yml.dist/runner.yml
* as shown below:
Expand All @@ -270,20 +272,61 @@ public function drushSetup(array $options = [
* > sync: '../config/sync'
* > prod: '../config/prod'
*
* The settings override file name can be changed in the Task Runner
* configuration by setting the "drupal.site.settings_override_file" property.
*
* @command drupal:settings-setup
*
* @option root Drupal root.
* @option root Drupal root.
* @option sites-subdir Drupal site subdirectory.
* @option settings-override-file Drupal site settings override filename.
* @option force Drupal force generation of a new settings.php.
*
* @param array $options
*
* @return \Robo\Collection\CollectionBuilder
*/
public function settingsSetup(array $options = [
'root' => InputOption::VALUE_REQUIRED,
'root' => InputOption::VALUE_REQUIRED,
'sites-subdir' => InputOption::VALUE_REQUIRED,
'settings-override-file' => InputOption::VALUE_REQUIRED,
'force' => false,
])
{
return $this->collectionBuilder()->addTaskList([
$this->taskAppendConfiguration($options['root'].'/sites/default/default.settings.php', $this->getConfig())->setConfigKey('drupal.settings'),
]);
$settings_default_path = $options['root'] . '/sites/' . $options['sites-subdir'] . '/default.settings.php';
$settings_path = $options['root'] . '/sites/' . $options['sites-subdir'] . '/settings.php';
$settings_override_path = $options['root'] . '/sites/' . $options['sites-subdir'] . '/' . $options['settings-override-file'];

// Save the filename of the override file in a single variable to use it
// in the heredoc variable $custom_config hereunder.
$settings_override_filename = $options['settings-override-file'];

$custom_config = <<< EOF
/**
* Include Drupal settings overrides.
*
* The following file is generated by the openeuropa/task-runner project
* using configuration from your local "runner.yml.dist/runner.yml" files.
*
* Keep this code block at the end of the file.
*/
if (file_exists(\$app_root . '/' . \$site_path . '/$settings_override_filename')) {
include \$app_root . '/' . \$site_path . '/$settings_override_filename';
}
EOF;
$collection = [];

if (true === (bool) $options['force'] || !file_exists($settings_path)) {
$collection[] = $this->taskFilesystemStack()->copy($settings_default_path, $settings_path, true);
$collection[] = $this->taskWriteToFile($settings_path)->append()->lines([$custom_config]);
}

$collection[] = $this->taskWriteConfiguration(
$settings_override_path,
$this->getConfig()
)->setConfigKey('drupal.settings');

return $this->collectionBuilder()->addTaskList($collection);
}
}
14 changes: 14 additions & 0 deletions tests/AbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,18 @@ protected function getSandboxRoot()
{
return __DIR__."/sandbox";
}

/**
* Generate a random string.
*
* @param int $length
* The desired length.
*
* @return string
* The random string.
*/
protected function generateRandomString($length = 10)
{
return substr(str_shuffle(str_repeat($x = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil($length/strlen($x)))), 1, $length);
}
}
95 changes: 93 additions & 2 deletions tests/CommandsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,98 @@ public function testSettingsSetup(array $config, array $expected)

file_put_contents($configFile, Yaml::dump($config));

$sites_subdir = isset($config['drupal']['site']['sites_subdir']) ? $config['drupal']['site']['sites_subdir'] : 'default';
mkdir($this->getSandboxRoot() . '/build/sites/' . $sites_subdir . '/', 0777, true);
file_put_contents($this->getSandboxRoot() . '/build/sites/' . $sites_subdir . '/default.settings.php', '');

$input = new StringInput("drupal:settings-setup --working-dir=".$this->getSandboxRoot());
$runner = new TaskRunner($input, new BufferedOutput(), $this->getClassLoader());
$runner->run();

foreach ($expected as $row) {
$content = file_get_contents($this->getSandboxFilepath($row['file']));
$this->assertContainsNotContains($content, $row);
}

// Generate a random function name.
$fct = $this->generateRandomString(20);

// Generate a dummy PHP code.
$config_override_dummy_script = <<< EOF
<?php
function $fct() {}
EOF;

$config_override_filename = isset($config['drupal']['site']['settings_override_file']) ?
$config['drupal']['site']['settings_override_file'] :
'settings.override.php';

// Add the dummy PHP code to the config override file.
file_put_contents(
$this->getSandboxRoot() . '/build/sites/' . $sites_subdir . '/' . $config_override_filename,
$config_override_dummy_script
);

// Include the config override file.
include_once $this->getSandboxRoot() . '/build/sites/' . $sites_subdir . '/' . $config_override_filename;

// Test if the dummy PHP code has been properly included.
$this->assertTrue(\function_exists($fct));
}

/**
* @param array $config
* @param array $expected
*
* @dataProvider settingsSetupForceDataProvider
*/
public function testSettingsSetupForce(array $config, array $expected)
{
$configFile = $this->getSandboxFilepath('runner.yml');
file_put_contents($configFile, Yaml::dump($config));

$sites_subdir = isset($config['drupal']['site']['sites_subdir']) ? $config['drupal']['site']['sites_subdir'] : 'default';
mkdir($this->getSandboxRoot() . '/build/sites/' . $sites_subdir . '/', 0777, true);
file_put_contents($this->getSandboxRoot() . '/build/sites/' . $sites_subdir . '/default.settings.php', '');
file_put_contents($this->getSandboxRoot() . '/build/sites/' . $sites_subdir . '/settings.php', '# Already existing file.');

$input = new StringInput('drupal:settings-setup --working-dir=' . $this->getSandboxRoot());

if (true === $config['drupal']['site']['force']) {
$input = new StringInput('drupal:settings-setup --working-dir=' . $this->getSandboxRoot() . ' --force');
}
$runner = new TaskRunner($input, new BufferedOutput(), $this->getClassLoader());
$runner->run();

foreach ($expected as $row) {
$content = file_get_contents($this->getSandboxFilepath($row['file']));
$this->assertContainsNotContains($content, $row);
}

// Generate a random function name.
$fct = $this->generateRandomString(20);

// Generate a dummy PHP code.
$config_override_dummy_script = <<< EOF
<?php
function $fct() {}
EOF;

$config_override_filename = isset($config['drupal']['site']['settings_override_file']) ?
$config['drupal']['site']['settings_override_file'] :
'settings.override.php';

// Add the dummy PHP code to the config override file.
file_put_contents(
$this->getSandboxRoot() . '/build/sites/' . $sites_subdir . '/' . $config_override_filename,
$config_override_dummy_script
);

// Include the config override file.
include_once $this->getSandboxRoot() . '/build/sites/' . $sites_subdir . '/' . $config_override_filename;

// Test if the dummy PHP code has been properly included.
$this->assertTrue(\function_exists($fct));
}

/**
Expand Down Expand Up @@ -200,6 +283,14 @@ public function settingsSetupDataProvider()
return $this->getFixtureContent('commands/drupal-settings-setup.yml');
}

/**
* @return array
*/
public function settingsSetupForceDataProvider()
{
return $this->getFixtureContent('commands/drupal-settings-setup-force.yml');
}

/**
* @return array
*/
Expand All @@ -223,8 +314,8 @@ public function changelogDataProvider()
protected function assertContainsNotContains($content, array $expected)
{
$this->assertContains($expected['contains'], $content);
if (!empty($row['not_contains'])) {
$this->assertNotContains($row['not_contains'], $content);
if (!empty($expected['not_contains'])) {
$this->assertNotContains($expected['not_contains'], $content);
}
}
}
17 changes: 17 additions & 0 deletions tests/fixtures/commands/drupal-settings-setup-force.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
- configuration:
drupal:
site:
force: false
expected:
- file: "build/sites/default/settings.php"
contains: "# Already existing file."
not_contains: "include $app_root . '/' . $site_path . '/settings.override.php';"

- configuration:
drupal:
site:
force: true
expected:
- file: "build/sites/default/settings.php"
contains: "include $app_root . '/' . $site_path . '/settings.override.php';"
not_contains: "# Already existing file."
42 changes: 39 additions & 3 deletions tests/fixtures/commands/drupal-settings-setup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
- "vendor"
- "${drupal.root}"
expected:
- file: "build/sites/default/default.settings.php"
- file: "build/sites/default/settings.override.php"
contains: "$conf[\"file_scan_ignore_directories\"] = array(0 => 'node_modules',1 => 'bower_components',2 => 'vendor',3 => 'build');"
not_contains: ~
- file: "build/sites/default/settings.php"
contains: "include $app_root . '/' . $site_path . '/settings.override.php';"
not_contains: ~

- configuration:
drupal:
Expand All @@ -19,14 +22,47 @@
file_scan_ignore_directories:
- "${drupal.root}"
expected:
- file: "build/sites/default/default.settings.php"
- file: "build/sites/default/settings.override.php"
contains: "$settings[\"file_scan_ignore_directories\"] = array(0 => 'build');"
not_contains: ~

- configuration:
drupal:
settings: []
expected:
- file: "build/sites/default/default.settings.php"
- file: "build/sites/default/settings.override.php"
contains: "// Start settings processor block."
not_contains: "file_scan_ignore_directories"

- configuration:
drupal:
settings:
settings:
file_scan_ignore_directories:
- "${drupal.root}"
site:
settings_override_file: "settings.overridemeplease.php"
expected:
- file: "build/sites/default/settings.overridemeplease.php"
contains: "$settings[\"file_scan_ignore_directories\"] = array(0 => 'build');"
not_contains: ~
- file: "build/sites/default/settings.php"
contains: "include $app_root . '/' . $site_path . '/settings.overridemeplease.php';"
not_contains: ~

- configuration:
drupal:
settings:
settings:
file_scan_ignore_directories:
- "${drupal.root}"
site:
sites_subdir: "inea"
settings_override_file: "settings.overridemeplease.php"
expected:
- file: "build/sites/inea/settings.overridemeplease.php"
contains: "$settings[\"file_scan_ignore_directories\"] = array(0 => 'build');"
not_contains: ~
- file: "build/sites/inea/settings.php"
contains: "include $app_root . '/' . $site_path . '/settings.overridemeplease.php';"
not_contains: ~
4 changes: 2 additions & 2 deletions tests/fixtures/simulation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,13 @@
configuration: []
composer: ''
contains:
- "AppendConfiguration('build/sites/default/default.settings.php'"
- "WriteConfiguration('build/sites/default/settings.override.php'"

- command: 'drupal:settings-setup --root=web'
configuration: []
composer: ''
contains:
- "AppendConfiguration('web/sites/default/default.settings.php'"
- "WriteConfiguration('web/sites/default/settings.override.php'"

- command: 'drupal:site-setup'
configuration:
Expand Down

0 comments on commit 77372d2

Please sign in to comment.