Skip to content

Commit e9c8460

Browse files
committed
Merge branch 'MAGETWO-83204' into 2.1.13-PR-0.5
2 parents f377e18 + 56c855c commit e9c8460

File tree

11 files changed

+303
-129
lines changed

11 files changed

+303
-129
lines changed

app/code/Magento/Catalog/Model/Product.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,12 +1058,7 @@ protected function _afterLoad()
10581058
*/
10591059
public function cleanCache()
10601060
{
1061-
if ($this->getId()) {
1062-
$this->_cacheManager->clean(
1063-
self::CACHE_TAG . '_' . $this->getId()
1064-
);
1065-
}
1066-
return $this;
1061+
return $this->cleanModelCache();
10671062
}
10681063

10691064
/**

app/code/Magento/Catalog/Test/Unit/Model/ProductTest.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,21 +1391,6 @@ public function testGetOptionByIdForProductWithoutOptions()
13911391
$this->assertNull($this->model->getOptionById(100));
13921392
}
13931393

1394-
public function testCleanCache()
1395-
{
1396-
//Without an ID cleanCache won't clean anything because the entity is
1397-
//not identified and it will be called later exactly once.
1398-
$this->model->setId(null);
1399-
$this->cacheManagerMock
1400-
->expects($this->once())
1401-
->method('clean');
1402-
$this->model->cleanCache();
1403-
1404-
//Now that ID is set clean will be called.
1405-
$this->model->setId(1);
1406-
$this->model->cleanCache();
1407-
}
1408-
14091394
public function testGetCacheTags()
14101395
{
14111396
//If entity is identified getCacheTags has to return the same values
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\ConfigurableProduct\Model\Plugin;
7+
8+
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
9+
use Magento\Catalog\Api\ProductRepositoryInterface;
10+
use Magento\Catalog\Model\Product;
11+
12+
/**
13+
* Extender of product identities for child of configurable products
14+
*/
15+
class ProductIdentitiesExtender
16+
{
17+
/**
18+
* @var Configurable
19+
*/
20+
private $configurableType;
21+
22+
/**
23+
* @var ProductRepositoryInterface
24+
*/
25+
private $productRepository;
26+
27+
/**
28+
* @param Configurable $configurableType
29+
* @param ProductRepositoryInterface $productRepository
30+
*/
31+
public function __construct(Configurable $configurableType, ProductRepositoryInterface $productRepository)
32+
{
33+
$this->configurableType = $configurableType;
34+
$this->productRepository = $productRepository;
35+
}
36+
37+
/**
38+
* Add parent identities to product identities
39+
*
40+
* @param Product $subject
41+
* @param array $identities
42+
* @return array
43+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
44+
*/
45+
public function afterGetIdentities(Product $subject, $identities)
46+
{
47+
$identities = (array) $identities;
48+
49+
foreach ($this->configurableType->getParentIdsByChild($subject->getId()) as $parentId) {
50+
$parentProduct = $this->productRepository->getById($parentId);
51+
$identities = array_merge($identities, (array) $parentProduct->getIdentities());
52+
}
53+
54+
return array_unique($identities);
55+
}
56+
}

app/code/Magento/ConfigurableProduct/Model/Product/Cache/Tag/Configurable.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ public function getTags($object)
4343

4444
$result = $object->getIdentities();
4545

46-
foreach ($this->catalogProductTypeConfigurable->getParentIdsByChild($object->getId()) as $parentId) {
47-
$result[] = \Magento\Catalog\Model\Product::CACHE_TAG . '_' . $parentId;
48-
}
4946
return $result;
5047
}
5148
}

app/code/Magento/ConfigurableProduct/Model/ResourceModel/Product/Type/Configurable.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ public function getChildrenIds($parentId, $required = true)
169169
*/
170170
public function getParentIdsByChild($childId)
171171
{
172-
$parentIds = [];
173172
$select = $this->getConnection()
174173
->select()
175174
->from(['l' => $this->getMainTable()], [])
@@ -178,10 +177,7 @@ public function getParentIdsByChild($childId)
178177
'e.' . $this->optionProvider->getProductEntityLinkField() . ' = l.parent_id',
179178
['e.entity_id']
180179
)->where('l.product_id IN(?)', $childId);
181-
182-
foreach ($this->getConnection()->fetchAll($select) as $row) {
183-
$parentIds[] = $row['entity_id'];
184-
}
180+
$parentIds = $this->getConnection()->fetchCol($select);
185181

186182
return $parentIds;
187183
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\ConfigurableProduct\Test\Unit\Model\Plugin;
7+
8+
use Magento\ConfigurableProduct\Model\Plugin\ProductIdentitiesExtender;
9+
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Catalog\Model\Product;
12+
13+
/**
14+
* Class ProductIdentitiesExtenderTest
15+
*/
16+
class ProductIdentitiesExtenderTest extends \PHPUnit_Framework_TestCase
17+
{
18+
/**
19+
* @var \PHPUnit_Framework_MockObject_MockObject|Configurable
20+
*/
21+
private $configurableTypeMock;
22+
23+
/**
24+
* @var \PHPUnit_Framework_MockObject_MockObject|ProductRepositoryInterface
25+
*/
26+
private $productRepositoryMock;
27+
28+
/**
29+
* @var ProductIdentitiesExtender
30+
*/
31+
private $plugin;
32+
33+
protected function setUp()
34+
{
35+
$this->configurableTypeMock = $this->getMockBuilder(Configurable::class)
36+
->disableOriginalConstructor()
37+
->getMock();
38+
$this->productRepositoryMock = $this->getMockBuilder(ProductRepositoryInterface::class)
39+
->getMock();
40+
41+
$this->plugin = new ProductIdentitiesExtender($this->configurableTypeMock, $this->productRepositoryMock);
42+
}
43+
44+
public function testAfterGetIdentities()
45+
{
46+
$productId = 1;
47+
$productIdentity = 'cache_tag_1';
48+
$productMock = $this->getMockBuilder(Product::class)
49+
->disableOriginalConstructor()
50+
->getMock();
51+
$parentProductId = 2;
52+
$parentProductIdentity = 'cache_tag_2';
53+
$parentProductMock = $this->getMockBuilder(Product::class)
54+
->disableOriginalConstructor()
55+
->getMock();
56+
57+
$productMock->expects($this->once())
58+
->method('getId')
59+
->willReturn($productId);
60+
$this->configurableTypeMock->expects($this->once())
61+
->method('getParentIdsByChild')
62+
->with($productId)
63+
->willReturn([$parentProductId]);
64+
$this->productRepositoryMock->expects($this->once())
65+
->method('getById')
66+
->with($parentProductId)
67+
->willReturn($parentProductMock);
68+
$parentProductMock->expects($this->once())
69+
->method('getIdentities')
70+
->willReturn([$parentProductIdentity]);
71+
72+
$productIdentities = $this->plugin->afterGetIdentities($productMock, [$productIdentity]);
73+
$this->assertEquals([$productIdentity, $parentProductIdentity], $productIdentities);
74+
}
75+
}

app/code/Magento/ConfigurableProduct/Test/Unit/Model/Product/Cache/Tag/ConfigurableTest.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,6 @@ public function testGetTagsWithVariation()
5656
->method('getIdentities')
5757
->willReturn($identities);
5858

59-
$parentId = 4;
60-
$this->typeResource->expects($this->once())
61-
->method('getParentIdsByChild')
62-
->willReturn([$parentId]);
63-
64-
$expected = array_merge($identities, [\Magento\Catalog\Model\Product::CACHE_TAG . '_' . $parentId]);
65-
66-
$this->assertEquals($expected, $this->model->getTags($product));
59+
$this->assertEquals($identities, $this->model->getTags($product));
6760
}
6861
}

app/code/Magento/ConfigurableProduct/etc/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,7 @@
178178
</argument>
179179
</arguments>
180180
</type>
181+
<type name="Magento\Catalog\Model\Product">
182+
<plugin name="product_identities_extender" type="Magento\ConfigurableProduct\Model\Plugin\ProductIdentitiesExtender" />
183+
</type>
181184
</config>

app/code/Magento/Swatches/Helper/Data.php

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,28 @@ private function populateAdditionalDataEavAttribute(Attribute $attribute)
189189
return $this;
190190
}
191191

192+
/**
193+
* Check is media attribute available
194+
*
195+
* @param ModelProduct $product
196+
* @param string $attributeCode
197+
* @return bool
198+
*/
199+
private function isMediaAvailable(ModelProduct $product, $attributeCode)
200+
{
201+
$isAvailable = false;
202+
203+
$mediaGallery = $product->getMediaGalleryEntries();
204+
foreach ($mediaGallery as $mediaEntry) {
205+
if (in_array($attributeCode, $mediaEntry->getTypes(), true)) {
206+
$isAvailable = !$mediaEntry->isDisabled();
207+
break;
208+
}
209+
}
210+
211+
return $isAvailable;
212+
}
213+
192214
/**
193215
* @param string $attributeCode swatch_image|image
194216
* @param ModelProduct $configurableProduct
@@ -201,8 +223,9 @@ private function loadFirstVariation($attributeCode, ModelProduct $configurablePr
201223
$usedProducts = $configurableProduct->getTypeInstance()->getUsedProducts($configurableProduct);
202224

203225
foreach ($usedProducts as $simpleProduct) {
204-
if (!in_array($simpleProduct->getData($attributeCode), [null, self::EMPTY_IMAGE_VALUE], true)
205-
&& !array_diff_assoc($requiredAttributes, $simpleProduct->getData())
226+
if (
227+
!array_diff_assoc($requiredAttributes, $simpleProduct->getData())
228+
&& $this->isMediaAvailable($simpleProduct, $attributeCode)
206229
) {
207230
return $simpleProduct;
208231
}
@@ -313,50 +336,34 @@ private function addFilterByParent(ProductCollection $productCollection, $parent
313336
*/
314337
public function getProductMediaGallery(ModelProduct $product)
315338
{
316-
if (!in_array($product->getData('image'), [null, self::EMPTY_IMAGE_VALUE], true)) {
317-
$baseImage = $product->getData('image');
318-
} else {
319-
$productMediaAttributes = array_filter($product->getMediaAttributeValues(), function ($value) {
320-
return $value !== self::EMPTY_IMAGE_VALUE && $value !== null;
321-
});
322-
foreach ($productMediaAttributes as $attributeCode => $value) {
323-
if ($attributeCode !== 'swatch_image') {
324-
$baseImage = (string)$value;
325-
break;
326-
}
339+
$baseImage = null;
340+
$gallery = [];
341+
342+
$mediaGallery = $product->getMediaGalleryEntries();
343+
foreach ($mediaGallery as $mediaEntry) {
344+
if ($mediaEntry->isDisabled()) {
345+
continue;
327346
}
347+
348+
if (in_array('image', $mediaEntry->getTypes(), true)) {
349+
$baseImage = $mediaEntry->getFile();
350+
} elseif (!$baseImage) {
351+
$baseImage = $mediaEntry->getFile();
352+
}
353+
354+
$gallery[$mediaEntry->getId()] = $this->getAllSizeImages($product, $mediaEntry->getFile());
328355
}
329356

330-
if (empty($baseImage)) {
357+
if (!$baseImage) {
331358
return [];
332359
}
333360

334361
$resultGallery = $this->getAllSizeImages($product, $baseImage);
335-
$resultGallery['gallery'] = $this->getGalleryImages($product);
362+
$resultGallery['gallery'] = $gallery;
336363

337364
return $resultGallery;
338365
}
339366

340-
/**
341-
* @param ModelProduct $product
342-
* @return array
343-
*/
344-
private function getGalleryImages(ModelProduct $product)
345-
{
346-
//TODO: remove after fix MAGETWO-48040
347-
$product = $this->productRepository->getById($product->getId());
348-
349-
$result = [];
350-
$mediaGallery = $product->getMediaGalleryImages();
351-
foreach ($mediaGallery as $media) {
352-
$result[$media->getData('value_id')] = $this->getAllSizeImages(
353-
$product,
354-
$media->getData('file')
355-
);
356-
}
357-
return $result;
358-
}
359-
360367
/**
361368
* @param ModelProduct $product
362369
* @param string $imageFile

0 commit comments

Comments
 (0)