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-1739: Add services setup command. #104

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ The Task Runner comes with the following built-in commands:
| `drupal:site-install` | Install a target Drupal site using default configuration values and/or CLI options |
| `drupal:site-pre-install` | Run Drupal pre-install commands as listed under the `drupal.pre_install` property |
| `drupal:site-post-install` | Run Drupal post-install commands as listed under the `drupal.post_install` property |
| `drupal:services-setup` | Create services file in site directory with parameters present in `drupal.service_parameters` |
| `drupal:settings-setup` | Setup default Drupal settings file by appending values specified at `drupal.settings` |
| `drupal:drush-setup` | Setup Drush 8 and 9 configuration files |
| `release:create-archive` | Create and archive a release for the current project |
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
"prefer-stable": true,
"require": {
"consolidation/robo": "^1.4",
"cweagans/composer-patches": "^1.6",
"gitonomy/gitlib": "^1.0",
"jakeasmith/http_build_url": "^1.0.1",
"nuvoleweb/robo-config": "^0.2.1"
"nuvoleweb/robo-config": "^0.2.1",
"symfony/yaml": "^3.4||4"
},
"require-dev": {
"openeuropa/code-review": "~1.0.0-beta3",
Expand Down
4 changes: 4 additions & 0 deletions config/commands/drupal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ command:
drush-setup:
options:
config-dir: ${drupal.root}/drush
services-setup:
options:
sites-subdir: ${drupal.site.sites_subdir}
force: ${drupal.site.force}
settings-setup:
options:
sites-subdir: ${drupal.site.sites_subdir}
Expand Down
54 changes: 52 additions & 2 deletions src/Commands/AbstractDrupalCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use OpenEuropa\TaskRunner\Traits as TaskRunnerTraits;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Yaml\Dumper;
use Symfony\Component\Yaml\Yaml;

/**
Expand Down Expand Up @@ -307,7 +308,7 @@ public function drushSetup(array $options = [
])
{
$config = $this->getConfig();
$yaml = Yaml::dump($config->get('drupal.drush'));
$yaml = $this->dumpYaml($config->get('drupal.drush'));

return $this->collectionBuilder()->addTaskList([
$this->taskWriteConfiguration($options['root'].'/sites/default/drushrc.php', $config)->setConfigKey('drupal.drush'),
Expand All @@ -316,8 +317,45 @@ public function drushSetup(array $options = [
}

/**
* Setup Drupal settings overrides.
* Setup Drupal services file.
*
* This command will add the configuration under service_parameters present in
* runner.yml into "services.yml" file in the site directory.
*

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to add example. Something like:

> drupal:
>   service_parameters:
>     session.storage.options:
>       cookie_lifetime: 0

* @command drupal:services-setup
*
* @option root Drupal root.
* @option sites-subdir Drupal site subdirectory.
* @option force Drupal force generation of a new services.yml.
*
* @param array $options
*
* @return \Robo\Collection\CollectionBuilder
*/
voidtek marked this conversation as resolved.
Show resolved Hide resolved
public function servicesSetup(array $options = [
'root' => InputOption::VALUE_REQUIRED,
'sites-subdir' => InputOption::VALUE_REQUIRED,
'force' => false,
])
{
// Read given parameters.
$service_parameters['parameters'] = $this->getConfig()->get('drupal.service_parameters', []);
$yaml = $this->dumpYaml($service_parameters);
Comment on lines +342 to +343

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This better go to the IF statement (line 349) as they are used in the statement only.


// Set the destination file.
$services_destination_file = $options['root'] . '/sites/' . $options['sites-subdir'] . '/services.yml';

$collection = [];
if ($options['force'] || !file_exists($services_destination_file)) {
$collection[] = $this->taskWriteToFile($services_destination_file)->append(false)->text($yaml);
}

return $this->collectionBuilder()->addTaskList($collection);
}

/**
* Setup Drupal settings overrides.
**
* This command will:
*
* - Copy "default.settings.php" to "settings.php", which will be overridden if existing
Expand Down Expand Up @@ -437,4 +475,16 @@ protected function processPrePostInstallCommands(array &$commands, array $tokens
}
}
}

/**
* Dump Yaml into file using same format as in Drupal.
*
* @param $yaml
* @return string
*/
protected function dumpYaml($yaml)
{
$dumper = new Dumper(2);
return $dumper->dump($yaml, PHP_INT_MAX, 0, Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE);
}
}
48 changes: 48 additions & 0 deletions tests/Commands/DrupalCommandsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,59 @@ public function testPermissions(array $config, $command, $expected_error, $expec
$this->assertEquals($expected_settings_file_permission, $siteSettingsPermissions);
}

/**
* Test the services file setup.
*
* @param array $configs
* @param array $expected
*
* @dataProvider servicesSetupDataProvider
*/
public function testServicesSetup(array $configs, array $expected)
{
$config = $configs['runner'];
$configFile = $this->getSandboxFilepath('runner.yml');
file_put_contents($configFile, $config);

// Process information for destination file services.yml.
$sites_subdir = isset($config['drupal']['site']['sites_subdir']) ? $config['drupal']['site']['sites_subdir'] : 'default';
$services_destination_dir = $this->getSandboxRoot() . '/sites/' . $sites_subdir;
$services_destination_file = $services_destination_dir . '/services.yml';

$force = isset($configs['force'])? ' --force' : '';

if (isset($configs['services_file_content'])) {
mkdir($services_destination_dir, 0777, true);
file_put_contents($services_destination_file, $configs['services_file_content']);
}

// Run the command.
$command = 'drupal:services-setup --root=' . $this->getSandboxRoot() . ' --working-dir=' . $this->getSandboxRoot() . $force;
$input = new StringInput($command);
$runner = new TaskRunner($input, new BufferedOutput(), $this->getClassLoader());
$exit_code = $runner->run();
$this->assertEquals(0, $exit_code, 'Command run returned an error.');

// Process given assertions.
$content = file_get_contents($services_destination_file);
foreach ($expected as $row) {
$this->assertContains($row['contains'], $content);
}
}

/**
* @return array
*/
public function drupalSettingsDataProvider()
{
return $this->getFixtureContent('commands/drupal-site-install.yml');
}

/**
* @return array
*/
public function servicesSetupDataProvider()
{
return $this->getFixtureContent('commands/drupal-services-setup.yml');
}
}
47 changes: 47 additions & 0 deletions tests/fixtures/commands/drupal-services-setup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
- config:
runner: |
my_settings:
foo: bar
drupal:
service_parameters:
settings:
foo: ${my_settings.foo}
expected:
- contains: |
parameters:
settings:
foo: bar
dxvargas marked this conversation as resolved.
Show resolved Hide resolved
- config:
force: true
services_file_content: "parameters: false"
runner: |
my_settings:
foo: bar
drupal:
service_parameters:
settings:
foo: ${my_settings.foo}
expected:
- contains: |
parameters:
settings:
foo: bar
- config:
services_file_content: "parameters: false"
runner: |
my_settings:
foo: bar
drupal:
service_parameters:
settings:
foo: ${my_settings.foo}
expected:
- contains: "parameters: false"
- config:
runner: |
my_settings:
foo: bar
drupal: []
expected:
- contains: |
parameters: { }
10 changes: 10 additions & 0 deletions tests/fixtures/simulation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,16 @@
- "WriteConfiguration('web/sites/default/drushrc.php'"
- "File\\Write('./drush/drush.yml')"

- command: 'drupal:services-setup'
configuration:
drupal:
service_parameters:
cors.config: false

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be:

 cors.config:
    enabled: false


composer: ''
contains:
- "File\\Write('build/sites/default/services.yml'"

- command: 'drupal:settings-setup'
configuration: []
composer: ''
Expand Down