6
6
namespace Magento \Catalog \Test \Unit \Model \Category \Attribute \Backend ;
7
7
8
8
use Magento \Framework \App \Filesystem \DirectoryList ;
9
+ use Magento \Framework \Filesystem \Directory \WriteInterface ;
9
10
11
+ /**
12
+ * Test for Magento\Catalog\Model\Category\Attribute\Backend\Image class.
13
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
14
+ */
10
15
class ImageTest extends \PHPUnit \Framework \TestCase
11
16
{
12
17
/**
@@ -67,7 +72,7 @@ protected function setUp()
67
72
68
73
$ this ->imageUploader = $ this ->createPartialMock (
69
74
\Magento \Catalog \Model \ImageUploader::class,
70
- ['moveFileFromTmp ' ]
75
+ ['moveFileFromTmp ' , ' getBasePath ' ]
71
76
);
72
77
73
78
$ this ->filesystem = $ this ->getMockBuilder (\Magento \Framework \Filesystem::class)->disableOriginalConstructor ()
@@ -95,9 +100,7 @@ public function testBeforeSaveValueDeletion($value)
95
100
$ model = $ this ->objectManager ->getObject (\Magento \Catalog \Model \Category \Attribute \Backend \Image::class);
96
101
$ model ->setAttribute ($ this ->attribute );
97
102
98
- $ object = new \Magento \Framework \DataObject ([
99
- 'test_attribute ' => $ value
100
- ]);
103
+ $ object = new \Magento \Framework \DataObject (['test_attribute ' => $ value ]);
101
104
102
105
$ model ->beforeSave ($ object );
103
106
@@ -132,9 +135,7 @@ public function testBeforeSaveValueInvalid($value)
132
135
$ model = $ this ->objectManager ->getObject (\Magento \Catalog \Model \Category \Attribute \Backend \Image::class);
133
136
$ model ->setAttribute ($ this ->attribute );
134
137
135
- $ object = new \Magento \Framework \DataObject ([
136
- 'test_attribute ' => $ value
137
- ]);
138
+ $ object = new \Magento \Framework \DataObject (['test_attribute ' => $ value ]);
138
139
139
140
$ model ->beforeSave ($ object );
140
141
@@ -146,14 +147,25 @@ public function testBeforeSaveValueInvalid($value)
146
147
*/
147
148
public function testBeforeSaveAttributeFileName ()
148
149
{
149
- $ model = $ this ->objectManager ->getObject (\Magento \Catalog \Model \Category \Attribute \Backend \Image::class);
150
- $ model ->setAttribute ($ this ->attribute );
150
+ $ model = $ this ->setUpModelForAfterSave ();
151
+ $ mediaDirectoryMock = $ this ->createMock (WriteInterface::class);
152
+ $ this ->filesystem ->expects ($ this ->once ())
153
+ ->method ('getDirectoryWrite ' )
154
+ ->with (DirectoryList::MEDIA )
155
+ ->willReturn ($ mediaDirectoryMock );
156
+ $ this ->imageUploader ->expects ($ this ->once ())->method ('getBasePath ' )->willReturn ('base/path ' );
157
+ $ mediaDirectoryMock ->expects ($ this ->once ())
158
+ ->method ('getAbsolutePath ' )
159
+ ->with ('base/path/test123.jpg ' )
160
+ ->willReturn ('absolute/path/base/path/test123.jpg ' );
151
161
152
- $ object = new \Magento \Framework \DataObject ([
153
- 'test_attribute ' => [
154
- ['name ' => 'test123.jpg ' ]
162
+ $ object = new \Magento \Framework \DataObject (
163
+ [
164
+ 'test_attribute ' => [
165
+ ['name ' => 'test123.jpg ' ],
166
+ ],
155
167
]
156
- ] );
168
+ );
157
169
158
170
$ model ->beforeSave ($ object );
159
171
@@ -165,30 +177,37 @@ public function testBeforeSaveAttributeFileName()
165
177
*/
166
178
public function testBeforeSaveAttributeFileNameOutsideOfCategoryDir ()
167
179
{
168
- $ model = $ this ->objectManager ->getObject (\Magento \Catalog \Model \Category \Attribute \Backend \Image::class, [
169
- 'filesystem ' => $ this ->filesystem
170
- ]);
171
-
180
+ $ model = $ this ->setUpModelForAfterSave ();
172
181
$ model ->setAttribute ($ this ->attribute );
173
182
183
+ $ mediaDirectoryMock = $ this ->createMock (WriteInterface::class);
184
+ $ this ->filesystem ->expects ($ this ->once ())
185
+ ->method ('getDirectoryWrite ' )
186
+ ->with (DirectoryList::MEDIA )
187
+ ->willReturn ($ mediaDirectoryMock );
174
188
$ this ->filesystem
175
189
->expects ($ this ->once ())
176
190
->method ('getUri ' )
177
191
->with (DirectoryList::MEDIA )
178
192
->willReturn ('pub/media ' );
193
+ $ mediaDirectoryMock ->expects ($ this ->once ())
194
+ ->method ('getAbsolutePath ' )
195
+ ->willReturn ('/pub/media/wysiwyg/test123.jpg ' );
179
196
180
- $ object = new \Magento \Framework \DataObject ([
181
- 'test_attribute ' => [
182
- [
183
- 'name ' => '/test123.jpg ' ,
184
- 'url ' => '/pub/media/wysiwyg/test123.jpg ' ,
185
- ]
197
+ $ object = new \Magento \Framework \DataObject (
198
+ [
199
+ 'test_attribute ' => [
200
+ [
201
+ 'name ' => 'test123.jpg ' ,
202
+ 'url ' => '/pub/media/wysiwyg/test123.jpg ' ,
203
+ ],
204
+ ],
186
205
]
187
- ] );
206
+ );
188
207
189
208
$ model ->beforeSave ($ object );
190
209
191
- $ this ->assertEquals ('/pub/media/wysiwyg/ test123.jpg ' , $ object ->getTestAttribute ());
210
+ $ this ->assertEquals ('test123.jpg ' , $ object ->getTestAttribute ());
192
211
$ this ->assertEquals (
193
212
[['name ' => '/pub/media/wysiwyg/test123.jpg ' , 'url ' => '/pub/media/wysiwyg/test123.jpg ' ]],
194
213
$ object ->getData ('_additional_data_test_attribute ' )
@@ -200,20 +219,31 @@ public function testBeforeSaveAttributeFileNameOutsideOfCategoryDir()
200
219
*/
201
220
public function testBeforeSaveTemporaryAttribute ()
202
221
{
203
- $ model = $ this ->objectManager -> getObject (\ Magento \ Catalog \ Model \ Category \ Attribute \ Backend \Image::class );
222
+ $ model = $ this ->setUpModelForAfterSave ( );
204
223
$ model ->setAttribute ($ this ->attribute );
205
224
206
- $ object = new \Magento \Framework \DataObject ([
207
- 'test_attribute ' => [
208
- ['name ' => 'test123.jpg ' , 'tmp_name ' => 'abc123 ' , 'url ' => 'http://www.example.com/test123.jpg ' ]
225
+ $ mediaDirectoryMock = $ this ->createMock (WriteInterface::class);
226
+ $ this ->filesystem ->expects ($ this ->once ())
227
+ ->method ('getDirectoryWrite ' )
228
+ ->with (DirectoryList::MEDIA )
229
+ ->willReturn ($ mediaDirectoryMock );
230
+
231
+ $ object = new \Magento \Framework \DataObject (
232
+ [
233
+ 'test_attribute ' => [
234
+ ['name ' => 'test123.jpg ' , 'tmp_name ' => 'abc123 ' , 'url ' => 'http://www.example.com/test123.jpg ' ],
235
+ ],
209
236
]
210
- ] );
237
+ );
211
238
212
239
$ model ->beforeSave ($ object );
213
240
214
- $ this ->assertEquals ([
215
- ['name ' => 'test123.jpg ' , 'tmp_name ' => 'abc123 ' , 'url ' => 'http://www.example.com/test123.jpg ' ]
216
- ], $ object ->getData ('_additional_data_test_attribute ' ));
241
+ $ this ->assertEquals (
242
+ [
243
+ ['name ' => 'test123.jpg ' , 'tmp_name ' => 'abc123 ' , 'url ' => 'http://www.example.com/test123.jpg ' ],
244
+ ],
245
+ $ object ->getData ('_additional_data_test_attribute ' )
246
+ );
217
247
}
218
248
219
249
/**
@@ -224,9 +254,7 @@ public function testBeforeSaveAttributeStringValue()
224
254
$ model = $ this ->objectManager ->getObject (\Magento \Catalog \Model \Category \Attribute \Backend \Image::class);
225
255
$ model ->setAttribute ($ this ->attribute );
226
256
227
- $ object = new \Magento \Framework \DataObject ([
228
- 'test_attribute ' => 'test123.jpg '
229
- ]);
257
+ $ object = new \Magento \Framework \DataObject (['test_attribute ' => 'test123.jpg ' ]);
230
258
231
259
$ model ->beforeSave ($ object );
232
260
@@ -245,18 +273,26 @@ private function setUpModelForAfterSave()
245
273
246
274
$ objectManagerMock ->expects ($ this ->any ())
247
275
->method ('get ' )
248
- ->will ($ this ->returnCallback (function ($ class , $ params = []) use ($ imageUploaderMock ) {
249
- if ($ class == \Magento \Catalog \CategoryImageUpload::class) {
250
- return $ imageUploaderMock ;
251
- }
252
-
253
- return $ this ->objectManager ->get ($ class , $ params );
254
- }));
255
-
256
- $ model = $ this ->objectManager ->getObject (\Magento \Catalog \Model \Category \Attribute \Backend \Image::class, [
257
- 'objectManager ' => $ objectManagerMock ,
258
- 'logger ' => $ this ->logger
259
- ]);
276
+ ->will (
277
+ $ this ->returnCallback (
278
+ function ($ class , $ params = []) use ($ imageUploaderMock ) {
279
+ if ($ class == \Magento \Catalog \CategoryImageUpload::class) {
280
+ return $ imageUploaderMock ;
281
+ }
282
+
283
+ return $ this ->objectManager ->get ($ class , $ params );
284
+ }
285
+ )
286
+ );
287
+
288
+ $ model = $ this ->objectManager ->getObject (
289
+ \Magento \Catalog \Model \Category \Attribute \Backend \Image::class,
290
+ [
291
+ 'objectManager ' => $ objectManagerMock ,
292
+ 'logger ' => $ this ->logger ,
293
+ 'filesystem ' => $ this ->filesystem ,
294
+ ]
295
+ );
260
296
$ this ->objectManager ->setBackwardCompatibleProperty ($ model , 'imageUploader ' , $ this ->imageUploader );
261
297
262
298
return $ model ->setAttribute ($ this ->attribute );
0 commit comments