66namespace Magento \Catalog \Test \Unit \Model \Category \Attribute \Backend ;
77
88use Magento \Framework \App \Filesystem \DirectoryList ;
9+ use Magento \Framework \Filesystem \Directory \WriteInterface ;
910
11+ /**
12+ * Test for Magento\Catalog\Model\Category\Attribute\Backend\Image class.
13+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
14+ */
1015class ImageTest extends \PHPUnit \Framework \TestCase
1116{
1217 /**
@@ -67,7 +72,7 @@ protected function setUp()
6772
6873 $ this ->imageUploader = $ this ->createPartialMock (
6974 \Magento \Catalog \Model \ImageUploader::class,
70- ['moveFileFromTmp ' ]
75+ ['moveFileFromTmp ' , ' getBasePath ' ]
7176 );
7277
7378 $ this ->filesystem = $ this ->getMockBuilder (\Magento \Framework \Filesystem::class)->disableOriginalConstructor ()
@@ -95,9 +100,7 @@ public function testBeforeSaveValueDeletion($value)
95100 $ model = $ this ->objectManager ->getObject (\Magento \Catalog \Model \Category \Attribute \Backend \Image::class);
96101 $ model ->setAttribute ($ this ->attribute );
97102
98- $ object = new \Magento \Framework \DataObject ([
99- 'test_attribute ' => $ value
100- ]);
103+ $ object = new \Magento \Framework \DataObject (['test_attribute ' => $ value ]);
101104
102105 $ model ->beforeSave ($ object );
103106
@@ -132,9 +135,7 @@ public function testBeforeSaveValueInvalid($value)
132135 $ model = $ this ->objectManager ->getObject (\Magento \Catalog \Model \Category \Attribute \Backend \Image::class);
133136 $ model ->setAttribute ($ this ->attribute );
134137
135- $ object = new \Magento \Framework \DataObject ([
136- 'test_attribute ' => $ value
137- ]);
138+ $ object = new \Magento \Framework \DataObject (['test_attribute ' => $ value ]);
138139
139140 $ model ->beforeSave ($ object );
140141
@@ -146,14 +147,25 @@ public function testBeforeSaveValueInvalid($value)
146147 */
147148 public function testBeforeSaveAttributeFileName ()
148149 {
149- $ model = $ this ->objectManager ->getObject (\Magento \Catalog \Model \Category \Attribute \Backend \Image::class);
150- $ model ->setAttribute ($ this ->attribute );
150+ $ model = $ this ->setUpModelForAfterSave ();
151+ $ mediaDirectoryMock = $ this ->createMock (WriteInterface::class);
152+ $ this ->filesystem ->expects ($ this ->once ())
153+ ->method ('getDirectoryWrite ' )
154+ ->with (DirectoryList::MEDIA )
155+ ->willReturn ($ mediaDirectoryMock );
156+ $ this ->imageUploader ->expects ($ this ->once ())->method ('getBasePath ' )->willReturn ('base/path ' );
157+ $ mediaDirectoryMock ->expects ($ this ->once ())
158+ ->method ('getAbsolutePath ' )
159+ ->with ('base/path/test123.jpg ' )
160+ ->willReturn ('absolute/path/base/path/test123.jpg ' );
151161
152- $ object = new \Magento \Framework \DataObject ([
153- 'test_attribute ' => [
154- ['name ' => 'test123.jpg ' ]
162+ $ object = new \Magento \Framework \DataObject (
163+ [
164+ 'test_attribute ' => [
165+ ['name ' => 'test123.jpg ' ],
166+ ],
155167 ]
156- ] );
168+ );
157169
158170 $ model ->beforeSave ($ object );
159171
@@ -165,30 +177,37 @@ public function testBeforeSaveAttributeFileName()
165177 */
166178 public function testBeforeSaveAttributeFileNameOutsideOfCategoryDir ()
167179 {
168- $ model = $ this ->objectManager ->getObject (\Magento \Catalog \Model \Category \Attribute \Backend \Image::class, [
169- 'filesystem ' => $ this ->filesystem
170- ]);
171-
180+ $ model = $ this ->setUpModelForAfterSave ();
172181 $ model ->setAttribute ($ this ->attribute );
173182
183+ $ mediaDirectoryMock = $ this ->createMock (WriteInterface::class);
184+ $ this ->filesystem ->expects ($ this ->once ())
185+ ->method ('getDirectoryWrite ' )
186+ ->with (DirectoryList::MEDIA )
187+ ->willReturn ($ mediaDirectoryMock );
174188 $ this ->filesystem
175189 ->expects ($ this ->once ())
176190 ->method ('getUri ' )
177191 ->with (DirectoryList::MEDIA )
178192 ->willReturn ('pub/media ' );
193+ $ mediaDirectoryMock ->expects ($ this ->once ())
194+ ->method ('getAbsolutePath ' )
195+ ->willReturn ('/pub/media/wysiwyg/test123.jpg ' );
179196
180- $ object = new \Magento \Framework \DataObject ([
181- 'test_attribute ' => [
182- [
183- 'name ' => '/test123.jpg ' ,
184- 'url ' => '/pub/media/wysiwyg/test123.jpg ' ,
185- ]
197+ $ object = new \Magento \Framework \DataObject (
198+ [
199+ 'test_attribute ' => [
200+ [
201+ 'name ' => 'test123.jpg ' ,
202+ 'url ' => '/pub/media/wysiwyg/test123.jpg ' ,
203+ ],
204+ ],
186205 ]
187- ] );
206+ );
188207
189208 $ model ->beforeSave ($ object );
190209
191- $ this ->assertEquals ('/pub/media/wysiwyg/ test123.jpg ' , $ object ->getTestAttribute ());
210+ $ this ->assertEquals ('test123.jpg ' , $ object ->getTestAttribute ());
192211 $ this ->assertEquals (
193212 [['name ' => '/pub/media/wysiwyg/test123.jpg ' , 'url ' => '/pub/media/wysiwyg/test123.jpg ' ]],
194213 $ object ->getData ('_additional_data_test_attribute ' )
@@ -200,20 +219,31 @@ public function testBeforeSaveAttributeFileNameOutsideOfCategoryDir()
200219 */
201220 public function testBeforeSaveTemporaryAttribute ()
202221 {
203- $ model = $ this ->objectManager -> getObject (\ Magento \ Catalog \ Model \ Category \ Attribute \ Backend \Image::class );
222+ $ model = $ this ->setUpModelForAfterSave ( );
204223 $ model ->setAttribute ($ this ->attribute );
205224
206- $ object = new \Magento \Framework \DataObject ([
207- 'test_attribute ' => [
208- ['name ' => 'test123.jpg ' , 'tmp_name ' => 'abc123 ' , 'url ' => 'http://www.example.com/test123.jpg ' ]
225+ $ mediaDirectoryMock = $ this ->createMock (WriteInterface::class);
226+ $ this ->filesystem ->expects ($ this ->once ())
227+ ->method ('getDirectoryWrite ' )
228+ ->with (DirectoryList::MEDIA )
229+ ->willReturn ($ mediaDirectoryMock );
230+
231+ $ object = new \Magento \Framework \DataObject (
232+ [
233+ 'test_attribute ' => [
234+ ['name ' => 'test123.jpg ' , 'tmp_name ' => 'abc123 ' , 'url ' => 'http://www.example.com/test123.jpg ' ],
235+ ],
209236 ]
210- ] );
237+ );
211238
212239 $ model ->beforeSave ($ object );
213240
214- $ this ->assertEquals ([
215- ['name ' => 'test123.jpg ' , 'tmp_name ' => 'abc123 ' , 'url ' => 'http://www.example.com/test123.jpg ' ]
216- ], $ object ->getData ('_additional_data_test_attribute ' ));
241+ $ this ->assertEquals (
242+ [
243+ ['name ' => 'test123.jpg ' , 'tmp_name ' => 'abc123 ' , 'url ' => 'http://www.example.com/test123.jpg ' ],
244+ ],
245+ $ object ->getData ('_additional_data_test_attribute ' )
246+ );
217247 }
218248
219249 /**
@@ -224,9 +254,7 @@ public function testBeforeSaveAttributeStringValue()
224254 $ model = $ this ->objectManager ->getObject (\Magento \Catalog \Model \Category \Attribute \Backend \Image::class);
225255 $ model ->setAttribute ($ this ->attribute );
226256
227- $ object = new \Magento \Framework \DataObject ([
228- 'test_attribute ' => 'test123.jpg '
229- ]);
257+ $ object = new \Magento \Framework \DataObject (['test_attribute ' => 'test123.jpg ' ]);
230258
231259 $ model ->beforeSave ($ object );
232260
@@ -245,18 +273,26 @@ private function setUpModelForAfterSave()
245273
246274 $ objectManagerMock ->expects ($ this ->any ())
247275 ->method ('get ' )
248- ->will ($ this ->returnCallback (function ($ class , $ params = []) use ($ imageUploaderMock ) {
249- if ($ class == \Magento \Catalog \CategoryImageUpload::class) {
250- return $ imageUploaderMock ;
251- }
252-
253- return $ this ->objectManager ->get ($ class , $ params );
254- }));
255-
256- $ model = $ this ->objectManager ->getObject (\Magento \Catalog \Model \Category \Attribute \Backend \Image::class, [
257- 'objectManager ' => $ objectManagerMock ,
258- 'logger ' => $ this ->logger
259- ]);
276+ ->will (
277+ $ this ->returnCallback (
278+ function ($ class , $ params = []) use ($ imageUploaderMock ) {
279+ if ($ class == \Magento \Catalog \CategoryImageUpload::class) {
280+ return $ imageUploaderMock ;
281+ }
282+
283+ return $ this ->objectManager ->get ($ class , $ params );
284+ }
285+ )
286+ );
287+
288+ $ model = $ this ->objectManager ->getObject (
289+ \Magento \Catalog \Model \Category \Attribute \Backend \Image::class,
290+ [
291+ 'objectManager ' => $ objectManagerMock ,
292+ 'logger ' => $ this ->logger ,
293+ 'filesystem ' => $ this ->filesystem ,
294+ ]
295+ );
260296 $ this ->objectManager ->setBackwardCompatibleProperty ($ model , 'imageUploader ' , $ this ->imageUploader );
261297
262298 return $ model ->setAttribute ($ this ->attribute );
0 commit comments