Skip to content

Commit 6118965

Browse files
authored
MQE-446: Unable to pass ActionGroup argument into parameterized selector
- ActionGroupObject now correctly handles $datakey$ references. - Refactoring of existing methods, creation of new methods to consolidate code.
1 parent d498246 commit 6118965

File tree

1 file changed

+71
-19
lines changed

1 file changed

+71
-19
lines changed

src/Magento/FunctionalTestingFramework/Test/Objects/ActionGroupObject.php

Lines changed: 71 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
class ActionGroupObject
1515
{
16-
const VAR_ATTRIBUTES = ['userInput', 'selector', 'page'];
16+
const VAR_ATTRIBUTES = ['userInput', 'selector', 'page', 'url'];
1717

1818
/**
1919
* The name of the action group
@@ -80,7 +80,11 @@ public function getSteps($arguments, $actionReferenceKey)
8080
private function getResolvedActionsWithArgs($arguments, $actionReferenceKey)
8181
{
8282
$resolvedActions = [];
83-
$regexPattern = '/{{([\w]+)/';
83+
84+
// $regexPattern match on: $matches[0] {{section.element(arg.field)}}
85+
// $matches[1] = section.element
86+
// $matches[2] = arg.field
87+
$regexPattern = '/{{([\w.]+)\(*([\w.$\']+)*\)*}}/';
8488

8589
foreach ($this->parsedActions as $action) {
8690
$varAttributes = array_intersect(self::VAR_ATTRIBUTES, array_keys($action->getCustomActionAttributes()));
@@ -90,11 +94,15 @@ private function getResolvedActionsWithArgs($arguments, $actionReferenceKey)
9094
foreach ($varAttributes as $varAttribute) {
9195
$attributeValue = $action->getCustomActionAttributes()[$varAttribute];
9296
preg_match_all($regexPattern, $attributeValue, $matches);
93-
if (empty($matches[0]) & empty($matches[1])) {
97+
98+
if (empty($matches[0])) {
9499
continue;
95100
}
96101

97-
$newActionAttributes[$varAttribute] = $this->resolveNewAttribute(
102+
//get rid of full match {{arg.field(arg.field)}}
103+
unset($matches[0]);
104+
105+
$newActionAttributes[$varAttribute] = $this->replaceAttributeArguments(
98106
$arguments,
99107
$attributeValue,
100108
$matches
@@ -114,34 +122,78 @@ private function getResolvedActionsWithArgs($arguments, $actionReferenceKey)
114122
}
115123

116124
/**
117-
* Function which takes an array of arguments to use for replacement of var name, the string which contains
118-
* the variable for replacement, an array of matching vars.
125+
* Function that takes an array of replacement arguments, and matches them with args in an actionGroup's attribute.
126+
* Determines if the replacement arguments are persisted data, and replaces them accordingly.
119127
*
120128
* @param array $arguments
121129
* @param string $attributeValue
122130
* @param array $matches
123131
* @return string
124132
*/
125-
private function resolveNewAttribute($arguments, $attributeValue, $matches)
133+
private function replaceAttributeArguments($arguments, $attributeValue, $matches)
126134
{
135+
$matchParametersKey = 2;
127136
$newAttributeVal = $attributeValue;
128-
foreach ($matches[1] as $var) {
129-
if (array_key_exists($var, $arguments)) {
130-
if (preg_match('/\$\$[\w.\[\]\',]+\$\$/', $arguments[$var])) {
131-
//if persisted $$data$$ was passed, return $$param.id$$ instead of {{$$param$$.id}}
132-
$newAttributeVal = str_replace($var, trim($arguments[$var], '$'), $newAttributeVal);
133-
$newAttributeVal = str_replace('{{', '$$', str_replace('}}', '$$', $newAttributeVal));
134-
} elseif (preg_match('/\$[\w.\[\]\',]+\$/', $arguments[$var])) {
135-
//elseif persisted $data$ was passed, return $param.id$ instead of {{$param$.id}}
136-
$newAttributeVal = str_replace($var, trim($arguments[$var], '$'), $newAttributeVal);
137-
$newAttributeVal = str_replace('{{', '$', str_replace('}}', '$', $newAttributeVal));
137+
138+
foreach ($matches as $key => $match) {
139+
foreach ($match as $variable) {
140+
if (empty($variable)) {
141+
continue;
142+
}
143+
// Truncate arg.field into arg
144+
$variableName = strstr($variable, '.', true);
145+
// Check if arguments has a mapping for the given variableName
146+
if (!array_key_exists($variableName, $arguments)) {
147+
continue;
148+
}
149+
$isPersisted = strstr($arguments[$variableName], '$');
150+
if ($isPersisted) {
151+
$newAttributeVal = $this->replacePersistedArgument(
152+
$arguments[$variableName],
153+
$attributeValue,
154+
$variable,
155+
$variableName,
156+
$key == $matchParametersKey ? true : false
157+
);
138158
} else {
139-
//else normal param replacement
140-
$newAttributeVal = str_replace($var, $arguments[$var], $newAttributeVal);
159+
$newAttributeVal = str_replace($variableName, $arguments[$variableName], $attributeValue);
141160
}
142161
}
143162
}
144163

145164
return $newAttributeVal;
146165
}
166+
167+
/**
168+
* Replaces args with replacements given, behavior is specific to persisted arguments.
169+
* @param string $replacement
170+
* @param string $attributeValue
171+
* @param string $fullVariable
172+
* @param string $variable
173+
* @param boolean $isParameter
174+
* @return string
175+
*/
176+
private function replacePersistedArgument($replacement, $attributeValue, $fullVariable, $variable, $isParameter)
177+
{
178+
//hookPersisted will be true if replacement passed in is $$arg.field$$, otherwise assume it's $arg.field$
179+
$hookPersistedArgumentRegex = '/\$\$[\w.\[\]\',]+\$\$/';
180+
$hookPersisted = (preg_match($hookPersistedArgumentRegex, $replacement));
181+
182+
$newAttributeValue = $attributeValue;
183+
184+
$scope = '$';
185+
if ($hookPersisted) {
186+
$scope = '$$';
187+
}
188+
189+
// parameter replacements require changing of (arg.field) to ($arg.field$)
190+
if ($isParameter) {
191+
$newAttributeValue = str_replace($fullVariable, $scope . $fullVariable . $scope, $newAttributeValue);
192+
} else {
193+
$newAttributeValue = str_replace('{{', $scope, str_replace('}}', $scope, $newAttributeValue));
194+
}
195+
$newAttributeValue = str_replace($variable, trim($replacement, '$'), $newAttributeValue);
196+
197+
return $newAttributeValue;
198+
}
147199
}

0 commit comments

Comments
 (0)