Skip to content

Commit 5fe3796

Browse files
authored
MQE-905: Persistent entity variable not interpolated with page urls with type "admin"
- resolveEnvReferences now alters the passed in set of $args, allowing further use of the new $args. - Refactored resolve* functions to consume and transform $args instead of the whole string.
1 parent e2b88bf commit 5fe3796

File tree

3 files changed

+40
-43
lines changed

3 files changed

+40
-43
lines changed

dev/tests/verification/Resources/PersistedReplacementTest.txt

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class PersistedReplacementTestCest
5050
$createdData->createEntity();
5151
$I->fillField("#selector", "StringBefore " . $createdData->getCreatedDataByName('firstname') . " StringAfter");
5252
$I->fillField("#" . $createdData->getCreatedDataByName('firstname'), "input");
53+
$I->fillField("#" . getenv("MAGENTO_BASE_URL") . "#" . $createdData->getCreatedDataByName('firstname'), "input");
5354
$I->dragAndDrop("#" . $createdData->getCreatedDataByName('firstname'), $createdData->getCreatedDataByName('lastname'));
5455
$I->conditionalClick($createdData->getCreatedDataByName('lastname'), "#" . $createdData->getCreatedDataByName('firstname'), true);
5556
$I->amOnUrl($createdData->getCreatedDataByName('firstname') . ".html");

dev/tests/verification/TestModule/Test/PersistedReplacementTest.xml

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
<fillField stepKey="inputReplace" selector="#selector" userInput="StringBefore $createdData.firstname$ StringAfter"/>
1818
<fillField stepKey="selectorReplace" selector="#$createdData.firstname$" userInput="input"/>
19+
<fillField stepKey="selectorReplace2" selector="#{{_ENV.MAGENTO_BASE_URL}}#$createdData.firstname$" userInput="input"/>
1920
<dragAndDrop stepKey="selector12Replace" selector1="#$createdData.firstname$" selector2="$createdData.lastname$"/>
2021
<conditionalClick stepKey="dependentSelectorReplace" dependentSelector="#$createdData.firstname$" selector="$createdData.lastname$" visible="true"/>
2122
<amOnUrl stepKey="urlReplace" url="$createdData.firstname$.html"/>

src/Magento/FunctionalTestingFramework/Util/TestGenerator.php

+38-43
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ public function generateStepsPhp($actionObjects, $hookObject = false, $actor = "
702702
);
703703
}
704704

705-
if (isset($storeCode)) {
705+
if ($storeCode) {
706706
$createEntityFunctionCall .= sprintf("\"%s\");\n", $storeCode);
707707
} else {
708708
$createEntityFunctionCall .= ");\n";
@@ -743,13 +743,14 @@ public function generateStepsPhp($actionObjects, $hookObject = false, $actor = "
743743
$testSteps .= $contextSetter;
744744
$testSteps .= $deleteEntityFunctionCall;
745745
} else {
746+
$url = $this->resolveEnvReferences([$url])[0];
747+
$url = $this->resolveTestVariable([$url], null)[0];
746748
$output = sprintf(
747749
"\t\t$%s->deleteEntityByUrl(%s);\n",
748750
$actor,
749751
$url
750752
);
751-
$output = $this->resolveEnvReferences($output, [$url]);
752-
$testSteps .= $this->resolveTestVariable($output, [$url], null);
753+
$testSteps .= $output;
753754
}
754755
break;
755756
case "updateData":
@@ -798,7 +799,7 @@ public function generateStepsPhp($actionObjects, $hookObject = false, $actor = "
798799
);
799800
}
800801

801-
if (isset($storeCode)) {
802+
if ($storeCode) {
802803
$updateEntityFunctionCall .= sprintf(", \"%s\");\n", $storeCode);
803804
} else {
804805
$updateEntityFunctionCall .= ");\n";
@@ -865,7 +866,7 @@ public function generateStepsPhp($actionObjects, $hookObject = false, $actor = "
865866
$getEntityFunctionCall .= 'null';
866867
}
867868

868-
if (isset($storeCode)) {
869+
if ($storeCode) {
869870
$getEntityFunctionCall .= sprintf(", \"%s\");\n", $storeCode);
870871
} else {
871872
$getEntityFunctionCall .= ");\n";
@@ -1222,9 +1223,10 @@ public function generateStepsPhp($actionObjects, $hookObject = false, $actor = "
12221223
break;
12231224
case "field":
12241225
$fieldKey = $actionObject->getCustomActionAttributes()['key'];
1226+
$input = $this->resolveTestVariable([$input], $actionObject->getActionOrigin())[0];
12251227
$argRef = "\t\t\$";
12261228
$argRef .= str_replace(ucfirst($fieldKey), "", $stepKey) . "Fields['{$fieldKey}'] = ${input};\n";
1227-
$testSteps .= $this->resolveTestVariable($argRef, [$input], $actionObject->getActionOrigin());
1229+
$testSteps .= $argRef;
12281230
break;
12291231
default:
12301232
$testSteps .= $this->wrapFunctionCall($actor, $actionObject, $selector, $input, $parameter);
@@ -1253,19 +1255,16 @@ private function resolveLocatorFunctionInAttribute($attribute)
12531255
* Resolves replacement of $input$ and $$input$$ in given function, recursing and replacing individual arguments
12541256
* Also determines if each argument requires any quote replacement.
12551257
*
1256-
* @param string $inputString
12571258
* @param array $args
12581259
* @param array $actionOrigin
1259-
* @return string
1260+
* @return array
12601261
* @throws \Exception
12611262
*/
1262-
private function resolveTestVariable($inputString, $args, $actionOrigin)
1263+
private function resolveTestVariable($args, $actionOrigin)
12631264
{
1264-
$outputString = $inputString;
1265-
1266-
//Loop through each argument, replace and then replace
1267-
foreach ($args as $arg) {
1268-
if ($arg == null) {
1265+
$newArgs = [];
1266+
foreach ($args as $key => $arg) {
1267+
if ($arg === null) {
12691268
continue;
12701269
}
12711270
$outputArg = $arg;
@@ -1282,10 +1281,10 @@ private function resolveTestVariable($inputString, $args, $actionOrigin)
12821281

12831282
$outputArg = $this->resolveStepKeyReferences($outputArg, $actionOrigin);
12841283

1285-
$outputString = str_replace($arg, $outputArg, $outputString);
1284+
$newArgs[$key] = $outputArg;
12861285
}
12871286

1288-
return $outputString;
1287+
return $newArgs;
12891288
}
12901289

12911290
/**
@@ -1665,20 +1664,17 @@ private function wrapFunctionCall($actor, $action, ...$args)
16651664
if (null === $args[$i]) {
16661665
continue;
16671666
}
1668-
if (!$isFirst) {
1669-
$output .= ', ';
1670-
}
16711667
if ($args[$i] === "") {
16721668
$args[$i] = '"' . $args[$i] . '"';
16731669
}
1674-
$output .= $args[$i];
1675-
$isFirst = false;
16761670
}
1677-
$output .= ");\n";
1678-
1679-
$output = $this->resolveEnvReferences($output, $args);
1680-
1681-
return $this->resolveTestVariable($output, $args, $action->getActionOrigin());
1671+
if (!is_array($args)) {
1672+
$args = [$args];
1673+
}
1674+
$args = $this->resolveEnvReferences($args);
1675+
$args = $this->resolveTestVariable($args, $action->getActionOrigin());
1676+
$output .= implode(", ", array_filter($args, function($value) { return $value !== null; })) . ");\n";
1677+
return $output;
16821678
}
16831679

16841680
/**
@@ -1699,36 +1695,32 @@ private function wrapFunctionCallWithReturnValue($returnVariable, $actor, $actio
16991695
if (null === $args[$i]) {
17001696
continue;
17011697
}
1702-
if (!$isFirst) {
1703-
$output .= ', ';
1704-
}
17051698
if ($args[$i] === "") {
17061699
$args[$i] = '"' . $args[$i] . '"';
17071700
}
1708-
$output .= $args[$i];
1709-
$isFirst = false;
17101701
}
1711-
$output .= ");\n";
1712-
1713-
$output = $this->resolveEnvReferences($output, $args);
1714-
1715-
return $this->resolveTestVariable($output, $args, $action->getActionOrigin());
1702+
if (!is_array($args)) {
1703+
$args = [$args];
1704+
}
1705+
$args = $this->resolveEnvReferences($args);
1706+
$args = $this->resolveTestVariable($args, $action->getActionOrigin());
1707+
$output .= implode(", ", array_filter($args, function($value) { return $value !== null; })) . ");\n";
1708+
return $output;
17161709
}
17171710
// @codingStandardsIgnoreEnd
17181711

17191712
/**
17201713
* Resolves {{_ENV.variable}} into getenv("variable") for test-runtime ENV referencing.
1721-
* @param string $inputString
17221714
* @param array $args
1723-
* @return string
1715+
* @return array
17241716
*/
1725-
private function resolveEnvReferences($inputString, $args)
1717+
private function resolveEnvReferences($args)
17261718
{
17271719
$envRegex = "/{{_ENV\.([\w]+)}}/";
17281720

1729-
$outputString = $inputString;
1721+
$newArgs = [];
17301722

1731-
foreach ($args as $arg) {
1723+
foreach ($args as $key => $arg) {
17321724
preg_match_all($envRegex, $arg, $matches);
17331725
if (!empty($matches[0])) {
17341726
$fullMatch = $matches[0][0];
@@ -1737,11 +1729,14 @@ private function resolveEnvReferences($inputString, $args)
17371729
$replacement = "getenv(\"{$envVariable}\")";
17381730

17391731
$outputArg = $this->processQuoteBreaks($fullMatch, $arg, $replacement);
1740-
$outputString = str_replace($arg, $outputArg, $outputString);
1732+
$newArgs[$key] = $outputArg;
1733+
continue;
17411734
}
1735+
$newArgs[$key] = $arg;
17421736
}
17431737

1744-
return $outputString;
1738+
// override passed in args for use later.
1739+
return $newArgs;
17451740
}
17461741

17471742
/**

0 commit comments

Comments
 (0)