|
| 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