public function testRefreshSpecialPrices() { $idsToProcess = [1, 2, 3]; $this->metadataPool->expects($this->atLeastOnce())->method('getMetadata')->willReturn($this->metadataMock); $this->metadataMock->expects($this->atLeastOnce())->method('getLinkField')->willReturn('row_id'); $this->metadataMock->expects($this->atLeastOnce())->method('getIdentifierField')->willReturn('entity_id'); $selectMock = $this->getMock('Magento\\Framework\\DB\\Select', [], [], '', false); $selectMock->expects($this->any())->method('from')->will($this->returnSelf()); $selectMock->expects($this->any())->method('joinLeft')->will($this->returnSelf()); $selectMock->expects($this->any())->method('where')->will($this->returnSelf()); $connectionMock = $this->getMock('Magento\\Framework\\DB\\Adapter\\AdapterInterface', [], [], '', false); $connectionMock->expects($this->any())->method('select')->will($this->returnValue($selectMock)); $connectionMock->expects($this->any())->method('fetchCol')->will($this->returnValue($idsToProcess)); $this->_resourceMock->expects($this->once())->method('getConnection')->will($this->returnValue($connectionMock)); $this->_resourceMock->expects($this->any())->method('getTableName')->will($this->returnValue('category')); $storeMock = $this->getMock('\\Magento\\Store\\Model\\Store', [], [], '', false); $storeMock->expects($this->any())->method('getId')->will($this->returnValue(1)); $this->_storeManagerMock->expects($this->once())->method('getStores')->with(true)->will($this->returnValue([$storeMock])); $this->_localeDateMock->expects($this->once())->method('scopeTimeStamp')->with($storeMock)->will($this->returnValue(32000)); $indexerMock = $this->getMock('Magento\\Indexer\\Model\\Indexer', [], [], '', false); $indexerMock->expects($this->exactly(2))->method('reindexList'); $this->_priceProcessorMock->expects($this->exactly(2))->method('getIndexer')->will($this->returnValue($indexerMock)); $attributeMock = $this->getMockForAbstractClass('Magento\\Eav\\Model\\Entity\\Attribute\\AbstractAttribute', [], '', false, true, true, ['__wakeup', 'getAttributeId']); $attributeMock->expects($this->any())->method('getAttributeId')->will($this->returnValue(1)); $this->_eavConfigMock->expects($this->any())->method('getAttribute')->will($this->returnValue($attributeMock)); $this->_model->execute(); }
/** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testGetConfigurableOptions() { $context = $this->getMockBuilder(Context::class)->disableOriginalConstructor()->getMock(); /** @var Configurable|\PHPUnit_Framework_MockObject_MockObject $configurable */ $configurable = $this->getMockBuilder(Configurable::class)->setMethods(['getTable', 'getConnection'])->setConstructorArgs([$context, $this->relation])->getMock(); $reflection = new \ReflectionClass(Configurable::class); $reflectionProperty = $reflection->getProperty('metadataPool'); $reflectionProperty->setAccessible(true); $reflectionProperty->setValue($configurable, $this->metadataPoolMock); /** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject $product */ $product = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)->setMethods(['__sleep', '__wakeup', 'getData'])->disableOriginalConstructor()->getMock(); $this->metadataMock->expects($this->once())->method('getLinkField')->willReturn('link'); $product->expects($this->once())->method('getData')->with('link')->willReturn('getId value'); $configurable->expects($this->exactly(7))->method('getTable')->will($this->returnValueMap([['catalog_product_super_attribute', 'catalog_product_super_attribute value'], ['catalog_product_entity', 'catalog_product_entity value'], ['catalog_product_super_link', 'catalog_product_super_link value'], ['eav_attribute', 'eav_attribute value'], ['catalog_product_entity', 'catalog_product_entity value'], ['eav_attribute_option_value', 'eav_attribute_option_value value'], ['catalog_product_super_attribute_label', 'catalog_product_super_attribute_label value']])); $select = $this->getMock(\Magento\Framework\DB\Select::class, ['from', 'joinInner', 'joinLeft', 'where'], [], '', false); $select->expects($this->once())->method('from')->with(['super_attribute' => 'catalog_product_super_attribute value'], ['sku' => 'entity.sku', 'product_id' => 'product_entity.entity_id', 'attribute_code' => 'attribute.attribute_code', 'option_title' => 'option_value.value', 'super_attribute_label' => 'attribute_label.value'])->will($this->returnSelf()); $superAttribute = $this->getMock(\Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute::class, ['getBackendTable', 'getAttributeId'], [], '', false); $superAttribute->expects($this->once())->method('getBackendTable')->willReturn('getBackendTable value'); $superAttribute->expects($this->once())->method('getAttributeId')->willReturn('getAttributeId value'); $attributes = [$superAttribute]; $select->expects($this->exactly(5))->method('joinInner')->will($this->returnSelf())->withConsecutive([['product_entity' => 'catalog_product_entity value'], 'product_entity.link = super_attribute.product_id', []], [['product_link' => 'catalog_product_super_link value'], 'product_link.parent_id = super_attribute.product_id', []], [['attribute' => 'eav_attribute value'], 'attribute.attribute_id = super_attribute.attribute_id', []], [['entity' => 'catalog_product_entity value'], 'entity.entity_id = product_link.product_id', []], [['entity_value' => 'getBackendTable value'], implode(' AND ', ['entity_value.attribute_id = super_attribute.attribute_id', 'entity_value.store_id = 0', 'entity_value.link = entity.link']), []]); $select->expects($this->exactly(2))->method('joinLeft')->will($this->returnSelf())->withConsecutive([['option_value' => 'eav_attribute_option_value value'], implode(' AND ', ['option_value.option_id = entity_value.value', 'option_value.store_id = ' . \Magento\Store\Model\Store::DEFAULT_STORE_ID]), []], [['attribute_label' => 'catalog_product_super_attribute_label value'], implode(' AND ', ['super_attribute.product_super_attribute_id = attribute_label.product_super_attribute_id', 'attribute_label.store_id = ' . \Magento\Store\Model\Store::DEFAULT_STORE_ID]), []]); $select->expects($this->once())->method('where')->will($this->returnSelf())->with('super_attribute.product_id = ?', 'getId value'); $readerAdapter = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)->setMethods(['select', 'fetchAll'])->disableOriginalConstructor()->getMockForAbstractClass(); $readerAdapter->expects($this->once())->method('select')->willReturn($select); $readerAdapter->expects($this->once())->method('fetchAll')->with($select)->willReturn('fetchAll value'); $configurable->expects($this->exactly(2))->method('getConnection')->willReturn($readerAdapter); $expectedAttributesOptionsData = ['getAttributeId value' => 'fetchAll value']; $actualAttributesOptionsData = $configurable->getConfigurableOptions($product, $attributes); $this->assertEquals($expectedAttributesOptionsData, $actualAttributesOptionsData); }
/** * @param \Magento\Catalog\Model\Product $product * @return $this */ protected function duplicate($product) { $mediaGalleryData = $product->getData($this->getAttribute()->getAttributeCode()); if (!isset($mediaGalleryData['images']) || !is_array($mediaGalleryData['images'])) { return $this; } $this->resourceModel->duplicate($this->getAttribute()->getAttributeId(), isset($mediaGalleryData['duplicate']) ? $mediaGalleryData['duplicate'] : [], $product->getOriginalId(), $product->getData($this->metadata->getLinkField())); return $this; }
/** * Returns product images in specific stores. * * @param \Magento\Catalog\Model\Product $product * @param int|array $storeIds * @return array */ public function getProductImages($product, $storeIds) { if (!is_array($storeIds)) { $storeIds = [$storeIds]; } $mainTable = $product->getResource()->getAttribute('image')->getBackend()->getTable(); $select = $this->getConnection()->select()->from(['images' => $mainTable], ['value as filepath', 'store_id'])->joinLeft(['attr' => $this->getTable('eav_attribute')], 'images.attribute_id = attr.attribute_id', ['attribute_code'])->where($this->metadata->getLinkField() . ' = ?', $product->getData($this->metadata->getLinkField()))->where('store_id IN (?)', $storeIds)->where('attribute_code IN (?)', ['small_image', 'thumbnail', 'image']); return $this->getConnection()->fetchAll($select); }
/** * @param AdapterInterface $connection * @param string $attributeTableName * @return array * @throws \Zend_Db_Statement_Exception */ private function getAffectedAttributeIds(AdapterInterface $connection, $attributeTableName) { $linkField = $this->metadata->getLinkField(); $select = $connection->select()->reset(); $select->from(['e' => $this->attributeResource->getTable('catalog_product_entity')], 'ei.value_id'); $select->join(['ei' => $attributeTableName], 'ei.' . $linkField . ' = e.' . $linkField . ' AND ei.store_id != 0', ''); $select->join(['s' => $this->attributeResource->getTable('store')], 's.store_id = ei.store_id', ''); $select->join(['sg' => $this->attributeResource->getTable('store_group')], 'sg.group_id = s.group_id', ''); $select->joinLeft(['pw' => $this->attributeResource->getTable('catalog_product_website')], 'pw.website_id = sg.website_id AND pw.product_id = e.entity_id', ''); $select->where('pw.product_id is null'); return $connection->fetchCol($select); }
/** * Return all product children ids * * @param int $productId Product Entity Id * @param string $typeId Super Product Link Type * @return array|null */ public function getProductChildIds($productId, $typeId) { $typeInstance = $this->getProductTypeInstance($typeId); $relation = $typeInstance->isComposite($this->getProductEmulator($typeId)) ? $typeInstance->getRelationInfo() : false; if ($relation && $relation->getTable() && $relation->getParentFieldName() && $relation->getChildFieldName()) { $select = $this->connection->select()->from(['main' => $this->getTable($relation->getTable())], [$relation->getChildFieldName()]); $select->join(['e' => $this->resource->getTableName('catalog_product_entity')], 'e.' . $this->metadata->getLinkField() . ' = main.' . $relation->getParentFieldName())->where('e.entity_id = ?', $productId); if ($relation->getWhere() !== null) { $select->where($relation->getWhere()); } return $this->connection->fetchCol($select); } return null; }
public function testGetConfigurableAttributes() { $expectedData = [1]; $configurableAttributes = '_cache_instance_configurable_attributes'; /** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject $product */ $product = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->setMethods(['getData', 'hasData', 'setData', 'getIdentities', 'getId', 'getStoreId'])->disableOriginalConstructor()->getMock(); $product->expects($this->once())->method('hasData')->with($configurableAttributes)->willReturn(false); $product->expects($this->once())->method('setData')->willReturnSelf(); $product->expects($this->exactly(2))->method('getData')->willReturnMap([[$configurableAttributes, null, $expectedData], ['link', null, 1]]); $product->expects($this->once())->method('getIdentities')->willReturn([1, 2, 3]); $this->entityMetadata->expects($this->once())->method('getLinkField')->willReturn('link'); $attributeCollection = $this->getMockBuilder('Magento\\ConfigurableProduct\\Model\\ResourceModel\\Product\\Type\\Configurable\\Attribute\\Collection')->setMethods(['setProductFilter', 'orderByPosition', 'load'])->disableOriginalConstructor()->getMock(); $attributeCollection->expects($this->any())->method('setProductFilter')->will($this->returnSelf()); $attributeCollection->expects($this->any())->method('orderByPosition')->will($this->returnSelf()); $this->_attributeCollectionFactory->expects($this->any())->method('create')->willReturn($attributeCollection); $this->extensionAttributesJoinProcessorMock->expects($this->once())->method('process')->with($this->isInstanceOf('Magento\\ConfigurableProduct\\Model\\ResourceModel\\Product\\Type\\Configurable\\Attribute\\Collection')); $this->assertEquals($expectedData, $this->_model->getConfigurableAttributes($product)); }
/** * @param EntityMetadata $metadata * @param array $data * @return array */ protected function prepareData(EntityMetadata $metadata, $data) { $output = []; foreach ($metadata->getEntityConnection()->describeTable($metadata->getEntityTable()) as $column) { if ($column['DEFAULT'] == 'CURRENT_TIMESTAMP') { continue; } if (isset($data[strtolower($column['COLUMN_NAME'])])) { $output[strtolower($column['COLUMN_NAME'])] = $data[strtolower($column['COLUMN_NAME'])]; } elseif ($column['DEFAULT'] === null) { $output[strtolower($column['COLUMN_NAME'])] = null; } } if (empty($data[$metadata->getIdentifierField()])) { $output[$metadata->getIdentifierField()] = $metadata->generateIdentifier(); } return $output; }
public function testRemoveChild() { $this->productRepository->expects($this->any())->method('get')->will($this->returnValue($this->product)); $bundle = $this->getMock('\\Magento\\Bundle\\Model\\ResourceModel\\Bundle', [], [], '', false); $this->bundleFactoryMock->expects($this->once())->method('create')->will($this->returnValue($bundle)); $productSku = 'productSku'; $optionId = 1; $productId = 1; $childSku = 'childSku'; $this->product->expects($this->any())->method('getTypeId')->will($this->returnValue(\Magento\Catalog\Model\Product\Type::TYPE_BUNDLE)); $this->getRemoveOptions(); $selection = $this->getMockBuilder('\\Magento\\Bundle\\Model\\Selection')->setMethods(['getSku', 'getOptionId', 'getSelectionId', 'getProductId', '__wakeup'])->disableOriginalConstructor()->getMock(); $selection->expects($this->any())->method('getSku')->will($this->returnValue($childSku)); $selection->expects($this->any())->method('getOptionId')->will($this->returnValue($optionId)); $selection->expects($this->any())->method('getSelectionId')->will($this->returnValue(55)); $selection->expects($this->any())->method('getProductId')->willReturn($productId); $this->option->expects($this->any())->method('getSelections')->will($this->returnValue([$selection])); $this->metadataMock->expects($this->any())->method('getLinkField')->willReturn($this->linkField); $this->product->expects($this->any())->method('getData')->with($this->linkField)->willReturn(3); $bundle->expects($this->once())->method('dropAllUnneededSelections')->with(3, []); $bundle->expects($this->once())->method('removeProductRelations')->with(3, [$productId]); //Params come in lowercase to method $this->assertTrue($this->model->removeChild($productSku, $optionId, $childSku)); }
/** * @magentoAppArea adminhtml * @magentoDbIsolation enabled * @magentoAppIsolation enabled * * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testDownloadableImport() { // import data from CSV file $pathToFile = __DIR__ . '/../../_files/import_downloadable.csv'; $filesystem = $this->objectManager->create(\Magento\Framework\Filesystem::class); $directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT); $source = $this->objectManager->create(\Magento\ImportExport\Model\Import\Source\Csv::class, ['file' => $pathToFile, 'directory' => $directory]); $errors = $this->model->setSource($source)->setParameters(['behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND, 'entity' => 'catalog_product'])->validateData(); $this->assertTrue($errors->getErrorsCount() == 0); $this->model->importData(); $resource = $this->objectManager->get(\Magento\Catalog\Model\ResourceModel\Product::class); $productId = $resource->getIdBySku(self::TEST_PRODUCT_NAME); $this->assertTrue(is_numeric($productId)); /** @var \Magento\Catalog\Model\Product $product */ $product = $this->objectManager->create(\Magento\Catalog\Model\Product::class); $product->load($productId); $this->assertFalse($product->isObjectNew()); $this->assertEquals(self::TEST_PRODUCT_NAME, $product->getName()); $this->assertEquals(self::TEST_PRODUCT_TYPE, $product->getTypeId()); $downloadableProductLinks = $product->getExtensionAttributes()->getDownloadableProductLinks(); $downloadableLinks = $product->getDownloadableLinks(); $downloadableProductSamples = $product->getExtensionAttributes()->getDownloadableProductSamples(); $downloadableSamples = $product->getDownloadableSamples(); //TODO: Track Fields: id, link_id, link_file and sample_file) $expectedLinks = ['file' => ['title' => 'TEST Import Link Title File', 'sort_order' => '78', 'sample_type' => 'file', 'price' => '123.0000', 'number_of_downloads' => '123', 'is_shareable' => '0', 'link_type' => 'file'], 'url' => ['title' => 'TEST Import Link Title URL', 'sort_order' => '42', 'sample_type' => 'url', 'sample_url' => 'http://www.bing.com', 'price' => '1.0000', 'number_of_downloads' => '0', 'is_shareable' => '1', 'link_type' => 'url', 'link_url' => 'http://www.google.com']]; foreach ($downloadableProductLinks as $link) { $actualLink = $link->getData(); $this->assertArrayHasKey('link_type', $actualLink); foreach ($expectedLinks[$actualLink['link_type']] as $expectedKey => $expectedValue) { $this->assertArrayHasKey($expectedKey, $actualLink); $this->assertEquals($actualLink[$expectedKey], $expectedValue); } } foreach ($downloadableLinks as $link) { $actualLink = $link->getData(); $this->assertArrayHasKey('link_type', $actualLink); $this->assertArrayHasKey('product_id', $actualLink); $this->assertEquals($actualLink['product_id'], $product->getData($this->productMetadata->getLinkField())); foreach ($expectedLinks[$actualLink['link_type']] as $expectedKey => $expectedValue) { $this->assertArrayHasKey($expectedKey, $actualLink); $this->assertEquals($actualLink[$expectedKey], $expectedValue); } } //TODO: Track Fields: id, sample_id and sample_file) $expectedSamples = ['file' => ['title' => 'TEST Import Sample File', 'sort_order' => '178', 'sample_type' => 'file'], 'url' => ['title' => 'TEST Import Sample URL', 'sort_order' => '178', 'sample_type' => 'url', 'sample_url' => 'http://www.yahoo.com']]; foreach ($downloadableProductSamples as $sample) { $actualSample = $sample->getData(); $this->assertArrayHasKey('sample_type', $actualSample); foreach ($expectedSamples[$actualSample['sample_type']] as $expectedKey => $expectedValue) { $this->assertArrayHasKey($expectedKey, $actualSample); $this->assertEquals($actualSample[$expectedKey], $expectedValue); } } foreach ($downloadableSamples as $sample) { $actualSample = $sample->getData(); $this->assertArrayHasKey('sample_type', $actualSample); $this->assertArrayHasKey('product_id', $actualSample); $this->assertEquals($actualSample['product_id'], $product->getData($this->productMetadata->getLinkField())); foreach ($expectedSamples[$actualSample['sample_type']] as $expectedKey => $expectedValue) { $this->assertArrayHasKey($expectedKey, $actualSample); $this->assertEquals($actualSample[$expectedKey], $expectedValue); } } }
/** * @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_attribute.php * @magentoAppArea adminhtml */ public function testConfigurableImport() { // import data from CSV file $pathToFile = __DIR__ . '/../../_files/import_configurable.csv'; $filesystem = $this->objectManager->create(\Magento\Framework\Filesystem::class); $directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT); $source = $this->objectManager->create(\Magento\ImportExport\Model\Import\Source\Csv::class, ['file' => $pathToFile, 'directory' => $directory]); $errors = $this->model->setSource($source)->setParameters(['behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND, 'entity' => 'catalog_product'])->validateData(); $this->assertTrue($errors->getErrorsCount() == 0); $this->model->importData(); /** @var \Magento\Catalog\Model\ResourceModel\Product $resource */ $resource = $this->objectManager->get(\Magento\Catalog\Model\ResourceModel\Product::class); $productId = $resource->getIdBySku(self::TEST_PRODUCT_NAME); $this->assertTrue(is_numeric($productId)); /** @var \Magento\Catalog\Model\Product $product */ $product = $this->objectManager->get(ProductRepositoryInterface::class)->getById($productId); $this->assertFalse($product->isObjectNew()); $this->assertEquals(self::TEST_PRODUCT_NAME, $product->getName()); $this->assertEquals(self::TEST_PRODUCT_TYPE, $product->getTypeId()); $optionCollection = $product->getTypeInstance()->getConfigurableOptions($product); foreach ($optionCollection as $option) { foreach ($option as $optionData) { $this->assertContains($optionData['sku'], $this->optionSkuList); } } $optionIdList = $resource->getProductsIdsBySkus($this->optionSkuList); foreach ($optionIdList as $optionId) { $this->assertArrayHasKey($optionId, $product->getExtensionAttributes()->getConfigurableProductLinks()); } $configurableOptionCollection = $product->getExtensionAttributes()->getConfigurableProductOptions(); $this->assertEquals(1, count($configurableOptionCollection)); foreach ($configurableOptionCollection as $option) { $optionData = $option->getData(); $this->assertArrayHasKey('product_super_attribute_id', $optionData); $this->assertArrayHasKey('product_id', $optionData); $this->assertEquals($product->getData($this->productMetadata->getLinkField()), $optionData['product_id']); $this->assertArrayHasKey('attribute_id', $optionData); $this->assertArrayHasKey('position', $optionData); $this->assertArrayHasKey('extension_attributes', $optionData); $this->assertArrayHasKey('product_attribute', $optionData); $productAttributeData = $optionData['product_attribute']->getData(); $this->assertArrayHasKey('attribute_id', $productAttributeData); $this->assertArrayHasKey('entity_type_id', $productAttributeData); $this->assertArrayHasKey('attribute_code', $productAttributeData); $this->assertEquals('test_configurable', $productAttributeData['attribute_code']); $this->assertArrayHasKey('frontend_label', $productAttributeData); $this->assertEquals('Test Configurable', $productAttributeData['frontend_label']); $this->assertArrayHasKey('label', $optionData); $this->assertEquals('test_configurable', $optionData['label']); $this->assertArrayHasKey('use_default', $optionData); $this->assertArrayHasKey('options', $optionData); $this->assertEquals('Option 1', $optionData['options'][0]['label']); $this->assertEquals('Option 1', $optionData['options'][0]['default_label']); $this->assertEquals('Option 1', $optionData['options'][0]['store_label']); $this->assertEquals('Option 2', $optionData['options'][1]['label']); $this->assertEquals('Option 2', $optionData['options'][1]['default_label']); $this->assertEquals('Option 2', $optionData['options'][1]['store_label']); $this->assertArrayHasKey('values', $optionData); $valuesData = $optionData['values']; $this->assertEquals(2, count($valuesData)); } }