-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathModuleResolver.php
374 lines (324 loc) · 10.2 KB
/
ModuleResolver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\Util;
/**
* Class ModuleResolver, resolve module path based on enabled modules of target Magento instance.
*
* @api
*/
class ModuleResolver
{
/**
* Environment field name for module whitelist.
*/
const MODULE_WHITELIST = 'MODULE_WHITELIST';
/**
* Environment field name for custom module paths.
*/
const CUSTOM_MODULE_PATHS = 'CUSTOM_MODULE_PATHS';
/**
* Enabled modules.
*
* @var array|null
*/
protected $enabledModules = null;
/**
* Paths for enabled modules.
*
* @var array|null
*/
protected $enabledModulePaths = null;
/**
* Configuration instance.
*
* @var \Magento\FunctionalTestingFramework\Config\DataInterface
*/
protected $configuration;
/**
* Admin url for integration token.
*
* @var string
*/
protected $adminTokenUrl = "rest/V1/integration/admin/token";
/**
* Url for with module list.
*
* @var string
*/
protected $moduleUrl = "rest/V1/modules";
/**
* Url for magento version information.
*
* @var string
*/
protected $versionUrl = "magento_version ";
/**
* List of known directory that does not map to a Magento module.
*
* @var array
*/
protected $knownDirectories = ['SampleData' => 1];
/**
* ModuleResolver instance.
*
* @var ModuleResolver
*/
private static $instance = null;
/**
* SequenceSorter instance.
*
* @var ModuleResolver\SequenceSorterInterface
*/
protected $sequenceSorter;
/**
* List of module names that will be ignored.
*
* @var array
*/
protected $moduleBlacklist = [
'SampleTests', 'SampleTemplates'
];
/**
* Get ModuleResolver instance.
*
* @return ModuleResolver
*/
public static function getInstance()
{
if (!self::$instance) {
self::$instance = new ModuleResolver();
}
return self::$instance;
}
/**
* ModuleResolver constructor.
*/
private function __construct()
{
$objectManager = \Magento\FunctionalTestingFramework\ObjectManagerFactory::getObjectManager();
$this->sequenceSorter = $objectManager->get(
\Magento\FunctionalTestingFramework\Util\ModuleResolver\SequenceSorterInterface::class
);
}
/**
* Return an array of enabled modules of target Magento instance.
*
* @return array
*/
public function getEnabledModules()
{
$testGenerationPhase = $GLOBALS['GENERATE_TESTS'] ?? false;
if (isset($this->enabledModules)) {
return $this->enabledModules;
}
if ($testGenerationPhase) {
$this->printMagentoVersionInfo();
}
$token = $this->getAdminToken();
if (!$token || !is_string($token)) {
$this->enabledModules = [];
return $this->enabledModules;
}
$url = ConfigSanitizerUtil::sanitizeUrl(getenv('MAGENTO_BASE_URL')) . $this->moduleUrl;
$headers = [
'Authorization: Bearer ' . $token,
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
if (!$response) {
$this->enabledModules = [];
} else {
$this->enabledModules = json_decode($response);
}
return $this->enabledModules;
}
/**
* Return an array of module whitelist that not exist in target Magento instance.
*
* @return array
*/
protected function getModuleWhitelist()
{
$moduleWhitelist = getenv(self::MODULE_WHITELIST);
if (empty($moduleWhitelist)) {
return [];
}
return array_map('trim', explode(',', $moduleWhitelist));
}
/**
* Return the modules path based on which modules are enabled in the target Magento instance.
*
* @return array
*/
public function getModulesPath()
{
if (isset($this->enabledModulePaths)) {
return $this->enabledModulePaths;
}
$enabledModules = $this->getEnabledModules();
$forceGeneration = $GLOBALS['FORCE_PHP_GENERATE'] ?? false;
if (empty($enabledModules) && !$forceGeneration) {
trigger_error(
"Could not retrieve enabled modules from provided 'MAGENTO_BASE_URL'," .
"please make sure Magento is available at this url",
E_USER_ERROR
);
}
$modulePath = defined('TESTS_MODULE_PATH') ? TESTS_MODULE_PATH : TESTS_BP;
// Build an associative array of module name to existing module filepaths based on defined TEST MODULE PATH
$allModulePaths = [];
foreach (glob($modulePath . '*/*') as $modPath) {
$modName = basename($modPath);
$allModulePaths[$modName] = $modPath;
}
if (empty($enabledModules)) {
$this->enabledModulePaths = $this->applyCustomModuleMethods($allModulePaths);
return $this->enabledModulePaths;
}
$enabledModules = array_merge($enabledModules, $this->getModuleWhitelist());
$enabledDirectoryPaths = $this->getEnabledDirectoryPaths($enabledModules, $allModulePaths);
$this->enabledModulePaths = $this->applyCustomModuleMethods($enabledDirectoryPaths);
return $this->enabledModulePaths;
}
/**
* Runs through enabled modules and maps them known module paths by name.
* @param array $enabledModules
* @param array $allModulePaths
* @return array
*/
private function getEnabledDirectoryPaths($enabledModules, $allModulePaths)
{
$enabledDirectoryPaths = [];
foreach ($enabledModules as $magentoModuleName) {
$moduleShortName = explode('_', $magentoModuleName)[1];
if (!isset($this->knownDirectories[$moduleShortName]) && !isset($allModulePaths[$moduleShortName])) {
continue;
} else {
$enabledDirectoryPaths[$moduleShortName] = $allModulePaths[$moduleShortName];
}
}
return $enabledDirectoryPaths;
}
/**
* Executes a REST call to the supplied Magento Base Url for version information to display during generation
*
* @return void
*/
private function printMagentoVersionInfo()
{
$url = ConfigSanitizerUtil::sanitizeUrl($_ENV['MAGENTO_BASE_URL']) . $this->versionUrl;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (!$response) {
$response = "No version information available.";
}
print "\nVersion Information: {$response}\n";
}
/**
* Get the API token for admin.
*
* @return string|bool
*/
protected function getAdminToken()
{
$login = $_ENV['MAGENTO_ADMIN_USERNAME'] ?? null;
$password = $_ENV['MAGENTO_ADMIN_PASSWORD'] ?? null;
if (!$login || !$password || !isset($_ENV['MAGENTO_BASE_URL'])) {
return false;
}
$url = ConfigSanitizerUtil::sanitizeUrl($_ENV['MAGENTO_BASE_URL']) . $this->adminTokenUrl;
$data = [
'username' => $login,
'password' => $password
];
$headers = [
'Content-Type: application/json',
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
if (!$response) {
return $response;
}
return json_decode($response);
}
/**
* Sort files according module sequence.
*
* @param array $files
* @return array
*/
public function sortFilesByModuleSequence(array $files)
{
return $this->sequenceSorter->sort($files);
}
/**
* A wrapping method for any custom logic which needs to be applied to the module list
*
* @param array $modulesPath
* @return string[]
*/
protected function applyCustomModuleMethods($modulesPath)
{
$modulePathsResult = $this->removeBlacklistModules($modulesPath);
$customModulePaths = $this->getCustomModulePaths();
array_map(function ($value) {
print "Including module path: {$value}\n";
}, $customModulePaths);
return array_merge($modulePathsResult, $customModulePaths);
}
/**
* Remove blacklist modules from input module paths.
*
* @param array $modulePaths
* @return string[]
*/
private function removeBlacklistModules($modulePaths)
{
$modulePathsResult = $modulePaths;
foreach ($modulePathsResult as $moduleName => $modulePath) {
if (in_array($moduleName, $this->getModuleBlacklist())) {
unset($modulePathsResult[$moduleName]);
print "Excluding module: {$moduleName}\n";
}
}
return $modulePathsResult;
}
/**
* Returns an array of custom module paths defined by the user
*
* @return string[]
*/
private function getCustomModulePaths()
{
$customModulePaths = getenv(self::CUSTOM_MODULE_PATHS);
if (!$customModulePaths) {
return [];
}
return array_map('trim', explode(',', $customModulePaths));
}
/**
* Getter for moduleBlacklist.
*
* @return string[]
*/
private function getModuleBlacklist()
{
return $this->moduleBlacklist;
}
}