Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 775
Thomas Avatar asked Jul 06 '17 00:07

Thomas


1 Answers

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.

like image 88
John Huang Avatar answered Sep 29 '22 02:09

John Huang