14
14
use Magento \Framework \Api \Search \Document ;
15
15
use Magento \Framework \Api \SearchCriteriaBuilder ;
16
16
use Magento \Framework \App \Filesystem \DirectoryList ;
17
- use Magento \Framework \Exception \IntegrationException ;
18
17
use Magento \Framework \Filesystem ;
19
18
use Magento \Framework \Filesystem \Driver \Https ;
20
- use Magento \Framework \ Filesystem \ DriverInterface ;
19
+ use Magento \MediaGalleryApi \ Api \ GetAssetsByPathsInterface ;
21
20
use Magento \TestFramework \Helper \Bootstrap ;
22
21
use PHPUnit \Framework \TestCase ;
23
22
26
25
*/
27
26
class SaveImageTest extends TestCase
28
27
{
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
-
41
28
/**
42
29
* @var Filesystem
43
30
*/
44
31
private $ fileSystem ;
45
32
46
33
/**
47
- * @var AssetRepositoryInterface
34
+ * @inheritdoc
48
35
*/
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
+ }
50
45
51
46
/**
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
53
54
*/
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
+ }
55
69
56
70
/**
57
71
* @return array
@@ -64,93 +78,98 @@ public function getSaveTestDataProvider(): array
64
78
'id ' => 1 ,
65
79
'comp_url ' => 'https://test.url/magento-logo.png ' ,
66
80
'width ' => 110 ,
67
- 'title ' => 'test ' ,
81
+ 'title ' => 'test adobe image title ' ,
68
82
'content_type ' => 'image/png ' ,
69
83
'height ' => 210 ,
70
84
'some_bool_param ' => false ,
71
85
'some_nullable_param ' => null ,
72
86
'extension_attributes ' => [
73
- 'title ' => 'test ' ,
87
+ 'title ' => 'test adobe image title ' ,
74
88
'is_downloaded ' => 0 ,
75
89
'is_licensed_locally ' => 0 ,
76
90
'thumbnail_240_url ' => 'https://test.url/magento-logo.png ' ,
77
- 'creator_id ' => 1122 ,
91
+ 'creator_id ' => random_int ( 0 , 2147483647 ) ,
78
92
'creator_name ' => 'Test ' ,
79
93
'path ' => 'catalog/category/tmp.png ' ,
80
94
'content_type ' => 'image/png ' ,
81
95
'category ' => [
82
- 'id ' => 1 ,
96
+ 'id ' => random_int ( 0 , 2147483647 ) ,
83
97
'name ' => 'Test '
84
98
],
85
99
]
86
100
],
87
101
'sourcePath ' => 'magento-logo.png ' ,
88
- 'destinationPath ' => 'catalog/category/tmp .png ' ,
102
+ 'destinationPath ' => 'catalog/category/adobe-stock-save-image-test .png ' ,
89
103
]
90
104
];
91
105
}
92
106
93
107
/**
94
- * @inheritdoc
108
+ * Document for save.
109
+ *
110
+ * @param array $documentData
111
+ * @return Document
95
112
*/
96
- protected function setUp ( ): void
113
+ private function getDocument ( array $ documentData ): Document
97
114
{
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 ;
108
118
}
109
119
110
120
/**
111
- * Test with image.
121
+ * Check if image saved by destination path
112
122
*
113
- * @param array $documentData
114
- * @param string $sourceFile
115
123
* @param string $destinationPath
116
124
* @return void
117
- * @dataProvider getSaveTestDataProvider
118
125
*/
119
- public function testSave ( array $ documentData , string $ sourceFile , string $ destinationPath ): void
126
+ private function assertImageSavedToDirectory ( string $ destinationPath ): void
120
127
{
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
- );
129
128
self ::assertTrue (
130
129
$ this ->fileSystem ->getDirectoryRead (DirectoryList::MEDIA )->isExist ($ destinationPath ),
131
130
'File was not saved by destination '
132
131
);
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 );
141
132
}
142
133
143
134
/**
144
- * Document for save.
135
+ * Assert saved assets data
145
136
*
137
+ * @param string $destinationPath
146
138
* @param array $documentData
147
- * @return Document
139
+ * @return void
148
140
*/
149
- private function getDocument ( array $ documentData ): Document
141
+ private function assertAssets ( string $ destinationPath , array $ documentData ): void
150
142
{
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
+ );
154
173
}
155
174
156
175
/**
@@ -163,11 +182,11 @@ private function getDocument(array $documentData): Document
163
182
private function addAttributes (Document $ document , array $ attributes ): Document
164
183
{
165
184
$ customAttributes = $ document ->getCustomAttributes ();
166
- $ attributeValueFactory = Bootstrap::getObjectManager ()->create (
185
+ $ valueFactory = Bootstrap::getObjectManager ()->create (
167
186
AttributeValueFactory::class
168
187
);
169
188
foreach ($ attributes as $ code => $ value ) {
170
- $ attribute = $ attributeValueFactory ->create ();
189
+ $ attribute = $ valueFactory ->create ();
171
190
$ attribute ->setAttributeCode ($ code );
172
191
$ attribute ->setValue ($ value );
173
192
$ customAttributes [$ code ] = $ attribute ;
@@ -186,15 +205,14 @@ private function addAttributes(Document $document, array $attributes): Document
186
205
*/
187
206
private function getImageFilePath (string $ filename ): string
188
207
{
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
+ );
198
216
}
199
217
200
218
/**
@@ -205,9 +223,6 @@ private function getImageFilePath(string $filename): string
205
223
*/
206
224
private function deleteImage (string $ destinationPath ): void
207
225
{
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 );
212
227
}
213
228
}
0 commit comments