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?

Thomas
  • 71
  • 4

2 Answers2

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.

John Huang
  • 1,298
  • 10
  • 14
  • 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." – Liam Morland May 11 '23 at 16:40
  • @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. – TwoD May 30 '23 at 14:07
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.

Rainer Feike
  • 191
  • 1
  • 9
  • This requires at the top of the file: `use Drupal\Core\Config\FileStorage;` – Liam Morland Jan 31 '23 at 18:04
  • 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. – Liam Morland May 11 '23 at 18:29