Skip to content

Commit 45cad02

Browse files
committed
MQE-275: Implemented data uniqueness for input fields. (Resolved merge conflict)
(cherry picked from commit b2b8eca)
1 parent f1b1644 commit 45cad02

File tree

4 files changed

+109
-20
lines changed

4 files changed

+109
-20
lines changed

src/Magento/AcceptanceTestFramework/DataGenerator/Handlers/DataObjectHandler.php

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ class DataObjectHandler implements ObjectHandlerInterface
3333
const DATA_VALUES = 'data';
3434
const DATA_ELEMENT_KEY = 'key';
3535
const DATA_ELEMENT_VALUE = 'value';
36+
const DATA_ELEMENT_UNIQUENESS_ATTR = 'unique';
37+
const DATA_ELEMENT_UNIQUENESS_ATTR_VALUE_PREFIX = 'prefix';
38+
const DATA_ELEMENT_UNIQUENESS_ATTR_VALUE_SUFFIX = 'suffix';
3639

3740
const ARRAY_VALUES = 'array';
3841
const ARRAY_ELEMENT_KEY = 'key';
@@ -120,7 +123,12 @@ private function parseEnvVariables()
120123
}
121124
$envData[strtolower(trim($params[0]))] = trim($params[1]);
122125
}
123-
$envDataObject = new EntityDataObject(self::ENV_DATA_OBJECT_NAME, 'environment', $envData, null);
126+
$envDataObject = new EntityDataObject(
127+
self::ENV_DATA_OBJECT_NAME,
128+
'environment',
129+
$envData,
130+
null
131+
);
124132
$this->data[$envDataObject->getName()] = $envDataObject;
125133
}
126134
}
@@ -133,38 +141,42 @@ private function parseDataEntities()
133141
{
134142
$entities = $this->arrayData;
135143

136-
foreach ($entities[DataObjectHandler::ENTITY_DATA] as $entityName => $entity) {
137-
$entityType = $entity[DataObjectHandler::ENTITY_DATA_TYPE];
144+
foreach ($entities[self::ENTITY_DATA] as $entityName => $entity) {
145+
$entityType = $entity[self::ENTITY_DATA_TYPE];
138146

139147
$dataValues = [];
140148
$linkedEntities = [];
141149
$arrayValues = [];
142-
143-
if (array_key_exists(DataObjectHandler::DATA_VALUES, $entity)) {
144-
foreach ($entity[DataObjectHandler::DATA_VALUES] as $dataElement) {
145-
$dataElementKey = strtolower($dataElement[DataObjectHandler::DATA_ELEMENT_KEY]);
146-
$dataElementValue = $dataElement[DataObjectHandler::DATA_ELEMENT_VALUE];
150+
$uniquenessValues = [];
151+
152+
if (array_key_exists(self::DATA_VALUES, $entity)) {
153+
foreach ($entity[self::DATA_VALUES] as $dataElement) {
154+
$dataElementKey = strtolower($dataElement[self::DATA_ELEMENT_KEY]);
155+
$dataElementValue = $dataElement[self::DATA_ELEMENT_VALUE];
156+
if (array_key_exists(self::DATA_ELEMENT_UNIQUENESS_ATTR, $dataElement)) {
157+
$uniquenessValues[$dataElementKey] = $dataElement[self::DATA_ELEMENT_UNIQUENESS_ATTR];
158+
}
147159

148160
$dataValues[$dataElementKey] = $dataElementValue;
149161
}
150162
unset($dataElement);
151163
}
152164

153-
if (array_key_exists(DataObjectHandler::REQUIRED_ENTITY, $entity)) {
154-
foreach ($entity[DataObjectHandler::REQUIRED_ENTITY] as $linkedEntity) {
155-
$linkedEntityName = $linkedEntity[DataObjectHandler::REQUIRED_ENTITY_VALUE];
156-
$linkedEntityType = $linkedEntity[DataObjectHandler::REQUIRED_ENTITY_TYPE];
165+
if (array_key_exists(self::REQUIRED_ENTITY, $entity)) {
166+
foreach ($entity[self::REQUIRED_ENTITY] as $linkedEntity) {
167+
$linkedEntityName = $linkedEntity[self::REQUIRED_ENTITY_VALUE];
168+
$linkedEntityType = $linkedEntity[self::REQUIRED_ENTITY_TYPE];
157169

158170
$linkedEntities[$linkedEntityName] = $linkedEntityType;
159171
}
160172
unset($linkedEntity);
161173
}
162174

163-
if (array_key_exists(DataObjectHandler::ARRAY_VALUES, $entity)) {
164-
foreach ($entity[DataObjectHandler::ARRAY_VALUES] as $arrayElement) {
165-
$arrayKey = $arrayElement[DataObjectHandler::ARRAY_ELEMENT_KEY];
166-
foreach ($arrayElement[DataObjectHandler::ARRAY_ELEMENT_ITEM] as $arrayValue) {
167-
$arrayValues[] = $arrayValue[DataObjectHandler::ARRAY_ELEMENT_ITEM_VALUE];
175+
if (array_key_exists(self::ARRAY_VALUES, $entity)) {
176+
foreach ($entity[self::ARRAY_VALUES] as $arrayElement) {
177+
$arrayKey = $arrayElement[self::ARRAY_ELEMENT_KEY];
178+
foreach ($arrayElement[self::ARRAY_ELEMENT_ITEM] as $arrayValue) {
179+
$arrayValues[] = $arrayValue[self::ARRAY_ELEMENT_ITEM_VALUE];
168180
}
169181

170182
$dataValues[$arrayKey] = $arrayValues;
@@ -175,7 +187,8 @@ private function parseDataEntities()
175187
$entityName,
176188
$entityType,
177189
$dataValues,
178-
$linkedEntities
190+
$linkedEntities,
191+
$uniquenessValues
179192
);
180193

181194
$this->data[$entityDataObject->getName()] = $entityDataObject;

src/Magento/AcceptanceTestFramework/DataGenerator/Objects/EntityDataObject.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,30 @@ class EntityDataObject
3939
*/
4040
private $data = [];
4141

42+
/**
43+
* Array of data name and its uniqueness attribute value.
44+
*
45+
* @var array
46+
*/
47+
private $uniquenessData = [];
48+
4249
/**
4350
* EntityDataObject constructor.
4451
* @param string $entityName
4552
* @param string $entityType
4653
* @param array $data
4754
* @param array $linkedEntities
55+
* @param array $uniquenessData
4856
*/
49-
public function __construct($entityName, $entityType, $data, $linkedEntities)
57+
public function __construct($entityName, $entityType, $data, $linkedEntities, $uniquenessData = null)
5058
{
5159
$this->name = $entityName;
5260
$this->type = $entityType;
5361
$this->data = $data;
5462
$this->linkedEntities = $linkedEntities;
63+
if ($uniquenessData) {
64+
$this->uniquenessData = $uniquenessData;
65+
}
5566
}
5667

5768
/**
@@ -129,4 +140,21 @@ public function getLinkedEntitiesOfType($fieldType)
129140

130141
return $groupedArray;
131142
}
143+
144+
/**
145+
* This function retrieves uniqueness data by its name.
146+
*
147+
* @param string $dataName
148+
* @return string|null
149+
*/
150+
public function getUniquenessDataByName($dataName)
151+
{
152+
$name = strtolower($dataName);
153+
154+
if (array_key_exists($name, $this->uniquenessData)) {
155+
return $this->uniquenessData[$name];
156+
}
157+
158+
return null;
159+
}
132160
}

src/Magento/AcceptanceTestFramework/Test/Objects/ActionObject.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class ActionObject
2323
const ACTION_ATTRIBUTE_URL = 'url';
2424
const ACTION_ATTRIBUTE_SELECTOR = 'selector';
2525
const ACTION_ATTRIBUTE_VARIABLE_REGEX_PATTERN = '/{{[\w.]+}}/';
26+
const UNIQUENESS_FUNCTION = 'msq';
2627

2728
/**
2829
* The unique identifier for the action
@@ -288,6 +289,7 @@ private function findAndReplaceReferences($objectHandler, $inputString)
288289
case (get_class($obj) == EntityDataObject::class):
289290
list(,$objField) = $this->stripAndSplitReference($match);
290291
$replacement = $obj->getDataByName($objField);
292+
$replacement = $this->resolveEntityDataUniquenessReference($replacement, $obj, $objField);
291293
break;
292294
}
293295

@@ -302,4 +304,23 @@ private function findAndReplaceReferences($objectHandler, $inputString)
302304

303305
return $outputString;
304306
}
307+
308+
/**
309+
* @param string $reference
310+
* @param EntityDataObject $entityDataObject
311+
* @param string $entityKey
312+
* @return string
313+
*/
314+
private function resolveEntityDataUniquenessReference($reference, $entityDataObject, $entityKey)
315+
{
316+
$uniquenessData = $entityDataObject->getUniquenessDataByName($entityKey);
317+
$entityName = $entityDataObject->getName();
318+
319+
if ($uniquenessData == DataObjectHandler::DATA_ELEMENT_UNIQUENESS_ATTR_VALUE_PREFIX) {
320+
$reference = self::UNIQUENESS_FUNCTION . '("' . $entityName . '.' . $entityKey . '")' . $reference;
321+
} elseif ($uniquenessData == DataObjectHandler::DATA_ELEMENT_UNIQUENESS_ATTR_VALUE_SUFFIX) {
322+
$reference .= self::UNIQUENESS_FUNCTION . '("' . $entityName . '.' . $entityKey . '")';
323+
}
324+
return $reference;
325+
}
305326
}

src/Magento/AcceptanceTestFramework/Util/TestGenerator.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Magento\AcceptanceTestFramework\Util;
88

99
use Magento\AcceptanceTestFramework\Test\Handlers\CestObjectHandler;
10+
use Magento\AcceptanceTestFramework\Test\Objects\ActionObject;
1011

1112
class TestGenerator
1213
{
@@ -287,7 +288,33 @@ private function generateStepsPhp($stepsObject, $stepsData, $hookObject = false)
287288
} elseif (isset($customActionAttributes['url']) && isset($customActionAttributes['userInput'])) {
288289
$input = sprintf("\"%s\"", $customActionAttributes['userInput']);
289290
} elseif (isset($customActionAttributes['userInput'])) {
290-
$input = sprintf("\"%s\"", $customActionAttributes['userInput']);
291+
preg_match(
292+
'/' . ActionObject::UNIQUENESS_FUNCTION .'\("[\w]+.[\w]+"\)/',
293+
$customActionAttributes['userInput'],
294+
$matches
295+
);
296+
if (!empty($matches)) {
297+
$parts = preg_split(
298+
'/' . ActionObject::UNIQUENESS_FUNCTION . '\("[\w]+.[\w]+"\)/',
299+
$customActionAttributes['userInput'],
300+
-1
301+
);
302+
foreach ($parts as $part) {
303+
if (!$part) {
304+
if (!empty($input)) {
305+
$input .= '.';
306+
}
307+
$input .= $matches[0];
308+
} else {
309+
if (!empty($input)) {
310+
$input .= '.';
311+
}
312+
$input .= sprintf("\"%s\"", $part);
313+
}
314+
}
315+
} else {
316+
$input = sprintf("\"%s\"", $customActionAttributes['userInput']);
317+
}
291318
} elseif (isset($customActionAttributes['url'])) {
292319
$input = sprintf("\"%s\"", $customActionAttributes['url']);
293320
} elseif (isset($customActionAttributes['time'])) {

0 commit comments

Comments
 (0)