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

Drupal7Placeholder::beforeFeature()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
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()
1 ignored issue
show
Coding Style introduced by
getCurrentUser 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...
76
    {
77
        return $GLOBALS['user'];
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     *
83
     * @param \stdClass $user
84
     */
85
    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...
86
    {
87
        $GLOBALS['user'] = $user;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public static function setCurrentPath($path)
1 ignored issue
show
Coding Style introduced by
setCurrentPath uses the super-global variable $_GET 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...
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
        $definitions = [];
132
133
        foreach (field_info_instances($entityType, $bundle) as $name => $definition) {
134
            $definitions[$name] = [
135
                'label' => $definition['label'],
136
                'required' => $definition['required'],
137
            ];
138
        }
139
140
        return $definitions;
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146
    public static function getDatabaseConnectionInfo($connection)
147
    {
148
        return \Database::getConnectionInfo($connection);
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154
    public static function entityLoad($entityType, $id)
155
    {
156
        $entities = entity_load($entityType, [$id]);
157
158
        return empty($entities) ? null : reset($entities);
159
    }
160
161
    /**
162
     * {@inheritdoc}
163
     */
164
    public static function entityHasField($entity, $fieldName)
165
    {
166
        return isset($entity->{$fieldName});
167
    }
168
169
    /**
170
     * {@inheritdoc}
171
     *
172
     * @param \EntityDrupalWrapper $entity
173
     */
174
    public static function entityFieldValue($entity, $fieldName)
175
    {
176
        return $entity->{$fieldName}->value();
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182
    public static function switchMailSystem($useTesting)
183
    {
184
        static $original = [
185
            'default-system' => 'DefaultMailSystem',
186
        ];
187
188
        if ($useTesting) {
189
            // Store original mail system to restore it after scenario.
190
            $original = variable_get('mail_system', $original);
191
            // Set the mail system for testing. It will store an emails in
192
            // "drupal_test_email_collector" Drupal variable instead of sending.
193
            $value = array_merge($original, [
194
                'default-system' => 'TestingMailSystem',
195
            ]);
196
        } else {
197
            // Bring back the original mail system.
198
            $value = $original;
199
            // Flush the email buffer to be able to reuse it from scratch.
200
            // @see \TestingMailSystem
201
            variable_set('drupal_test_email_collector', []);
202
        }
203
204
        variable_set('mail_system', $value);
205
    }
206
207
    /**
208
     * {@inheritdoc}
209
     */
210
    public static function getEmailMessages()
211
    {
212
        // We can't use variable_get() because Behat has another bootstrapped
213
        // variable $conf that is not updated from cURL bootstrapped Drupal instance.
214
        $result = (new FetchField('variable', 'value'))
215
            ->condition('name', 'drupal_test_email_collector')
216
            ->execute();
217
218
        return empty($result) ? [] : unserialize($result);
219
    }
220
221
    /**
222
     * {@inheritdoc}
223
     */
224
    public static function getContentTypeName($contentType)
225
    {
226
        if (isset(node_type_get_types()[$contentType])) {
227
            return $contentType;
228
        }
229
230
        return (string) (new FetchField('node_type', 'type'))
231
            ->condition('name', $contentType)
232
            ->execute();
233
    }
234
235
    /**
236
     * {@inheritdoc}
237
     */
238
    public static function injectCustomJavascript($file, $delete = false)
239
    {
240
        $file .= '.js';
241
        $modulePath = static::drupalGetFilename('module', 'system');
242
        $destination = dirname($modulePath) . '/' . $file;
243
        $injection = "\ndrupal_add_js('$destination', array('every_page' => TRUE));";
244
245
        if ($delete) {
246
            static::fileUnmanagedDelete($destination);
247
248
            $search = $injection;
249
            $replace = '';
250
        } else {
251
            static::fileUnmanagedCopy(
252
                str_replace('Context', 'JavaScript', __DIR__) . '/' . $file,
253
                $destination,
254
                FILE_EXISTS_REPLACE
255
            );
256
257
            $search = 'system_add_module_assets();';
258
            $replace = $search . $injection;
259
        }
260
261
        file_put_contents($modulePath, str_replace($search, $replace, file_get_contents($modulePath)));
262
    }
263
}
264