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