Completed
Push — Leksat-patch-1 ( 31c1c0...66f278 )
by Jonathan
01:23
created

ConfigContext::getTranslationResources()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * @file
5
 * Contains \Drupal\DrupalExtension\Context\ConfigContext.
6
 */
7
8
namespace Drupal\DrupalExtension\Context;
9
10
use Behat\Behat\Context\TranslatableContext;
11
use Behat\Gherkin\Node\TableNode;
12
13
/**
14
 * Provides pre-built step definitions for interacting with Drupal config.
15
 */
16
class ConfigContext extends RawDrupalContext implements TranslatableContext {
17
18
  /**
19
   * {@inheritDoc}
20
   */
21
  public static function getTranslationResources() {
22
    return glob(__DIR__ . '/../../../../i18n/*.xliff');
23
  }
24
25
  /**
26
   * Keep track of any config that was changed so they can easily be reverted.
27
   *
28
   * @var array
29
   */
30
  protected $config = array();
31
32
  /**
33
   * Revert any changed config.
34
   *
35
   * @AfterScenario
36
   */
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);
42
      }
43
    }
44
    $this->config = array();
45
  }
46
47
  /**
48
   * Sets basic configuration item.
49
   *
50
   * @param string $name
51
   *   The name of the configuration object.
52
   * @param string $key
53
   *   Identifier to store value in configuration.
54
   * @param mixed $value
55
   *   Value to associate with identifier.
56
   *
57
   * @Given I set the configuration item :name with key :key to :value
58
   */
59
  public function setBasicConfig($name, $key, $value) {
60
    $this->setConfig($name, $key, $value);
61
  }
62
63
  /**
64
   * Sets complex configuration.
65
   *
66
   * @param string $name
67
   *   The name of the configuration object.
68
   * @param string $key
69
   *   Identifier to store value in configuration.
70
   * @param TableNode $config_table
71
   *   The table listing configuration keys and values.
72
   *
73
   * @Given I set the configuration item :name with key :key with values:
74
   *
75
   * Provide configuration data in the following format:
76
   *  | key   | value  |
77
   *  | foo   | bar    |
78
   */
79
  public function setComplexConfig($name, $key, TableNode $config_table) {
80
    $value = array();
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);
85
      }
86
      $value[$row['key']] = $row['value'];
87
    }
88
    $this->setConfig($name, $key, $value);
89
  }
90
91
  /**
92
   * Sets a value in a configuration object.
93
   *
94
   * @param string $name
95
   *   The name of the configuration object.
96
   * @param string $key
97
   *   Identifier to store value in configuration.
98
   * @param mixed $value
99
   *   Value to associate with identifier.
100
   */
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;
105
  }
106
107
}