How do you import config sync files in a Drupal 8 functional test?
Asked Answered
A

2

7

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?

Afterimage answered 6/7, 2017 at 0:59 Comment(1)
Hi Thomas, did you figure this out?Theosophy
L
6

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 module may helps you create the testing profile. There is a related issue #2788777 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

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

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

Leiker answered 17/8, 2017 at 2:52 Comment(5)
How did you use ::configImporter()? $this->configImporter()->import(); gave the error message "Drupal\Core\Config\ConfigImporterException: There were errors validating the config synchronization. This import is empty and if applied would delete all of your configuration, so has been rejected."Vermiculation
@LiamMorland The source config is empty because it's set up to pull from the "config sync store" (the filesystem), which in functional tests point to a subfolder within the temporary site created for every test, unless you have already exported the active config there. Many test-specific modules store config in their own /config/install folder for automatic import when installed, or a /config/sync folder to be pulled in during tests by first copying them into the temp site. See FieldIMportCreateTest for an example.Phillisphilly
@Phillisphilly Thanks for your answer. Do I understand correctly that it will pull from /config/install in the same module as the test? Is there a way to set the directory it pulls from?Vermiculation
@LiamMorland Yes, and no. The specific test I mentioned indirectly pulls the config in by simply installing the module, \Drupal::service('module_installer')->install(['field_test_config']);. which ends up calling \Drupal::service('config.installer')->installDefaultConfig('module', $module);, so that is not configurable. But you can of course replicate what that last call does to install config from anywhere. In some cases at work we have site-specific tests pull a lot of config from config/sync instead. However, note that the more you put in, the longer it usually takes to run the test.Phillisphilly
@Phillisphilly Installing from config/sync is what I want to do. We're using \Drupal::service('config.installer')->installOptionalConfig($config_source);, but that only brings in some config; see my comment to another answer on this page.Vermiculation
A
3

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.

Astrict answered 10/9, 2019 at 11:43 Comment(2)
This requires at the top of the file: use Drupal\Core\Config\FileStorage;Vermiculation
This is working for things like content types. But config of registration_role module (and perhaps other contrib modules as well) is not coming in despite being in the config sync directory.Vermiculation

© 2022 - 2024 — McMap. All rights reserved.