Skip to content

Commit

Permalink
Merge pull request #16 from acquia/ACMS-3281
Browse files Browse the repository at this point in the history
ACMS-3281: Refactor Drupal recommended settings and add tests.
  • Loading branch information
vishalkhode1 authored Nov 20, 2023
2 parents 724e59d + 4b04a75 commit 6c55be0
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 43 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/drs_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@ jobs:
coverage: xdebug
- name: Download ORCA
run: composer create-project --no-dev --ignore-platform-req=php acquia/orca ../orca "$ORCA_VERSION" -n
- name: Before Install
run: ../orca/bin/ci/before_install.sh
- name: Install
run: ../orca/bin/ci/install.sh
- name: Before script
run: ../orca/bin/ci/before_script.sh
- name: Script
run: ../orca/bin/ci/script.sh
run: ./tests/ci/script.sh
- name: After script
run: |
../orca/bin/ci/after_success.sh
Expand Down
87 changes: 54 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# Acquia Drupal Recommended Settings
The Acquia Drupal Recommended Settings plugin adds the recommended settings to the Drupal project, so developers won't have to edit settings.php manually.
The Acquia Drupal Recommended Settings plugin adds the recommended settings to
the Drupal project, so developers won't have to edit settings.php manually.

The recommended settings includes:
- the required database credentials
- configuration sync directory path.
- public/private etc. file directory path.
- The required database credentials.
- Configuration sync directory path.
- File directory path i.e public/private etc.
- Acquia site studio sync directory path.
- Includes Drupal module [Config sync without site uuid](https://www.drupal.org/project/config_sync_without_site_uuid) features.

It allows your websites to be easily installed in both Acquia Cloud IDE & local and deployable on Acquia Cloud.
It allows your websites to be easily installed in both Acquia Cloud IDE & local
and deployable on Acquia Cloud.

## Installation

Expand All @@ -18,43 +19,63 @@ You can also install this using Composer like so:
composer require acquia/drupal-recommended-settings
```

## Steps to switch from BLT to Acquia Drupal Recommended Settings
This plugin doesn't work with the acquia/blt plugin. If the plugin exists, it must be removed.
## Steps to use Acquia Drupal Recommended Settings with BLT.
This plugin works with acquia/blt plugin.

- Remove BLT plugin using below command
- Update BLT plugin to latest release.
```
composer remove acquia/blt
composer update acquia/blt -W
```

- Remove BLT reference from settings.php file located at
``/docroot/sites/default/settings.php``.
```
require DRUPAL_ROOT . "/../vendor/acquia/blt/settings/blt.settings.php";
- Latest release of BLT will download the acquia/drupal-recommended-settings
plugin automatically as dependency.

## Steps to use Acquia Drupal Recommended Settings with BLT.
- Create an Settings object & call generate method.
```
<?php
/**
* IMPORTANT.
*
* Do not include additional settings here. Instead, add them to settings
* included by `blt.settings.php`. See BLT's documentation for more detail.
*
* @link https://docs.acquia.com/blt/
* @file
* Include DRS settings.
*/
```
- Require Acquia Drupal Recommended Settings plugin using
```
composer require acquia/drupal-recommended-settings
```

- Update BLT references from below settings files with Recommended Settings:
- default.local.settings.php
- local.settings.php update use statement from
``use Acquia\Blt\Robo\Common\EnvironmentDetector;`` to
``use Acquia\Drupal\RecommendedSettings\Helpers\EnvironmentDetector;``
use Acquia\Drupal\RecommendedSettings\Settings;
// Create settings object.
$settings = new Settings(DRUPAL_ROOT, 'site-uri');
// Database details.
$dbSpec = [
'drupal' => [
'db' => [
// Database name.
'database' => 'drupal',
// Mysql database login username.
'username' => 'drupal',
// Mysql database login password.
'password' => 'drupal',
// Mysql host.
'host' => 'localhost',
// Mysql port.
'port' => '3306',
],
],
];
// Call generate method with database details.
$settings->generate($dbSpec);
```

# License

Copyright (C) 2023 Acquia, Inc.

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation.
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License version 2 as published by the
Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
6 changes: 2 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@
"acquia/coding-standards": "^1.0",
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.2",
"ergebnis/composer-normalize": "~2.21.0",
"phpro/grumphp-shim": "^1.5"
},
"conflict": {
"acquia/blt": "*"
"phpro/grumphp-shim": "^1.5",
"phpunit/phpunit": "^9 || ^10"
},
"minimum-stability": "dev",
"prefer-stable": true,
Expand Down
2 changes: 1 addition & 1 deletion config/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ drupal:
username: drupal
password: drupal
host: localhost
port: 3306
port: 3306
4 changes: 3 additions & 1 deletion settings/acquia-recommended.settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

/**
* @file
* Setup Acquia Drupal Recommended Settings utility variables, include required files.
* Setup Acquia Drupal Recommended Settings utility variables.
*
* Includes required settings files.
*/

use Acquia\Drupal\RecommendedSettings\Exceptions\SettingsException;
Expand Down
7 changes: 5 additions & 2 deletions settings/global/default.global.settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

/**
* @file
* Generated by Acquia Drupal Recommended Settings. Serves as an example of global includes.
* Generated by Acquia Drupal Recommended Settings.
*
* Serves as an example of global includes.
*/

/**
Expand All @@ -14,7 +16,8 @@
/**
* Include settings files in docroot/sites/settings.
*
* If instead you want to add settings to a specific site, see Acquia Drupal Recommended Settings includes
* If instead you want to add settings to a specific site, see
* Acquia Drupal Recommended Settings includes
* file in docroot/sites/{site-name}/settings/default.includes.settings.php.
*/
$additionalSettingsFiles = [
Expand Down
2 changes: 2 additions & 0 deletions src/Helpers/EnvironmentDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,9 @@ public static function getRepoRoot(): string {
// This is a web or Drush request.
return dirname(DRUPAL_ROOT);
}
// phpcs:ignore
global $repo_root;
// phpcs:enable
return $repo_root;
}

Expand Down
1 change: 0 additions & 1 deletion src/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ public function generate(array $overrideData = []): void {

// The config directory for given site must exists, otherwise Drupal will
// add database credentials to settings.php.
// @todo Using hardcoded sync directory path. Need to better optimise it.
$this->fileSystem->ensureDirectoryExists($this->drupalRoot . "/../config/$site");
}

Expand Down
19 changes: 19 additions & 0 deletions tests/ci/script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash

# NAME
# script.sh - Run ORCA tests.
#
# SYNOPSIS
# script.sh
#
# DESCRIPTION
# Runs static code analysis and automated tests.

cd "$(dirname "$0")"

# Reuse ORCA's own includes.
source ../../../orca/bin/travis/_includes.sh
cd /home/runner/work/drupal-recommended-settings/drupal-recommended-settings
composer install
./vendor/bin/phpunit tests
cd -
106 changes: 106 additions & 0 deletions tests/unit/SettingsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

namespace Acquia\Drupal\RecommendedSettings\Tests;

use Acquia\Drupal\RecommendedSettings\Helpers\Filesystem as DrsFilesystem;
use Acquia\Drupal\RecommendedSettings\Settings;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Filesystem;

class SettingsTest extends TestCase {

/**
* The recommended settings object.
*
* @var string
*/
protected $settings;

/**
* The path to drupal webroot directory.
*
* @var string
*/
protected $drupalRoot;

/**
* The symfony file-system object.
*
* @var \Symfony\Component\Filesystem\Filesystem
*/
protected Filesystem $fileSystem;

/**
* The symfony file-system object.
*
* @var \Acquia\Drupal\RecommendedSettings\Helpers\Filesystem
*/
protected DrsFilesystem $drsFileSystem;

/**
* Set up test environmemt.
*/
public function setUp(): void {
$this->drupalRoot = dirname(__FILE__);
$docroot = $this->drupalRoot . '/docroot';
$this->drsFileSystem = new DrsFilesystem();
$this->drsFileSystem->ensureDirectoryExists($docroot . '/sites/default');
$this->fileSystem = new Filesystem();
$this->fileSystem->touch($docroot . '/sites/default/default.settings.php');
$this->settings = new Settings($docroot, "default");
$this->settings->generate([
'drupal' => [
'db' => [
'database' => 'drs',
'username' => 'drupal',
'password' => 'drupal',
'host' => 'localhost',
'port' => '3306',
],
],
]);
}

/**
* Test that the file is created.
*/
public function testFileIsCreated() {
// Assert that settings/default.global.settings.php file exist.
$this->assertTrue($this->fileSystem->exists($this->drupalRoot . '/docroot/sites/settings/default.global.settings.php'));
// Assert that settings.php file exist.
$this->assertTrue($this->fileSystem->exists($this->drupalRoot . '/docroot/sites/default/settings.php'));
// Assert that settings.php file has content.
$content = '
require DRUPAL_ROOT . "/../vendor/acquia/drupal-recommended-settings/settings/acquia-recommended.settings.php";
/**
* IMPORTANT.
*
* Do not include additional settings here. Instead, add them to settings
* included by `acquia-recommended.settings.php`. See Acquia\'s documentation for more detail.
*
* @link https://docs.acquia.com/
*/
';
$this->assertEquals($content, file_get_contents($this->drupalRoot . '/docroot/sites/default/settings.php'));
// Assert that default.includes.settings.php file exist.
$this->assertTrue($this->fileSystem->exists($this->drupalRoot . '/docroot/sites/default/settings/default.includes.settings.php'));
// Assert that default.local.settings.php file exist.
$this->assertTrue($this->fileSystem->exists($this->drupalRoot . '/docroot/sites/default/settings/default.local.settings.php'));
// Assert that local.settings.php file exist.
$this->assertTrue($this->fileSystem->exists($this->drupalRoot . '/docroot/sites/default/settings/local.settings.php'));
// Get the local.settings.php file content.
$localSettings = file_get_contents($this->drupalRoot . '/docroot/sites/default/settings/local.settings.php');
// Verify database credentials.
$this->assertStringContainsString("db_name = 'drs'", $localSettings, "The local.settings.php doesn't contains the 'drs' database.");
$this->assertStringContainsString("'username' => 'drupal'", $localSettings, "The local.settings.php doesn't contains the 'drupal' username.");
$this->assertStringContainsString("'password' => 'drupal'", $localSettings, "The local.settings.php doesn't contains the 'drupal' password.");
$this->assertStringContainsString("'host' => 'localhost'", $localSettings, "The local.settings.php doesn't contains the 'localhost' host.");
$this->assertStringContainsString("'port' => '3306'", $localSettings, "The local.settings.php doesn't contains the '3306' port.");
}

public function tearDown(): void {
$this->fileSystem->remove($this->drupalRoot . '/docroot');
$this->fileSystem->remove($this->drupalRoot . '/config');
}

}

0 comments on commit 6c55be0

Please sign in to comment.