diff --git a/dev/tests/verification/Resources/PersistedReplacementTest.txt b/dev/tests/verification/Resources/PersistedReplacementTest.txt index ca09c767e..2c335c7f0 100644 --- a/dev/tests/verification/Resources/PersistedReplacementTest.txt +++ b/dev/tests/verification/Resources/PersistedReplacementTest.txt @@ -50,6 +50,7 @@ class PersistedReplacementTestCest $createdData->createEntity(); $I->fillField("#selector", "StringBefore " . $createdData->getCreatedDataByName('firstname') . " StringAfter"); $I->fillField("#" . $createdData->getCreatedDataByName('firstname'), "input"); + $I->fillField("#" . getenv("MAGENTO_BASE_URL") . "#" . $createdData->getCreatedDataByName('firstname'), "input"); $I->dragAndDrop("#" . $createdData->getCreatedDataByName('firstname'), $createdData->getCreatedDataByName('lastname')); $I->conditionalClick($createdData->getCreatedDataByName('lastname'), "#" . $createdData->getCreatedDataByName('firstname'), true); $I->amOnUrl($createdData->getCreatedDataByName('firstname') . ".html"); diff --git a/dev/tests/verification/TestModule/Test/PersistedReplacementTest.xml b/dev/tests/verification/TestModule/Test/PersistedReplacementTest.xml index d46381748..26885fda4 100644 --- a/dev/tests/verification/TestModule/Test/PersistedReplacementTest.xml +++ b/dev/tests/verification/TestModule/Test/PersistedReplacementTest.xml @@ -16,6 +16,7 @@ + diff --git a/src/Magento/FunctionalTestingFramework/Util/TestGenerator.php b/src/Magento/FunctionalTestingFramework/Util/TestGenerator.php index dfbf20dac..e10626f64 100644 --- a/src/Magento/FunctionalTestingFramework/Util/TestGenerator.php +++ b/src/Magento/FunctionalTestingFramework/Util/TestGenerator.php @@ -702,7 +702,7 @@ public function generateStepsPhp($actionObjects, $hookObject = false, $actor = " ); } - if (isset($storeCode)) { + if ($storeCode) { $createEntityFunctionCall .= sprintf("\"%s\");\n", $storeCode); } else { $createEntityFunctionCall .= ");\n"; @@ -743,13 +743,14 @@ public function generateStepsPhp($actionObjects, $hookObject = false, $actor = " $testSteps .= $contextSetter; $testSteps .= $deleteEntityFunctionCall; } else { + $url = $this->resolveEnvReferences([$url])[0]; + $url = $this->resolveTestVariable([$url], null)[0]; $output = sprintf( "\t\t$%s->deleteEntityByUrl(%s);\n", $actor, $url ); - $output = $this->resolveEnvReferences($output, [$url]); - $testSteps .= $this->resolveTestVariable($output, [$url], null); + $testSteps .= $output; } break; case "updateData": @@ -798,7 +799,7 @@ public function generateStepsPhp($actionObjects, $hookObject = false, $actor = " ); } - if (isset($storeCode)) { + if ($storeCode) { $updateEntityFunctionCall .= sprintf(", \"%s\");\n", $storeCode); } else { $updateEntityFunctionCall .= ");\n"; @@ -865,7 +866,7 @@ public function generateStepsPhp($actionObjects, $hookObject = false, $actor = " $getEntityFunctionCall .= 'null'; } - if (isset($storeCode)) { + if ($storeCode) { $getEntityFunctionCall .= sprintf(", \"%s\");\n", $storeCode); } else { $getEntityFunctionCall .= ");\n"; @@ -1222,9 +1223,10 @@ public function generateStepsPhp($actionObjects, $hookObject = false, $actor = " break; case "field": $fieldKey = $actionObject->getCustomActionAttributes()['key']; + $input = $this->resolveTestVariable([$input], $actionObject->getActionOrigin())[0]; $argRef = "\t\t\$"; $argRef .= str_replace(ucfirst($fieldKey), "", $stepKey) . "Fields['{$fieldKey}'] = ${input};\n"; - $testSteps .= $this->resolveTestVariable($argRef, [$input], $actionObject->getActionOrigin()); + $testSteps .= $argRef; break; default: $testSteps .= $this->wrapFunctionCall($actor, $actionObject, $selector, $input, $parameter); @@ -1253,19 +1255,16 @@ private function resolveLocatorFunctionInAttribute($attribute) * Resolves replacement of $input$ and $$input$$ in given function, recursing and replacing individual arguments * Also determines if each argument requires any quote replacement. * - * @param string $inputString * @param array $args * @param array $actionOrigin - * @return string + * @return array * @throws \Exception */ - private function resolveTestVariable($inputString, $args, $actionOrigin) + private function resolveTestVariable($args, $actionOrigin) { - $outputString = $inputString; - - //Loop through each argument, replace and then replace - foreach ($args as $arg) { - if ($arg == null) { + $newArgs = []; + foreach ($args as $key => $arg) { + if ($arg === null) { continue; } $outputArg = $arg; @@ -1282,10 +1281,10 @@ private function resolveTestVariable($inputString, $args, $actionOrigin) $outputArg = $this->resolveStepKeyReferences($outputArg, $actionOrigin); - $outputString = str_replace($arg, $outputArg, $outputString); + $newArgs[$key] = $outputArg; } - return $outputString; + return $newArgs; } /** @@ -1665,20 +1664,17 @@ private function wrapFunctionCall($actor, $action, ...$args) if (null === $args[$i]) { continue; } - if (!$isFirst) { - $output .= ', '; - } if ($args[$i] === "") { $args[$i] = '"' . $args[$i] . '"'; } - $output .= $args[$i]; - $isFirst = false; } - $output .= ");\n"; - - $output = $this->resolveEnvReferences($output, $args); - - return $this->resolveTestVariable($output, $args, $action->getActionOrigin()); + if (!is_array($args)) { + $args = [$args]; + } + $args = $this->resolveEnvReferences($args); + $args = $this->resolveTestVariable($args, $action->getActionOrigin()); + $output .= implode(", ", array_filter($args, function($value) { return $value !== null; })) . ");\n"; + return $output; } /** @@ -1699,36 +1695,32 @@ private function wrapFunctionCallWithReturnValue($returnVariable, $actor, $actio if (null === $args[$i]) { continue; } - if (!$isFirst) { - $output .= ', '; - } if ($args[$i] === "") { $args[$i] = '"' . $args[$i] . '"'; } - $output .= $args[$i]; - $isFirst = false; } - $output .= ");\n"; - - $output = $this->resolveEnvReferences($output, $args); - - return $this->resolveTestVariable($output, $args, $action->getActionOrigin()); + if (!is_array($args)) { + $args = [$args]; + } + $args = $this->resolveEnvReferences($args); + $args = $this->resolveTestVariable($args, $action->getActionOrigin()); + $output .= implode(", ", array_filter($args, function($value) { return $value !== null; })) . ");\n"; + return $output; } // @codingStandardsIgnoreEnd /** * Resolves {{_ENV.variable}} into getenv("variable") for test-runtime ENV referencing. - * @param string $inputString * @param array $args - * @return string + * @return array */ - private function resolveEnvReferences($inputString, $args) + private function resolveEnvReferences($args) { $envRegex = "/{{_ENV\.([\w]+)}}/"; - $outputString = $inputString; + $newArgs = []; - foreach ($args as $arg) { + foreach ($args as $key => $arg) { preg_match_all($envRegex, $arg, $matches); if (!empty($matches[0])) { $fullMatch = $matches[0][0]; @@ -1737,11 +1729,14 @@ private function resolveEnvReferences($inputString, $args) $replacement = "getenv(\"{$envVariable}\")"; $outputArg = $this->processQuoteBreaks($fullMatch, $arg, $replacement); - $outputString = str_replace($arg, $outputArg, $outputString); + $newArgs[$key] = $outputArg; + continue; } + $newArgs[$key] = $arg; } - return $outputString; + // override passed in args for use later. + return $newArgs; } /**