Completed
Pull Request — master (#16)
by Sergii
04:34
created

Drupal7Placeholder::getDatabaseConnectionInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * @author Sergii Bondarenko, <[email protected]>
4
 */
5
namespace Drupal\TqExtension\Cores;
6
7
use Behat\Behat\Hook\Scope\BeforeFeatureScope;
8
use Drupal\TqExtension\Utils\Database\FetchField;
9
10
final class Drupal7Placeholder extends DrupalKernelPlaceholder
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15
    public static function beforeFeature(BeforeFeatureScope $scope)
16
    {
17
        // Set to "false", because the administration menu will not be rendered.
18
        // @see https://www.drupal.org/node/2023625#comment-8607207
19
        variable_set('admin_menu_cache_client', false);
20
    }
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public static function t($string, array $arguments = [], array $options = [])
26
    {
27
        return t($string, $arguments, $options);
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public static function formatString($string, array $arguments = [])
34
    {
35
        return format_string($string, $arguments);
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public static function arg()
42
    {
43
        return arg();
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public static function tokenReplace($text, array $data = [], array $options = [])
50
    {
51
        return token_replace($text, $data, $options);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public static function sitePath()
58
    {
59
        return conf_path();
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public static function jsonEncode($data)
66
    {
67
        return drupal_json_encode($data);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     *
73
     * @return \stdClass
74
     */
75
    public static function getCurrentUser()
76
    {
77
        return $GLOBALS['user'];
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     *
83
     * @param \stdClass $user
84
     */
85
    public static function setCurrentUser($user)
86
    {
87
        $GLOBALS['user'] = $user;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public static function setCurrentPath($path)
94
    {
95
        $_GET['q'] = $path;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public static function getUidByName($username)
102
    {
103
        return (int) (new FetchField('users', 'uid'))
104
            ->condition('name', $username)
105
            ->execute();
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public static function deleteUser($user_id)
112
    {
113
        user_delete($user_id);
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     *
119
     * @return \SelectQuery
120
     */
121
    public static function selectQuery($table, $alias = null, array $options = [])
122
    {
123
        return db_select($table, $alias, $options);
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public static function getFieldDefinitions($entityType, $bundle)
130
    {
131
        // Load entity properties because in Drupal 8 they are assumed as regular fields.
132
        $entityProperties = entity_get_property_info($entityType);
133
        $fieldInstances = field_info_instances($entityType, $bundle);
134
        $definitions = [];
135
136
        if (!empty($entityProperties['properties'])) {
137
            $fieldInstances += $entityProperties['properties'];
138
        }
139
140
        foreach ($fieldInstances as $name => $definition) {
141
            $definitions[$name] = [
142
                'label' => $definition['label'],
143
                'required' => !empty($definition['required']),
144
            ];
145
        }
146
147
        return $definitions;
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153
    public static function getDatabaseConnectionInfo($connection)
154
    {
155
        return \Database::getConnectionInfo($connection);
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161
    public static function entityCreate($entityType, array $values)
162
    {
163
        $entity = entity_create($entityType, $values);
164
165
        entity_save($entityType, $entity);
166
        list($entityId, , $bundle) = entity_extract_ids($entityType, $entity);
167
168
        return [$entityId, $entityType, $bundle];
169
    }
170
171
    /**
172
     * {@inheritdoc}
173
     */
174
    public static function entityLoad($entityType, $id)
175
    {
176
        $entities = entity_load($entityType, [$id]);
177
178
        return empty($entities) ? null : reset($entities);
179
    }
180
181
    /**
182
     * {@inheritdoc}
183
     */
184
    public static function entityHasField($entity, $fieldName)
185
    {
186
        return isset($entity->{$fieldName});
187
    }
188
189
    /**
190
     * {@inheritdoc}
191
     *
192
     * @param \EntityDrupalWrapper $entity
193
     */
194
    public static function entityFieldValue($entity, $fieldName)
195
    {
196
        if ($entity instanceof \EntityDrupalWrapper) {
197
            return $entity->{$fieldName}->value();
198
        }
199
200
        throw new \InvalidArgumentException(sprintf(
201
            'First argument for "%s" method must be of "%s" type.',
202
            __METHOD__,
203
            \EntityDrupalWrapper::class
204
        ));
205
    }
206
207
    /**
208
     * {@inheritdoc}
209
     */
210
    public static function switchMailSystem($useTesting)
211
    {
212
        static $original = [
213
            'default-system' => 'DefaultMailSystem',
214
        ];
215
216
        if ($useTesting) {
217
            // Store original mail system to restore it after scenario.
218
            $original = variable_get('mail_system', $original);
219
            // Set the mail system for testing. It will store an emails in
220
            // "drupal_test_email_collector" Drupal variable instead of sending.
221
            $value = array_merge($original, [
222
                'default-system' => 'TestingMailSystem',
223
            ]);
224
        } else {
225
            // Bring back the original mail system.
226
            $value = $original;
227
            // Flush the email buffer to be able to reuse it from scratch.
228
            // @see \TestingMailSystem
229
            variable_set('drupal_test_email_collector', []);
230
        }
231
232
        variable_set('mail_system', $value);
233
    }
234
235
    /**
236
     * {@inheritdoc}
237
     */
238
    public static function getEmailMessages()
239
    {
240
        // We can't use variable_get() because Behat has another bootstrapped
241
        // variable $conf that is not updated from cURL bootstrapped Drupal instance.
242
        $result = (new FetchField('variable', 'value'))
243
            ->condition('name', 'drupal_test_email_collector')
244
            ->execute();
245
246
        return empty($result) ? [] : unserialize($result);
247
    }
248
249
    /**
250
     * {@inheritdoc}
251
     */
252
    public static function getContentTypeName($contentType)
253
    {
254
        if (isset(node_type_get_types()[$contentType])) {
255
            return $contentType;
256
        }
257
258
        return (string) (new FetchField('node_type', 'type'))
259
            ->condition('name', $contentType)
260
            ->execute();
261
    }
262
263
    /**
264
     * {@inheritdoc}
265
     */
266
    public static function injectCustomJavascript($file, $delete = false)
267
    {
268
        $file .= '.js';
269
        $modulePath = static::drupalGetFilename('module', 'system');
270
        $destination = dirname($modulePath) . '/' . $file;
271
        $injection = "\ndrupal_add_js('$destination', array('every_page' => TRUE));";
272
273
        if ($delete) {
274
            static::fileUnmanagedDelete($destination);
275
276
            $search = $injection;
277
            $replace = '';
278
        } else {
279
            static::fileUnmanagedCopy(
280
                str_replace('Context', 'JavaScript', __DIR__) . '/' . $file,
281
                $destination,
282
                FILE_EXISTS_REPLACE
283
            );
284
285
            $search = 'system_add_module_assets();';
286
            $replace = $search . $injection;
287
        }
288
289
        file_put_contents($modulePath, str_replace($search, $replace, file_get_contents($modulePath)));
290
    }
291
}
292