5 * Contains \Drupal\DrupalExtension\Context\ConfigContext.
8 namespace Drupal\DrupalExtension\Context;
10 use Behat\Behat\Context\TranslatableContext;
11 use Behat\Gherkin\Node\TableNode;
14 * Provides pre-built step definitions for interacting with Drupal config.
16 class ConfigContext extends RawDrupalContext implements TranslatableContext {
21 public static function getTranslationResources() {
22 return glob(__DIR__ . '/../../../../i18n/*.xliff');
26 * Keep track of any config that was changed so they can easily be reverted.
30 protected $config = array();
33 * Revert any changed config.
37 public function cleanConfig() {
38 // Revert config that was changed.
39 foreach ($this->config as $name => $key_value) {
40 foreach ($key_value as $key => $value) {
41 $this->getDriver()->configSet($name, $key, $value);
44 $this->config = array();
48 * Sets basic configuration item.
51 * The name of the configuration object.
53 * Identifier to store value in configuration.
55 * Value to associate with identifier.
57 * @Given I set the configuration item :name with key :key to :value
59 public function setBasicConfig($name, $key, $value) {
60 $this->setConfig($name, $key, $value);
64 * Sets complex configuration.
67 * The name of the configuration object.
69 * Identifier to store value in configuration.
70 * @param TableNode $config_table
71 * The table listing configuration keys and values.
73 * @Given I set the configuration item :name with key :key with values:
75 * Provide configuration data in the following format:
79 public function setComplexConfig($name, $key, TableNode $config_table) {
81 foreach ($config_table->getHash() as $row) {
82 // Allow json values for extra complexity.
83 if (json_decode($row['value'])) {
84 $row['value'] = json_decode($row['value'], TRUE);
86 $value[$row['key']] = $row['value'];
88 $this->setConfig($name, $key, $value);
92 * Sets a value in a configuration object.
95 * The name of the configuration object.
97 * Identifier to store value in configuration.
99 * Value to associate with identifier.
101 public function setConfig($name, $key, $value) {
102 $backup = $this->getDriver()->configGet($name, $key);
103 $this->getDriver()->configSet($name, $key, $value);
104 $this->config[$name][$key] = $backup;