|
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 jsonEncode($data) |
|
58
|
|
|
{ |
|
59
|
|
|
return drupal_json_encode($data); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
* |
|
65
|
|
|
* @return \stdClass |
|
66
|
|
|
*/ |
|
67
|
|
|
public static function getCurrentUser() |
|
68
|
|
|
{ |
|
69
|
|
|
return $GLOBALS['user']; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
* |
|
75
|
|
|
* @param \stdClass $user |
|
76
|
|
|
*/ |
|
77
|
|
|
public static function setCurrentUser($user) |
|
78
|
|
|
{ |
|
79
|
|
|
$GLOBALS['user'] = $user; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* {@inheritdoc} |
|
84
|
|
|
*/ |
|
85
|
|
|
public static function setCurrentPath($path) |
|
86
|
|
|
{ |
|
87
|
|
|
$_GET['q'] = $path; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritdoc} |
|
92
|
|
|
*/ |
|
93
|
|
|
public static function getUidByName($username) |
|
94
|
|
|
{ |
|
95
|
|
|
return (int) (new FetchField('users', 'uid')) |
|
96
|
|
|
->condition('name', $username) |
|
97
|
|
|
->execute(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* {@inheritdoc} |
|
102
|
|
|
*/ |
|
103
|
|
|
public static function deleteUser($user_id) |
|
104
|
|
|
{ |
|
105
|
|
|
user_delete($user_id); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* {@inheritdoc} |
|
110
|
|
|
* |
|
111
|
|
|
* @return \SelectQuery |
|
112
|
|
|
*/ |
|
113
|
|
|
public static function selectQuery($table, $alias = null, array $options = []) |
|
114
|
|
|
{ |
|
115
|
|
|
return db_select($table, $alias, $options); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* {@inheritdoc} |
|
120
|
|
|
*/ |
|
121
|
|
|
public static function getFieldDefinitions($entityType, $bundle) |
|
122
|
|
|
{ |
|
123
|
|
|
// Load entity properties because in Drupal 8 they are assumed as regular fields. |
|
124
|
|
|
$entityProperties = entity_get_property_info($entityType); |
|
125
|
|
|
$fieldInstances = field_info_instances($entityType, $bundle); |
|
126
|
|
|
$definitions = []; |
|
127
|
|
|
|
|
128
|
|
|
if (!empty($entityProperties['properties'])) { |
|
129
|
|
|
$fieldInstances += $entityProperties['properties']; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
foreach ($fieldInstances as $name => $definition) { |
|
133
|
|
|
$definitions[$name] = [ |
|
134
|
|
|
'label' => $definition['label'], |
|
135
|
|
|
'required' => !empty($definition['required']), |
|
136
|
|
|
]; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return $definitions; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* {@inheritdoc} |
|
144
|
|
|
*/ |
|
145
|
|
|
public static function getDatabaseConnectionInfo($connection) |
|
146
|
|
|
{ |
|
147
|
|
|
return \Database::getConnectionInfo($connection); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* {@inheritdoc} |
|
152
|
|
|
*/ |
|
153
|
|
|
public static function entityCreate($entityType, array $values) |
|
154
|
|
|
{ |
|
155
|
|
|
$entity = entity_create($entityType, $values); |
|
156
|
|
|
|
|
157
|
|
|
entity_save($entityType, $entity); |
|
158
|
|
|
list($entityId, , $bundle) = entity_extract_ids($entityType, $entity); |
|
159
|
|
|
|
|
160
|
|
|
return [$entityId, $entityType, $bundle]; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* {@inheritdoc} |
|
165
|
|
|
*/ |
|
166
|
|
|
public static function entityLoad($entityType, $id) |
|
167
|
|
|
{ |
|
168
|
|
|
$entities = entity_load($entityType, [$id]); |
|
169
|
|
|
|
|
170
|
|
|
return empty($entities) ? null : reset($entities); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* {@inheritdoc} |
|
175
|
|
|
*/ |
|
176
|
|
|
public static function entityHasField($entity, $fieldName) |
|
177
|
|
|
{ |
|
178
|
|
|
return isset($entity->{$fieldName}); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* {@inheritdoc} |
|
183
|
|
|
* |
|
184
|
|
|
* @param \EntityDrupalWrapper $entity |
|
185
|
|
|
*/ |
|
186
|
|
|
public static function entityFieldValue($entity, $fieldName) |
|
187
|
|
|
{ |
|
188
|
|
|
if ($entity instanceof \EntityDrupalWrapper) { |
|
189
|
|
|
return $entity->{$fieldName}->value(); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
193
|
|
|
'First argument for "%s" method must be of "%s" type.', |
|
194
|
|
|
__METHOD__, |
|
195
|
|
|
\EntityDrupalWrapper::class |
|
196
|
|
|
)); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* {@inheritdoc} |
|
201
|
|
|
*/ |
|
202
|
|
|
public static function switchMailSystem($useTesting) |
|
203
|
|
|
{ |
|
204
|
|
|
static $original = [ |
|
205
|
|
|
'default-system' => 'DefaultMailSystem', |
|
206
|
|
|
]; |
|
207
|
|
|
|
|
208
|
|
|
if ($useTesting) { |
|
209
|
|
|
// Store original mail system to restore it after scenario. |
|
210
|
|
|
$original = variable_get('mail_system', $original); |
|
211
|
|
|
// Set the mail system for testing. It will store an emails in |
|
212
|
|
|
// "drupal_test_email_collector" Drupal variable instead of sending. |
|
213
|
|
|
$value = array_merge($original, [ |
|
214
|
|
|
'default-system' => 'TestingMailSystem', |
|
215
|
|
|
]); |
|
216
|
|
|
} else { |
|
217
|
|
|
// Bring back the original mail system. |
|
218
|
|
|
$value = $original; |
|
219
|
|
|
// Flush the email buffer to be able to reuse it from scratch. |
|
220
|
|
|
// @see \TestingMailSystem |
|
221
|
|
|
variable_set('drupal_test_email_collector', []); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
variable_set('mail_system', $value); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* {@inheritdoc} |
|
229
|
|
|
*/ |
|
230
|
|
|
public static function getEmailMessages() |
|
231
|
|
|
{ |
|
232
|
|
|
// We can't use variable_get() because Behat has another bootstrapped |
|
233
|
|
|
// variable $conf that is not updated from cURL bootstrapped Drupal instance. |
|
234
|
|
|
$result = (new FetchField('variable', 'value')) |
|
235
|
|
|
->condition('name', 'drupal_test_email_collector') |
|
236
|
|
|
->execute(); |
|
237
|
|
|
|
|
238
|
|
|
return empty($result) ? [] : unserialize($result); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* {@inheritdoc} |
|
243
|
|
|
*/ |
|
244
|
|
|
public static function getContentTypeName($contentType) |
|
245
|
|
|
{ |
|
246
|
|
|
if (isset(node_type_get_types()[$contentType])) { |
|
247
|
|
|
return $contentType; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
return (string) (new FetchField('node_type', 'type')) |
|
251
|
|
|
->condition('name', $contentType) |
|
252
|
|
|
->execute(); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* {@inheritdoc} |
|
257
|
|
|
*/ |
|
258
|
|
|
public static function injectCustomJavascript($file, $delete = false) |
|
259
|
|
|
{ |
|
260
|
|
|
$file .= '.js'; |
|
261
|
|
|
$modulePath = static::drupalGetFilename('module', 'system'); |
|
262
|
|
|
$destination = dirname($modulePath) . '/' . $file; |
|
263
|
|
|
$injection = "\ndrupal_add_js('$destination', array('every_page' => TRUE));"; |
|
264
|
|
|
|
|
265
|
|
|
if ($delete) { |
|
266
|
|
|
static::fileUnmanagedDelete($destination); |
|
267
|
|
|
|
|
268
|
|
|
$search = $injection; |
|
269
|
|
|
$replace = ''; |
|
270
|
|
|
} else { |
|
271
|
|
|
static::fileUnmanagedCopy( |
|
272
|
|
|
str_replace('Context', 'JavaScript', __DIR__) . '/' . $file, |
|
273
|
|
|
$destination, |
|
274
|
|
|
FILE_EXISTS_REPLACE |
|
275
|
|
|
); |
|
276
|
|
|
|
|
277
|
|
|
$search = 'system_add_module_assets();'; |
|
278
|
|
|
$replace = $search . $injection; |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
file_put_contents($modulePath, str_replace($search, $replace, file_get_contents($modulePath))); |
|
282
|
|
|
} |
|
283
|
|
|
} |
|
284
|
|
|
|