Skip to content

Commit

Permalink
Merge pull request #5450 from magento-tsg-csl3/2.4-develop-pr16
Browse files Browse the repository at this point in the history
[TSG-CSL3] For 2.4 (pr16)
  • Loading branch information
zakdma authored Mar 16, 2020
2 parents 81bb39b + 22957d7 commit ba5f8eb
Show file tree
Hide file tree
Showing 50 changed files with 1,481 additions and 652 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,15 @@ private function processEntries(ProductInterface $product, array $newEntries, ar
$this->processNewMediaGalleryEntry($product, $newEntry);

$finalGallery = $product->getData('media_gallery');
$newEntryId = key(array_diff_key($product->getData('media_gallery')['images'], $entriesById));

$entryIds = array_keys(
array_diff_key(
$product->getData('media_gallery')['images'],
$entriesById
)
);
$newEntryId = array_pop($entryIds);

$newEntry = array_replace_recursive($newEntry, $finalGallery['images'][$newEntryId]);
$entriesById[$newEntryId] = $newEntry;
$finalGallery['images'][$newEntryId] = $newEntry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\EntityManager\MetadataPool;

/**
* Class Link used for assign website to the product
*/
class Link
{
/**
Expand All @@ -28,6 +30,7 @@ public function __construct(

/**
* Retrieve associated with product websites ids
*
* @param int $productId
* @return array
*/
Expand All @@ -48,29 +51,53 @@ public function getWebsiteIdsByProductId($productId)

/**
* Return true - if websites was changed, and false - if not
*
* @param ProductInterface $product
* @param array $websiteIds
* @return bool
*/
public function saveWebsiteIds(ProductInterface $product, array $websiteIds)
{
$productId = (int) $product->getId();
return $this->updateProductWebsite($productId, $websiteIds);
}

/**
* Get Product website table
*
* @return string
*/
private function getProductWebsiteTable()
{
return $this->resourceConnection->getTableName('catalog_product_website');
}

/**
* Update product website table
*
* @param int $productId
* @param array $websiteIds
* @return bool
*/
public function updateProductWebsite(int $productId, array $websiteIds): bool
{
$connection = $this->resourceConnection->getConnection();

$oldWebsiteIds = $this->getWebsiteIdsByProductId($product->getId());
$oldWebsiteIds = $this->getWebsiteIdsByProductId($productId);
$insert = array_diff($websiteIds, $oldWebsiteIds);
$delete = array_diff($oldWebsiteIds, $websiteIds);

if (!empty($insert)) {
$data = [];
foreach ($insert as $websiteId) {
$data[] = ['product_id' => (int) $product->getId(), 'website_id' => (int) $websiteId];
$data[] = ['product_id' => $productId, 'website_id' => (int)$websiteId];
}
$connection->insertMultiple($this->getProductWebsiteTable(), $data);
}

if (!empty($delete)) {
foreach ($delete as $websiteId) {
$condition = ['product_id = ?' => (int) $product->getId(), 'website_id = ?' => (int) $websiteId];
$condition = ['product_id = ?' => $productId, 'website_id = ?' => (int)$websiteId];
$connection->delete($this->getProductWebsiteTable(), $condition);
}
}
Expand All @@ -81,12 +108,4 @@ public function saveWebsiteIds(ProductInterface $product, array $websiteIds)

return false;
}

/**
* @return string
*/
private function getProductWebsiteTable()
{
return $this->resourceConnection->getTableName('catalog_product_website');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Catalog\Model\ResourceModel;

use Magento\Catalog\Model\ResourceModel\Product\Website\Link;
use Magento\Framework\EntityManager\Operation\AttributeInterface;

/**
* Class purpose is to handle product websites assignment
*/
class ProductWebsiteAssignmentHandler implements AttributeInterface
{
/**
* @var Link
*/
private $productLink;

/**
* ProductWebsiteAssignmentHandler constructor
*
* @param Link $productLink
*/
public function __construct(
Link $productLink
) {
$this->productLink = $productLink;
}

/**
* Assign product website entity to the product repository
*
* @param string $entityType
* @param array $entityData
* @param array $arguments
* @return array
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @throws \Exception
*/
public function execute($entityType, $entityData, $arguments = []): array
{
$websiteIds = array_key_exists('website_ids', $entityData) ?
array_filter($entityData['website_ids'], function ($websiteId) {
return $websiteId !== null;
}) : [];
$productId = array_key_exists('entity_id', $entityData) ? (int) $entityData['entity_id'] : null;

if (!empty($productId) && !empty($websiteIds)) {
$this->productLink->updateProductWebsite($productId, $websiteIds);
}
return $entityData;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->

<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
<actionGroup name="AssertSeeProductDetailsOnStorefrontRecentlyViewedWidgetActionGroup">
<annotations>
<description>Goes to the home Page Recently VIewed Product and Grab the Prdouct name and Position from it.</description>
</annotations>
<arguments>
<argument name="productName" type="string"/>
<argument name="productPosition" type="string"/>
</arguments>
<grabTextFrom selector="{{StoreFrontRecentlyViewedProductSection.ProductName(productPosition)}}" stepKey="grabRelatedProductPosition"/>
<assertContains expected="{{productName}}" actual="$grabRelatedProductPosition" stepKey="assertRelatedProductName"/>
</actionGroup>
</actionGroups>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->

<entities xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:DataGenerator/etc/dataProfileSchema.xsd">
<entity name="RecentlyViewedProductScopeStore">
<data key="path">catalog/recently_products/scope</data>
<data key="value">store</data>
</entity>
<entity name="RecentlyViewedProductScopeWebsite">
<data key="path">catalog/recently_products/scope</data>
<data key="value">website</data>
</entity>
<entity name="RecentlyViewedProductScopeStoreGroup">
<data key="path">catalog/recently_products/scope</data>
<data key="value">group</data>
</entity>
</entities>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->

<sections xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Page/etc/SectionObject.xsd">

<section name="StoreFrontRecentlyViewedProductSection">
<element name="ProductName" type="text" selector="//div[@class='products-grid']/ol/li[position()={{position}}]/div/div[@class='product-item-details']/strong/a" parameterized="true"/>
</section>
</sections>
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
<createData entity="FirstLevelSubCat" stepKey="createDefaultCategory">
<field key="is_active">true</field>
</createData>
<!-- Perform reindex and flush cache -->
<magentoCLI command="indexer:reindex" stepKey="reindex"/>
<magentoCLI command="cache:flush" stepKey="flushCache"/>
</before>
<after>
<deleteData createDataKey="createDefaultCategory" stepKey="deleteCategory"/>
Expand Down
Loading

0 comments on commit ba5f8eb

Please sign in to comment.