Skip to content

Commit 093891f

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-45070' into BugFestW6
2 parents 257791c + 6014a12 commit 093891f

File tree

11 files changed

+460
-28
lines changed

11 files changed

+460
-28
lines changed

app/code/Magento/ConfigurableProduct/Controller/Adminhtml/Product/Attribute/GetAttributes.php

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,10 @@
77
namespace Magento\ConfigurableProduct\Controller\Adminhtml\Product\Attribute;
88

99
use Magento\Backend\App\Action;
10+
use Magento\ConfigurableProduct\Model\AttributesListInterface;
1011

1112
class GetAttributes extends Action
1213
{
13-
/**
14-
* @var \Magento\Framework\Json\Helper\Data
15-
*/
16-
protected $jsonHelper;
17-
1814
/**
1915
* Store manager
2016
*
@@ -23,25 +19,25 @@ class GetAttributes extends Action
2319
protected $storeManager;
2420

2521
/**
26-
* @var \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory
22+
* @var \Magento\Framework\Json\Helper\Data
2723
*/
28-
protected $collectionFactory;
24+
protected $jsonHelper;
2925

3026
/**
3127
* @param Action\Context $context
32-
* @param \Magento\Framework\Json\Helper\Data $jsonHelper
3328
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
34-
* @param \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $collectionFactory
29+
* @param \Magento\Framework\Json\Helper\Data $jsonHelper
30+
* @param AttributesListInterface $attributesList
3531
*/
3632
public function __construct(
3733
Action\Context $context,
38-
\Magento\Framework\Json\Helper\Data $jsonHelper,
3934
\Magento\Store\Model\StoreManagerInterface $storeManager,
40-
\Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $collectionFactory
35+
\Magento\Framework\Json\Helper\Data $jsonHelper,
36+
AttributesListInterface $attributesList
4137
) {
42-
$this->jsonHelper = $jsonHelper;
4338
$this->storeManager = $storeManager;
44-
$this->collectionFactory = $collectionFactory;
39+
$this->jsonHelper = $jsonHelper;
40+
$this->attributesList = $attributesList;
4541
parent::__construct($context);
4642
}
4743

@@ -56,27 +52,14 @@ protected function _isAllowed()
5652
}
5753

5854
/**
59-
* Search for attributes by part of attribute's label in admin store
55+
* Get attributes
6056
*
6157
* @return void
6258
*/
6359
public function execute()
6460
{
6561
$this->storeManager->setCurrentStore(\Magento\Store\Model\Store::ADMIN_CODE);
66-
$collection = $this->collectionFactory->create();
67-
$collection->addFieldToFilter(
68-
'main_table.attribute_id',
69-
$this->getRequest()->getParam('attributes')
70-
);
71-
$attributes = [];
72-
foreach ($collection->getItems() as $attribute) {
73-
$attributes[] = [
74-
'id' => $attribute->getId(),
75-
'label' => $attribute->getFrontendLabel(),
76-
'code' => $attribute->getAttributeCode(),
77-
'options' => $attribute->getSource()->getAllOptions(false),
78-
];
79-
}
62+
$attributes = $this->attributesList->getAttributes($this->getRequest()->getParam('attributes'));
8063
$this->getResponse()->representJson($this->jsonHelper->jsonEncode($attributes));
8164
}
8265
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\ConfigurableProduct\Model;
7+
8+
class AttributesList implements AttributesListInterface
9+
{
10+
/**
11+
* @var \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory
12+
*/
13+
protected $collectionFactory;
14+
15+
/**
16+
* @param \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $collectionFactory
17+
*/
18+
public function __construct(
19+
\Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $collectionFactory
20+
) {
21+
$this->collectionFactory = $collectionFactory;
22+
}
23+
24+
/**
25+
* Retrieve list of attributes
26+
*
27+
* @param array $ids
28+
* @return array
29+
*/
30+
public function getAttributes($ids)
31+
{
32+
$collection = $this->collectionFactory->create();
33+
$collection->addFieldToFilter('main_table.attribute_id', $ids);
34+
$attributes = [];
35+
foreach ($collection->getItems() as $attribute) {
36+
$attributes[] = [
37+
'id' => $attribute->getId(),
38+
'label' => $attribute->getFrontendLabel(),
39+
'code' => $attribute->getAttributeCode(),
40+
'options' => $attribute->getSource()->getAllOptions(false)
41+
];
42+
}
43+
return $attributes;
44+
}
45+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\ConfigurableProduct\Model;
7+
8+
interface AttributesListInterface
9+
{
10+
/**
11+
* Retrieve list of attributes
12+
*
13+
* @param array $ids
14+
* @return array
15+
*/
16+
public function getAttributes($ids);
17+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\ConfigurableProduct\Test\Unit\Model;
8+
9+
class AttributesListTest extends \PHPUnit_Framework_TestCase
10+
{
11+
/**
12+
* @var \Magento\ConfigurableProduct\Model\AttributesList
13+
*/
14+
protected $attributeListModel;
15+
16+
/**
17+
* @var \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection|\PHPUnit_Framework_MockObject_MockObject
18+
*/
19+
protected $collectionMock;
20+
21+
/**
22+
* @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute|\PHPUnit_Framework_MockObject_MockObject
23+
*/
24+
protected $attributeMock;
25+
26+
27+
protected function setUp()
28+
{
29+
$this->collectionMock = $this->getMock(
30+
'Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection',
31+
[],
32+
[],
33+
'',
34+
false
35+
);
36+
37+
/** @var \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $collectionFactoryMock */
38+
$collectionFactoryMock = $this->getMock(
39+
'Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory',
40+
['create'],
41+
[],
42+
'',
43+
false
44+
);
45+
$collectionFactoryMock->expects($this->once())->method('create')->willReturn($this->collectionMock);
46+
47+
$methods = ['getId', 'getFrontendLabel', 'getAttributeCode', 'getSource'];
48+
$this->attributeMock = $this->getMock(
49+
'Magento\Catalog\Model\ResourceModel\Eav\Attribute',
50+
$methods,
51+
[],
52+
'',
53+
false
54+
);
55+
$this->collectionMock
56+
->expects($this->once())
57+
->method('getItems')
58+
->will($this->returnValue(['id' => $this->attributeMock]));
59+
60+
$this->attributeListModel = new \Magento\ConfigurableProduct\Model\AttributesList(
61+
$collectionFactoryMock
62+
);
63+
}
64+
65+
public function testGetAttributes()
66+
{
67+
$ids = [1];
68+
$result = [
69+
[
70+
'id' => 'id',
71+
'label' => 'label',
72+
'code' => 'code',
73+
'options' => ['options']
74+
]
75+
];
76+
77+
$this->collectionMock
78+
->expects($this->any())
79+
->method('addFieldToFilter')
80+
->with('main_table.attribute_id', $ids);
81+
82+
$this->attributeMock->expects($this->once())->method('getId')->will($this->returnValue('id'));
83+
$this->attributeMock->expects($this->once())->method('getFrontendLabel')->will($this->returnValue('label'));
84+
$this->attributeMock->expects($this->once())->method('getAttributeCode')->will($this->returnValue('code'));
85+
86+
$source = $this->getMock('Magento\Eav\Model\Entity\Attribute\Source\AbstractSource', [], [], '', false);
87+
$source->expects($this->once())->method('getAllOptions')->with(false)->will($this->returnValue(['options']));
88+
$this->attributeMock->expects($this->once())->method('getSource')->will($this->returnValue($source));
89+
90+
$this->assertEquals($result, $this->attributeListModel->getAttributes($ids));
91+
}
92+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@
3434
<argument name="attributeRepository" xsi:type="object">Magento\ConfigurableProduct\Ui\Component\Listing\AssociatedProduct\Attribute\Repository</argument>
3535
</arguments>
3636
</type>
37+
<preference for="Magento\ConfigurableProduct\Model\AttributesListInterface" type="Magento\ConfigurableProduct\Model\AttributesList" />
3738
</config>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Swatches\Model;
7+
8+
use Magento\ConfigurableProduct\Model\AttributesListInterface;
9+
10+
class AttributesList implements AttributesListInterface
11+
{
12+
/**
13+
* @var \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory
14+
*/
15+
protected $collectionFactory;
16+
17+
/**
18+
* @var \Magento\Swatches\Helper\Data
19+
*/
20+
protected $dataHelper;
21+
22+
/**
23+
* @param \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $collectionFactory
24+
* @param \Magento\Swatches\Helper\Data $dataHelper
25+
*/
26+
public function __construct(
27+
\Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $collectionFactory,
28+
\Magento\Swatches\Helper\Data $dataHelper
29+
) {
30+
$this->collectionFactory = $collectionFactory;
31+
$this->dataHelper = $dataHelper;
32+
}
33+
34+
/**
35+
* Retrieve list of attributes
36+
*
37+
* @param array $ids
38+
* @return array
39+
*/
40+
public function getAttributes($ids)
41+
{
42+
$collection = $this->collectionFactory->create();
43+
$collection->addFieldToFilter('main_table.attribute_id', $ids);
44+
$attributes = [];
45+
foreach ($collection->getItems() as $attribute) {
46+
$attributes[] = [
47+
'id' => $attribute->getId(),
48+
'label' => $attribute->getFrontendLabel(),
49+
'code' => $attribute->getAttributeCode(),
50+
'options' => $attribute->getSource()->getAllOptions(false),
51+
'canCreateOption' => !$this->dataHelper->isSwatchAttribute($attribute),
52+
];
53+
}
54+
return $attributes;
55+
}
56+
}

app/code/Magento/Swatches/Model/Plugin/EavAttribute.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ public function afterAfterSave(Attribute $attribute)
120120
*/
121121
protected function setProperOptionsArray(Attribute $attribute)
122122
{
123+
$canReplace = false;
123124
if ($this->swatchHelper->isVisualSwatch($attribute)) {
124125
$canReplace = true;
125126
$defaultValue = $attribute->getData('defaultvisual');

0 commit comments

Comments
 (0)