/**
  * @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;
 }
Example #5
0
 public function testGetLinkField()
 {
     $entityTableName = 'entity_table';
     $connectionName = 'default';
     $this->appResourceMock->expects($this->exactly(2))->method('getConnectionByName')->with($connectionName)->willReturn($this->connectionMock);
     $this->appResourceMock->expects($this->exactly(2))->method('getTableName')->with($entityTableName)->willReturn($entityTableName);
     $linkField = 'entity_id';
     $primaryKeyName = 'id';
     $indexList = [$primaryKeyName => ['COLUMNS_LIST' => [$linkField]]];
     $this->connectionMock->expects($this->once())->method('getIndexList')->with($entityTableName)->willReturn($indexList);
     $this->connectionMock->expects($this->once())->method('getPrimaryKeyName')->with($entityTableName)->willReturn($primaryKeyName);
     $metadata = new EntityMetadata($this->appResourceMock, $entityTableName, '', null, null, $connectionName);
     $this->assertEquals($linkField, $metadata->getLinkField());
 }
 /**
  * @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);
         }
     }
 }
 /**
  * Return attribute values for given entities and store of specific attribute type
  *
  * @param string $type
  * @param array $entityIds
  * @param integer $storeId
  * @return array
  */
 protected function getAttributeTypeValues($type, $entityIds, $storeId)
 {
     $linkField = $this->categoryMetadata->getLinkField();
     $select = $this->connection->select()->from(['def' => $this->connection->getTableName($this->getTableName('catalog_category_entity_' . $type))], [$linkField, 'attribute_id'])->joinLeft(['e' => $this->connection->getTableName($this->getTableName('catalog_category_entity'))], "def.{$linkField} = e.{$linkField}")->joinLeft(['store' => $this->connection->getTableName($this->getTableName('catalog_category_entity_' . $type))], "store.{$linkField} = def.{$linkField} AND store.attribute_id = def.attribute_id " . 'AND store.store_id = ' . $storeId, ['value' => $this->connection->getCheckSql('store.value_id > 0', $this->connection->quoteIdentifier('store.value'), $this->connection->quoteIdentifier('def.value'))])->where("e.entity_id IN (?)", $entityIds)->where('def.store_id IN (?)', [\Magento\Store\Model\Store::DEFAULT_STORE_ID, $storeId]);
     return $this->connection->fetchAll($select);
 }
 /**
  * @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));
     }
 }