Skip to content

Commit

Permalink
ACMS-3461: Test case for settings class.
Browse files Browse the repository at this point in the history
  • Loading branch information
apathak18 committed Feb 8, 2024
1 parent acc368c commit 5115909
Showing 1 changed file with 110 additions and 34 deletions.
144 changes: 110 additions & 34 deletions tests/src/unit/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Acquia\Drupal\RecommendedSettings\Tests\unit;

use Acquia\Drupal\RecommendedSettings\Config\ConfigInitializer;
use Acquia\Drupal\RecommendedSettings\Helpers\Filesystem as DrsFilesystem;
use Acquia\Drupal\RecommendedSettings\Settings;
use PHPUnit\Framework\TestCase;
Expand All @@ -25,10 +26,15 @@ class SettingsTest extends TestCase {
protected Filesystem $fileSystem;

/**
* The symfony file-system object.
* The DRS file-system object.
*/
protected DrsFilesystem $drsFileSystem;

/**
* The drupal recommended settings config intializer.
*/
protected ConfigInitializer $configInitializer;

/**
* Set up test environmemt.
*/
Expand All @@ -40,56 +46,126 @@ public function setUp(): void {
$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',
],
],
]);
$this->configInitializer = new ConfigInitializer();

}

/**
* Test that the file is created.
* Test Generate/Copy settings files.
*
* @param array<array|string> $override_data
* Database config data.
* @param string $site_uri
* The site uri.
*
* @dataProvider overrideDataProvider
*/
public function testFileIsCreated(): void {
// Assert that settings/default.global.settings.php file exist.
$this->assertTrue($this->fileSystem->exists($this->drupalRoot . '/docroot/sites/settings/default.global.settings.php'));
public function testGenerate(array $override_data = [], string $site_uri = 'default'): void {
$this->testCopySiteSettings($site_uri);

// Create settings.php file from default.settings.php.
$this->drsFileSystem->copyFile(
$this->drupalRoot . "/sites/default/default.settings.php",
$this->drupalRoot . "/sites/$site_uri/settings.php"
);
// Assert that settings.php file exist.
$this->assertTrue($this->fileSystem->exists($this->drupalRoot . '/docroot/sites/default/settings.php'));
$this->assertTrue($this->fileSystem->exists("$this->drupalRoot/docroot/sites/$site_uri/settings.php"));

// Create local.settings.php file from default.local.settings.php.
$this->drsFileSystem->copyFile(
$this->drupalRoot . "/sites/$site_uri/settings/default.local.settings.php",
$this->drupalRoot . "/sites/$site_uri/settings/local.settings.php"
);
// Assert that local.settings.php file exist.
$this->assertTrue($this->fileSystem->exists("$this->drupalRoot/docroot/sites/$site_uri/settings/local.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/
*/
';
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'));

// Replace variables in local.settings.php file.
$config = $this->configInitializer->loadAllConfig();
if ($override_data) {
$config->addConfig($override_data);
}
$settings = new SettingsConfig($config->processConfig()->export());
$settings->replaceFileVariables($this->drupalRoot . "/sites/$site_uri/settings/local.settings.php");
$this->assertTrue($this->drsFileSystem->ensureDirectoryExists("$this->drupalRoot/docroot/sites/$site_uri"));
// 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("db_name = $site_uri", $settings, "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.");
}

/**
* Test copies the default site specific setting files.
*/
protected function testCopySiteSettings(string $site_uri): void {
$this->drsFileSystem->copyFiles(
$this->drsFileSystem::getPluginPath() . "/settings/site",
$this->drupalRoot . "/sites/" . $site_uri . "/settings"
);

// Assert that default.includes.settings.php file exist.
$this->assertTrue($this->fileSystem->exists("$this->drupalRoot/docroot/sites/$site_uri/settings/default.includes.settings.php"));
// Assert that default.local.settings.php file exist.
$this->assertTrue($this->fileSystem->exists("$this->drupalRoot/docroot/sites/$site_uri/settings/default.local.settings.php"));
}

/**
* Data provider for ::testGenerate().
*
* @return array<array|string>
* Returns an array of settings file.
*/
public function overrideDataProvider(): array {
return [
[
[
'drupal' => [
'db' => [
'database' => 'site1',
'username' => 'drupal',
'password' => 'drupal',
'host' => 'localhost',
'port' => '3306',
],
],
],
'site1',
],
[
[
'drupal' => [
'db' => [
'database' => 'site2',
'username' => 'drupal',
'password' => 'drupal',
'host' => 'localhost',
'port' => '3306',
],
],
],
'site2',
],

];
}

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

0 comments on commit 5115909

Please sign in to comment.