diff --git a/README.md b/README.md index 6edc2fe6..96b33b23 100644 --- a/README.md +++ b/README.md @@ -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 | diff --git a/composer.json b/composer.json index 11402243..cbcc1c4f 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/config/commands/drupal.yml b/config/commands/drupal.yml index b70aa52b..a0669e32 100644 --- a/config/commands/drupal.yml +++ b/config/commands/drupal.yml @@ -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} diff --git a/src/Commands/AbstractDrupalCommands.php b/src/Commands/AbstractDrupalCommands.php index 28e1592d..b565673f 100644 --- a/src/Commands/AbstractDrupalCommands.php +++ b/src/Commands/AbstractDrupalCommands.php @@ -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; /** @@ -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'), @@ -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. + * + * @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 + */ + 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); + + // 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 @@ -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); + } } diff --git a/tests/Commands/DrupalCommandsTest.php b/tests/Commands/DrupalCommandsTest.php index 30beb9ae..0452b85e 100644 --- a/tests/Commands/DrupalCommandsTest.php +++ b/tests/Commands/DrupalCommandsTest.php @@ -68,6 +68,46 @@ 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 */ @@ -75,4 +115,12 @@ public function drupalSettingsDataProvider() { return $this->getFixtureContent('commands/drupal-site-install.yml'); } + + /** + * @return array + */ + public function servicesSetupDataProvider() + { + return $this->getFixtureContent('commands/drupal-services-setup.yml'); + } } diff --git a/tests/fixtures/commands/drupal-services-setup.yml b/tests/fixtures/commands/drupal-services-setup.yml new file mode 100644 index 00000000..4416b786 --- /dev/null +++ b/tests/fixtures/commands/drupal-services-setup.yml @@ -0,0 +1,47 @@ +- config: + runner: | + my_settings: + foo: bar + drupal: + service_parameters: + settings: + foo: ${my_settings.foo} + expected: + - contains: | + parameters: + settings: + foo: bar +- 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: { } \ No newline at end of file diff --git a/tests/fixtures/simulation.yml b/tests/fixtures/simulation.yml index 0d60fb4a..8dcd0dc5 100644 --- a/tests/fixtures/simulation.yml +++ b/tests/fixtures/simulation.yml @@ -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 + + composer: '' + contains: + - "File\\Write('build/sites/default/services.yml'" + - command: 'drupal:settings-setup' configuration: [] composer: ''