1414use Magento \Framework \Api \Search \Document ;
1515use Magento \Framework \Api \SearchCriteriaBuilder ;
1616use Magento \Framework \App \Filesystem \DirectoryList ;
17- use Magento \Framework \Exception \IntegrationException ;
1817use Magento \Framework \Filesystem ;
1918use Magento \Framework \Filesystem \Driver \Https ;
20- use Magento \Framework \ Filesystem \ DriverInterface ;
19+ use Magento \MediaGalleryApi \ Api \ GetAssetsByPathsInterface ;
2120use Magento \TestFramework \Helper \Bootstrap ;
2221use PHPUnit \Framework \TestCase ;
2322
2625 */
2726class SaveImageTest extends TestCase
2827{
29- const URL_FIELD = 'thumbnail_240_url ' ;
30-
31- /**
32- * @var SaveImageInterface
33- */
34- private $ saveImage ;
35-
36- /**
37- * @var DriverInterface
38- */
39- private $ driver ;
40-
4128 /**
4229 * @var Filesystem
4330 */
4431 private $ fileSystem ;
4532
4633 /**
47- * @var AssetRepositoryInterface
34+ * @inheritdoc
4835 */
49- private $ assetRepository ;
36+ protected function setUp (): void
37+ {
38+ $ this ->fileSystem = Bootstrap::getObjectManager ()->get (Filesystem::class);
39+ Bootstrap::getObjectManager ()->configure ([
40+ 'preferences ' => [
41+ Https::class => HttpsDriverMock::class
42+ ]
43+ ]);
44+ }
5045
5146 /**
52- * @var SearchCriteriaBuilder
47+ * Test with image.
48+ *
49+ * @param array $documentData
50+ * @param string $sourceFile
51+ * @param string $destinationPath
52+ * @return void
53+ * @dataProvider getSaveTestDataProvider
5354 */
54- private $ criteriaBuilder ;
55+ public function testSave (array $ documentData , string $ sourceFile , string $ destinationPath ): void
56+ {
57+ $ this ->deleteImage ($ destinationPath );
58+ $ document = $ this ->getDocument ($ documentData );
59+ $ saveImage = Bootstrap::getObjectManager ()->create (SaveImageInterface::class);
60+ $ saveImage ->execute (
61+ $ document ,
62+ $ this ->getImageFilePath ($ sourceFile ),
63+ $ destinationPath
64+ );
65+ $ this ->assertImageSavedToDirectory ($ destinationPath );
66+ $ this ->assertAssets ($ destinationPath , $ documentData );
67+ $ this ->deleteImage ($ destinationPath );
68+ }
5569
5670 /**
5771 * @return array
@@ -64,93 +78,98 @@ public function getSaveTestDataProvider(): array
6478 'id ' => 1 ,
6579 'comp_url ' => 'https://test.url/magento-logo.png ' ,
6680 'width ' => 110 ,
67- 'title ' => 'test ' ,
81+ 'title ' => 'test adobe image title ' ,
6882 'content_type ' => 'image/png ' ,
6983 'height ' => 210 ,
7084 'some_bool_param ' => false ,
7185 'some_nullable_param ' => null ,
7286 'extension_attributes ' => [
73- 'title ' => 'test ' ,
87+ 'title ' => 'test adobe image title ' ,
7488 'is_downloaded ' => 0 ,
7589 'is_licensed_locally ' => 0 ,
7690 'thumbnail_240_url ' => 'https://test.url/magento-logo.png ' ,
77- 'creator_id ' => 1122 ,
91+ 'creator_id ' => random_int ( 0 , 2147483647 ) ,
7892 'creator_name ' => 'Test ' ,
7993 'path ' => 'catalog/category/tmp.png ' ,
8094 'content_type ' => 'image/png ' ,
8195 'category ' => [
82- 'id ' => 1 ,
96+ 'id ' => random_int ( 0 , 2147483647 ) ,
8397 'name ' => 'Test '
8498 ],
8599 ]
86100 ],
87101 'sourcePath ' => 'magento-logo.png ' ,
88- 'destinationPath ' => 'catalog/category/tmp .png ' ,
102+ 'destinationPath ' => 'catalog/category/adobe-stock-save-image-test .png ' ,
89103 ]
90104 ];
91105 }
92106
93107 /**
94- * @inheritdoc
108+ * Document for save.
109+ *
110+ * @param array $documentData
111+ * @return Document
95112 */
96- protected function setUp ( ): void
113+ private function getDocument ( array $ documentData ): Document
97114 {
98- $ this ->driver = Bootstrap::getObjectManager ()->get (DriverInterface::class);
99- $ this ->fileSystem = Bootstrap::getObjectManager ()->get (Filesystem::class);
100- $ this ->assetRepository = Bootstrap::getObjectManager ()->get (AssetRepositoryInterface::class);
101- $ this ->criteriaBuilder = Bootstrap::getObjectManager ()->get (SearchCriteriaBuilder::class);
102- Bootstrap::getObjectManager ()->configure ([
103- 'preferences ' => [
104- Https::class => HttpsDriverMock::class
105- ]
106- ]);
107- $ this ->saveImage = Bootstrap::getObjectManager ()->create (SaveImageInterface::class);
115+ $ document = new Document ($ documentData );
116+ $ this ->addAttributes ($ document , $ documentData ['extension_attributes ' ]);
117+ return $ document ;
108118 }
109119
110120 /**
111- * Test with image.
121+ * Check if image saved by destination path
112122 *
113- * @param array $documentData
114- * @param string $sourceFile
115123 * @param string $destinationPath
116124 * @return void
117- * @dataProvider getSaveTestDataProvider
118125 */
119- public function testSave ( array $ documentData , string $ sourceFile , string $ destinationPath ): void
126+ private function assertImageSavedToDirectory ( string $ destinationPath ): void
120127 {
121- $ this ->deleteImage ($ destinationPath );
122- $ document = $ this ->getDocument ($ documentData );
123- $ mediaDir = $ this ->fileSystem ->getDirectoryWrite (DirectoryList::MEDIA );
124- $ this ->saveImage ->execute (
125- $ document ,
126- $ this ->getImageFilePath ($ sourceFile ),
127- $ mediaDir ->getAbsolutePath ($ destinationPath )
128- );
129128 self ::assertTrue (
130129 $ this ->fileSystem ->getDirectoryRead (DirectoryList::MEDIA )->isExist ($ destinationPath ),
131130 'File was not saved by destination '
132131 );
133- $ searchCriteria = $ this ->criteriaBuilder
134- ->addFilter ('creator_id ' , $ document ->getCustomAttribute ('creator_id ' )->getValue (), 'eq ' )
135- ->create ();
136- self ::assertNotEmpty (
137- $ this ->assetRepository ->getList ($ searchCriteria ),
138- 'Image asset was not saved '
139- );
140- $ this ->deleteImage ($ destinationPath );
141132 }
142133
143134 /**
144- * Document for save.
135+ * Assert saved assets data
145136 *
137+ * @param string $destinationPath
146138 * @param array $documentData
147- * @return Document
139+ * @return void
148140 */
149- private function getDocument ( array $ documentData ): Document
141+ private function assertAssets ( string $ destinationPath , array $ documentData ): void
150142 {
151- $ document = new Document ($ documentData );
152- $ this ->addAttributes ($ document , $ documentData ['extension_attributes ' ]);
153- return $ document ;
143+ $ galleryAssets = Bootstrap::getObjectManager ()->get (GetAssetsByPathsInterface::class);
144+ $ mediaAssets = $ galleryAssets ->execute ([$ destinationPath ]);
145+ self ::assertCount (1 , $ mediaAssets , 'Wrong gallery assets count ' );
146+ self ::assertEquals (
147+ $ documentData ['extension_attributes ' ]['title ' ],
148+ $ mediaAssets [0 ]->getTitle (),
149+ 'Wrong gallery assets image title saved '
150+ );
151+ $ criteriaBuilder = Bootstrap::getObjectManager ()->get (SearchCriteriaBuilder::class);
152+ $ searchCriteria = $ criteriaBuilder
153+ ->addFilter ('media_gallery_id ' , $ mediaAssets [0 ]->getId ())
154+ ->create ();
155+ /** @var AssetRepositoryInterface $stockAssets */
156+ $ stockAssets = Bootstrap::getObjectManager ()->get (AssetRepositoryInterface::class);
157+ $ items = $ stockAssets ->getList ($ searchCriteria )->getItems ();
158+ self ::assertNotEmpty (
159+ $ items ,
160+ 'Image asset was not saved '
161+ );
162+ $ item = reset ($ items );
163+ self ::assertEquals (
164+ $ documentData ['extension_attributes ' ]['creator_id ' ],
165+ $ item ->getCreatorId (),
166+ 'Wrong stock asset creator id saved '
167+ );
168+ self ::assertEquals (
169+ $ documentData ['extension_attributes ' ]['category ' ]['id ' ],
170+ $ item ->getCategoryId (),
171+ 'Wrong stock asset category id saved '
172+ );
154173 }
155174
156175 /**
@@ -163,11 +182,11 @@ private function getDocument(array $documentData): Document
163182 private function addAttributes (Document $ document , array $ attributes ): Document
164183 {
165184 $ customAttributes = $ document ->getCustomAttributes ();
166- $ attributeValueFactory = Bootstrap::getObjectManager ()->create (
185+ $ valueFactory = Bootstrap::getObjectManager ()->create (
167186 AttributeValueFactory::class
168187 );
169188 foreach ($ attributes as $ code => $ value ) {
170- $ attribute = $ attributeValueFactory ->create ();
189+ $ attribute = $ valueFactory ->create ();
171190 $ attribute ->setAttributeCode ($ code );
172191 $ attribute ->setValue ($ value );
173192 $ customAttributes [$ code ] = $ attribute ;
@@ -186,15 +205,14 @@ private function addAttributes(Document $document, array $attributes): Document
186205 */
187206 private function getImageFilePath (string $ filename ): string
188207 {
189- return dirname (__DIR__ , 1 )
190- . DIRECTORY_SEPARATOR
191- . implode (
192- DIRECTORY_SEPARATOR ,
193- [
194- '_files ' ,
195- $ filename
196- ]
197- );
208+ return implode (
209+ DIRECTORY_SEPARATOR ,
210+ [
211+ dirname (__DIR__ , 1 ),
212+ '_files ' ,
213+ $ filename
214+ ]
215+ );
198216 }
199217
200218 /**
@@ -205,9 +223,6 @@ private function getImageFilePath(string $filename): string
205223 */
206224 private function deleteImage (string $ destinationPath ): void
207225 {
208- $ mediaDir = $ this ->fileSystem ->getDirectoryWrite (DirectoryList::MEDIA );
209- if ($ mediaDir ->isExist ($ destinationPath )) {
210- $ this ->driver ->deleteFile ($ mediaDir ->getAbsolutePath ($ destinationPath ));
211- }
226+ $ this ->fileSystem ->getDirectoryWrite (DirectoryList::MEDIA )->delete ($ destinationPath );
212227 }
213228}
0 commit comments