Skip to content

Commit 3f59cae

Browse files
committed
Merge branch 'ACP2E-55' of https://github.com/magento-l3/magento2ce into PR1-21-01-2022
2 parents be4c4e0 + 1a339cc commit 3f59cae

File tree

2 files changed

+128
-14
lines changed

2 files changed

+128
-14
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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\Product\Type;
11+
use Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection;
12+
use Magento\ConfigurableProduct\Model\ConfigurableAttributeHandler;
13+
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
14+
use Magento\ConfigurableProduct\Ui\DataProvider\Attributes;
15+
use Magento\Framework\DataObject;
16+
use Magento\Framework\DB\Select;
17+
use PHPUnit\Framework\TestCase;
18+
19+
class AttributesTest extends TestCase
20+
{
21+
/**
22+
* @var Collection
23+
*/
24+
private Collection $collectionMock;
25+
26+
/**
27+
* @var Select
28+
*/
29+
private Select $selectMock;
30+
31+
/**
32+
* @var Attributes
33+
*/
34+
private Attributes $attributes;
35+
36+
/**
37+
* @return void
38+
*/
39+
protected function setUp(): void
40+
{
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 = new Attributes(
56+
'myName',
57+
'myPrimaryFieldName',
58+
'myRequestFieldName',
59+
$collectionAttributeHandlerMock
60+
);
61+
}
62+
63+
/**
64+
* @return void
65+
*/
66+
public function testGetData()
67+
{
68+
$expectedResult = [
69+
'totalRecords' => 1,
70+
'items' => [
71+
0 => ['attribute' => 'color']
72+
]
73+
];
74+
$this->collectionMock->expects($this->once())
75+
->method('getSelect')
76+
->willReturn($this->selectMock);
77+
$this->selectMock->expects($this->once())
78+
->method('where')
79+
->with('(`apply_to` IS NULL) OR
80+
(
81+
FIND_IN_SET(' .
82+
sprintf("'%s'", Type::TYPE_SIMPLE) . ',
83+
`apply_to`
84+
) AND
85+
FIND_IN_SET(' .
86+
sprintf("'%s'", Type::TYPE_VIRTUAL) . ',
87+
`apply_to`
88+
) AND
89+
FIND_IN_SET(' .
90+
sprintf("'%s'", Configurable::TYPE_CODE) . ',
91+
`apply_to`
92+
)
93+
)')
94+
->willReturnSelf();
95+
$this->collectionMock->expects($this->once())
96+
->method('getItems')
97+
->willReturn([new DataObject(['attribute' => 'color'])]);
98+
$this->collectionMock->expects($this->once())
99+
->method('getSize')
100+
->willReturn(1);
101+
$this->assertEquals($expectedResult, $this->attributes->getData());
102+
}
103+
}

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

Lines changed: 25 additions & 14 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,11 +33,12 @@ 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

4239
/**
40+
* Getting the product attribute collection.
41+
*
4342
* @return \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection
4443
*/
4544
public function getCollection()
@@ -48,21 +47,33 @@ public function getCollection()
4847
}
4948

5049
/**
51-
* {@inheritdoc}
50+
* @inheritdoc
5251
*/
5352
public function getData()
5453
{
5554
$items = [];
56-
$skippedItems = 0;
55+
$this->getCollection()->getSelect()->where(
56+
'(`apply_to` IS NULL) OR
57+
(
58+
FIND_IN_SET(' .
59+
sprintf("'%s'", Type::TYPE_SIMPLE) . ',
60+
`apply_to`
61+
) AND
62+
FIND_IN_SET(' .
63+
sprintf("'%s'", Type::TYPE_VIRTUAL) . ',
64+
`apply_to`
65+
) AND
66+
FIND_IN_SET(' .
67+
sprintf("'%s'", Configurable::TYPE_CODE) . ',
68+
`apply_to`
69+
)
70+
)'
71+
);
5772
foreach ($this->getCollection()->getItems() as $attribute) {
58-
if ($this->configurableAttributeHandler->isAttributeApplicable($attribute)) {
59-
$items[] = $attribute->toArray();
60-
} else {
61-
$skippedItems++;
62-
}
73+
$items[] = $attribute->toArray();
6374
}
6475
return [
65-
'totalRecords' => $this->collection->getSize() - $skippedItems,
76+
'totalRecords' => $this->collection->getSize(),
6677
'items' => $items
6778
];
6879
}

0 commit comments

Comments
 (0)