5
5
*/
6
6
namespace Magento \Catalog \Controller \Adminhtml \Product \Action ;
7
7
8
+ use Magento \Backend \Model \Session ;
9
+ use Magento \Catalog \Block \Product \ListProduct ;
10
+ use Magento \Catalog \Helper \Product \Edit \Action \Attribute ;
11
+ use Magento \Catalog \Model \CategoryFactory ;
8
12
use Magento \Catalog \Model \Product \Visibility ;
13
+ use Magento \Framework \Message \MessageInterface ;
9
14
use Magento \Catalog \Model \ProductRepository ;
10
15
use Magento \Framework \App \Request \Http as HttpRequest ;
16
+ use Magento \Framework \UrlInterface ;
11
17
use Magento \TestFramework \Helper \Bootstrap ;
18
+ use Magento \TestFramework \MessageQueue \EnvironmentPreconditionException ;
19
+ use Magento \TestFramework \MessageQueue \PreconditionFailedException ;
12
20
use Magento \TestFramework \MessageQueue \PublisherConsumerController ;
21
+ use Magento \TestFramework \TestCase \AbstractBackendController ;
13
22
14
23
/**
15
24
* @magentoAppArea adminhtml
25
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
16
26
*/
17
- class AttributeTest extends \ Magento \ TestFramework \ TestCase \ AbstractBackendController
27
+ class AttributeTest extends AbstractBackendController
18
28
{
19
29
/** @var PublisherConsumerController */
20
30
private $ publisherConsumerController ;
21
31
private $ consumers = ['product_action_attribute.update ' ];
22
32
23
33
protected function setUp ()
24
34
{
25
- $ this ->publisherConsumerController = Bootstrap::getObjectManager ()->create (
35
+ parent ::setUp ();
36
+
37
+ $ this ->publisherConsumerController = $ this ->_objectManager ->create (
26
38
PublisherConsumerController::class,
27
39
[
28
40
'consumers ' => $ this ->consumers ,
@@ -34,15 +46,13 @@ protected function setUp()
34
46
35
47
try {
36
48
$ this ->publisherConsumerController ->startConsumers ();
37
- } catch (\ Magento \ TestFramework \ MessageQueue \ EnvironmentPreconditionException $ e ) {
49
+ } catch (EnvironmentPreconditionException $ e ) {
38
50
$ this ->markTestSkipped ($ e ->getMessage ());
39
- } catch (\ Magento \ TestFramework \ MessageQueue \ PreconditionFailedException $ e ) {
51
+ } catch (PreconditionFailedException $ e ) {
40
52
$ this ->fail (
41
53
$ e ->getMessage ()
42
54
);
43
55
}
44
-
45
- parent ::setUp ();
46
56
}
47
57
48
58
protected function tearDown ()
@@ -59,21 +69,19 @@ protected function tearDown()
59
69
*/
60
70
public function testSaveActionRedirectsSuccessfully ()
61
71
{
62
- $ objectManager = Bootstrap::getObjectManager ();
63
-
64
- /** @var $session \Magento\Backend\Model\Session */
65
- $ session = $ objectManager ->get (\Magento \Backend \Model \Session::class);
72
+ /** @var $session Session */
73
+ $ session = $ this ->_objectManager ->get (Session::class);
66
74
$ session ->setProductIds ([1 ]);
67
75
$ this ->getRequest ()->setMethod (HttpRequest::METHOD_POST );
68
76
69
77
$ this ->dispatch ('backend/catalog/product_action_attribute/save/store/0 ' );
70
78
71
79
$ this ->assertEquals (302 , $ this ->getResponse ()->getHttpResponseCode ());
72
80
/** @var \Magento\Backend\Model\UrlInterface $urlBuilder */
73
- $ urlBuilder = $ objectManager -> get (\ Magento \ Framework \ UrlInterface::class);
81
+ $ urlBuilder = $ this -> _objectManager -> get (UrlInterface::class);
74
82
75
- /** @var \Magento\Catalog\Helper\Product\Edit\Action\ Attribute $attributeHelper */
76
- $ attributeHelper = $ objectManager -> get (\ Magento \ Catalog \ Helper \ Product \ Edit \ Action \ Attribute::class);
83
+ /** @var Attribute $attributeHelper */
84
+ $ attributeHelper = $ this -> _objectManager -> get (Attribute::class);
77
85
$ expectedUrl = $ urlBuilder ->getUrl (
78
86
'catalog/product/index ' ,
79
87
['store ' => $ attributeHelper ->getSelectedStoreId ()]
@@ -98,32 +106,25 @@ public function testSaveActionRedirectsSuccessfully()
98
106
*/
99
107
public function testSaveActionChangeVisibility ($ attributes )
100
108
{
101
- $ objectManager = Bootstrap::getObjectManager ();
102
109
/** @var ProductRepository $repository */
103
- $ repository = Bootstrap::getObjectManager ()->create (
104
- ProductRepository::class
105
- );
110
+ $ repository = $ this ->_objectManager ->create (ProductRepository::class);
106
111
$ product = $ repository ->get ('simple ' );
107
112
$ product ->setOrigData ();
108
113
$ product ->setVisibility (Visibility::VISIBILITY_NOT_VISIBLE );
109
114
$ product ->save ();
110
115
111
- /** @var $session \Magento\Backend\Model\ Session */
112
- $ session = $ objectManager -> get (\ Magento \ Backend \ Model \ Session::class);
116
+ /** @var $session Session */
117
+ $ session = $ this -> _objectManager -> get (Session::class);
113
118
$ session ->setProductIds ([$ product ->getId ()]);
114
119
$ this ->getRequest ()->setParam ('attributes ' , $ attributes );
115
120
$ this ->getRequest ()->setMethod (HttpRequest::METHOD_POST );
116
121
117
122
$ this ->dispatch ('backend/catalog/product_action_attribute/save/store/0 ' );
118
123
119
124
/** @var \Magento\Catalog\Model\Category $category */
120
- $ categoryFactory = Bootstrap::getObjectManager ()->get (
121
- \Magento \Catalog \Model \CategoryFactory::class
122
- );
123
- /** @var \Magento\Catalog\Block\Product\ListProduct $listProduct */
124
- $ listProduct = Bootstrap::getObjectManager ()->get (
125
- \Magento \Catalog \Block \Product \ListProduct::class
126
- );
125
+ $ categoryFactory = $ this ->_objectManager ->get (CategoryFactory::class);
126
+ /** @var ListProduct $listProduct */
127
+ $ listProduct = $ this ->_objectManager ->get (ListProduct::class);
127
128
128
129
$ this ->publisherConsumerController ->waitForAsynchronousResult (
129
130
function () use ($ repository ) {
@@ -159,10 +160,8 @@ function () use ($repository) {
159
160
*/
160
161
public function testValidateActionWithMassUpdate ($ attributes )
161
162
{
162
- $ objectManager = Bootstrap::getObjectManager ();
163
-
164
- /** @var $session \Magento\Backend\Model\Session */
165
- $ session = $ objectManager ->get (\Magento \Backend \Model \Session::class);
163
+ /** @var $session Session */
164
+ $ session = $ this ->_objectManager ->get (Session::class);
166
165
$ session ->setProductIds ([1 , 2 ]);
167
166
168
167
$ this ->getRequest ()->setParam ('attributes ' , $ attributes );
@@ -214,4 +213,34 @@ public function saveActionVisibilityAttrDataProvider()
214
213
['arguments ' => ['visibility ' => Visibility::VISIBILITY_IN_CATALOG ]]
215
214
];
216
215
}
216
+
217
+ /**
218
+ * Assert that custom layout update can not be change for existing entity.
219
+ *
220
+ * @return void
221
+ * @magentoDataFixture Magento/Catalog/_files/product_simple.php
222
+ */
223
+ public function testSaveActionCantChangeCustomLayoutUpdate (): void
224
+ {
225
+ /** @var ProductRepository $repository */
226
+ $ repository = $ this ->_objectManager ->get (ProductRepository::class);
227
+ $ product = $ repository ->get ('simple ' );
228
+
229
+ $ product ->setOrigData ('custom_layout_update ' , 'test ' );
230
+ $ product ->setData ('custom_layout_update ' , 'test ' );
231
+ $ product ->save ();
232
+ /** @var $session Session */
233
+ $ session = $ this ->_objectManager ->get (Session::class);
234
+ $ session ->setProductIds ([$ product ->getId ()]);
235
+ $ this ->getRequest ()->setParam ('attributes ' , ['custom_layout_update ' => 'test2 ' ]);
236
+ $ this ->getRequest ()->setMethod (HttpRequest::METHOD_POST );
237
+
238
+ $ this ->dispatch ('backend/catalog/product_action_attribute/save/store/0 ' );
239
+
240
+ $ this ->assertSessionMessages (
241
+ $ this ->equalTo (['Custom layout update text cannot be changed, only removed ' ]),
242
+ MessageInterface::TYPE_ERROR
243
+ );
244
+ $ this ->assertEquals ('test ' , $ product ->getData ('custom_layout_update ' ));
245
+ }
217
246
}
0 commit comments