Skip to content

MQE-905: Persistent entity variable not interpolated with page urls with type "admin" #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

<fillField stepKey="inputReplace" selector="#selector" userInput="StringBefore $createdData.firstname$ StringAfter"/>
<fillField stepKey="selectorReplace" selector="#$createdData.firstname$" userInput="input"/>
<fillField stepKey="selectorReplace2" selector="#{{_ENV.MAGENTO_BASE_URL}}#$createdData.firstname$" userInput="input"/>
<dragAndDrop stepKey="selector12Replace" selector1="#$createdData.firstname$" selector2="$createdData.lastname$"/>
<conditionalClick stepKey="dependentSelectorReplace" dependentSelector="#$createdData.firstname$" selector="$createdData.lastname$" visible="true"/>
<amOnUrl stepKey="urlReplace" url="$createdData.firstname$.html"/>
Expand Down
81 changes: 38 additions & 43 deletions src/Magento/FunctionalTestingFramework/Util/TestGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ public function generateStepsPhp($actionObjects, $hookObject = false, $actor = "
);
}

if (isset($storeCode)) {
if ($storeCode) {
$createEntityFunctionCall .= sprintf("\"%s\");\n", $storeCode);
} else {
$createEntityFunctionCall .= ");\n";
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -798,7 +799,7 @@ public function generateStepsPhp($actionObjects, $hookObject = false, $actor = "
);
}

if (isset($storeCode)) {
if ($storeCode) {
$updateEntityFunctionCall .= sprintf(", \"%s\");\n", $storeCode);
} else {
$updateEntityFunctionCall .= ");\n";
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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];
Expand All @@ -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;
}

/**
Expand Down