Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OPENEUROPA-1149: Provide alternative method to override settings in settings.php #82

Merged
merged 26 commits into from
Oct 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
56fb495
OPENEUROPA-1149: Implementing workaround for avoiding writing setting…
sergepavle Sep 26, 2018
a98ce50
OPENEUROPA-1149: Make included additional settings file configurable.
sergepavle Sep 26, 2018
9455a21
OPENEUROPA-1149: Correct notes according to notes in PR.
sergepavle Sep 27, 2018
d447b3d
OPENEUROPA-1149: Change in task runner command class.
sergepavle Sep 27, 2018
c78584b
OPENEUROPA-1149: Changes in task runner configs.
sergepavle Sep 27, 2018
d29e8e2
OPENEUROPA-1149: Minor code amend for DrupalCommand.
Sep 27, 2018
c2e4dd5
OPENEUROPA-1149: Change structure of task runner config file and add …
sergepavle Sep 27, 2018
2832d19
OPENEUROPA-1149: Apply additional options in settingsSetup method.
sergepavle Sep 27, 2018
549a5cd
OPENEUROPA-1149: Prepare files for phpunit.
sergepavle Sep 27, 2018
c6e51f7
OPENEUROPA-1149: Change configurations for unittests.
sergepavle Sep 27, 2018
8bd6151
OPENEUROPA-1149: Fix code issue in DrupalCommands class.
Sep 28, 2018
a04cb6a
OPENEUROPA-1149: Fix code sniffer issues.
sergepavle Sep 28, 2018
04172b7
OPENEUROPA-1149: Fix Composer issue with --prefer-lowest on Drone.
ademarco Sep 28, 2018
e008ddf
OPENEUROPA-1149: Implement all remarks and fix issues.
voidtek Sep 28, 2018
63cfe78
OPENEUROPA-1149: Update command description.
ademarco Sep 28, 2018
7798fb3
OPENEUROPA-1149: Add the option force to override the setting.php.
voidtek Sep 28, 2018
c08a52f
OPENEUROPA-1149: Change logic for the force and add test.
voidtek Sep 28, 2018
5c0715c
OPENEUROPA-1149: Resolve file permissions issues.
voidtek Sep 28, 2018
e9659ee
OPENEUROPA-1149: Fix configuration issues.
voidtek Sep 28, 2018
72295b6
OPENEUROPA-1149: Fix check for sites_subdir definition.
Sep 28, 2018
f663bcb
OPENEUROPA-1149: Improve force option processing.
Sep 28, 2018
bbddc87
OPENEUROPA-1149: Minor code improvements.
Sep 29, 2018
2a9c672
OPENEUROPA-1149: Update the logic of the force option.
voidtek Oct 1, 2018
6822357
OPENEUROPA-1149: Update the syntax and add documentation block.
drupol Oct 1, 2018
e636f9e
OPENEUROPA-1149: Update the tests to make sure that the settings over…
drupol Oct 1, 2018
4fe0890
OPENEUROPA-1149: Update generated comment.
ademarco Oct 1, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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