-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathActionMergeUtil.php
340 lines (303 loc) · 11.2 KB
/
ActionMergeUtil.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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\Test\Util;
use Magento\FunctionalTestingFramework\Exceptions\TestReferenceException;
use Magento\FunctionalTestingFramework\Exceptions\XmlException;
use Magento\FunctionalTestingFramework\Test\Handlers\ActionGroupObjectHandler;
use Magento\FunctionalTestingFramework\Test\Objects\ActionObject;
/**
* Class ActionMergeUtil
*/
class ActionMergeUtil
{
const STEP_MISSING_ERROR_MSG =
"Merge Error - Step could not be found in either TestXML or DeltaXML.
\t%s: '%s'\tTestStep: '%s'\tLinkedStep: '%s'";
const WAIT_ATTR = 'timeout';
const WAIT_ACTION_NAME = 'waitForPageLoad';
const WAIT_ACTION_SUFFIX = 'WaitForPageLoad';
const SKIP_READINESS_ACTION_NAME = 'skipReadinessCheck';
const SKIP_READINESS_OFF_SUFFIX = 'SkipReadinessOff';
const SKIP_READINESS_ON_SUFFIX = 'SkipReadinessOn';
const DEFAULT_SKIP_ON_ORDER = 'before';
const DEFAULT_SKIP_OFF_ORDER = 'after';
const DEFAULT_WAIT_ORDER = 'after';
const APPROVED_ACTIONS = ['fillField', 'magentoCLI', 'field'];
const SECRET_MAPPING = ['fillField' => 'fillSecretField', 'magentoCLI' => 'magentoCLISecret'];
const CREDS_REGEX = "/{{_CREDS\.([\w|\/]+)}}/";
/**
* Array holding final resulting steps
*
* @var array
*/
private $orderedSteps = [];
/**
* Array holding action to be merged
*
* @var array
*/
private $stepsToMerge = [];
/**
* Name of calling context.
*
* @var string
*/
private $name;
/**
* Type of calling context.
*
* @var string
*/
private $type;
/**
* ActionMergeUtil constructor.
*
* @param string $contextName
* @param string $contextType
*/
public function __construct($contextName, $contextType)
{
$this->name = $contextName;
$this->type = $contextType;
}
/**
* Method to execute merge of steps and insert wait steps.
*
* @param array $parsedSteps
* @param boolean $skipActionGroupResolution
* @return array
* @throws TestReferenceException
* @throws XmlException
*/
public function resolveActionSteps($parsedSteps, $skipActionGroupResolution = false)
{
$this->mergeActions($parsedSteps);
$this->insertWaits();
$this->insertReadinessSkips();
if ($skipActionGroupResolution) {
return $this->orderedSteps;
}
$resolvedActions = $this->resolveActionGroups($this->orderedSteps);
return $this->resolveSecretFieldAccess($resolvedActions);
}
/**
* Takes an array of actions and resolves any references to secret fields. The function then validates whether the
* reference is valid and replaces the function name accordingly to hide arguments at runtime.
*
* @param ActionObject[] $resolvedActions
* @return ActionObject[]
* @throws TestReferenceException
*/
private function resolveSecretFieldAccess($resolvedActions)
{
$actions = [];
foreach ($resolvedActions as $resolvedAction) {
$action = $resolvedAction;
$actionHasSecretRef = $this->actionAttributeContainsSecretRef($resolvedAction->getCustomActionAttributes());
$actionType = $resolvedAction->getType();
if ($actionHasSecretRef && !(in_array($actionType, self::APPROVED_ACTIONS))) {
throw new TestReferenceException("You cannot reference secret data outside " .
"of the fillField, magentoCLI and createData actions");
}
// Do NOT remap actions that don't need it.
if (isset(self::SECRET_MAPPING[$actionType]) && $actionHasSecretRef) {
$actionType = self::SECRET_MAPPING[$actionType];
}
$action = new ActionObject(
$action->getStepKey(),
$actionType,
$action->getCustomActionAttributes(),
$action->getLinkedAction(),
$action->getOrderOffset(),
$action->getActionOrigin(),
$action->getDeprecatedUsages()
);
$actions[$action->getStepKey()] = $action;
}
return $actions;
}
/**
* Returns a boolean based on whether or not the action attributes contain a reference to a secret field.
*
* @param array $actionAttributes
* @return boolean
*/
private function actionAttributeContainsSecretRef($actionAttributes)
{
foreach ($actionAttributes as $actionAttribute) {
if (is_array($actionAttribute)) {
return $this->actionAttributeContainsSecretRef($actionAttribute);
}
preg_match_all(self::CREDS_REGEX, $actionAttribute, $matches);
if (!empty($matches[0])) {
return true;
}
}
return false;
}
/**
* Method to resolve action group references and insert relevant actions into step flow
*
* @param array $mergedSteps
* @throws TestReferenceException
* @return array
*/
private function resolveActionGroups($mergedSteps)
{
$newOrderedList = [];
foreach ($mergedSteps as $key => $mergedStep) {
/**@var ActionObject $mergedStep**/
if ($mergedStep->getType() == ActionObjectExtractor::ACTION_GROUP_TAG) {
$actionGroupRef = $mergedStep->getCustomActionAttributes()[ActionObjectExtractor::ACTION_GROUP_REF];
$actionGroup = ActionGroupObjectHandler::getInstance()->getObject($actionGroupRef);
if ($actionGroup == null) {
throw new TestReferenceException("Could not find ActionGroup by ref \"{$actionGroupRef}\"");
}
$args = $mergedStep->getCustomActionAttributes()[ActionObjectExtractor::ACTION_GROUP_ARGUMENTS] ?? null;
$actionsToMerge = $actionGroup->getSteps($args, $key);
$newOrderedList = $newOrderedList + $actionsToMerge;
} else {
$newOrderedList[$key] = $mergedStep;
}
}
return $newOrderedList;
}
/**
* This method runs a step sort, loops steps which need to be merged, and runs the mergeStep function on each one.
*
* @param array $parsedSteps
* @return void
* @throws XmlException
*/
private function mergeActions($parsedSteps)
{
$this->sortActions($parsedSteps);
foreach ($this->stepsToMerge as $stepName => $stepToMerge) {
if (!array_key_exists($stepName, $this->orderedSteps)) {
$this->mergeAction($stepToMerge);
}
}
unset($stepName);
unset($stepToMerge);
}
/**
* Runs through the prepared orderedSteps and calls insertWait if a step requires a wait after it.
*
* @return void
*/
private function insertWaits()
{
foreach ($this->orderedSteps as $step) {
if ($step->getTimeout()) {
$waitStepAttributes = [self::WAIT_ATTR => $step->getTimeout()];
$waitStep = new ActionObject(
$step->getStepKey() . self::WAIT_ACTION_SUFFIX,
self::WAIT_ACTION_NAME,
$waitStepAttributes,
$step->getStepKey(),
self::DEFAULT_WAIT_ORDER
);
$this->insertStep($waitStep);
}
}
}
/**
* Runs through the prepared orderedSteps and calls insertWait if a step requires a wait after it.
*
* @return void
*/
private function insertReadinessSkips()
{
foreach ($this->orderedSteps as $step) {
if (array_key_exists("skipReadiness", $step->getCustomActionAttributes())) {
if ($step->getCustomActionAttributes()['skipReadiness'] == "true") {
$skipReadinessOn = new ActionObject(
$step->getStepKey() . self::SKIP_READINESS_ON_SUFFIX,
self::SKIP_READINESS_ACTION_NAME,
['state' => "true"],
$step->getStepKey(),
self::DEFAULT_SKIP_ON_ORDER
);
$skipReadinessOff = new ActionObject(
$step->getStepKey() . self::SKIP_READINESS_OFF_SUFFIX,
self::SKIP_READINESS_ACTION_NAME,
['state' => "false"],
$step->getStepKey(),
self::DEFAULT_SKIP_OFF_ORDER
);
$this->insertStep($skipReadinessOn);
$this->insertStep($skipReadinessOff);
}
}
}
}
/**
* This method takes the steps from the parser and splits steps which need merge from steps that are ordered.
*
* @param array $parsedSteps
* @return void
* @throws TestReferenceException
*/
private function sortActions($parsedSteps)
{
foreach ($parsedSteps as $parsedStep) {
try {
$parsedStep->resolveReferences();
if ($parsedStep->getLinkedAction()) {
$this->stepsToMerge[$parsedStep->getStepKey()] = $parsedStep;
} else {
$this->orderedSteps[$parsedStep->getStepKey()] = $parsedStep;
}
} catch (\Exception $e) {
throw new TestReferenceException(
$e->getMessage() .
".\nException occurred parsing action at StepKey \"" . $parsedStep->getStepKey() . "\""
);
}
}
}
/**
* Recursively merges in each step and its dependencies
*
* @param ActionObject $stepToMerge
* @throws XmlException
* @return void
*/
private function mergeAction($stepToMerge)
{
$linkedStep = $stepToMerge->getLinkedAction();
if (!array_key_exists($linkedStep, $this->orderedSteps)
and
!array_key_exists($linkedStep, $this->stepsToMerge)) {
throw new XmlException(sprintf(
self::STEP_MISSING_ERROR_MSG,
$this->type,
$this->name,
$stepToMerge->getStepKey(),
$linkedStep
));
} elseif (!array_key_exists($linkedStep, $this->orderedSteps)) {
$this->mergeAction($this->stepsToMerge[$linkedStep]);
}
$this->insertStep($stepToMerge);
}
/**
* Inserts a step into the ordered steps array based on position and step referenced.
*
* @param ActionObject $stepToMerge
* @return void
*/
private function insertStep($stepToMerge)
{
$position = array_search(
$stepToMerge->getLinkedAction(),
array_keys($this->orderedSteps)
) + $stepToMerge->getOrderOffset();
$previous_items = array_slice($this->orderedSteps, 0, $position, true);
$next_items = array_slice($this->orderedSteps, $position, null, true);
$this->orderedSteps = $previous_items + [$stepToMerge->getStepKey() => $stepToMerge] + $next_items;
}
}