Skip to content

Commit 46ce94c

Browse files
committed
MQE-275: Implemented data uniqueness for input fields.
1 parent 28a3065 commit 46ce94c

File tree

9 files changed

+172
-9
lines changed

9 files changed

+172
-9
lines changed

src/Magento/AcceptanceTestFramework/DataGenerator/DataGeneratorConstants.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ class DataGeneratorConstants
1111
const DATA_VALUES = 'data';
1212
const DATA_ELEMENT_KEY = 'key';
1313
const DATA_ELEMENT_VALUE = 'value';
14+
const DATA_ELEMENT_UNIQUENESS_ATTR = 'unique';
15+
const DATA_ELEMENT_UNIQUENESS_ATTR_VALUE_PREFIX = 'prefix';
16+
const DATA_ELEMENT_UNIQUENESS_ATTR_VALUE_SUFFIX = 'suffix';
1417

1518
const ARRAY_VALUES = 'array';
1619
const ARRAY_ELEMENT_KEY = 'key';

src/Magento/AcceptanceTestFramework/DataGenerator/Managers/DataManager.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,17 @@ private function parseDataEntities()
8585
$dataValues = [];
8686
$linkedEntities = [];
8787
$arrayValues = [];
88+
$uniquenessValues = [];
8889

8990
if (array_key_exists(DataGeneratorConstants::DATA_VALUES, $entity)) {
9091
foreach ($entity[DataGeneratorConstants::DATA_VALUES] as $dataElement) {
9192
$dataElementKey = strtolower($dataElement[DataGeneratorConstants::DATA_ELEMENT_KEY]);
9293
$dataElementValue = $dataElement[DataGeneratorConstants::DATA_ELEMENT_VALUE];
94+
if (array_key_exists(DataGeneratorConstants::DATA_ELEMENT_UNIQUENESS_ATTR, $dataElement)) {
95+
$uniquenessValues[$dataElementKey] = $dataElement[
96+
DataGeneratorConstants::DATA_ELEMENT_UNIQUENESS_ATTR
97+
];
98+
}
9399

94100
$dataValues[$dataElementKey] = $dataElementValue;
95101
}
@@ -121,7 +127,8 @@ private function parseDataEntities()
121127
$entityName,
122128
$entityType,
123129
$dataValues,
124-
$linkedEntities
130+
$linkedEntities,
131+
$uniquenessValues
125132
);
126133

127134
$this->data[$entityXmlObject->getName()] = $entityXmlObject;

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

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,62 @@
33
namespace Magento\AcceptanceTestFramework\DataGenerator\Objects;
44

55
use Magento\AcceptanceTestFramework\DataGenerator\DataGeneratorConstants;
6-
use Magento\AcceptanceTestFramework\DataGenerator\Managers\EntityDataManager;
6+
use Magento\AcceptanceTestFramework\DataGenerator\Managers\DataManager;
77

88
class EntityDataObject
99
{
10+
/**
11+
* Entity name.
12+
*
13+
* @var string
14+
*/
1015
private $name;
16+
17+
/**
18+
* Entity type.
19+
*
20+
* @var string
21+
*/
1122
private $type;
12-
private $linkedEntities = []; //array of required entity name to corresponding type
13-
private $data = []; //array of Data Name to Data Value
23+
24+
/**
25+
* Array of required entity name to corresponding type.
26+
*
27+
* @var array
28+
*/
29+
private $linkedEntities = [];
30+
31+
/**
32+
* Array of data name to data value.
33+
*
34+
* @var array
35+
*/
36+
private $data = [];
37+
38+
/**
39+
* Array of data name and its uniqueness attribute value.
40+
*
41+
* @var array
42+
*/
43+
private $uniquenessData = [];
1444

1545
/**
1646
* EntityDataObject constructor.
1747
* @param string $entityName
1848
* @param string $entityType
1949
* @param array $data
2050
* @param array $linkedEntities
51+
* @param array $uniquenessData
2152
*/
22-
public function __construct($entityName, $entityType, $data, $linkedEntities)
53+
public function __construct($entityName, $entityType, $data, $linkedEntities, $uniquenessData = null)
2354
{
2455
$this->name = $entityName;
2556
$this->type = $entityType;
2657
$this->data = $data;
2758
$this->linkedEntities = $linkedEntities;
59+
if ($uniquenessData) {
60+
$this->uniquenessData = $uniquenessData;
61+
}
2862
}
2963

3064
public function getLinkedEntities()
@@ -68,7 +102,7 @@ public function getDataByName($dataName)
68102
*/
69103
public function getLinkedEntitiesOfType($fieldType)
70104
{
71-
$groupedArray = array();
105+
$groupedArray = [];
72106

73107
foreach ($this->linkedEntities as $entityName => $entityType) {
74108
if ($entityType == $fieldType) {
@@ -78,4 +112,21 @@ public function getLinkedEntitiesOfType($fieldType)
78112

79113
return $groupedArray;
80114
}
115+
116+
/**
117+
* This function retrieves uniqueness data by its name.
118+
*
119+
* @param string $dataName
120+
* @return string|null
121+
*/
122+
public function getUniquenessDataByName($dataName)
123+
{
124+
$name = strtolower($dataName);
125+
126+
if (array_key_exists($name, $this->uniquenessData)) {
127+
return $this->uniquenessData[$name];
128+
}
129+
130+
return null;
131+
}
81132
}

src/Magento/AcceptanceTestFramework/DataGenerator/etc/dataProfileSchema.xsd

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@
7272
</xs:documentation>
7373
</xs:annotation>
7474
</xs:attribute>
75+
<xs:attribute type="uniquenessEnumType" name="unique" use="optional">
76+
<xs:annotation>
77+
<xs:documentation>
78+
Add suite or test wide unique sequence as "prefix" or "suffix" to the data value if specified.
79+
</xs:documentation>
80+
</xs:annotation>
81+
</xs:attribute>
7582
</xs:extension>
7683
</xs:simpleContent>
7784
</xs:complexType>
@@ -103,4 +110,11 @@
103110
</xs:simpleContent>
104111
</xs:complexType>
105112

113+
<xs:simpleType name="uniquenessEnumType">
114+
<xs:restriction base="xs:string">
115+
<xs:enumeration value="prefix" />
116+
<xs:enumeration value="suffix" />
117+
</xs:restriction>
118+
</xs:simpleType>
119+
106120
</xs:schema>

src/Magento/AcceptanceTestFramework/Module/MagentoRestDriver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
namespace Magento\AcceptanceTestFramework\Module;
33

44
use Codeception\Module\REST;
5-
use Codeception\Module\Sequence;
5+
use Magento\AcceptanceTestFramework\Module\MagentoSequence;
66
use Flow\JSONPath;
77

88
/**
@@ -107,7 +107,7 @@ public function _beforeSuite($settings = [])
107107
$this->seeResponseCodeIs(\Codeception\Util\HttpCode::OK);
108108
$this->haveHttpHeader('Authorization', 'Bearer ' . $token);
109109
self::$adminTokens[$this->config['username']] = $token;
110-
$this->getModule('Sequence')->_initialize();
110+
$this->getModule('\Magento\AcceptanceTestFramework\Module\MagentoSequence')->_initialize();
111111
}
112112

113113
/**
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
namespace Magento\AcceptanceTestFramework\Module;
3+
4+
use Codeception\Module\Sequence;
5+
use Codeception\Exception\ModuleException;
6+
7+
/**
8+
* MagentoSequence module.
9+
*/
10+
class MagentoSequence extends Sequence
11+
{
12+
protected $config = ['prefix' => ''];
13+
}
14+
15+
if (!function_exists('msq') && !function_exists('msqs')) {
16+
require_once __DIR__ . '/../Util/msq.php';
17+
} else {
18+
throw new ModuleException('Magento\AcceptanceTestFramework\Module\MagentoSequence', "function 'msq' and 'msqs' already defined");
19+
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Magento\AcceptanceTestFramework\PageObject\Page\Page;
66
use Magento\AcceptanceTestFramework\PageObject\Section\Section;
77
use Magento\AcceptanceTestFramework\DataGenerator\Managers\DataManager;
8+
use Magento\AcceptanceTestFramework\DataGenerator\DataGeneratorConstants;
89

910
class ActionObject
1011
{
@@ -26,6 +27,7 @@ class ActionObject
2627
private $timeout;
2728

2829
const DATA_ENABLED_ATTRIBUTES = ["userInput", "parameterArray"];
30+
const UNIQUENESS_FUNCTION = 'msq';
2931

3032
/**
3133
* ActionObject constructor.
@@ -239,6 +241,14 @@ private function resolveDataInputReferences()
239241
return;
240242
}
241243

244+
$uniquenessData = $entityObj->getUniquenessDataByName($entityKey);
245+
246+
if ($uniquenessData == DataGeneratorConstants::DATA_ELEMENT_UNIQUENESS_ATTR_VALUE_PREFIX) {
247+
$replacement = self::UNIQUENESS_FUNCTION . '("' . $entityName . '.' . $entityKey . '")' . $replacement;
248+
} elseif ($uniquenessData == DataGeneratorConstants::DATA_ELEMENT_UNIQUENESS_ATTR_VALUE_SUFFIX) {
249+
$replacement .= self::UNIQUENESS_FUNCTION . '("' . $entityName . '.' . $entityKey . '")';
250+
}
251+
242252
$varInput = str_replace($reference, $replacement, $varInput);
243253
}
244254

src/Magento/AcceptanceTestFramework/Util/TestGenerator.php

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

55
use Magento\AcceptanceTestFramework\Test\Managers\CestArrayProcessor;
6+
use Magento\AcceptanceTestFramework\Test\Objects\ActionObject;
67

78
class TestGenerator
89
{
@@ -281,7 +282,33 @@ private function generateStepsPhp($stepsObject, $hookObject = false)
281282
if (isset($customActionAttributes['url']) && isset($customActionAttributes['userInput'])) {
282283
$input = sprintf("\"%s\"", $customActionAttributes['userInput']);
283284
} elseif (isset($customActionAttributes['userInput'])) {
284-
$input = sprintf("\"%s\"", $customActionAttributes['userInput']);
285+
preg_match(
286+
'/' . ActionObject::UNIQUENESS_FUNCTION .'\("[\w]+.[\w]+"\)/',
287+
$customActionAttributes['userInput'],
288+
$matches
289+
);
290+
if (!empty($matches)) {
291+
$parts = preg_split(
292+
'/' . ActionObject::UNIQUENESS_FUNCTION . '\("[\w]+.[\w]+"\)/',
293+
$customActionAttributes['userInput'],
294+
-1
295+
);
296+
foreach ($parts as $part) {
297+
if (!$part) {
298+
if (!empty($input)) {
299+
$input .= '.';
300+
}
301+
$input .= $matches[0];
302+
} else {
303+
if (!empty($input)) {
304+
$input .= '.';
305+
}
306+
$input .= sprintf("\"%s\"", $part);
307+
}
308+
}
309+
} else {
310+
$input = sprintf("\"%s\"", $customActionAttributes['userInput']);
311+
}
285312
} elseif (isset($customActionAttributes['url'])) {
286313
$input = sprintf("\"%s\"", $customActionAttributes['url']);
287314
} elseif (isset($customActionAttributes['time'])) {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
use Magento\AcceptanceTestFramework\Module\MagentoSequence;
3+
4+
if (!function_exists('msq')) {
5+
function msq($id = null)
6+
{
7+
if ($id and isset(MagentoSequence::$hash[$id])) {
8+
return MagentoSequence::$hash[$id];
9+
}
10+
$prefix = MagentoSequence::$prefix;
11+
$sequence = $prefix . uniqid($id);
12+
if ($id) {
13+
MagentoSequence::$hash[$id] = $sequence;
14+
}
15+
return $sequence;
16+
}
17+
}
18+
19+
if (!function_exists('msqs')) {
20+
function msqs($id = null)
21+
{
22+
if ($id and isset(MagentoSequence::$suiteHash[$id])) {
23+
return MagentoSequence::$suiteHash[$id];
24+
}
25+
$prefix = MagentoSequence::$prefix;
26+
$sequence = $prefix . uniqid($id);
27+
if ($id) {
28+
MagentoSequence::$suiteHash[$id] = $sequence;
29+
}
30+
return $sequence;
31+
}
32+
}

0 commit comments

Comments
 (0)