Completed
Pull Request — master (#16)
by Sergii
03:45
created

DrupalKernelPlaceholder   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 229
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 25
lcom 0
cbo 2
dl 0
loc 229
rs 10
c 0
b 0
f 0

19 Methods

Rating   Name   Duplication   Size   Complexity  
A beforeFeature() 0 6 1
A t() 0 4 1
A arg() 0 4 1
A formatString() 0 4 1
A tokenReplace() 0 4 1
A sitePath() 0 4 1
A jsonEncode() 0 4 1
A setCurrentUser() 0 4 1
A getUidByName() 0 6 1
A deleteUser() 0 4 1
A selectQuery() 0 4 1
A getFieldDefinitions() 0 13 2
A entityLoad() 0 4 1
A entityHasField() 0 4 1
A entityFieldValue() 0 4 1
B switchMailSystem() 0 24 3
A getEmailMessages() 0 10 2
A getContentTypeName() 0 10 3
B injectCustomJavascript() 0 25 1
1
<?php
2
/**
3
 * @author Sergii Bondarenko, <[email protected]>
4
 */
5
6
use Behat\Behat\Hook\Scope\BeforeFeatureScope;
7
use Drupal\TqExtension\Utils\Database\FetchField;
8
9
// @codingStandardsIgnoreStart
10
final class DrupalKernelPlaceholder extends DrupalKernelPlaceholderBase
11
  // @codingStandardsIgnoreEnd
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16
    public static function beforeFeature(BeforeFeatureScope $scope)
17
    {
18
        // Set to "false", because the administration menu will not be rendered.
19
        // @see https://www.drupal.org/node/2023625#comment-8607207
20
        variable_set('admin_menu_cache_client', false);
21
    }
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public static function t($string, array $args = [], array $options = [])
27
    {
28
        return t($string, $args, $options);
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public static function arg()
35
    {
36
        return arg();
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public static function formatString($string, array $args = [])
43
    {
44
        return format_string($string, $args);
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public static function tokenReplace($text, array $data = [], array $options = [])
51
    {
52
        return token_replace($text, $data, $options);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public static function sitePath()
59
    {
60
        return conf_path();
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public static function jsonEncode($data)
67
    {
68
        return drupal_json_encode($data);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     *
74
     * @param \stdClass $user
75
     */
76
    public static function setCurrentUser($user)
1 ignored issue
show
Coding Style introduced by
setCurrentUser uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
77
    {
78
        $GLOBALS['user'] = $user;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public static function getUidByName($username)
85
    {
86
        return (int) (new FetchField('users', 'uid'))
87
            ->condition('name', $username)
88
            ->execute();
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public static function deleteUser($user_id)
95
    {
96
        user_delete($user_id);
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     *
102
     * @return \SelectQuery
103
     */
104
    public static function selectQuery($table, $alias = null, array $options = [])
105
    {
106
        return db_select($table, $alias, $options);
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public static function getFieldDefinitions($entityType, $bundle)
113
    {
114
        $definitions = [];
115
116
        foreach (field_info_instances($entityType, $bundle) as $name => $definition) {
117
            $definitions[$name] = [
118
                'label' => $definition['label'],
119
                'required' => $definition['required'],
120
            ];
121
        }
122
123
        return $definitions;
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public static function entityLoad($entityType, $id)
130
    {
131
        return entity_metadata_wrapper($entityType, $id);
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     *
137
     * @param \EntityDrupalWrapper $entity
138
     */
139
    public static function entityHasField($entity, $fieldName)
140
    {
141
        return isset($entity->{$fieldName});
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     *
147
     * @param \EntityDrupalWrapper $entity
148
     */
149
    public static function entityFieldValue($entity, $fieldName)
150
    {
151
        return $entity->{$fieldName}->value();
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157
    public static function switchMailSystem($useTesting)
158
    {
159
        static $original = [
160
            'default-system' => 'DefaultMailSystem',
161
        ];
162
163
        if ($useTesting) {
164
            // Store original mail system to restore it after scenario.
165
            $original = variable_get('mail_system', $original);
166
            // Set the mail system for testing. It will store an emails in
167
            // "drupal_test_email_collector" Drupal variable instead of sending.
168
            $value = array_merge($original, [
169
                'default-system' => 'TestingMailSystem',
170
            ]);
171
        } else {
172
            // Bring back the original mail system.
173
            $value = $original;
174
            // Flush the email buffer to be able to reuse it from scratch.
175
            // @see \TestingMailSystem
176
            variable_set('drupal_test_email_collector', []);
177
        }
178
179
        variable_set('mail_system', $value);
180
    }
181
182
    /**
183
     * {@inheritdoc}
184
     */
185
    public static function getEmailMessages()
186
    {
187
        // We can't use variable_get() because Behat has another bootstrapped
188
        // variable $conf that is not updated from cURL bootstrapped Drupal instance.
189
        $result = (new FetchField('variable', 'value'))
190
            ->condition('name', 'drupal_test_email_collector')
191
            ->execute();
192
193
        return empty($result) ? [] : unserialize($result);
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199
    public static function getContentTypeName($contentType)
200
    {
201
        if (isset(node_type_get_types()[$contentType])) {
202
            return $contentType;
203
        }
204
205
        return (string) (new FetchField('node_type', 'type'))
206
            ->condition('name', $contentType)
207
            ->execute();
208
    }
209
210
    /**
211
     * {@inheritdoc}
212
     */
213
    public static function injectCustomJavascript($file, $delete = false)
214
    {
215
        $file .= '.js';
216
        $modulePath = static::drupalGetFilename('module', 'system');
217
        $destination = dirname($modulePath) . '/' . $file;
218
        $injection = "\ndrupal_add_js('$destination', array('every_page' => TRUE));";
219
220
        if ($delete) {
221
            static::fileUnmanagedDelete($destination);
222
223
            $search = $injection;
224
            $replace = '';
225
        } else {
226
            static::fileUnmanagedCopy(
227
                str_replace('Context', 'JavaScript', __DIR__) . '/' . $file,
228
                $destination,
229
                FILE_EXISTS_REPLACE
230
            );
231
232
            $search = 'system_add_module_assets();';
233
            $replace = $search . $injection;
234
        }
235
236
        file_put_contents($modulePath, str_replace($search, $replace, file_get_contents($modulePath)));
237
    }
238
}
239