3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace Magento \Catalog \Controller \Adminhtml \Product \Set ;
7
9
8
10
use Magento \Eav \Api \AttributeSetRepositoryInterface ;
9
11
use Magento \Eav \Api \Data \AttributeSetInterface ;
10
12
use Magento \Framework \Api \SearchCriteriaBuilder ;
11
13
use Magento \TestFramework \Helper \Bootstrap ;
12
14
use Magento \Framework \App \Request \Http as HttpRequest ;
15
+ use Magento \Eav \Api \AttributeManagementInterface ;
16
+ use Magento \Catalog \Api \Data \ProductInterfaceFactory ;
17
+ use Magento \Framework \Api \DataObjectHelper ;
18
+ use Magento \Catalog \Api \ProductRepositoryInterface ;
19
+ use Magento \Catalog \Api \Data \ProductInterface ;
20
+ use Magento \Developer \Model \Logger \Handler \Syslog ;
21
+ use Magento \Framework \Logger \Monolog ;
22
+ use Magento \Catalog \Model \Product \Attribute \Repository ;
13
23
24
+ /**
25
+ * Test save attribute set
26
+ *
27
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
28
+ */
14
29
class SaveTest extends \Magento \TestFramework \TestCase \AbstractBackendController
15
30
{
31
+ /**
32
+ * @var string
33
+ */
34
+ private $ systemLogPath = '' ;
35
+
36
+ /**
37
+ * @var Monolog
38
+ */
39
+ private $ logger ;
40
+
41
+ /**
42
+ * @var Syslog
43
+ */
44
+ private $ syslogHandler ;
45
+
46
+ /**
47
+ * @var AttributeManagementInterface
48
+ */
49
+ private $ attributeManagement ;
50
+
51
+ /**
52
+ * @var DataObjectHelper
53
+ */
54
+ private $ dataObjectHelper ;
55
+
56
+ /**
57
+ * @var ProductRepositoryInterface
58
+ */
59
+ private $ productRepository ;
60
+
61
+ /**
62
+ * @var Repository
63
+ */
64
+ private $ attributeRepository ;
65
+
66
+ /**
67
+ * @inheritDoc
68
+ */
69
+ public function setUp ()
70
+ {
71
+ parent ::setUp ();
72
+ $ this ->logger = $ this ->_objectManager ->get (Monolog::class);
73
+ $ this ->syslogHandler = $ this ->_objectManager ->create (
74
+ Syslog::class,
75
+ [
76
+ 'filePath ' => Bootstrap::getInstance ()->getAppTempDir (),
77
+ ]
78
+ );
79
+ $ this ->attributeManagement = $ this ->_objectManager ->get (AttributeManagementInterface::class);
80
+ $ this ->productRepository = $ this ->_objectManager ->get (ProductRepositoryInterface::class);
81
+ $ this ->attributeRepository = $ this ->_objectManager ->get (Repository::class);
82
+ $ this ->dataObjectHelper = $ this ->_objectManager ->get (DataObjectHelper::class);
83
+ }
84
+
85
+ /**
86
+ * @inheritdoc
87
+ * @throws \Magento\Framework\Exception\FileSystemException
88
+ */
89
+ public function tearDown ()
90
+ {
91
+ $ this ->attributeRepository ->get ('country_of_manufacture ' )->setIsUserDefined (false );
92
+ parent ::tearDown ();
93
+ }
94
+
16
95
/**
17
96
* @magentoDataFixture Magento/Catalog/_files/attribute_set_with_renamed_group.php
18
97
*/
@@ -22,17 +101,22 @@ public function testAlreadyExistsExceptionProcessingWhenGroupCodeIsDuplicated()
22
101
$ this ->assertNotEmpty ($ attributeSet , 'Attribute set with name "attribute_set_test" is missed ' );
23
102
24
103
$ this ->getRequest ()->setMethod (HttpRequest::METHOD_POST );
25
- $ this ->getRequest ()->setPostValue ('data ' , json_encode ([
26
- 'attribute_set_name ' => 'attribute_set_test ' ,
27
- 'groups ' => [
28
- ['ynode-418 ' , 'attribute-group-name ' , 1 ],
29
- ],
30
- 'attributes ' => [
31
- ['9999 ' , 'ynode-418 ' , 1 , null ]
32
- ],
33
- 'not_attributes ' => [],
34
- 'removeGroups ' => [],
35
- ]));
104
+ $ this ->getRequest ()->setPostValue (
105
+ 'data ' ,
106
+ json_encode (
107
+ [
108
+ 'attribute_set_name ' => 'attribute_set_test ' ,
109
+ 'groups ' => [
110
+ ['ynode-418 ' , 'attribute-group-name ' , 1 ],
111
+ ],
112
+ 'attributes ' => [
113
+ ['9999 ' , 'ynode-418 ' , 1 , null ]
114
+ ],
115
+ 'not_attributes ' => [],
116
+ 'removeGroups ' => [],
117
+ ]
118
+ )
119
+ );
36
120
$ this ->dispatch ('backend/catalog/product_set/save/id/ ' . $ attributeSet ->getAttributeSetId ());
37
121
38
122
$ jsonResponse = json_decode ($ this ->getResponse ()->getBody ());
@@ -63,4 +147,86 @@ protected function getAttributeSetByName($attributeSetName)
63
147
$ items = $ result ->getItems ();
64
148
return $ result ->getTotalCount () ? array_pop ($ items ) : null ;
65
149
}
150
+
151
+ /**
152
+ * Test behavior when attribute set was changed to a new set
153
+ * with deleted attribute from the previous set
154
+ *
155
+ * @magentoDataFixture Magento/Catalog/_files/product_simple.php
156
+ * @magentoDataFixture Magento/Catalog/_files/attribute_set_based_on_default.php
157
+ * @magentoDbIsolation disabled
158
+ */
159
+ public function testRemoveAttributeFromAttributeSet ()
160
+ {
161
+ $ message = 'Attempt to load value of nonexistent EAV attribute ' ;
162
+ $ this ->removeSyslog ();
163
+ $ attributeSet = $ this ->getAttributeSetByName ('new_attribute_set ' );
164
+ $ product = $ this ->productRepository ->get ('simple ' );
165
+ $ this ->attributeRepository ->get ('country_of_manufacture ' )->setIsUserDefined (true );
166
+ $ this ->attributeManagement ->unassign ($ attributeSet ->getId (), 'country_of_manufacture ' );
167
+ $ productData = [
168
+ 'country_of_manufacture ' => 'Angola '
169
+ ];
170
+ $ this ->dataObjectHelper ->populateWithArray ($ product , $ productData , ProductInterface::class);
171
+ $ this ->productRepository ->save ($ product );
172
+ $ product ->setAttributeSetId ($ attributeSet ->getId ());
173
+ $ product = $ this ->productRepository ->save ($ product );
174
+ $ this ->dispatch ('backend/catalog/product/edit/id/ ' . $ product ->getEntityId ());
175
+ $ this ->assertNotContains ($ message , $ this ->getSyslogContent ());
176
+ }
177
+
178
+ /**
179
+ * Retrieve system.log file path
180
+ *
181
+ * @return string
182
+ */
183
+ private function getSyslogPath ()
184
+ {
185
+ if (!$ this ->systemLogPath ) {
186
+ foreach ($ this ->logger ->getHandlers () as $ handler ) {
187
+ if ($ handler instanceof \Magento \Framework \Logger \Handler \System) {
188
+ $ this ->systemLogPath = $ handler ->getUrl ();
189
+ }
190
+ }
191
+ }
192
+
193
+ return $ this ->systemLogPath ;
194
+ }
195
+
196
+ /**
197
+ * Remove system.log file
198
+ *
199
+ * @return void
200
+ */
201
+ private function removeSyslog ()
202
+ {
203
+ $ this ->detachLogger ();
204
+ if (file_exists ($ this ->getSyslogPath ())) {
205
+ unlink ($ this ->getSyslogPath ());
206
+ }
207
+ }
208
+
209
+ /**
210
+ * Detach system log handler.
211
+ *
212
+ * @return void
213
+ */
214
+ private function detachLogger ()
215
+ {
216
+ $ this ->syslogHandler ->close ();
217
+ }
218
+
219
+ /**
220
+ * Retrieve content of system.log file
221
+ *
222
+ * @return bool|string
223
+ */
224
+ private function getSyslogContent ()
225
+ {
226
+ if (!file_exists ($ this ->getSyslogPath ())) {
227
+ return '' ;
228
+ }
229
+
230
+ return file_get_contents ($ this ->getSyslogPath ());
231
+ }
66
232
}
0 commit comments