3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+
6
7
namespace Magento \Catalog \Test \Unit \Model \Category \Attribute \Backend ;
7
8
8
9
use Magento \Framework \App \Filesystem \DirectoryList ;
10
+ use Magento \Framework \Exception \FileSystemException ;
9
11
use Magento \Framework \Filesystem \Directory \WriteInterface ;
12
+ use Magento \Store \Model \Store ;
13
+ use Magento \Store \Model \StoreManagerInterface ;
10
14
11
15
/**
12
16
* Test for Magento\Catalog\Model\Category\Attribute\Backend\Image class.
@@ -39,6 +43,16 @@ class ImageTest extends \PHPUnit\Framework\TestCase
39
43
*/
40
44
private $ filesystem ;
41
45
46
+ /**
47
+ * @var StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject;
48
+ */
49
+ private $ storeManagerInterfaceMock ;
50
+
51
+ /**
52
+ * @var Store|\PHPUnit_Framework_MockObject_MockObject
53
+ */
54
+ private $ storeMock ;
55
+
42
56
/**
43
57
* @inheritdoc
44
58
*/
@@ -56,10 +70,6 @@ protected function setUp()
56
70
['getName ' ]
57
71
);
58
72
59
- $ this ->attribute ->expects ($ this ->once ())
60
- ->method ('getName ' )
61
- ->will ($ this ->returnValue ('test_attribute ' ));
62
-
63
73
$ this ->logger = $ this ->getMockForAbstractClass (
64
74
\Psr \Log \LoggerInterface::class,
65
75
[],
@@ -75,6 +85,14 @@ protected function setUp()
75
85
['moveFileFromTmp ' , 'getBasePath ' ]
76
86
);
77
87
88
+ $ this ->storeManagerInterfaceMock = $ this ->getMockBuilder (
89
+ StoreManagerInterface::class
90
+ )->disableOriginalConstructor ()->getMock ();
91
+
92
+ $ this ->storeMock = $ this ->getMockBuilder (
93
+ Store::class
94
+ )->disableOriginalConstructor ()->getMock ();
95
+
78
96
$ this ->filesystem = $ this ->getMockBuilder (\Magento \Framework \Filesystem::class)->disableOriginalConstructor ()
79
97
->getMock ();
80
98
}
@@ -97,6 +115,10 @@ public function deletedValueDataProvider()
97
115
*/
98
116
public function testBeforeSaveValueDeletion ($ value )
99
117
{
118
+ $ this ->attribute ->expects ($ this ->once ())
119
+ ->method ('getName ' )
120
+ ->will ($ this ->returnValue ('test_attribute ' ));
121
+
100
122
$ model = $ this ->objectManager ->getObject (\Magento \Catalog \Model \Category \Attribute \Backend \Image::class);
101
123
$ model ->setAttribute ($ this ->attribute );
102
124
@@ -132,6 +154,10 @@ public function invalidValueDataProvider()
132
154
*/
133
155
public function testBeforeSaveValueInvalid ($ value )
134
156
{
157
+ $ this ->attribute ->expects ($ this ->once ())
158
+ ->method ('getName ' )
159
+ ->will ($ this ->returnValue ('test_attribute ' ));
160
+
135
161
$ model = $ this ->objectManager ->getObject (\Magento \Catalog \Model \Category \Attribute \Backend \Image::class);
136
162
$ model ->setAttribute ($ this ->attribute );
137
163
@@ -147,7 +173,11 @@ public function testBeforeSaveValueInvalid($value)
147
173
*/
148
174
public function testBeforeSaveAttributeFileName ()
149
175
{
150
- $ model = $ this ->setUpModelForAfterSave ();
176
+ $ this ->attribute ->expects ($ this ->once ())
177
+ ->method ('getName ' )
178
+ ->will ($ this ->returnValue ('test_attribute ' ));
179
+
180
+ $ model = $ this ->setUpModelForTests ();
151
181
$ mediaDirectoryMock = $ this ->createMock (WriteInterface::class);
152
182
$ this ->filesystem ->expects ($ this ->once ())
153
183
->method ('getDirectoryWrite ' )
@@ -177,7 +207,11 @@ public function testBeforeSaveAttributeFileName()
177
207
*/
178
208
public function testBeforeSaveAttributeFileNameOutsideOfCategoryDir ()
179
209
{
180
- $ model = $ this ->setUpModelForAfterSave ();
210
+ $ this ->attribute ->expects ($ this ->once ())
211
+ ->method ('getName ' )
212
+ ->will ($ this ->returnValue ('test_attribute ' ));
213
+
214
+ $ model = $ this ->setUpModelForTests ();
181
215
$ model ->setAttribute ($ this ->attribute );
182
216
$ imagePath = '/pub/media/wysiwyg/test123.jpg ' ;
183
217
$ this ->filesystem
@@ -211,7 +245,19 @@ public function testBeforeSaveAttributeFileNameOutsideOfCategoryDir()
211
245
*/
212
246
public function testBeforeSaveTemporaryAttribute ()
213
247
{
214
- $ model = $ this ->setUpModelForAfterSave ();
248
+ $ this ->attribute ->expects ($ this ->once ())
249
+ ->method ('getName ' )
250
+ ->will ($ this ->returnValue ('test_attribute ' ));
251
+
252
+ $ this ->storeManagerInterfaceMock ->expects ($ this ->once ())
253
+ ->method ('getStore ' )
254
+ ->willReturn ($ this ->storeMock );
255
+
256
+ $ this ->storeMock ->expects ($ this ->once ())
257
+ ->method ('getBaseMediaDir ' )
258
+ ->willReturn ('pub/media ' );
259
+
260
+ $ model = $ this ->setUpModelForTests ();
215
261
$ model ->setAttribute ($ this ->attribute );
216
262
217
263
$ mediaDirectoryMock = $ this ->createMock (WriteInterface::class);
@@ -220,10 +266,16 @@ public function testBeforeSaveTemporaryAttribute()
220
266
->with (DirectoryList::MEDIA )
221
267
->willReturn ($ mediaDirectoryMock );
222
268
269
+ $ this ->imageUploader ->expects ($ this ->any ())->method ('moveFileFromTmp ' )->willReturn ('test123.jpg ' );
270
+
223
271
$ object = new \Magento \Framework \DataObject (
224
272
[
225
273
'test_attribute ' => [
226
- ['name ' => 'test123.jpg ' , 'tmp_name ' => 'abc123 ' , 'url ' => 'http://www.example.com/test123.jpg ' ],
274
+ [
275
+ 'name ' => 'test123.jpg ' ,
276
+ 'tmp_name ' => 'abc123 ' ,
277
+ 'url ' => 'http://www.example.com/pub/media/temp/test123.jpg '
278
+ ],
227
279
],
228
280
]
229
281
);
@@ -232,7 +284,7 @@ public function testBeforeSaveTemporaryAttribute()
232
284
233
285
$ this ->assertEquals (
234
286
[
235
- ['name ' => 'test123.jpg ' , 'tmp_name ' => 'abc123 ' , 'url ' => 'http://www.example.com /test123.jpg ' ],
287
+ ['name ' => '/pub/media/ test123.jpg ' , 'tmp_name ' => 'abc123 ' , 'url ' => '/pub/media /test123.jpg ' ],
236
288
],
237
289
$ object ->getData ('_additional_data_test_attribute ' )
238
290
);
@@ -257,7 +309,7 @@ public function testBeforeSaveAttributeStringValue()
257
309
/**
258
310
* @return \Magento\Catalog\Model\Category\Attribute\Backend\Image
259
311
*/
260
- private function setUpModelForAfterSave ()
312
+ private function setUpModelForTests ()
261
313
{
262
314
$ objectManagerMock = $ this ->createPartialMock (\Magento \Framework \App \ObjectManager::class, ['get ' ]);
263
315
@@ -283,6 +335,7 @@ function ($class, $params = []) use ($imageUploaderMock) {
283
335
'objectManager ' => $ objectManagerMock ,
284
336
'logger ' => $ this ->logger ,
285
337
'filesystem ' => $ this ->filesystem ,
338
+ 'storeManager ' => $ this ->storeManagerInterfaceMock
286
339
]
287
340
);
288
341
$ this ->objectManager ->setBackwardCompatibleProperty ($ model , 'imageUploader ' , $ this ->imageUploader );
@@ -307,12 +360,13 @@ public function attributeValueDataProvider()
307
360
* @dataProvider attributeValueDataProvider
308
361
*
309
362
* @param array $value
363
+ * @throws FileSystemException
310
364
*/
311
- public function testAfterSaveWithAdditionalData ($ value )
365
+ public function testBeforeSaveWithAdditionalData ($ value )
312
366
{
313
- $ model = $ this ->setUpModelForAfterSave ();
367
+ $ model = $ this ->setUpModelForTests ();
314
368
315
- $ this ->imageUploader ->expects ($ this ->once ())
369
+ $ this ->imageUploader ->expects ($ this ->never ())
316
370
->method ('moveFileFromTmp ' )
317
371
->with ($ this ->equalTo ('test1234.jpg ' ));
318
372
@@ -323,17 +377,18 @@ public function testAfterSaveWithAdditionalData($value)
323
377
]
324
378
);
325
379
326
- $ model ->afterSave ($ object );
380
+ $ model ->beforeSave ($ object );
327
381
}
328
382
329
383
/**
330
384
* @dataProvider attributeValueDataProvider
331
385
*
332
386
* @param array $value
387
+ * @throws FileSystemException
333
388
*/
334
- public function testAfterSaveWithoutAdditionalData ($ value )
389
+ public function testBeforeSaveWithoutAdditionalData ($ value )
335
390
{
336
- $ model = $ this ->setUpModelForAfterSave ();
391
+ $ model = $ this ->setUpModelForTests ();
337
392
338
393
$ this ->imageUploader ->expects ($ this ->never ())
339
394
->method ('moveFileFromTmp ' );
@@ -344,15 +399,38 @@ public function testAfterSaveWithoutAdditionalData($value)
344
399
]
345
400
);
346
401
347
- $ model ->afterSave ($ object );
402
+ $ model ->beforeSave ($ object );
348
403
}
349
404
350
405
/**
351
406
* Test afterSaveWithExceptions.
352
407
*/
353
- public function testAfterSaveWithExceptions ()
408
+ public function testBeforeSaveWithExceptions ()
354
409
{
355
- $ model = $ this ->setUpModelForAfterSave ();
410
+ $ model = $ this ->setUpModelForTests ();
411
+
412
+ $ this ->storeManagerInterfaceMock ->expects ($ this ->once ())
413
+ ->method ('getStore ' )
414
+ ->willReturn ($ this ->storeMock );
415
+
416
+ $ this ->storeMock ->expects ($ this ->once ())
417
+ ->method ('getBaseMediaDir ' )
418
+ ->willReturn ('pub/media ' );
419
+
420
+ $ this ->attribute ->expects ($ this ->once ())
421
+ ->method ('getName ' )
422
+ ->will ($ this ->returnValue ('_additional_data_test_attribute ' ));
423
+
424
+ $ mediaDirectoryMock = $ this ->createMock (WriteInterface::class);
425
+ $ this ->filesystem ->expects ($ this ->any ())
426
+ ->method ('getDirectoryWrite ' )
427
+ ->with (DirectoryList::MEDIA )
428
+ ->willReturn ($ mediaDirectoryMock );
429
+ $ this ->imageUploader ->expects ($ this ->any ())->method ('getBasePath ' )->willReturn ('base/path ' );
430
+ $ mediaDirectoryMock ->expects ($ this ->any ())
431
+ ->method ('getAbsolutePath ' )
432
+ ->with ('base/path/test1234.jpg ' )
433
+ ->willReturn ('absolute/path/base/path/test1234.jpg ' );
356
434
357
435
$ exception = new \Exception ();
358
436
@@ -370,6 +448,6 @@ public function testAfterSaveWithExceptions()
370
448
]
371
449
);
372
450
373
- $ model ->afterSave ($ object );
451
+ $ model ->beforeSave ($ object );
374
452
}
375
453
}
0 commit comments