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

Cleanup project config setup in tests #4804

Merged
1 change: 1 addition & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Added `craft\helpers\UrlHelper::buildQuery()`.

### Changed
- `craft\test\TestSetup::setupCraftDb()` no longer accepts a second argument. Ensure that `craft\test\Craft::$testConfig` is set before calling this function. ([#4804](https://github.com/craftcms/cms/pull/4804))
- Element arrays no longer include `hasDescendants` or `totalDescendants` keys by default.
- Relational fields without a specific target site will now only return related elements from the same site as the source element by default, as they did before Craft 3.2. ([#4751](https://github.com/craftcms/cms/issues/4751))
- Improved the performance of element duplication on multi-site installs.
Expand Down
2 changes: 1 addition & 1 deletion codeception.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ groups:
test: [tests/unit/test]
validators: [tests/unit/validators]
web: [tests/unit/web]
app: [tests/unit/AppTest.php]
app: [tests/unit/AppTest.php]
74 changes: 43 additions & 31 deletions src/test/Craft.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,11 @@ class Craft extends Yii2
// =========================================================================

/**
* A static version of the config for use on the tests/_craft/config/test.php file
* A static version of the testing config.
*
* Will be set very early on in the testing processes so it can be used in configuration files such as `general.php` and `test.php`.
* This variable is equivelant to calling $this->_getConfig(); but is available for public access.
*
* @var array
*/
public static $testConfig;
Expand All @@ -86,6 +89,7 @@ class Craft extends Yii2
* @var array
*/
protected $addedConfig = [
'migrations' => [],
'plugins' => [],
'setupDb' => null,
'projectConfig' => null,
Expand Down Expand Up @@ -167,7 +171,7 @@ public function _before(TestInterface $test)
}

// Re-apply project config
if ($projectConfig = $this->_getConfig('projectConfig')) {
if ($projectConfig = TestSetup::useProjectConfig()) {
// Tests just beginning. . Reset the project config to its original state.
TestSetup::setupProjectConfig($projectConfig['file']);

Expand Down Expand Up @@ -222,21 +226,26 @@ public function setupDb()

$dbSetupConfig = $this->_getConfig('dbSetup');

// Get rid of everything.
if (isset($dbSetupConfig['clean']) && $dbSetupConfig['clean'] === true) {
TestSetup::cleanseDb($dbConnection);
}

// Setup the project config from the passed file.
$projectConfig = $this->_getConfig('projectConfig');
if ($projectConfig && isset($projectConfig['file'])) {
// Just set it up.
if ($projectConfig = TestSetup::useProjectConfig()) {
// Fail hard if someone has specified a project config file but doesn't have project config enabled.
// Prevent's confusion of https://github.com/craftcms/cms/pulls/4711
if (!\Craft::$app->getConfig()->getGeneral()->useProjectConfigFile) {
throw new InvalidArgumentException('Please enable the `useProjectConfigFile` option in `general.php`');
}

TestSetup::setupProjectConfig($projectConfig['file']);
}

// Get rid of everything.
if (isset($dbSetupConfig['clean']) && $dbSetupConfig['clean'] === true) {
TestSetup::cleanseDb($dbConnection);
}

// Install the db from install.php
if (isset($dbSetupConfig['setupCraft']) && $dbSetupConfig['setupCraft'] === true) {
TestSetup::setupCraftDb($dbConnection, $this);
TestSetup::setupCraftDb($dbConnection);
}

// Ready to rock.
Expand All @@ -250,8 +259,10 @@ public function setupDb()
}

// Add any plugins
foreach ($this->_getConfig('plugins') as $plugin) {
$this->installPlugin($plugin);
if ($plugins = $this->_getConfig('plugins')) {
foreach ($plugins as $plugin) {
$this->installPlugin($plugin);
}
}

// Trigger the end of a 'request'. This lets project config do its stuff.
Expand Down Expand Up @@ -300,6 +311,26 @@ public static function normalizePathSeparators($path)
return is_string($path) ? str_replace("\\", '/', $path) : false;
}


/**
* Creates a DB config according to the loaded .env variables.
*
* @return DbConfig
*/
public static function createDbConfig(): DbConfig
{
return new DbConfig([
'password' => getenv('DB_PASSWORD'),
'user' => getenv('DB_USER'),
'database' => getenv('DB_DATABASE'),
'tablePrefix' => getenv('DB_TABLE_PREFIX'),
'driver' => getenv('DB_DRIVER'),
'port' => getenv('DB_PORT'),
'schema' => getenv('DB_SCHEMA'),
'server' => getenv('DB_SERVER'),
]);
}

// Helpers for test methods
// =========================================================================

Expand Down Expand Up @@ -530,25 +561,6 @@ public function createEventItems(array $config = []): array
return $items;
}

/**
* Creates a DB config according to the loaded .env variables.
*
* @return DbConfig
*/
public static function createDbConfig(): DbConfig
{
return new DbConfig([
'password' => getenv('DB_PASSWORD'),
'user' => getenv('DB_USER'),
'database' => getenv('DB_DATABASE'),
'tablePrefix' => getenv('DB_TABLE_PREFIX'),
'driver' => getenv('DB_DRIVER'),
'port' => getenv('DB_PORT'),
'schema' => getenv('DB_SCHEMA'),
'server' => getenv('DB_SERVER'),
]);
}

// Protected Methods
// =========================================================================

Expand Down
35 changes: 27 additions & 8 deletions src/test/TestSetup.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,18 +336,39 @@ public static function setupProjectConfig(string $projectConfigFile)

$testSuiteProjectConfigPath = CRAFT_CONFIG_PATH . '/project.yaml';
$contents = file_get_contents($projectConfigFile);
$arrayContents = Yaml::parse($contents);

// Write to the file.
FileHelper::writeToFile($testSuiteProjectConfigPath, Yaml::dump($arrayContents));
FileHelper::writeToFile($testSuiteProjectConfigPath, $contents);
}

/**
* Whether project config should be used in tests.
*
* Returns the projectConfig configuration array if yes - `false` if not.
*
* @return array|false
*/
public static function useProjectConfig()
{
if (!Craft::$app) {
return false;
}

$projectConfig = \craft\test\Craft::$testConfig['projectConfig'] ?? [];
$useProjectConfig = Craft::$app->getConfig()->getGeneral();

if ($projectConfig && isset($projectConfig['file']) && $useProjectConfig) {
return $projectConfig;
}

return false;
}

/**
* @param Connection $connection
* @param CraftTest $craftTestModule
* @throws Exception
*/
public static function setupCraftDb(Connection $connection, CraftTest $craftTestModule)
public static function setupCraftDb(Connection $connection)
{
if ($connection->schema->getTableNames() !== []) {
throw new Exception('Not allowed to setup the DB if it has not been cleansed');
Expand All @@ -362,10 +383,8 @@ public static function setupCraftDb(Connection $connection, CraftTest $craftTest
'primary' => true,
];

// Replace the default site with what is desired by the project config (Currently). If project config is enabled.
$projectConfig = $craftTestModule->_getConfig('projectConfig');

if ($projectConfig && isset($projectConfig['file'])) {
// Replace the default site with what is desired by the project config. If project config is enabled.
if ($projectConfig = self::useProjectConfig()) {
$existingProjectConfig = Yaml::parse(
file_get_contents($projectConfig['file']) ?: ''
);
Expand Down