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.