Skip to content

Commit ee6ee6e

Browse files
authored
Merge pull request #937 from magento-epam/PR-4
[Epam] Extend functional test sprint 4 - MTO-138: [Test] Export Advanced Pricing - MTO-127: [Test] Import Advanced Pricing
2 parents 535860a + decd746 commit ee6ee6e

40 files changed

+1769
-94
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\AdvancedPricingImportExport\Test\Constraint;
7+
8+
use Magento\Mtf\Constraint\AbstractConstraint;
9+
use Magento\Mtf\Fixture\InjectableFixture;
10+
use Magento\Mtf\Util\Command\File\ExportInterface;
11+
12+
/**
13+
* Assert that exported file with advanced pricing options contains product data.
14+
*/
15+
class AssertExportAdvancedPricing extends AbstractConstraint
16+
{
17+
/**
18+
* Export data.
19+
*
20+
* @var array
21+
*/
22+
private $exportData;
23+
24+
/**
25+
* Assert that exported file with advanced pricing options contains product data.
26+
*
27+
* @param ExportInterface $export
28+
* @param array $products
29+
* @param array $exportedFields
30+
* @return void
31+
*/
32+
public function processAssert(
33+
ExportInterface $export,
34+
array $products,
35+
array $exportedFields
36+
) {
37+
$this->exportData = $export->getLatest();
38+
foreach ($products as $product) {
39+
$regexps = $this->prepareRegexpsForCheck($exportedFields, $product);
40+
\PHPUnit_Framework_Assert::assertTrue(
41+
$this->isProductDataExists($regexps),
42+
'A product with name ' . $product->getName() . ' was not found in exported file.'
43+
);
44+
}
45+
}
46+
47+
/**
48+
* Returns a string representation of the object.
49+
*
50+
* @return string
51+
*/
52+
public function toString()
53+
{
54+
return 'A product(s) with correct data was found in exported file.';
55+
}
56+
57+
/**
58+
* Prepare regular expressions for product data in exported file.
59+
*
60+
* @param array $fields
61+
* @param InjectableFixture $product
62+
* @return array
63+
*/
64+
private function prepareRegexpsForCheck(
65+
array $fields,
66+
InjectableFixture $product
67+
) {
68+
$regexpsForCheck = [];
69+
$tierPrices = count($product->getData()['tier_price']);
70+
for ($i = 0; $i < $tierPrices; $i++) {
71+
$regexp = '/';
72+
foreach ($fields as $field) {
73+
if (strpos($field, 'tier_price') !== false) {
74+
$replace = ($field == 'tier_price' || $field == 'tier_price_qty') ? 'tier_' : 'tier_price_';
75+
$regexp .= preg_replace(
76+
'/[\[\]]/',
77+
'.*',
78+
'.*(' . $product->getData()['tier_price'][$i][str_replace($replace, '', $field)] . ')'
79+
);
80+
} else {
81+
$regexp .= '.*(' . $product->getData($field) . ').*';
82+
}
83+
}
84+
$regexp .= '/U';
85+
86+
$regexpsForCheck[] = $regexp;
87+
}
88+
89+
return $regexpsForCheck;
90+
}
91+
92+
/**
93+
* Check product data existing in exported file.
94+
*
95+
* @param array $data
96+
* @return bool
97+
*/
98+
private function isProductDataExists(array $data)
99+
{
100+
foreach ($data as $regexp) {
101+
preg_match($regexp, $this->exportData->getContent(), $matches);
102+
if (empty($matches)) {
103+
return false;
104+
}
105+
}
106+
107+
return true;
108+
}
109+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\AdvancedPricingImportExport\Test\Constraint;
8+
9+
use Magento\Mtf\Constraint\AbstractConstraint;
10+
use Magento\Catalog\Test\Page\Adminhtml\CatalogProductEdit;
11+
use Magento\ImportExport\Test\Fixture\ImportData;
12+
13+
/**
14+
* Check imported advanced prices are correct.
15+
*/
16+
class AssertImportAdvancedPricing extends AbstractConstraint
17+
{
18+
/**
19+
* Array keys mapping for csv file.
20+
*
21+
* @var array
22+
*/
23+
private $mappingData = [
24+
'sku' => 'sku',
25+
'tier_price' => 'price',
26+
'tier_price_qty' => 'price_qty',
27+
'tier_price_website' => 'website',
28+
'tier_price_customer_group' => 'customer_group',
29+
'tier_price_value_type' => 'value_type'
30+
];
31+
32+
/**
33+
* Edit page on backend.
34+
*
35+
* @var CatalogProductEdit
36+
*/
37+
private $catalogProductEdit;
38+
39+
/**
40+
* Import fixture.
41+
*
42+
* @var ImportData
43+
*/
44+
private $import;
45+
46+
/**
47+
* Assert imported advanced prices are correct.
48+
*
49+
* @param CatalogProductEdit $catalogProductEdit
50+
* @param ImportData $import
51+
* @return void
52+
*/
53+
public function processAssert(
54+
CatalogProductEdit $catalogProductEdit,
55+
ImportData $import
56+
) {
57+
$this->catalogProductEdit = $catalogProductEdit;
58+
$this->import = $import;
59+
60+
$resultArrays = $this->getPreparePrices();
61+
62+
\PHPUnit_Framework_Assert::assertEquals(
63+
$resultArrays['pageData'],
64+
$resultArrays['csvData'],
65+
'Tier prices from page and csv are not match.'
66+
);
67+
}
68+
69+
/**
70+
* Prepare arrays for compare.
71+
*
72+
* @return array
73+
*/
74+
private function getPreparePrices()
75+
{
76+
$products = $this->import->getDataFieldConfig('import_file')['source']->getEntities();
77+
78+
// Prepare tier prices data from page form.
79+
$resultProductArray = [];
80+
foreach ($products as $product) {
81+
$this->catalogProductEdit->open(['id' => $product->getId()]);
82+
$advancedPricing = $this->catalogProductEdit->getProductForm()->openSection('advanced-pricing')
83+
->getSection('advanced-pricing');
84+
$tierPrices = $advancedPricing->getTierPriceForm()->getFieldsData();
85+
$productSku = $product->getSku();
86+
foreach ($tierPrices as $tierPrice) {
87+
$resultProductArray[$productSku][] = $tierPrice;
88+
}
89+
}
90+
91+
// Prepare tier prices data from csv file.
92+
$resultCsvArray = [];
93+
if ($this->import->getBehavior() !== 'Delete') {
94+
$resultCsvArray = $this->getResultCsv();
95+
}
96+
97+
return ['pageData' => $resultProductArray, 'csvData' => $resultCsvArray];
98+
}
99+
100+
/**
101+
* Prepare array from csv file.
102+
*
103+
* @return array
104+
*/
105+
private function getResultCsv()
106+
{
107+
$csvData = $this->import->getDataFieldConfig('import_file')['source']->getCsv();
108+
109+
$csvKeys = [];
110+
foreach (array_shift($csvData) as $csvKey) {
111+
$csvKeys[] = isset($this->mappingData[$csvKey]) ? $this->mappingData[$csvKey] : $csvKey;
112+
}
113+
114+
$resultCsvData = [];
115+
foreach ($csvData as $csvRowData) {
116+
$csvRowData = array_combine($csvKeys, $csvRowData);
117+
$sku = $csvRowData['sku'];
118+
unset($csvRowData['sku']);
119+
$resultCsvData[$sku][] = $csvRowData;
120+
}
121+
return $resultCsvData;
122+
}
123+
124+
/**
125+
* Return string representation of object.
126+
*
127+
* @return string
128+
*/
129+
public function toString()
130+
{
131+
return 'Imported advanced prices are correct.';
132+
}
133+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" ?>
2+
<!--
3+
/**
4+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../vendor/magento/mtf/Magento/Mtf/Repository/etc/repository.xsd">
9+
<repository class="Magento\ImportExport\Test\Repository\ExportData">
10+
<dataset name="csv_with_advanced_pricing">
11+
<field name="entity" xsi:type="string">Advanced Pricing</field>
12+
<field name="file_format" xsi:type="string">CSV</field>
13+
</dataset>
14+
</repository>
15+
</config>

0 commit comments

Comments
 (0)