9
9
use Magento \Catalog \Model \Entity \Attribute ;
10
10
use Magento \Catalog \Model \Product ;
11
11
use Magento \Framework \Phrase ;
12
+ use Magento \MediaStorage \Helper \File \Storage \Database ;
12
13
13
14
/**
14
15
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -50,6 +51,11 @@ class ContentTest extends \PHPUnit\Framework\TestCase
50
51
*/
51
52
protected $ imageHelper ;
52
53
54
+ /**
55
+ * @var \Magento\MediaStorage\Helper\File\Storage\Database|\PHPUnit_Framework_MockObject_MockObject
56
+ */
57
+ protected $ databaseMock ;
58
+
53
59
/**
54
60
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
55
61
*/
@@ -71,13 +77,18 @@ public function setUp()
71
77
->disableOriginalConstructor ()
72
78
->getMock ();
73
79
80
+ $ this ->databaseMock = $ this ->getMockBuilder (Database::class)
81
+ ->disableOriginalConstructor ()
82
+ ->getMock ();
83
+
74
84
$ this ->objectManager = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
75
85
$ this ->content = $ this ->objectManager ->getObject (
76
86
\Magento \Catalog \Block \Adminhtml \Product \Helper \Form \Gallery \Content::class,
77
87
[
78
88
'mediaConfig ' => $ this ->mediaConfigMock ,
79
89
'jsonEncoder ' => $ this ->jsonEncoderMock ,
80
- 'filesystem ' => $ this ->fileSystemMock
90
+ 'filesystem ' => $ this ->fileSystemMock ,
91
+ 'fileStorageDatabase ' => $ this ->databaseMock
81
92
]
82
93
);
83
94
}
@@ -143,6 +154,13 @@ public function testGetImagesJson()
143
154
$ this ->readMock ->expects ($ this ->any ())->method ('stat ' )->willReturnMap ($ sizeMap );
144
155
$ this ->jsonEncoderMock ->expects ($ this ->once ())->method ('encode ' )->willReturnCallback ('json_encode ' );
145
156
157
+ $ this ->readMock ->expects ($ this ->any ())
158
+ ->method ('isFile ' )
159
+ ->will ($ this ->returnValue (true ));
160
+ $ this ->databaseMock ->expects ($ this ->any ())
161
+ ->method ('checkDbUsage ' )
162
+ ->will ($ this ->returnValue (false ));
163
+
146
164
$ this ->assertSame (json_encode ($ imagesResult ), $ this ->content ->getImagesJson ());
147
165
}
148
166
@@ -210,6 +228,14 @@ public function testGetImagesJsonWithException()
210
228
$ this ->fileSystemMock ->expects ($ this ->any ())->method ('getDirectoryRead ' )->willReturn ($ this ->readMock );
211
229
$ this ->mediaConfigMock ->expects ($ this ->any ())->method ('getMediaUrl ' );
212
230
$ this ->mediaConfigMock ->expects ($ this ->any ())->method ('getMediaPath ' );
231
+
232
+ $ this ->readMock ->expects ($ this ->any ())
233
+ ->method ('isFile ' )
234
+ ->will ($ this ->returnValue (true ));
235
+ $ this ->databaseMock ->expects ($ this ->any ())
236
+ ->method ('checkDbUsage ' )
237
+ ->will ($ this ->returnValue (false ));
238
+
213
239
$ this ->readMock ->expects ($ this ->any ())->method ('stat ' )->willReturnOnConsecutiveCalls (
214
240
$ this ->throwException (
215
241
new \Magento \Framework \Exception \FileSystemException (new Phrase ('test ' ))
@@ -365,4 +391,52 @@ private function getMediaAttribute(string $label, string $attributeCode)
365
391
366
392
return $ mediaAttribute ;
367
393
}
394
+
395
+ /**
396
+ * Test GetImagesJson() calls MediaStorage functions to obtain image from DB prior to stat call
397
+ *
398
+ * @return void
399
+ */
400
+ public function testGetImagesJsonMediaStorageMode ()
401
+ {
402
+ $ images = [
403
+ 'images ' => [
404
+ [
405
+ 'value_id ' => '0 ' ,
406
+ 'file ' => 'file_1.jpg ' ,
407
+ 'media_type ' => 'image ' ,
408
+ 'position ' => '0 '
409
+ ]
410
+ ]
411
+ ];
412
+
413
+ $ mediaPath = [
414
+ ['file_1.jpg ' , 'catalog/product/image_1.jpg ' ]
415
+ ];
416
+
417
+ $ this ->content ->setElement ($ this ->galleryMock );
418
+
419
+ $ this ->galleryMock ->expects ($ this ->once ())
420
+ ->method ('getImages ' )
421
+ ->willReturn ($ images );
422
+ $ this ->fileSystemMock ->expects ($ this ->once ())
423
+ ->method ('getDirectoryRead ' )
424
+ ->willReturn ($ this ->readMock );
425
+ $ this ->mediaConfigMock ->expects ($ this ->any ())
426
+ ->method ('getMediaPath ' )
427
+ ->willReturnMap ($ mediaPath );
428
+
429
+ $ this ->readMock ->expects ($ this ->any ())
430
+ ->method ('isFile ' )
431
+ ->will ($ this ->returnValue (false ));
432
+ $ this ->databaseMock ->expects ($ this ->any ())
433
+ ->method ('checkDbUsage ' )
434
+ ->will ($ this ->returnValue (true ));
435
+
436
+ $ this ->databaseMock ->expects ($ this ->once ())
437
+ ->method ('saveFileToFilesystem ' )
438
+ ->with ('catalog/product/image_1.jpg ' );
439
+
440
+ $ this ->content ->getImagesJson ();
441
+ }
368
442
}
0 commit comments