Skip to content

Commit cc06f27

Browse files
committed
ACP2E-55: Simple only global attributes make configurable attributes hidden when creating product configurations
- Fixed the CR comments.
1 parent df24ed2 commit cc06f27

File tree

4 files changed

+110
-61
lines changed

4 files changed

+110
-61
lines changed

app/code/Magento/ConfigurableProduct/Plugin/Model/UpdateConfigurableProductAttributeCollection.php

Lines changed: 0 additions & 45 deletions
This file was deleted.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\ConfigurableProduct\Test\Unit\Ui\DataProvider;
9+
10+
use Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection;
11+
use Magento\ConfigurableProduct\Model\ConfigurableAttributeHandler;
12+
use Magento\ConfigurableProduct\Ui\DataProvider\Attributes;
13+
use Magento\Framework\DataObject;
14+
use Magento\Framework\DB\Select;
15+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
16+
use PHPUnit\Framework\TestCase;
17+
18+
class AttributesTest extends TestCase
19+
{
20+
/**
21+
* @var Collection
22+
*/
23+
private Collection $collectionMock;
24+
25+
/**
26+
* @var Select
27+
*/
28+
private Select $selectMock;
29+
30+
/**
31+
* @var Attributes
32+
*/
33+
private Attributes $attributes;
34+
35+
/**
36+
* @return void
37+
*/
38+
protected function setUp(): void
39+
{
40+
$objectManager = new ObjectManager($this);
41+
$this->collectionMock = $this->getMockBuilder(Collection::class)
42+
->disableOriginalConstructor()
43+
->getMock();
44+
$this->selectMock = $this->getMockBuilder(Select::class)
45+
->disableOriginalConstructor()
46+
->onlyMethods(['where'])
47+
->getMock();
48+
$collectionAttributeHandlerMock = $this->getMockBuilder(ConfigurableAttributeHandler::class)
49+
->disableOriginalConstructor()
50+
->onlyMethods(['getApplicableAttributes'])
51+
->getMock();
52+
$collectionAttributeHandlerMock->expects($this->once())
53+
->method('getApplicableAttributes')
54+
->willReturn($this->collectionMock);
55+
$this->attributes = $objectManager->getObject(
56+
Attributes::class,
57+
[
58+
'name' => 'myName',
59+
'primaryFieldName' => 'myPrimaryFieldName',
60+
'requestFieldName' => 'myRequestFieldName',
61+
'configurableAttributeHandler' => $collectionAttributeHandlerMock
62+
]
63+
);
64+
}
65+
66+
/**
67+
* @return void
68+
*/
69+
public function testGetData()
70+
{
71+
$expectedResult = [
72+
'totalRecords' => 1,
73+
'items' => [
74+
0 => ['attribute' => 'color']
75+
]
76+
];
77+
$this->collectionMock->expects($this->once())
78+
->method('getSelect')
79+
->willReturn($this->selectMock);
80+
$this->collectionMock->expects($this->once())
81+
->method('getItems')
82+
->willReturn([new DataObject(['attribute' => 'color'])]);
83+
$this->collectionMock->expects($this->once())
84+
->method('getSize')
85+
->willReturn(1);
86+
$this->assertEquals($expectedResult, $this->attributes->getData());
87+
}
88+
}

app/code/Magento/ConfigurableProduct/Ui/DataProvider/Attributes.php

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,16 @@
66

77
namespace Magento\ConfigurableProduct\Ui\DataProvider;
88

9+
use Magento\Catalog\Model\Product\Type;
10+
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
11+
912
class Attributes extends \Magento\Ui\DataProvider\AbstractDataProvider
1013
{
1114
/**
1215
* @var \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection
1316
*/
1417
protected $collection;
1518

16-
/**
17-
* @var \Magento\ConfigurableProduct\Model\ConfigurableAttributeHandler
18-
*/
19-
private $configurableAttributeHandler;
20-
2119
/**
2220
* @param string $name
2321
* @param string $primaryFieldName
@@ -35,7 +33,6 @@ public function __construct(
3533
array $data = []
3634
) {
3735
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
38-
$this->configurableAttributeHandler = $configurableAttributeHandler;
3936
$this->collection = $configurableAttributeHandler->getApplicableAttributes();
4037
}
4138

@@ -53,16 +50,28 @@ public function getCollection()
5350
public function getData()
5451
{
5552
$items = [];
56-
$skippedItems = 0;
53+
$this->getCollection()->getSelect()->where(
54+
'(`apply_to` IS NULL) OR
55+
(
56+
FIND_IN_SET(' .
57+
sprintf("'%s'", Type::TYPE_SIMPLE) . ',
58+
`apply_to`
59+
) AND
60+
FIND_IN_SET(' .
61+
sprintf("'%s'", Type::TYPE_VIRTUAL) . ',
62+
`apply_to`
63+
) AND
64+
FIND_IN_SET(' .
65+
sprintf("'%s'", Configurable::TYPE_CODE) . ',
66+
`apply_to`
67+
)
68+
)'
69+
);
5770
foreach ($this->getCollection()->getItems() as $attribute) {
58-
if ($this->configurableAttributeHandler->isAttributeApplicable($attribute)) {
59-
$items[] = $attribute->toArray();
60-
} else {
61-
$skippedItems++;
62-
}
71+
$items[] = $attribute->toArray();
6372
}
6473
return [
65-
'totalRecords' => $this->collection->getSize() - $skippedItems,
74+
'totalRecords' => $this->collection->getSize(),
6675
'items' => $items
6776
];
6877
}

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,4 @@
8181
<type name="Magento\Sales\Model\Order\Invoice">
8282
<plugin name="update_configurable_product_total_qty" type="Magento\ConfigurableProduct\Plugin\Model\Order\Invoice\UpdateConfigurableProductTotalQty"/>
8383
</type>
84-
<type name="Magento\ConfigurableProduct\Ui\DataProvider\Attributes">
85-
<plugin name="update_configurable_product_attribute_collection" type="Magento\ConfigurableProduct\Plugin\Model\UpdateConfigurableProductAttributeCollection"/>
86-
</type>
8784
</config>

0 commit comments

Comments
 (0)