Skip to content

Commit 0f22e60

Browse files
authored
MQE-496: Unable to pass multiple ActionGroup arguments into parameterized selector
- Persisted Data fixes to Anthoula's patch. - Fixed quote logic, caught via verification tests.
1 parent 422271e commit 0f22e60

File tree

2 files changed

+75
-43
lines changed

2 files changed

+75
-43
lines changed

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

Lines changed: 64 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -93,23 +93,23 @@ private function getResolvedActionsWithArgs($arguments, $actionReferenceKey)
9393
// $regexPattern match on: $matches[0] {{section.element(arg.field)}}
9494
// $matches[1] = section.element
9595
// $matches[2] = arg.field
96-
$regexPattern = '/{{([\w.\[\]]+)\(*([\w.$\']+)*\)*}}/';
96+
$regexPattern = '/{{([\w.\[\]]+)\(*([\w.$\',\s]+)*\)*}}/';
9797

9898
foreach ($this->parsedActions as $action) {
9999
$varAttributes = array_intersect($this->varAttributes, array_keys($action->getCustomActionAttributes()));
100100
$newActionAttributes = [];
101+
101102
if (!empty($varAttributes)) {
102103
// 1 check to see if we have pertinent var
103104
foreach ($varAttributes as $varAttribute) {
104105
$attributeValue = $action->getCustomActionAttributes()[$varAttribute];
105106
preg_match_all($regexPattern, $attributeValue, $matches);
106-
107107
if (empty($matches[0])) {
108108
continue;
109109
}
110110

111111
//get rid of full match {{arg.field(arg.field)}}
112-
unset($matches[0]);
112+
array_shift($matches);
113113

114114
$newActionAttributes[$varAttribute] = $this->replaceAttributeArguments(
115115
$arguments,
@@ -141,36 +141,68 @@ private function getResolvedActionsWithArgs($arguments, $actionReferenceKey)
141141
*/
142142
private function replaceAttributeArguments($arguments, $attributeValue, $matches)
143143
{
144-
$matchParametersKey = 2;
145-
$newAttributeVal = $attributeValue;
144+
list($mainValueList, $possibleArgumentsList) = $matches;
146145

147-
foreach ($matches as $key => $match) {
148-
foreach ($match as $variable) {
149-
if (empty($variable)) {
150-
continue;
151-
}
152-
// Truncate arg.field into arg. If 'Literal' was passed, variableName will be null.
153-
$variableName = strstr($variable, '.', true);
154-
// Check if arguments has a mapping for the given variableName
155-
if ($variableName == null || !array_key_exists($variableName, $arguments)) {
156-
continue;
157-
}
158-
$isPersisted = strstr($arguments[$variableName], '$');
159-
if ($isPersisted) {
160-
$newAttributeVal = $this->replacePersistedArgument(
161-
$arguments[$variableName],
162-
$attributeValue,
163-
$variable,
164-
$variableName,
165-
$key == $matchParametersKey ? true : false
166-
);
167-
} else {
168-
$newAttributeVal = str_replace($variableName, $arguments[$variableName], $attributeValue);
169-
}
146+
foreach ($mainValueList as $index => $mainValue) {
147+
$possibleArguments = $possibleArgumentsList[$index];
148+
149+
$attributeValue = $this->replaceAttributeArgumentInVariable($mainValue, $arguments, $attributeValue);
150+
151+
// Split on commas, trim all values, and finally filter out all FALSE values
152+
$argumentList = array_filter(array_map('trim', explode(',', $possibleArguments)));
153+
154+
foreach ($argumentList as $argumentValue) {
155+
$attributeValue = $this->replaceAttributeArgumentInVariable(
156+
$argumentValue,
157+
$arguments,
158+
$attributeValue,
159+
true
160+
);
170161
}
171162
}
172163

173-
return $newAttributeVal;
164+
return $attributeValue;
165+
}
166+
167+
/**
168+
* Replace attribute arguments in variable.
169+
*
170+
* @param string $variable
171+
* @param array $arguments
172+
* @param string $attributeValue
173+
* @param bool $isInnerArgument
174+
* @return string
175+
*/
176+
private function replaceAttributeArgumentInVariable(
177+
$variable,
178+
$arguments,
179+
$attributeValue,
180+
$isInnerArgument = false
181+
) {
182+
// Truncate arg.field into arg
183+
$variableName = strstr($variable, '.', true);
184+
// Check if arguments has a mapping for the given variableName
185+
186+
if ($variableName === false) {
187+
$variableName = $variable;
188+
}
189+
190+
if (!array_key_exists($variableName, $arguments)) {
191+
return $attributeValue;
192+
}
193+
194+
$isPersisted = strstr($arguments[$variableName], '$');
195+
if ($isPersisted) {
196+
return $this->replacePersistedArgument(
197+
$arguments[$variableName],
198+
$attributeValue,
199+
$variable,
200+
$variableName,
201+
$isInnerArgument
202+
);
203+
}
204+
205+
return str_replace($variableName, $arguments[$variableName], $attributeValue);
174206
}
175207

176208
/**
@@ -197,11 +229,12 @@ private function replacePersistedArgument($replacement, $attributeValue, $fullVa
197229

198230
// parameter replacements require changing of (arg.field) to ($arg.field$)
199231
if ($isParameter) {
200-
$newAttributeValue = str_replace($fullVariable, $scope . $fullVariable . $scope, $newAttributeValue);
232+
$fullReplacement = str_replace($variable, trim($replacement, '$'), $fullVariable);
233+
$newAttributeValue = str_replace($fullVariable, $scope . $fullReplacement . $scope, $newAttributeValue);
201234
} else {
202235
$newAttributeValue = str_replace('{{', $scope, str_replace('}}', $scope, $newAttributeValue));
236+
$newAttributeValue = str_replace($variable, trim($replacement, '$'), $newAttributeValue);
203237
}
204-
$newAttributeValue = str_replace($variable, trim($replacement, '$'), $newAttributeValue);
205238

206239
return $newAttributeValue;
207240
}

src/Magento/FunctionalTestingFramework/Util/TestGenerator.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,8 @@ private function resolveTestVariable($inputString, $args)
913913
*/
914914
private function replaceMatchesIntoArg($matches, &$outputArg, $delimiter)
915915
{
916+
// Remove Duplicate $matches from array. Duplicate matches are replaced all in one go.
917+
$matches = array_unique($matches);
916918
foreach ($matches as $match) {
917919
$replacement = null;
918920
$variable = $this->stripAndSplitReference($match, $delimiter);
@@ -950,25 +952,22 @@ private function processQuoteBreaks($match, $argument, $replacement)
950952
$outputArg = $argument;
951953
$beforeIndex = strpos($outputArg, $match) - 1;
952954
$afterIndex = $beforeIndex + strlen($match) + 1;
953-
$quoteBefore = true;
954-
$quoteAfter = true;
955955

956-
// Prepare replacement with quote breaks if needed
957-
if ($argument[$beforeIndex] != "\"") {
958-
$replacement = '" . ' . $replacement;
959-
$quoteBefore = false;
960-
}
961-
if ($argument[$afterIndex] != "\"") {
962-
$replacement = $replacement . ' . "';
963-
$quoteAfter = false;
964-
}
965-
//Remove quotes at either end of argument if they aren't necessary.
956+
// Determine if there is a " before/after the $match, and if there is only one " before/after match.
957+
$quoteBefore = $argument[$beforeIndex] == '"' && substr_count($argument, '"', 0, $beforeIndex)<1;
958+
$quoteAfter = $argument[$afterIndex] == '"' && substr_count($argument, '"', $afterIndex+1)<1;
959+
960+
//Remove quotes at either end of argument if they aren't necessary. Add double-quote concatenation if needed.
966961
if ($quoteBefore) {
967962
$outputArg = substr($outputArg, 0, $beforeIndex) . substr($outputArg, $beforeIndex+1);
968963
$afterIndex--;
964+
} else {
965+
$replacement = '" . ' . $replacement;
969966
}
970967
if ($quoteAfter) {
971968
$outputArg = substr($outputArg, 0, $afterIndex) . substr($outputArg, $afterIndex+1);
969+
} else {
970+
$replacement = $replacement . ' . "';
972971
}
973972
$outputArg = str_replace($match, $replacement, $outputArg);
974973
return $outputArg;

0 commit comments

Comments
 (0)