0Day Forums
How do you import config sync files in a Drupal 8 functional test? - Printable Version

+- 0Day Forums (https://0day.red)
+-- Forum: Coding (https://0day.red/Forum-Coding)
+--- Forum: CMS (https://0day.red/Forum-CMS)
+---- Forum: Drupal (https://0day.red/Forum-Drupal)
+---- Thread: How do you import config sync files in a Drupal 8 functional test? (/Thread-How-do-you-import-config-sync-files-in-a-Drupal-8-functional-test)



How do you import config sync files in a Drupal 8 functional test? - hibbitts444 - 07-27-2023

I would like to know how to import config sync files in my functional tests for modules I am testing. For instance, I have some custom content types I would like to test against, and there are a number of files in config/sync that pertain to the node modules that defines the custom content type.

class ArticleControllerTest extends BrowserTestBase {
protected static $modules = ['node', 'dist_source'];
}

At the top of my test I define the modules which do succesfully import, but it doesn't include the config sync settings so none of my custom content types are present. How can I import these into my test environment?


RE: How do you import config sync files in a Drupal 8 functional test? - pulpalgia520390 - 07-27-2023

At the beginning of testing for Drupal 8, I had the same question. After reading some documents and tutorials, I tried and know several methods:

### `$this->configImporter()` helps import the configurations from sync to active

`$this->configImporter()` exits in `Drupal\Tests\ConfigTestTrait`. And the trait has been used in some based test classes, like `BrowserTestBase`.

However, the method doesn't work for me. Because, I used thunder installation profile. The default content exists after the profile installation was completed. Once the `$this->configImporter()` starts to import sync configurations, it encounters errors that some entity types fail to be updated, because the entities already exists.

### Create a testing profile

*(Haven't tried)*

If the Drupal site installed by standard profile, you may try to put the sync configurations into a testing profile. And [Install Profile Generator][1] module may helps you create the testing profile. There is a related issue [#2788777][2] with config and profile

### Put the configurations which depend on module into `config/install` or `config/optional`

*(Work for me)*

Contributed modules always put the config into `config/install` and `config/optional`. Once the module is installed, the configurations will also write into database or active storage. [Documentation - Include default configuration in your Drupal 8 module][3]

When developing configurations in module, [Configuration development][4] helps export the config into `config/install` of the developed module.

If anyone has the same experience, look forward to share with us.

[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

[4]:

[To see links please register here]




RE: How do you import config sync files in a Drupal 8 functional test? - inerasable276 - 07-27-2023

I do it in my testing base class (`extends BrowserTestBase`) in `setUp()` like this:

// import config from sync
$config_path = '/home/rainer/src/asdent/config/sync';
$config_source = new FileStorage($config_path);
\Drupal::service('config.installer')->installOptionalConfig($config_source);

works great.

It's just like a `drush cim sync`

And provides the production config to my end-2-end automated phpunit tests in gitlab CI.