Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit 16d2036

Browse files
author
Oleksii Korshenko
authored
Merge pull request #1861 from magento-engcom/develop-prs
Public Pull Requests magento/magento2#11619 Re saving product attribute [backport] by @raumatbel magento/magento2#12681 Add error message if not logged in for adding products to wishlist by @PieterCappelle magento/magento2#12800 Update testsuite validate_image_info_rollback.php by @harrymt Fixed Public Issues magento/magento2#6770 M2.1.1 : Re-saving a product attribute with a different name than it's code results in an error
2 parents 5017fda + c7671c1 commit 16d2036

File tree

6 files changed

+102
-56
lines changed

6 files changed

+102
-56
lines changed

app/code/Magento/Catalog/Controller/Adminhtml/Product/Attribute/Save.php

Lines changed: 76 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,75 +7,93 @@
77

88
namespace Magento\Catalog\Controller\Adminhtml\Product\Attribute;
99

10+
use Magento\Backend\App\Action\Context;
11+
use Magento\Backend\Model\View\Result\Redirect;
12+
use Magento\Catalog\Api\Data\ProductAttributeInterface;
13+
use Magento\Catalog\Controller\Adminhtml\Product\Attribute;
14+
use Magento\Catalog\Helper\Product;
15+
use Magento\Catalog\Model\Product\AttributeSet\BuildFactory;
16+
use Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory;
17+
use Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\Validator;
18+
use Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\ValidatorFactory;
19+
use Magento\Eav\Model\Entity\Attribute\Set;
20+
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory;
21+
use Magento\Framework\Cache\FrontendInterface;
22+
use Magento\Framework\Controller\Result\Json;
1023
use Magento\Framework\Controller\ResultFactory;
1124
use Magento\Framework\Exception\AlreadyExistsException;
25+
use Magento\Framework\Exception\LocalizedException;
26+
use Magento\Framework\Filter\FilterManager;
27+
use Magento\Framework\Registry;
28+
use Magento\Framework\View\LayoutFactory;
29+
use Magento\Framework\View\Result\PageFactory;
1230

1331
/**
1432
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1533
*/
16-
class Save extends \Magento\Catalog\Controller\Adminhtml\Product\Attribute
34+
class Save extends Attribute
1735
{
1836
/**
19-
* @var \Magento\Catalog\Model\Product\AttributeSet\BuildFactory
37+
* @var BuildFactory
2038
*/
2139
protected $buildFactory;
2240

2341
/**
24-
* @var \Magento\Framework\Filter\FilterManager
42+
* @var FilterManager
2543
*/
2644
protected $filterManager;
2745

2846
/**
29-
* @var \Magento\Catalog\Helper\Product
47+
* @var Product
3048
*/
3149
protected $productHelper;
3250

3351
/**
34-
* @var \Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory
52+
* @var AttributeFactory
3553
*/
3654
protected $attributeFactory;
3755

3856
/**
39-
* @var \Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\ValidatorFactory
57+
* @var ValidatorFactory
4058
*/
4159
protected $validatorFactory;
4260

4361
/**
44-
* @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory
62+
* @var CollectionFactory
4563
*/
4664
protected $groupCollectionFactory;
4765

4866
/**
49-
* @var \Magento\Framework\View\LayoutFactory
67+
* @var LayoutFactory
5068
*/
5169
private $layoutFactory;
5270

5371
/**
54-
* @param \Magento\Backend\App\Action\Context $context
55-
* @param \Magento\Framework\Cache\FrontendInterface $attributeLabelCache
56-
* @param \Magento\Framework\Registry $coreRegistry
57-
* @param \Magento\Catalog\Model\Product\AttributeSet\BuildFactory $buildFactory
58-
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
59-
* @param \Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory $attributeFactory
60-
* @param \Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\ValidatorFactory $validatorFactory
61-
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory $groupCollectionFactory
62-
* @param \Magento\Framework\Filter\FilterManager $filterManager
63-
* @param \Magento\Catalog\Helper\Product $productHelper
64-
* @param \Magento\Framework\View\LayoutFactory $layoutFactory
72+
* @param Context $context
73+
* @param FrontendInterface $attributeLabelCache
74+
* @param Registry $coreRegistry
75+
* @param BuildFactory $buildFactory
76+
* @param PageFactory $resultPageFactory
77+
* @param AttributeFactory $attributeFactory
78+
* @param ValidatorFactory $validatorFactory
79+
* @param CollectionFactory $groupCollectionFactory
80+
* @param FilterManager $filterManager
81+
* @param Product $productHelper
82+
* @param LayoutFactory $layoutFactory
6583
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
6684
*/
6785
public function __construct(
68-
\Magento\Backend\App\Action\Context $context,
69-
\Magento\Framework\Cache\FrontendInterface $attributeLabelCache,
70-
\Magento\Framework\Registry $coreRegistry,
71-
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
72-
\Magento\Catalog\Model\Product\AttributeSet\BuildFactory $buildFactory,
73-
\Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory $attributeFactory,
74-
\Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\ValidatorFactory $validatorFactory,
75-
\Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory $groupCollectionFactory,
76-
\Magento\Framework\Filter\FilterManager $filterManager,
77-
\Magento\Catalog\Helper\Product $productHelper,
78-
\Magento\Framework\View\LayoutFactory $layoutFactory
86+
Context $context,
87+
FrontendInterface $attributeLabelCache,
88+
Registry $coreRegistry,
89+
PageFactory $resultPageFactory,
90+
BuildFactory $buildFactory,
91+
AttributeFactory $attributeFactory,
92+
ValidatorFactory $validatorFactory,
93+
CollectionFactory $groupCollectionFactory,
94+
FilterManager $filterManager,
95+
Product $productHelper,
96+
LayoutFactory $layoutFactory
7997
) {
8098
parent::__construct($context, $attributeLabelCache, $coreRegistry, $resultPageFactory);
8199
$this->buildFactory = $buildFactory;
@@ -88,7 +106,7 @@ public function __construct(
88106
}
89107

90108
/**
91-
* @return \Magento\Backend\Model\View\Result\Redirect
109+
* @return Redirect
92110
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
93111
* @SuppressWarnings(PHPMD.NPathComplexity)
94112
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
@@ -105,32 +123,43 @@ public function execute()
105123
$name = trim($name);
106124

107125
try {
108-
/** @var $attributeSet \Magento\Eav\Model\Entity\Attribute\Set */
126+
/** @var $attributeSet Set */
109127
$attributeSet = $this->buildFactory->create()
110128
->setEntityTypeId($this->_entityTypeId)
111129
->setSkeletonId($setId)
112130
->setName($name)
113131
->getAttributeSet();
114132
} catch (AlreadyExistsException $alreadyExists) {
115-
$this->messageManager->addError(__('An attribute set named \'%1\' already exists.', $name));
133+
$this->messageManager->addErrorMessage(__('An attribute set named \'%1\' already exists.', $name));
116134
$this->_session->setAttributeData($data);
117135
return $this->returnResult('catalog/*/edit', ['_current' => true], ['error' => true]);
118-
} catch (\Magento\Framework\Exception\LocalizedException $e) {
119-
$this->messageManager->addError($e->getMessage());
136+
} catch (LocalizedException $e) {
137+
$this->messageManager->addErrorMessage($e->getMessage());
120138
} catch (\Exception $e) {
121-
$this->messageManager->addException($e, __('Something went wrong while saving the attribute.'));
139+
$this->messageManager->addExceptionMessage(
140+
$e,
141+
__('Something went wrong while saving the attribute.')
142+
);
122143
}
123144
}
124145

125146
$attributeId = $this->getRequest()->getParam('attribute_id');
126-
$attributeCode = $this->getRequest()->getParam('attribute_code')
127-
?: $this->generateCode($this->getRequest()->getParam('frontend_label')[0]);
147+
148+
/** @var $model ProductAttributeInterface */
149+
$model = $this->attributeFactory->create();
150+
if ($attributeId) {
151+
$model->load($attributeId);
152+
}
153+
$attributeCode = $model && $model->getId()
154+
? $model->getAttributeCode()
155+
: $this->getRequest()->getParam('attribute_code');
156+
$attributeCode = $attributeCode ?: $this->generateCode($this->getRequest()->getParam('frontend_label')[0]);
128157
if (strlen($attributeCode) > 0) {
129158
$validatorAttrCode = new \Zend_Validate_Regex(
130159
['pattern' => '/^[a-z\x{600}-\x{6FF}][a-z\x{600}-\x{6FF}_0-9]{0,30}$/u']
131160
);
132161
if (!$validatorAttrCode->isValid($attributeCode)) {
133-
$this->messageManager->addError(
162+
$this->messageManager->addErrorMessage(
134163
__(
135164
'Attribute code "%1" is invalid. Please use only letters (a-z), ' .
136165
'numbers (0-9) or underscore(_) in this field, first character should be a letter.',
@@ -148,11 +177,11 @@ public function execute()
148177

149178
//validate frontend_input
150179
if (isset($data['frontend_input'])) {
151-
/** @var $inputType \Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\Validator */
180+
/** @var $inputType Validator */
152181
$inputType = $this->validatorFactory->create();
153182
if (!$inputType->isValid($data['frontend_input'])) {
154183
foreach ($inputType->getMessages() as $message) {
155-
$this->messageManager->addError($message);
184+
$this->messageManager->addErrorMessage($message);
156185
}
157186
return $this->returnResult(
158187
'catalog/*/edit',
@@ -162,18 +191,14 @@ public function execute()
162191
}
163192
}
164193

165-
/* @var $model \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
166-
$model = $this->attributeFactory->create();
167-
168194
if ($attributeId) {
169-
$model->load($attributeId);
170195
if (!$model->getId()) {
171-
$this->messageManager->addError(__('This attribute no longer exists.'));
196+
$this->messageManager->addErrorMessage(__('This attribute no longer exists.'));
172197
return $this->returnResult('catalog/*/', [], ['error' => true]);
173198
}
174199
// entity type check
175200
if ($model->getEntityTypeId() != $this->_entityTypeId) {
176-
$this->messageManager->addError(__('We can\'t update the attribute.'));
201+
$this->messageManager->addErrorMessage(__('We can\'t update the attribute.'));
177202
$this->_session->setAttributeData($data);
178203
return $this->returnResult('catalog/*/', [], ['error' => true]);
179204
}
@@ -193,7 +218,7 @@ public function execute()
193218
);
194219
}
195220

196-
$data += ['is_filterable' => 0, 'is_filterable_in_search' => 0, 'apply_to' => []];
221+
$data += ['is_filterable' => 0, 'is_filterable_in_search' => 0];
197222

198223
if ($model->getIsUserDefined() === null || $model->getIsUserDefined() != 0) {
199224
$data['backend_type'] = $model->getBackendTypeByInput($data['frontend_input']);
@@ -241,7 +266,7 @@ public function execute()
241266

242267
try {
243268
$model->save();
244-
$this->messageManager->addSuccess(__('You saved the product attribute.'));
269+
$this->messageManager->addSuccessMessage(__('You saved the product attribute.'));
245270

246271
$this->_attributeLabelCache->clean();
247272
$this->_session->setAttributeData(false);
@@ -265,7 +290,7 @@ public function execute()
265290
}
266291
return $this->returnResult('catalog/*/', [], ['error' => false]);
267292
} catch (\Exception $e) {
268-
$this->messageManager->addError($e->getMessage());
293+
$this->messageManager->addErrorMessage($e->getMessage());
269294
$this->_session->setAttributeData($data);
270295
return $this->returnResult(
271296
'catalog/*/edit',
@@ -281,7 +306,7 @@ public function execute()
281306
* @param string $path
282307
* @param array $params
283308
* @param array $response
284-
* @return \Magento\Framework\Controller\Result\Json|\Magento\Backend\Model\View\Result\Redirect
309+
* @return Json|Redirect
285310
*/
286311
private function returnResult($path = '', array $params = [], array $response = [])
287312
{

app/code/Magento/Wishlist/Controller/Index/Plugin.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,30 @@ class Plugin
3434
*/
3535
protected $redirector;
3636

37+
/**
38+
* @var \Magento\Framework\Message\ManagerInterface
39+
*/
40+
private $messageManager;
41+
3742
/**
3843
* @param CustomerSession $customerSession
3944
* @param \Magento\Wishlist\Model\AuthenticationStateInterface $authenticationState
4045
* @param ScopeConfigInterface $config
4146
* @param RedirectInterface $redirector
47+
* @param \Magento\Framework\Message\ManagerInterface $messageManager
4248
*/
4349
public function __construct(
4450
CustomerSession $customerSession,
4551
\Magento\Wishlist\Model\AuthenticationStateInterface $authenticationState,
4652
ScopeConfigInterface $config,
47-
RedirectInterface $redirector
53+
RedirectInterface $redirector,
54+
\Magento\Framework\Message\ManagerInterface $messageManager
4855
) {
4956
$this->customerSession = $customerSession;
5057
$this->authenticationState = $authenticationState;
5158
$this->config = $config;
5259
$this->redirector = $redirector;
60+
$this->messageManager = $messageManager;
5361
}
5462

5563
/**
@@ -72,6 +80,10 @@ public function beforeDispatch(\Magento\Framework\App\ActionInterface $subject,
7280
$this->customerSession->setBeforeModuleName('wishlist');
7381
$this->customerSession->setBeforeControllerName('index');
7482
$this->customerSession->setBeforeAction('add');
83+
84+
if ($request->getActionName() == 'add') {
85+
$this->messageManager->addErrorMessage(__('You must login or register to add items to your wishlist.'));
86+
}
7587
}
7688
if (!$this->config->isSetFlag('wishlist/general/active')) {
7789
throw new NotFoundException(__('Page not found.'));

app/code/Magento/Wishlist/Test/Unit/Controller/Index/PluginTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ class PluginTest extends \PHPUnit\Framework\TestCase
2828
*/
2929
protected $redirector;
3030

31+
/**
32+
* @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
33+
*/
34+
protected $messageManager;
35+
3136
/**
3237
* @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject
3338
*/
@@ -53,6 +58,7 @@ protected function setUp()
5358
$this->authenticationState = $this->createMock(\Magento\Wishlist\Model\AuthenticationState::class);
5459
$this->config = $this->createMock(\Magento\Framework\App\Config::class);
5560
$this->redirector = $this->createMock(\Magento\Store\App\Response\Redirect::class);
61+
$this->messageManager = $this->createMock(\Magento\Framework\Message\ManagerInterface::class);
5662
$this->request = $this->createMock(\Magento\Framework\App\Request\Http::class);
5763
}
5864

@@ -63,6 +69,7 @@ protected function tearDown()
6369
$this->authenticationState,
6470
$this->config,
6571
$this->redirector,
72+
$this->messageManager,
6673
$this->request
6774
);
6875
}
@@ -73,7 +80,8 @@ protected function getPlugin()
7380
$this->customerSession,
7481
$this->authenticationState,
7582
$this->config,
76-
$this->redirector
83+
$this->redirector,
84+
$this->messageManager
7785
);
7886
}
7987

app/code/Magento/Wishlist/i18n/en_US.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,4 @@ Action,Action
116116
Configure,Configure
117117
Delete,Delete
118118
"Product Details and Comment","Product Details and Comment"
119+
"You must login or register to add items to your wishlist.","You must login or register to add items to your wishlist."

dev/tests/integration/testsuite/Magento/Catalog/Controller/Adminhtml/Product/AttributeTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,12 @@ public function testAttributeWithoutId()
128128
*/
129129
public function testWrongAttributeCode()
130130
{
131-
$postData = $this->_getAttributeData() + ['attribute_id' => '2', 'attribute_code' => '_()&&&?'];
131+
$postData = $this->_getAttributeData() + ['attribute_code' => '_()&&&?'];
132132
$this->getRequest()->setPostValue($postData);
133133
$this->dispatch('backend/catalog/product_attribute/save');
134134
$this->assertEquals(302, $this->getResponse()->getHttpResponseCode());
135135
$this->assertContains(
136-
'catalog/product_attribute/edit/attribute_id/2',
136+
'catalog/product_attribute/edit',
137137
$this->getResponse()->getHeader('Location')->getFieldValue()
138138
);
139139
/** @var \Magento\Framework\Message\Collection $messages */

dev/tests/integration/testsuite/Magento/Catalog/_files/validate_image_info_rollback.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/** @var \Magento\Framework\Filesystem $filesystem */
1010
$filesystem = $objectManager->create(\Magento\Framework\Filesystem::class);
1111

12-
/** @var Magento\Catalog\Model\Product\Media\Config $config */
12+
/** @var \Magento\Catalog\Model\Product\Media\Config $config */
1313
$config = $objectManager->get(\Magento\Catalog\Model\Product\Media\Config::class);
1414

1515
/** @var $tmpDirectory \Magento\Framework\Filesystem\Directory\WriteInterface */

0 commit comments

Comments
 (0)