Skip to content

Commit a6de353

Browse files
author
Olexandr Lysenko
committed
MAGETWO-58896: Product Fields Auto-Generation does not work as expected
1 parent 75cbec4 commit a6de353

File tree

3 files changed

+170
-0
lines changed

3 files changed

+170
-0
lines changed

dev/tests/functional/tests/app/Magento/Catalog/Test/Repository/ConfigData.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,16 @@
3434
<item name="inherit" xsi:type="number">1</item>
3535
</field>
3636
</dataset>
37+
<dataset name="attribute_product_mask_sku">
38+
<field name="catalog/fields_masks/sku" xsi:type="array">
39+
<item name="value" xsi:type="string">{{name}} {{country_of_manufacture}}</item>
40+
</field>
41+
</dataset>
42+
<dataset name="attribute_product_mask_sku_rollback">
43+
<field name="catalog/fields_masks/sku" xsi:type="array">
44+
<item name="value" xsi:type="string">{{name}}</item>
45+
<item name="inherit" xsi:type="number">1</item>
46+
</field>
47+
</dataset>
3748
</repository>
3849
</config>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
/**
4+
* Copyright © 2016 Magento. 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/etc/variations.xsd">
9+
<testCase name="Magento\Catalog\Test\TestCase\Product\CreateSimpleProductEntityByAttributeMaskSkuTest" summary="Create Simple Product with attribute sku mask" ticketId="MAGETWO-59861">
10+
<variation name="CreateSimpleProductEntityByAttributeMaskSkuTest1">
11+
<data name="configData" xsi:type="string">attribute_product_mask_sku</data>
12+
<data name="description" xsi:type="string">Create product with country of manufacture attribute sku mask</data>
13+
<data name="product/data/url_key" xsi:type="string">simple-product-%isolation%</data>
14+
<data name="product/data/name" xsi:type="string">Simple Product %isolation%</data>
15+
<data name="product/data/price/value" xsi:type="string">10000</data>
16+
<data name="product/data/weight" xsi:type="string">50</data>
17+
<data name="product/data/quantity_and_stock_status/qty" xsi:type="string">657</data>
18+
<data name="product/data/country_of_manufacture" xsi:type="string">Ukraine</data>
19+
<constraint name="Magento\Catalog\Test\Constraint\AssertProductSaveMessage" />
20+
<constraint name="Magento\Catalog\Test\Constraint\AssertProductInGrid" />
21+
</variation>
22+
</testCase>
23+
</config>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Test\TestCase\Product;
8+
9+
use Magento\Catalog\Test\Fixture\CatalogProductSimple;
10+
use Magento\Catalog\Test\Page\Adminhtml\CatalogProductIndex;
11+
use Magento\Catalog\Test\Page\Adminhtml\CatalogProductNew;
12+
use Magento\Mtf\TestCase\Injectable;
13+
14+
/**
15+
* Steps:
16+
* 1. Login to the backend.
17+
* 2. Navigate to Products > Catalog.
18+
* 3. Start to create simple product.
19+
* 4. Fill in data according to data set.
20+
* 5. Save Product.
21+
* 6. Perform appropriate assertions.
22+
*
23+
* @group Products
24+
* @ZephyrId MAGETWO-59861
25+
*/
26+
class CreateSimpleProductEntityByAttributeMaskSkuTest extends Injectable
27+
{
28+
/**
29+
* Configuration setting.
30+
*
31+
* @var string
32+
*/
33+
protected $configData;
34+
35+
/**
36+
* Should cache be flushed
37+
*
38+
* @var bool
39+
*/
40+
private $flushCache;
41+
/**
42+
* @var \Magento\Mtf\Fixture\FixtureFactory
43+
*/
44+
private $fixtureFactory;
45+
46+
47+
/**
48+
* Run create product simple entity by attribute mask SKU test.
49+
*
50+
* @param CatalogProductSimple $product
51+
* @param CatalogProductIndex $productGrid
52+
* @param CatalogProductNew $newProductPage
53+
* @param string $configData
54+
* @param bool $flushCache
55+
* @return array
56+
*/
57+
public function testCreate(
58+
CatalogProductSimple $product,
59+
CatalogProductIndex $productGrid,
60+
CatalogProductNew $newProductPage,
61+
\Magento\Mtf\Fixture\FixtureFactory $fixtureFactory,
62+
$flushCache = false,
63+
$configData = null
64+
) {
65+
$this->configData = $configData;
66+
$this->flushCache = $flushCache;
67+
$this->fixtureFactory = $fixtureFactory;
68+
69+
// Preconditions
70+
$this->objectManager->create(
71+
\Magento\Config\Test\TestStep\SetupConfigurationStep::class,
72+
['configData' => $this->configData, 'flushCache' => $this->flushCache]
73+
)->run();
74+
75+
// Steps
76+
$productGrid->open();
77+
$productGrid->getGridPageActionBlock()->addProduct('simple');
78+
$newProductPage->getProductForm()->fill($product);
79+
$newProductPage->getFormPageActions()->save();
80+
81+
$data = $product->getData();
82+
$skuMask = $data['data']['sku'] = $this->prepareSkuByMask($product);
83+
84+
$productSimple = $fixtureFactory->createByCode(
85+
'catalogProductSimple',
86+
['data' => array_merge($product->getData(), ['sku' => $skuMask])]
87+
);
88+
89+
return ['product' => $productSimple];
90+
}
91+
92+
93+
/**
94+
* Obtains product sku based on attributes define in Stores > Configuration->Catalog > Catalog > Mask for SKU
95+
*
96+
* @param CatalogProductSimple $product
97+
* @return string
98+
*/
99+
private function prepareSkuByMask(CatalogProductSimple $product)
100+
{
101+
$productData = $product->getData();
102+
$skuMask = '';
103+
$config = $this->fixtureFactory->createByCode('configData', ['dataset' => $this->configData]);
104+
$section = $config->getData('section');
105+
if (is_array($section) && array_key_exists('catalog/fields_masks/sku', $section)) {
106+
$skuMask = $section['catalog/fields_masks/sku']['value'];
107+
}
108+
109+
$attributesInPattern = [];
110+
$count = preg_match_all('/{{(\w+)}}/', $skuMask, $matches);
111+
if ($count > 0 && is_array($matches[0])) {
112+
foreach($matches[1] as $attributeName) {
113+
if (array_key_exists($attributeName, $productData)) {
114+
$attributesInPattern[$attributeName] = $productData[$attributeName];
115+
}
116+
}
117+
}
118+
foreach($attributesInPattern as $attributeName => $attributeValue) {
119+
$skuMask = str_replace('{{'. $attributeName .'}}', $attributeValue, $skuMask);
120+
}
121+
return $skuMask;
122+
}
123+
124+
/**
125+
* Clean data after running test.
126+
*
127+
* @return void
128+
*/
129+
public function tearDown()
130+
{
131+
$this->objectManager->create(
132+
\Magento\Config\Test\TestStep\SetupConfigurationStep::class,
133+
['configData' => $this->configData, 'rollback' => true, 'flushCache' => $this->flushCache]
134+
)->run();
135+
}
136+
}

0 commit comments

Comments
 (0)