9
9
*/
10
10
namespace Magento \Theme \Test \Unit \Helper ;
11
11
12
+ use Magento \Framework \Filesystem \DriverInterface ;
12
13
use Magento \Theme \Helper \Storage ;
14
+ use PHPUnit \Framework \MockObject \MockObject ;
13
15
14
16
/**
15
17
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -37,7 +39,7 @@ class StorageTest extends \PHPUnit\Framework\TestCase
37
39
protected $ request ;
38
40
39
41
/**
40
- * @var \Magento\Theme\Helper\ Storage
42
+ * @var Storage
41
43
*/
42
44
protected $ helper ;
43
45
@@ -78,6 +80,11 @@ class StorageTest extends \PHPUnit\Framework\TestCase
78
80
79
81
protected $ requestParams ;
80
82
83
+ /**
84
+ * @var DriverInterface|MockObject
85
+ */
86
+ private $ filesystemDriver ;
87
+
81
88
protected function setUp ()
82
89
{
83
90
$ this ->customizationPath = '/ ' . implode ('/ ' , ['var ' , 'theme ' ]);
@@ -102,25 +109,28 @@ protected function setUp()
102
109
$ this ->contextHelper ->expects ($ this ->any ())->method ('getUrlEncoder ' )->willReturn ($ this ->urlEncoder );
103
110
$ this ->contextHelper ->expects ($ this ->any ())->method ('getUrlDecoder ' )->willReturn ($ this ->urlDecoder );
104
111
$ this ->themeFactory ->expects ($ this ->any ())->method ('create ' )->willReturn ($ this ->theme );
112
+ $ this ->filesystemDriver = $ this ->createMock (DriverInterface::class);
105
113
106
114
$ this ->theme ->expects ($ this ->any ())
107
115
->method ('getCustomization ' )
108
116
->will ($ this ->returnValue ($ this ->customization ));
109
117
110
118
$ this ->request ->expects ($ this ->at (0 ))
111
119
->method ('getParam ' )
112
- ->with (\ Magento \ Theme \ Helper \ Storage::PARAM_THEME_ID )
120
+ ->with (Storage::PARAM_THEME_ID )
113
121
->will ($ this ->returnValue (6 ));
114
122
$ this ->request ->expects ($ this ->at (1 ))
115
123
->method ('getParam ' )
116
- ->with (\ Magento \ Theme \ Helper \ Storage::PARAM_CONTENT_TYPE )
124
+ ->with (Storage::PARAM_CONTENT_TYPE )
117
125
->will ($ this ->returnValue (\Magento \Theme \Model \Wysiwyg \Storage::TYPE_IMAGE ));
118
126
119
- $ this ->helper = new \ Magento \ Theme \ Helper \ Storage (
127
+ $ this ->helper = new Storage (
120
128
$ this ->contextHelper ,
121
129
$ this ->filesystem ,
122
130
$ this ->session ,
123
- $ this ->themeFactory
131
+ $ this ->themeFactory ,
132
+ null ,
133
+ $ this ->filesystemDriver
124
134
);
125
135
}
126
136
@@ -196,7 +206,7 @@ public function testGetRequestParams()
196
206
)->method (
197
207
'getParam '
198
208
)->with (
199
- \ Magento \ Theme \ Helper \ Storage::PARAM_THEME_ID
209
+ Storage::PARAM_THEME_ID
200
210
)->will (
201
211
$ this ->returnValue (6 )
202
212
);
@@ -205,7 +215,7 @@ public function testGetRequestParams()
205
215
)->method (
206
216
'getParam '
207
217
)->with (
208
- \ Magento \ Theme \ Helper \ Storage::PARAM_CONTENT_TYPE
218
+ Storage::PARAM_CONTENT_TYPE
209
219
)->will (
210
220
$ this ->returnValue ('image ' )
211
221
);
@@ -214,15 +224,15 @@ public function testGetRequestParams()
214
224
)->method (
215
225
'getParam '
216
226
)->with (
217
- \ Magento \ Theme \ Helper \ Storage::PARAM_NODE
227
+ Storage::PARAM_NODE
218
228
)->will (
219
229
$ this ->returnValue ('node ' )
220
230
);
221
231
222
232
$ expectedResult = [
223
- \ Magento \ Theme \ Helper \ Storage::PARAM_THEME_ID => 6 ,
224
- \ Magento \ Theme \ Helper \ Storage::PARAM_CONTENT_TYPE => \Magento \Theme \Model \Wysiwyg \Storage::TYPE_IMAGE ,
225
- \ Magento \ Theme \ Helper \ Storage::PARAM_NODE => 'node ' ,
233
+ Storage::PARAM_THEME_ID => 6 ,
234
+ Storage::PARAM_CONTENT_TYPE => \Magento \Theme \Model \Wysiwyg \Storage::TYPE_IMAGE ,
235
+ Storage::PARAM_NODE => 'node ' ,
226
236
];
227
237
$ this ->assertEquals ($ expectedResult , $ this ->helper ->getRequestParams ());
228
238
}
@@ -234,7 +244,7 @@ public function testGetAllowedExtensionsByType()
234
244
)->method (
235
245
'getParam '
236
246
)->with (
237
- \ Magento \ Theme \ Helper \ Storage::PARAM_CONTENT_TYPE
247
+ Storage::PARAM_CONTENT_TYPE
238
248
)->will (
239
249
$ this ->returnValue (\Magento \Theme \Model \Wysiwyg \Storage::TYPE_FONT )
240
250
);
@@ -244,7 +254,7 @@ public function testGetAllowedExtensionsByType()
244
254
)->method (
245
255
'getParam '
246
256
)->with (
247
- \ Magento \ Theme \ Helper \ Storage::PARAM_CONTENT_TYPE
257
+ Storage::PARAM_CONTENT_TYPE
248
258
)->will (
249
259
$ this ->returnValue (\Magento \Theme \Model \Wysiwyg \Storage::TYPE_IMAGE )
250
260
);
@@ -273,17 +283,17 @@ public function testGetThumbnailPathNotFound()
273
283
->willReturnMap (
274
284
[
275
285
[
276
- \ Magento \ Theme \ Helper \ Storage::PARAM_THEME_ID ,
286
+ Storage::PARAM_THEME_ID ,
277
287
null ,
278
288
6 ,
279
289
],
280
290
[
281
- \ Magento \ Theme \ Helper \ Storage::PARAM_CONTENT_TYPE ,
291
+ Storage::PARAM_CONTENT_TYPE ,
282
292
null ,
283
293
\Magento \Theme \Model \Wysiwyg \Storage::TYPE_IMAGE
284
294
],
285
295
[
286
- \ Magento \ Theme \ Helper \ Storage::PARAM_NODE ,
296
+ Storage::PARAM_NODE ,
287
297
null ,
288
298
$ node
289
299
],
@@ -349,17 +359,17 @@ public function testGetRelativeUrl()
349
359
->willReturnMap (
350
360
[
351
361
'type ' => [
352
- \ Magento \ Theme \ Helper \ Storage::PARAM_CONTENT_TYPE ,
362
+ Storage::PARAM_CONTENT_TYPE ,
353
363
null ,
354
364
\Magento \Theme \Model \Wysiwyg \Storage::TYPE_IMAGE ,
355
365
],
356
366
'node ' => [
357
- \ Magento \ Theme \ Helper \ Storage::PARAM_NODE ,
367
+ Storage::PARAM_NODE ,
358
368
null ,
359
369
$ notRoot ,
360
370
],
361
371
'filenaem ' => [
362
- \ Magento \ Theme \ Helper \ Storage::PARAM_FILENAME ,
372
+ Storage::PARAM_FILENAME ,
363
373
null ,
364
374
$ filename ,
365
375
],
@@ -389,8 +399,8 @@ public function testGetRelativeUrl()
389
399
public function getStorageTypeForNameDataProvider ()
390
400
{
391
401
return [
392
- 'font ' => [\Magento \Theme \Model \Wysiwyg \Storage::TYPE_FONT , \ Magento \ Theme \ Helper \ Storage::FONTS ],
393
- 'image ' => [\Magento \Theme \Model \Wysiwyg \Storage::TYPE_IMAGE , \ Magento \ Theme \ Helper \ Storage::IMAGES ],
402
+ 'font ' => [\Magento \Theme \Model \Wysiwyg \Storage::TYPE_FONT , Storage::FONTS ],
403
+ 'image ' => [\Magento \Theme \Model \Wysiwyg \Storage::TYPE_IMAGE , Storage::IMAGES ],
394
404
];
395
405
}
396
406
@@ -405,7 +415,7 @@ public function testGetStorageTypeName($type, $name)
405
415
{
406
416
$ this ->request ->expects ($ this ->once ())
407
417
->method ('getParam ' )
408
- ->with (\ Magento \ Theme \ Helper \ Storage::PARAM_CONTENT_TYPE )
418
+ ->with (Storage::PARAM_CONTENT_TYPE )
409
419
->willReturn ($ type );
410
420
411
421
$ this ->assertEquals ($ name , $ this ->helper ->getStorageTypeName ());
@@ -433,12 +443,62 @@ public function testGetThemeNotFound()
433
443
$ this ->themeFactory ->expects ($ this ->once ())
434
444
->method ('create ' )
435
445
->willReturn (null );
436
- $ helper = new \ Magento \ Theme \ Helper \ Storage (
446
+ $ helper = new Storage (
437
447
$ this ->contextHelper ,
438
448
$ this ->filesystem ,
439
449
$ this ->session ,
440
450
$ this ->themeFactory
441
451
);
442
452
$ helper ->getStorageRoot ();
443
453
}
454
+
455
+ /**
456
+ * @dataProvider getCurrentPathDataProvider
457
+ */
458
+ public function testGetCurrentPath (
459
+ string $ expectedPath ,
460
+ string $ requestedPath ,
461
+ ?bool $ isDirectory = null ,
462
+ ?string $ relativePath = null ,
463
+ ?string $ resolvedPath = null
464
+ ) {
465
+ $ this ->directoryWrite ->method ('isDirectory ' )
466
+ ->willReturn ($ isDirectory );
467
+
468
+ $ this ->directoryWrite ->method ('getRelativePath ' )
469
+ ->willReturn ($ relativePath );
470
+
471
+ $ this ->urlDecoder ->method ('decode ' )
472
+ ->willReturnArgument (0 );
473
+
474
+ if ($ resolvedPath ) {
475
+ $ this ->filesystemDriver ->method ('getRealpathSafety ' )
476
+ ->willReturn ($ resolvedPath );
477
+ } else {
478
+ $ this ->filesystemDriver ->method ('getRealpathSafety ' )
479
+ ->willReturnArgument (0 );
480
+ }
481
+
482
+ $ this ->request ->expects ($ this ->once ())
483
+ ->method ('getParam ' )
484
+ ->with (Storage::PARAM_NODE )
485
+ ->willReturn ($ requestedPath );
486
+
487
+ $ actualPath = $ this ->helper ->getCurrentPath ();
488
+
489
+ self ::assertSame ($ expectedPath , $ actualPath );
490
+ }
491
+
492
+ public function getCurrentPathDataProvider (): array
493
+ {
494
+ $ rootPath = '/ ' . \Magento \Theme \Model \Wysiwyg \Storage::TYPE_IMAGE ;
495
+
496
+ return [
497
+ [$ rootPath , Storage::NODE_ROOT ],
498
+ [$ rootPath , $ rootPath . '/foo ' ],
499
+ [$ rootPath , $ rootPath . '/something ' , true , null , '/bar ' ],
500
+ ['foo/ ' , $ rootPath . '/foo ' , true , 'foo/ ' ],
501
+ [$ rootPath , $ rootPath . '/foo ' , false ],
502
+ ];
503
+ }
444
504
}
0 commit comments