From 51159095be00376def593cf4b51869ac3c258790 Mon Sep 17 00:00:00 2001 From: Ankit Pathak Date: Thu, 8 Feb 2024 12:09:22 +0530 Subject: [PATCH] ACMS-3461: Test case for settings class. --- tests/src/unit/SettingsTest.php | 144 ++++++++++++++++++++++++-------- 1 file changed, 110 insertions(+), 34 deletions(-) diff --git a/tests/src/unit/SettingsTest.php b/tests/src/unit/SettingsTest.php index b3bc61c..27ce21b 100644 --- a/tests/src/unit/SettingsTest.php +++ b/tests/src/unit/SettingsTest.php @@ -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; @@ -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. */ @@ -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 $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 + * 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');