|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2013-2017 Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\ConfigurableImportExport\Model\Import\Product\Type; |
| 7 | + |
| 8 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 9 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 10 | +use Magento\ImportExport\Model\Import; |
| 11 | + |
| 12 | +/** |
| 13 | + * @magentoAppArea adminhtml |
| 14 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 15 | + */ |
| 16 | +class ConfigurableTest extends \PHPUnit_Framework_TestCase |
| 17 | +{ |
| 18 | + /** |
| 19 | + * Configurable product test Name |
| 20 | + */ |
| 21 | + const TEST_PRODUCT_NAME = 'Configurable 1'; |
| 22 | + |
| 23 | + /** |
| 24 | + * Configurable product test Type |
| 25 | + */ |
| 26 | + const TEST_PRODUCT_TYPE = 'configurable'; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var \Magento\CatalogImportExport\Model\Import\Product |
| 30 | + */ |
| 31 | + protected $model; |
| 32 | + |
| 33 | + /** |
| 34 | + * Configurable product options SKU list |
| 35 | + * |
| 36 | + * @var array |
| 37 | + */ |
| 38 | + protected $optionSkuList = ['Configurable 1-Option 2-Option 1']; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var \Magento\Framework\ObjectManagerInterface |
| 42 | + */ |
| 43 | + protected $objectManager; |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | + protected function setUp() |
| 48 | + { |
| 49 | + $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
| 50 | + \Magento\CatalogImportExport\Model\Import\Product\Type\AbstractType::$commonAttributesCache = []; |
| 51 | + $this->model = $this->objectManager->create(\Magento\CatalogImportExport\Model\Import\Product::class); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @magentoDataFixture Magento/ConfigurableImportExport/Model/Import/_files/configurable_attributes.php |
| 56 | + * @magentoAppArea adminhtml |
| 57 | + */ |
| 58 | + public function testConfigurableWithAttributesSortingImport() |
| 59 | + { |
| 60 | + // import data from CSV file |
| 61 | + $pathToFile = __DIR__ . '/../../_files/import_configurable_with_attributes_sorting.csv'; |
| 62 | + $filesystem = $this->objectManager->create( |
| 63 | + \Magento\Framework\Filesystem::class |
| 64 | + ); |
| 65 | + |
| 66 | + $directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT); |
| 67 | + $source = $this->objectManager->create( |
| 68 | + \Magento\ImportExport\Model\Import\Source\Csv::class, |
| 69 | + [ |
| 70 | + 'file' => $pathToFile, |
| 71 | + 'directory' => $directory |
| 72 | + ] |
| 73 | + ); |
| 74 | + $errors = $this->model->setSource( |
| 75 | + $source |
| 76 | + )->setParameters( |
| 77 | + [ |
| 78 | + 'behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND, |
| 79 | + 'entity' => 'catalog_product' |
| 80 | + ] |
| 81 | + )->validateData(); |
| 82 | + |
| 83 | + $this->assertTrue($errors->getErrorsCount() == 0); |
| 84 | + $this->model->importData(); |
| 85 | + |
| 86 | + /** @var \Magento\Catalog\Model\ResourceModel\Product $resource */ |
| 87 | + $resource = $this->objectManager->get(\Magento\Catalog\Model\ResourceModel\Product::class); |
| 88 | + $productId = $resource->getIdBySku(self::TEST_PRODUCT_NAME); |
| 89 | + $this->assertTrue(is_numeric($productId)); |
| 90 | + /** @var \Magento\Catalog\Model\Product $product */ |
| 91 | + $product = $this->objectManager->get(ProductRepositoryInterface::class)->getById($productId); |
| 92 | + $configurableProductOptions = $product->getExtensionAttributes()->getConfigurableProductOptions(); |
| 93 | + |
| 94 | + $this->assertFalse($product->isObjectNew()); |
| 95 | + $this->assertEquals(self::TEST_PRODUCT_NAME, $product->getName()); |
| 96 | + $this->assertEquals(self::TEST_PRODUCT_TYPE, $product->getTypeId()); |
| 97 | + |
| 98 | + $optionCollection = $product->getTypeInstance()->getConfigurableOptions($product); |
| 99 | + foreach ($optionCollection as $option) { |
| 100 | + $this->assertContains($option[0]['sku'], $this->optionSkuList); |
| 101 | + } |
| 102 | + |
| 103 | + $optionIdList = $resource->getProductsIdsBySkus($this->optionSkuList); |
| 104 | + foreach ($optionIdList as $optionId) { |
| 105 | + $this->assertArrayHasKey($optionId, $product->getExtensionAttributes()->getConfigurableProductLinks()); |
| 106 | + } |
| 107 | + |
| 108 | + $this->assertEquals(2, count($optionCollection)); |
| 109 | + |
| 110 | + $attributesPositionExpectation = [ |
| 111 | + 'test_attribute_2' => 0, |
| 112 | + 'test_attribute_1' => 1, |
| 113 | + ]; |
| 114 | + |
| 115 | + /** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute $configurableProductOption */ |
| 116 | + foreach ($configurableProductOptions as $configurableProductOption){ |
| 117 | + $productAttribute = $configurableProductOption->getProductAttribute(); |
| 118 | + $productAttributeCode = $productAttribute->getAttributeCode(); |
| 119 | + $productAttributeData = $productAttribute->getData(); |
| 120 | + $productAttributeDataExpectation = self::getProductAttributesDataExpectation()[$productAttributeCode]; |
| 121 | + |
| 122 | + $productOptionData = $configurableProductOption->getData(); |
| 123 | + $productOptionLabel = $productOptionData['label']; |
| 124 | + $productOptionsDataExpectation = self::getProductOptionsDataExpectation()[$productOptionLabel]; |
| 125 | + |
| 126 | + if (isset($attributesPositionExpectation[$productAttributeCode])) { |
| 127 | + $expectedPosition = $attributesPositionExpectation[$productAttributeCode]; |
| 128 | + $this->assertEquals($expectedPosition, $configurableProductOption->getPosition()); |
| 129 | + } |
| 130 | + |
| 131 | + $this->assertArrayHasKey('product_super_attribute_id', $productOptionData); |
| 132 | + $this->assertArrayHasKey('product_id', $productOptionData); |
| 133 | + $this->assertArrayHasKey('attribute_id', $productOptionData); |
| 134 | + $this->assertArrayHasKey('position', $productOptionData); |
| 135 | + $this->assertArrayHasKey('extension_attributes', $productOptionData); |
| 136 | + $this->assertArrayHasKey('product_attribute', $productOptionData); |
| 137 | + $this->assertArrayHasKey('attribute_id', $productAttributeData); |
| 138 | + $this->assertArrayHasKey('entity_type_id', $productAttributeData); |
| 139 | + $this->assertArrayHasKey('attribute_code', $productAttributeData); |
| 140 | + $this->assertArrayHasKey('frontend_label', $productAttributeData); |
| 141 | + $this->assertArrayHasKey('label', $productOptionData); |
| 142 | + $this->assertArrayHasKey('use_default', $productOptionData); |
| 143 | + $this->assertArrayHasKey('options', $productOptionData); |
| 144 | + |
| 145 | + $this->assertEquals( |
| 146 | + $productAttributeDataExpectation['attribute_code'], |
| 147 | + $productAttributeData['attribute_code'] |
| 148 | + ); |
| 149 | + $this->assertEquals( |
| 150 | + $product->getData('entity_id'), |
| 151 | + $productOptionData['product_id']); |
| 152 | + $this->assertEquals( |
| 153 | + $productAttributeDataExpectation['frontend_label'], |
| 154 | + $productAttributeData['frontend_label'] |
| 155 | + ); |
| 156 | + $this->assertEquals( |
| 157 | + $productOptionsDataExpectation['label'], |
| 158 | + $productOptionData['label'] |
| 159 | + ); |
| 160 | + $this->assertEquals( |
| 161 | + $productOptionsDataExpectation['options']['label'], |
| 162 | + $productOptionData['options'][0]['label'] |
| 163 | + ); |
| 164 | + $this->assertEquals( |
| 165 | + $productOptionsDataExpectation['options']['default_label'], |
| 166 | + $productOptionData['options'][0]['default_label'] |
| 167 | + ); |
| 168 | + $this->assertEquals( |
| 169 | + $productOptionsDataExpectation['options']['store_label'], |
| 170 | + $productOptionData['options'][0]['store_label'] |
| 171 | + ); |
| 172 | + $this->assertArrayHasKey('values', $productOptionData); |
| 173 | + $valuesData = $productOptionData['values']; |
| 174 | + $this->assertEquals(1, count($valuesData)); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * @return array |
| 180 | + */ |
| 181 | + private function getProductOptionsDataExpectation() |
| 182 | + { |
| 183 | + return [ |
| 184 | + 'Test attribute 1' => [ |
| 185 | + 'label' => 'Test attribute 1', |
| 186 | + 'options' => [ |
| 187 | + 'label' => 'Option 1', |
| 188 | + 'default_label' => 'Option 1', |
| 189 | + 'store_label' => 'Option 1' |
| 190 | + ] |
| 191 | + ], |
| 192 | + |
| 193 | + |
| 194 | + 'Test attribute 2' => [ |
| 195 | + 'label' => 'Test attribute 2', |
| 196 | + 'options' => [ |
| 197 | + 'label' => 'Option 2', |
| 198 | + 'default_label' => 'Option 2', |
| 199 | + 'store_label' => 'Option 2' |
| 200 | + ] |
| 201 | + ] |
| 202 | + ]; |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * @return array |
| 207 | + */ |
| 208 | + private function getProductAttributesDataExpectation() |
| 209 | + { |
| 210 | + return [ |
| 211 | + 'test_attribute_1' => [ |
| 212 | + 'attribute_code' => 'test_attribute_1', |
| 213 | + 'frontend_label' => 'Test attribute 1' |
| 214 | + ], |
| 215 | + 'test_attribute_2' => [ |
| 216 | + 'attribute_code' => 'test_attribute_2', |
| 217 | + 'frontend_label' => 'Test attribute 2' |
| 218 | + ] |
| 219 | + ]; |
| 220 | + } |
| 221 | +} |
0 commit comments