|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Elasticsearch\Test\Unit\Model\Adapter\FieldMapper; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\Data\ProductAttributeInterface; |
| 11 | +use Magento\Elasticsearch\Model\Adapter\FieldType; |
| 12 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; |
| 13 | + |
| 14 | +class ProductFieldMapperTest extends \PHPUnit\Framework\TestCase |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @var \Magento\Elasticsearch\Model\Adapter\FieldMapper\ProductFieldMapper |
| 18 | + */ |
| 19 | + protected $mapper; |
| 20 | + |
| 21 | + /** |
| 22 | + * @var \Magento\Eav\Model\Config|\PHPUnit_Framework_MockObject_MockObject |
| 23 | + */ |
| 24 | + protected $eavConfig; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject |
| 28 | + */ |
| 29 | + protected $coreRegistry; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var \Magento\Customer\Model\Session|\PHPUnit_Framework_MockObject_MockObject |
| 33 | + */ |
| 34 | + protected $customerSession; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject |
| 38 | + */ |
| 39 | + protected $storeManager; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute|\PHPUnit_Framework_MockObject_MockObject |
| 43 | + */ |
| 44 | + protected $eavAttributeResource; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var FieldType|\PHPUnit_Framework_MockObject_MockObject |
| 48 | + */ |
| 49 | + protected $fieldType; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject |
| 53 | + */ |
| 54 | + protected $store; |
| 55 | + |
| 56 | + /** |
| 57 | + * Set up test environment |
| 58 | + * |
| 59 | + * @return void |
| 60 | + */ |
| 61 | + protected function setUp() |
| 62 | + { |
| 63 | + $this->eavConfig = $this->getMockBuilder(\Magento\Eav\Model\Config::class) |
| 64 | + ->disableOriginalConstructor() |
| 65 | + ->setMethods(['getEntityType', 'getAttribute', 'getEntityAttributeCodes']) |
| 66 | + ->getMock(); |
| 67 | + |
| 68 | + $this->fieldType = $this->getMockBuilder(FieldType::class) |
| 69 | + ->disableOriginalConstructor() |
| 70 | + ->setMethods(['getFieldType']) |
| 71 | + ->getMock(); |
| 72 | + |
| 73 | + $this->customerSession = $this->getMockBuilder(\Magento\Customer\Model\Session::class) |
| 74 | + ->disableOriginalConstructor() |
| 75 | + ->setMethods(['getCustomerGroupId']) |
| 76 | + ->getMock(); |
| 77 | + |
| 78 | + $this->storeManager = $this->storeManager = $this->getMockForAbstractClass( |
| 79 | + \Magento\Store\Model\StoreManagerInterface::class, |
| 80 | + [], |
| 81 | + '', |
| 82 | + false |
| 83 | + ); |
| 84 | + |
| 85 | + $this->store = $this->getMockForAbstractClass( |
| 86 | + \Magento\Store\Api\Data\StoreInterface::class, |
| 87 | + [], |
| 88 | + '', |
| 89 | + false, |
| 90 | + false, |
| 91 | + true, |
| 92 | + ['getWebsiteId', 'getRootCategoryId'] |
| 93 | + ); |
| 94 | + |
| 95 | + $this->coreRegistry = $this->createMock(\Magento\Framework\Registry::class); |
| 96 | + |
| 97 | + $objectManager = new ObjectManagerHelper($this); |
| 98 | + |
| 99 | + $this->eavAttributeResource = $this->createPartialMock( |
| 100 | + \Magento\Catalog\Model\ResourceModel\Eav\Attribute::class, |
| 101 | + [ |
| 102 | + '__wakeup', |
| 103 | + 'getBackendType', |
| 104 | + 'getFrontendInput' |
| 105 | + ] |
| 106 | + ); |
| 107 | + |
| 108 | + $this->mapper = $objectManager->getObject( |
| 109 | + \Magento\Elasticsearch\Model\Adapter\FieldMapper\ProductFieldMapper::class, |
| 110 | + [ |
| 111 | + 'eavConfig' => $this->eavConfig, |
| 112 | + 'storeManager' => $this->storeManager, |
| 113 | + 'fieldType' => $this->fieldType, |
| 114 | + 'customerSession' => $this->customerSession, |
| 115 | + 'coreRegistry' => $this->coreRegistry |
| 116 | + ] |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * @dataProvider attributeCodeProvider |
| 122 | + * @param string $attributeCode |
| 123 | + * @param string $fieldName |
| 124 | + * @param string $fieldType |
| 125 | + * @param array $context |
| 126 | + * |
| 127 | + * @return void |
| 128 | + */ |
| 129 | + public function testGetFieldName($attributeCode, $fieldName, $fieldType, $context = []) |
| 130 | + { |
| 131 | + $attributeMock = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class) |
| 132 | + ->setMethods(['getBackendType', 'getFrontendInput', 'getAttribute']) |
| 133 | + ->disableOriginalConstructor() |
| 134 | + ->getMock(); |
| 135 | + |
| 136 | + $this->customerSession->expects($this->any()) |
| 137 | + ->method('getCustomerGroupId') |
| 138 | + ->willReturn('0'); |
| 139 | + |
| 140 | + $this->storeManager->expects($this->any()) |
| 141 | + ->method('getStore') |
| 142 | + ->willReturn($this->store); |
| 143 | + $this->store->expects($this->any()) |
| 144 | + ->method('getWebsiteId') |
| 145 | + ->willReturn('1'); |
| 146 | + $this->store->expects($this->any()) |
| 147 | + ->method('getRootCategoryId') |
| 148 | + ->willReturn('1'); |
| 149 | + |
| 150 | + $this->eavConfig->expects($this->any())->method('getAttribute') |
| 151 | + ->with(ProductAttributeInterface::ENTITY_TYPE_CODE, $attributeCode) |
| 152 | + ->willReturn($attributeMock); |
| 153 | + |
| 154 | + $attributeMock->expects($this->any())->method('getFrontendInput') |
| 155 | + ->will($this->returnValue('select')); |
| 156 | + |
| 157 | + $this->fieldType->expects($this->any())->method('getFieldType') |
| 158 | + ->with($attributeMock) |
| 159 | + ->willReturn($fieldType); |
| 160 | + |
| 161 | + $this->assertEquals( |
| 162 | + $fieldName, |
| 163 | + $this->mapper->getFieldName($attributeCode, $context) |
| 164 | + ); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * @return void |
| 169 | + */ |
| 170 | + public function testGetFieldNameWithoutAttribute() |
| 171 | + { |
| 172 | + $this->eavConfig->expects($this->any())->method('getAttribute') |
| 173 | + ->with(ProductAttributeInterface::ENTITY_TYPE_CODE, 'attr1') |
| 174 | + ->willReturn(''); |
| 175 | + |
| 176 | + $this->assertEquals( |
| 177 | + 'attr1', |
| 178 | + $this->mapper->getFieldName('attr1', []) |
| 179 | + ); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * @dataProvider attributeProvider |
| 184 | + * @param string $attributeCode |
| 185 | + * |
| 186 | + * @return void |
| 187 | + */ |
| 188 | + public function testGetAllAttributesTypes($attributeCode, $inputType, $searchAttributes, $expected) |
| 189 | + { |
| 190 | + $attributeMock = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class) |
| 191 | + ->disableOriginalConstructor() |
| 192 | + ->getMock(); |
| 193 | + |
| 194 | + $this->eavConfig->expects($this->any())->method('getEntityAttributeCodes') |
| 195 | + ->with(ProductAttributeInterface::ENTITY_TYPE_CODE) |
| 196 | + ->willReturn([$attributeCode]); |
| 197 | + |
| 198 | + $this->eavConfig->expects($this->any())->method('getAttribute') |
| 199 | + ->with(ProductAttributeInterface::ENTITY_TYPE_CODE, $attributeCode) |
| 200 | + ->willReturn($attributeMock); |
| 201 | + |
| 202 | + $this->fieldType->expects($this->once())->method('getFieldType')->willReturn(FieldType::ES_DATA_TYPE_INT); |
| 203 | + |
| 204 | + $attributeMock->expects($this->any()) |
| 205 | + ->method('getIsSearchable') |
| 206 | + ->willReturn($searchAttributes['searchable']); |
| 207 | + $attributeMock->expects($this->any()) |
| 208 | + ->method('getIsFilterable') |
| 209 | + ->willReturn($searchAttributes['filterable']); |
| 210 | + $attributeMock->expects($this->any()) |
| 211 | + ->method('getIsFilterableInSearch') |
| 212 | + ->willReturn($searchAttributes['filterableInSearch']); |
| 213 | + $attributeMock->expects($this->any()) |
| 214 | + ->method('getIsVisibleInAdvancedSearch') |
| 215 | + ->willReturn($searchAttributes['advSearch']); |
| 216 | + |
| 217 | + $attributeMock->expects($this->any())->method('getFrontendInput') |
| 218 | + ->will($this->returnValue($inputType)); |
| 219 | + |
| 220 | + $this->assertEquals( |
| 221 | + $expected, |
| 222 | + $this->mapper->getAllAttributesTypes() |
| 223 | + ); |
| 224 | + } |
| 225 | + |
| 226 | + /** |
| 227 | + * @return array |
| 228 | + */ |
| 229 | + public function attributeCodeProvider() |
| 230 | + { |
| 231 | + return [ |
| 232 | + ['id', 'id', 'string'], |
| 233 | + ['status', 'status', 'string'], |
| 234 | + ['status', 'status_value', 'string', ['type'=>'default']], |
| 235 | + ['price', 'price_0_1', 'string', ['type'=>'default']], |
| 236 | + ['position', 'position_category_1', 'string', ['type'=>'default']], |
| 237 | + ['price', 'price_2_3', 'string', ['type'=>'default', 'customerGroupId'=>'2', 'websiteId'=>'3']], |
| 238 | + ['position', 'position_category_3', 'string', ['type'=>'default', 'categoryId'=>'3']], |
| 239 | + ['color', 'color_value', 'string', ['type'=>'text']], |
| 240 | + ['description', 'sort_description', 'string', ['type'=>'some']], |
| 241 | + ['*', '_all', 'string', ['type'=>'text']], |
| 242 | + ['description', 'description_value', 'string', ['type'=>'text']], |
| 243 | + ]; |
| 244 | + } |
| 245 | + |
| 246 | + /** |
| 247 | + * @return array |
| 248 | + */ |
| 249 | + public function attributeProvider() |
| 250 | + { |
| 251 | + return [ |
| 252 | + [ |
| 253 | + 'category_ids', |
| 254 | + 'text', |
| 255 | + ['searchable' => false, 'filterable' => false, 'filterableInSearch' => false, 'advSearch' => false], |
| 256 | + ['category_ids' => ['type' => 'integer']] |
| 257 | + ], |
| 258 | + [ |
| 259 | + 'attr_code', |
| 260 | + 'string', |
| 261 | + ['searchable' => false, 'filterable' => false, 'filterableInSearch' => false, 'advSearch' => false], |
| 262 | + ['attr_code' => ['type' => 'integer', 'index' => 'no']] |
| 263 | + ], |
| 264 | + [ |
| 265 | + 'attr_code', |
| 266 | + 'string', |
| 267 | + ['searchable' => '0', 'filterable' => '0', 'filterableInSearch' => '0', 'advSearch' => '0'], |
| 268 | + ['attr_code' => ['type' => 'integer', 'index' => 'no']] |
| 269 | + ], |
| 270 | + [ |
| 271 | + 'attr_code', |
| 272 | + 'string', |
| 273 | + ['searchable' => true, 'filterable' => false, 'filterableInSearch' => false, 'advSearch' => false], |
| 274 | + ['attr_code' => ['type' => 'integer']] |
| 275 | + ], |
| 276 | + [ |
| 277 | + 'attr_code', |
| 278 | + 'string', |
| 279 | + ['searchable' => '1', 'filterable' => '0', 'filterableInSearch' => '0', 'advSearch' => '0'], |
| 280 | + ['attr_code' => ['type' => 'integer']] |
| 281 | + ], |
| 282 | + [ |
| 283 | + 'attr_code', |
| 284 | + 'string', |
| 285 | + ['searchable' => false, 'filterable' => false, 'filterableInSearch' => false, 'advSearch' => true], |
| 286 | + ['attr_code' => ['type' => 'integer']] |
| 287 | + ], |
| 288 | + [ |
| 289 | + 'attr_code', |
| 290 | + 'string', |
| 291 | + ['searchable' => '0', 'filterable' => '0', 'filterableInSearch' => '1', 'advSearch' => '0'], |
| 292 | + ['attr_code' => ['type' => 'integer']] |
| 293 | + ], |
| 294 | + ]; |
| 295 | + } |
| 296 | +} |
0 commit comments