-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Add child identities to products instead of parent identities #19313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
magento-engcom-team
merged 6 commits into
magento:2.3-develop
from
mediact:feature/bundle-configurable-product-identies-plugin
Jan 16, 2019
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f04154e
Add child identities of bundle and configurable products on frontend …
jasperzeinstra edd9d4c
Make code more strict
jasperzeinstra c6fdd50
Add unit test classes
jasperzeinstra 2f3396f
Fix static test.
nmalevanec c0d6730
Merge remote-tracking branch 'mainline/2.3-develop' into feature/bund…
nmalevanec 3b22449
Add integration tests for frontend product and identities extender pl…
nmalevanec File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Bundle\Model\Plugin\Frontend; | ||
|
||
use Magento\Bundle\Model\Product\Type; | ||
use Magento\Catalog\Model\Product as CatalogProduct; | ||
|
||
/** | ||
* Add child identities to product identities on storefront. | ||
*/ | ||
class Product | ||
{ | ||
/** | ||
* @var Type | ||
*/ | ||
private $type; | ||
|
||
/** | ||
* @param Type $type | ||
*/ | ||
public function __construct(Type $type) | ||
{ | ||
$this->type = $type; | ||
} | ||
|
||
/** | ||
* Add child identities to product identities | ||
* | ||
* @param CatalogProduct $product | ||
* @param array $identities | ||
* @return array | ||
*/ | ||
public function afterGetIdentities(CatalogProduct $product, array $identities): array | ||
{ | ||
foreach ($this->type->getChildrenIds($product->getEntityId()) as $childIds) { | ||
foreach ($childIds as $childId) { | ||
$identities[] = CatalogProduct::CACHE_TAG . '_' . $childId; | ||
} | ||
} | ||
|
||
return array_unique($identities); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
app/code/Magento/Bundle/Test/Unit/Model/Plugin/Frontend/ProductTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\Bundle\Test\Unit\Model\Plugin\Frontend; | ||
|
||
use Magento\Bundle\Model\Plugin\Frontend\Product as ProductPlugin; | ||
use Magento\Bundle\Model\Product\Type; | ||
use Magento\Catalog\Model\Product; | ||
use PHPUnit_Framework_MockObject_MockObject as MockObject; | ||
|
||
class ProductTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
/** @var \Magento\Bundle\Model\Plugin\Product */ | ||
private $plugin; | ||
|
||
/** @var MockObject|Type */ | ||
private $type; | ||
|
||
/** @var MockObject|\Magento\Catalog\Model\Product */ | ||
private $product; | ||
|
||
protected function setUp() | ||
{ | ||
$this->product = $this->getMockBuilder(Product::class) | ||
->disableOriginalConstructor() | ||
->setMethods(['getEntityId']) | ||
->getMock(); | ||
|
||
$this->type = $this->getMockBuilder(Type::class) | ||
->disableOriginalConstructor() | ||
->setMethods(['getChildrenIds']) | ||
->getMock(); | ||
|
||
$this->plugin = new ProductPlugin($this->type); | ||
} | ||
|
||
public function testAfterGetIdentities() | ||
{ | ||
$baseIdentities = [ | ||
'SomeCacheId', | ||
'AnotherCacheId', | ||
]; | ||
$id = 12345; | ||
$childIds = [ | ||
1 => [1, 2, 5, 100500], | ||
12 => [7, 22, 45, 24612] | ||
]; | ||
$expectedIdentities = [ | ||
'SomeCacheId', | ||
'AnotherCacheId', | ||
Product::CACHE_TAG . '_' . 1, | ||
Product::CACHE_TAG . '_' . 2, | ||
Product::CACHE_TAG . '_' . 5, | ||
Product::CACHE_TAG . '_' . 100500, | ||
Product::CACHE_TAG . '_' . 7, | ||
Product::CACHE_TAG . '_' . 22, | ||
Product::CACHE_TAG . '_' . 45, | ||
Product::CACHE_TAG . '_' . 24612, | ||
]; | ||
$this->product->expects($this->once()) | ||
->method('getEntityId') | ||
->will($this->returnValue($id)); | ||
$this->type->expects($this->once()) | ||
->method('getChildrenIds') | ||
->with($id) | ||
->will($this->returnValue($childIds)); | ||
$identities = $this->plugin->afterGetIdentities($this->product, $baseIdentities); | ||
$this->assertEquals($expectedIdentities, $identities); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
app/code/Magento/ConfigurableProduct/Model/Plugin/Frontend/ProductIdentitiesExtender.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\ConfigurableProduct\Model\Plugin\Frontend; | ||
|
||
use Magento\ConfigurableProduct\Model\Product\Type\Configurable; | ||
use Magento\Catalog\Model\Product; | ||
|
||
/** | ||
* Extender of product identities for child of configurable products | ||
*/ | ||
class ProductIdentitiesExtender | ||
{ | ||
/** | ||
* @var Configurable | ||
*/ | ||
private $configurableType; | ||
|
||
/** | ||
* @param Configurable $configurableType | ||
*/ | ||
public function __construct(Configurable $configurableType) | ||
{ | ||
$this->configurableType = $configurableType; | ||
} | ||
|
||
/** | ||
* Add child identities to product identities | ||
* | ||
* @param Product $subject | ||
* @param array $identities | ||
* @return array | ||
*/ | ||
public function afterGetIdentities(Product $subject, array $identities): array | ||
{ | ||
foreach ($this->configurableType->getChildrenIds($subject->getId()) as $childIds) { | ||
foreach ($childIds as $childId) { | ||
$identities[] = Product::CACHE_TAG . '_' . $childId; | ||
} | ||
} | ||
|
||
return array_unique($identities); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
...nto/ConfigurableProduct/Test/Unit/Model/Plugin/Frontend/ProductIdentitiesExtenderTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\ConfigurableProduct\Test\Unit\Model\Plugin\Frontend; | ||
|
||
use Magento\ConfigurableProduct\Model\Plugin\Frontend\ProductIdentitiesExtender; | ||
use Magento\ConfigurableProduct\Model\Product\Type\Configurable; | ||
use Magento\Catalog\Model\Product; | ||
|
||
/** | ||
* Class ProductIdentitiesExtenderTest | ||
*/ | ||
class ProductIdentitiesExtenderTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
/** | ||
* @var \PHPUnit_Framework_MockObject_MockObject|Configurable | ||
*/ | ||
private $configurableTypeMock; | ||
|
||
/** | ||
* @var ProductIdentitiesExtender | ||
*/ | ||
private $plugin; | ||
|
||
/** @var MockObject|\Magento\Catalog\Model\Product */ | ||
private $product; | ||
|
||
protected function setUp() | ||
{ | ||
$this->product = $this->getMockBuilder(Product::class) | ||
->disableOriginalConstructor() | ||
->setMethods(['getId']) | ||
->getMock(); | ||
|
||
$this->configurableTypeMock = $this->getMockBuilder(Configurable::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$this->plugin = new ProductIdentitiesExtender($this->configurableTypeMock); | ||
} | ||
|
||
public function testAfterGetIdentities() | ||
{ | ||
$identities = [ | ||
'SomeCacheId', | ||
'AnotherCacheId', | ||
]; | ||
$productId = 12345; | ||
$childIdentities = [ | ||
0 => [1, 2, 5, 100500] | ||
]; | ||
$expectedIdentities = [ | ||
'SomeCacheId', | ||
'AnotherCacheId', | ||
Product::CACHE_TAG . '_' . 1, | ||
Product::CACHE_TAG . '_' . 2, | ||
Product::CACHE_TAG . '_' . 5, | ||
Product::CACHE_TAG . '_' . 100500, | ||
]; | ||
|
||
$this->product->expects($this->once()) | ||
->method('getId') | ||
->willReturn($productId); | ||
|
||
$this->configurableTypeMock->expects($this->once()) | ||
->method('getChildrenIds') | ||
->with($productId) | ||
->willReturn($childIdentities); | ||
|
||
$productIdentities = $this->plugin->afterGetIdentities($this->product, $identities); | ||
$this->assertEquals($expectedIdentities, $productIdentities); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
dev/tests/integration/testsuite/Magento/Bundle/Model/Plugin/Frontend/ProductTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Magento\Bundle\Model\Plugin\Frontend; | ||
|
||
use Magento\Catalog\Api\ProductRepositoryInterface; | ||
use Magento\TestFramework\Helper\Bootstrap; | ||
use Magento\TestFramework\Interception\PluginList; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* Test bundle fronted product plugin adds children products ids to bundle product identities. | ||
*/ | ||
class ProductTest extends TestCase | ||
{ | ||
/** | ||
* Check, product plugin is registered for storefront. | ||
* | ||
* @magentoAppArea frontend | ||
* @return void | ||
*/ | ||
public function testProductIsRegistered(): void | ||
{ | ||
$pluginInfo = Bootstrap::getObjectManager()->get(PluginList::class) | ||
->get(\Magento\Catalog\Model\Product::class, []); | ||
$this->assertSame(Product::class, $pluginInfo['bundle']['instance']); | ||
} | ||
|
||
/** | ||
* Check plugin will add children ids to bundle product identities on storefront. | ||
* | ||
* @magentoDataFixture Magento/Bundle/_files/product.php | ||
* @magentoAppArea frontend | ||
* @return void | ||
*/ | ||
public function testGetIdentitiesForBundleProductOnStorefront(): void | ||
{ | ||
$productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class); | ||
$bundleProduct = $productRepository->get('bundle-product'); | ||
$simpleProduct = $productRepository->get('simple'); | ||
$expectedIdentities = [ | ||
'cat_p_' . $bundleProduct->getId(), | ||
'cat_p', | ||
'cat_p_' . $simpleProduct->getId(), | ||
|
||
]; | ||
$this->assertEquals($expectedIdentities, $bundleProduct->getIdentities()); | ||
} | ||
|
||
/** | ||
* Check plugin won't add children ids to bundle product identities in admin area. | ||
* | ||
* @magentoDataFixture Magento/Bundle/_files/product.php | ||
* @magentoAppArea adminhtml | ||
* @return void | ||
*/ | ||
public function testGetIdentitiesForBundleProductInAdminArea(): void | ||
{ | ||
$productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class); | ||
$bundleProduct = $productRepository->get('bundle-product'); | ||
$expectedIdentities = [ | ||
'cat_p_' . $bundleProduct->getId(), | ||
]; | ||
$this->assertEquals($expectedIdentities, $bundleProduct->getIdentities()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.