99use Magento \Catalog \Model \Entity \Attribute ;
1010use Magento \Catalog \Model \Product ;
1111use Magento \Framework \Phrase ;
12+ use Magento \MediaStorage \Helper \File \Storage \Database ;
1213
1314/**
1415 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -50,6 +51,11 @@ class ContentTest extends \PHPUnit\Framework\TestCase
5051 */
5152 protected $ imageHelper ;
5253
54+ /**
55+ * @var \Magento\MediaStorage\Helper\File\Storage\Database|\PHPUnit_Framework_MockObject_MockObject
56+ */
57+ protected $ databaseMock ;
58+
5359 /**
5460 * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
5561 */
@@ -71,13 +77,18 @@ public function setUp()
7177 ->disableOriginalConstructor ()
7278 ->getMock ();
7379
80+ $ this ->databaseMock = $ this ->getMockBuilder (Database::class)
81+ ->disableOriginalConstructor ()
82+ ->getMock ();
83+
7484 $ this ->objectManager = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
7585 $ this ->content = $ this ->objectManager ->getObject (
7686 \Magento \Catalog \Block \Adminhtml \Product \Helper \Form \Gallery \Content::class,
7787 [
7888 'mediaConfig ' => $ this ->mediaConfigMock ,
7989 'jsonEncoder ' => $ this ->jsonEncoderMock ,
80- 'filesystem ' => $ this ->fileSystemMock
90+ 'filesystem ' => $ this ->fileSystemMock ,
91+ 'fileStorageDatabase ' => $ this ->databaseMock
8192 ]
8293 );
8394 }
@@ -143,6 +154,13 @@ public function testGetImagesJson()
143154 $ this ->readMock ->expects ($ this ->any ())->method ('stat ' )->willReturnMap ($ sizeMap );
144155 $ this ->jsonEncoderMock ->expects ($ this ->once ())->method ('encode ' )->willReturnCallback ('json_encode ' );
145156
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+
146164 $ this ->assertSame (json_encode ($ imagesResult ), $ this ->content ->getImagesJson ());
147165 }
148166
@@ -221,6 +239,13 @@ public function testGetImagesJsonWithException()
221239 $ this ->imageHelper ->expects ($ this ->any ())->method ('getDefaultPlaceholderUrl ' )->willReturn ($ placeholderUrl );
222240 $ this ->jsonEncoderMock ->expects ($ this ->once ())->method ('encode ' )->willReturnCallback ('json_encode ' );
223241
242+ $ this ->readMock ->expects ($ this ->any ())
243+ ->method ('isFile ' )
244+ ->will ($ this ->returnValue (true ));
245+ $ this ->databaseMock ->expects ($ this ->any ())
246+ ->method ('checkDbUsage ' )
247+ ->will ($ this ->returnValue (false ));
248+
224249 $ this ->assertSame (json_encode ($ imagesResult ), $ this ->content ->getImagesJson ());
225250 }
226251
@@ -365,4 +390,52 @@ private function getMediaAttribute(string $label, string $attributeCode)
365390
366391 return $ mediaAttribute ;
367392 }
393+
394+ /**
395+ * Test GetImagesJson() calls MediaStorage functions to obtain image from DB prior to stat call
396+ *
397+ * @return void
398+ */
399+ public function testGetImagesJsonMediaStorageMode ()
400+ {
401+ $ images = [
402+ 'images ' => [
403+ [
404+ 'value_id ' => '0 ' ,
405+ 'file ' => 'file_1.jpg ' ,
406+ 'media_type ' => 'image ' ,
407+ 'position ' => '0 '
408+ ]
409+ ]
410+ ];
411+
412+ $ mediaPath = [
413+ ['file_1.jpg ' , 'catalog/product/image_1.jpg ' ]
414+ ];
415+
416+ $ this ->content ->setElement ($ this ->galleryMock );
417+
418+ $ this ->galleryMock ->expects ($ this ->once ())
419+ ->method ('getImages ' )
420+ ->willReturn ($ images );
421+ $ this ->fileSystemMock ->expects ($ this ->once ())
422+ ->method ('getDirectoryRead ' )
423+ ->willReturn ($ this ->readMock );
424+ $ this ->mediaConfigMock ->expects ($ this ->any ())
425+ ->method ('getMediaPath ' )
426+ ->willReturnMap ($ mediaPath );
427+
428+ $ this ->readMock ->expects ($ this ->any ())
429+ ->method ('isFile ' )
430+ ->will ($ this ->returnValue (false ));
431+ $ this ->databaseMock ->expects ($ this ->any ())
432+ ->method ('checkDbUsage ' )
433+ ->will ($ this ->returnValue (true ));
434+
435+ $ this ->databaseMock ->expects ($ this ->once ())
436+ ->method ('saveFileToFilesystem ' )
437+ ->with ('catalog/product/image_1.jpg ' );
438+
439+ $ this ->content ->getImagesJson ();
440+ }
368441}
0 commit comments