10
10
use Magento \Framework \Exception \LocalizedException ;
11
11
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
12
12
use Magento \TestFramework \Dependency \Reader \ClassScanner ;
13
+ use Magento \TestFramework \Dependency \Route \RouteMapper ;
13
14
14
15
/**
15
16
* Test for PhpRule dependency check
@@ -36,34 +37,36 @@ class PhpRuleTest extends \PHPUnit\Framework\TestCase
36
37
*/
37
38
private $ webApiConfigReader ;
38
39
40
+ private $ pluginMap ;
41
+ private $ mapRoutes ;
42
+ private $ mapLayoutBlocks ;
43
+ private $ whitelist ;
44
+
39
45
/**
40
46
* @inheritDoc
41
47
* @throws \Exception
42
48
*/
43
49
protected function setUp (): void
44
50
{
45
- $ mapRoutes = ['someModule ' => ['Magento\SomeModule ' ], 'anotherModule ' => ['Magento\OneModule ' ]];
46
- $ mapLayoutBlocks = ['area ' => ['block.name ' => ['Magento\SomeModule ' => 'Magento\SomeModule ' ]]];
47
- $ pluginMap = [
51
+ $ this -> mapRoutes = ['someModule ' => ['Magento\SomeModule ' ], 'anotherModule ' => ['Magento\OneModule ' ]];
52
+ $ this -> mapLayoutBlocks = ['area ' => ['block.name ' => ['Magento\SomeModule ' => 'Magento\SomeModule ' ]]];
53
+ $ this -> pluginMap = [
48
54
'Magento\Module1\Plugin1 ' => 'Magento\Module1\Subject ' ,
49
55
'Magento\Module1\Plugin2 ' => 'Magento\Module2\Subject ' ,
50
56
];
51
- $ whitelist = [];
57
+ $ this -> whitelist = [];
52
58
53
59
$ this ->objectManagerHelper = new ObjectManagerHelper ($ this );
54
60
$ this ->classScanner = $ this ->createMock (ClassScanner::class);
55
61
$ this ->webApiConfigReader = $ this ->makeWebApiConfigReaderMock ();
56
62
57
- $ this ->model = $ this ->objectManagerHelper ->getObject (
58
- PhpRule::class,
59
- [
60
- 'mapRouters ' => $ mapRoutes ,
61
- 'mapLayoutBlocks ' => $ mapLayoutBlocks ,
62
- 'pluginMap ' => $ pluginMap ,
63
- 'configReader ' => $ this ->webApiConfigReader ,
64
- 'whitelists ' => $ whitelist ,
65
- 'classScanner ' => $ this ->classScanner
66
- ]
63
+ $ this ->model = new PhpRule (
64
+ $ this ->mapRoutes ,
65
+ $ this ->mapLayoutBlocks ,
66
+ $ this ->webApiConfigReader ,
67
+ $ this ->pluginMap ,
68
+ $ this ->whitelist ,
69
+ $ this ->classScanner
67
70
);
68
71
}
69
72
@@ -225,6 +228,7 @@ public function getDependencyInfoDataProvider()
225
228
];
226
229
}
227
230
231
+
228
232
/**
229
233
* @param string $class
230
234
* @param string $content
@@ -265,12 +269,17 @@ public function getDependencyInfoDataCaseGetUrlDataProvider()
265
269
]
266
270
]
267
271
],
268
- 'getUrl from API of same module ' => [
272
+ 'getUrl from API of same module with parameter ' => [
269
273
'Magento\Catalog\SomeClass ' ,
270
274
'$this->getUrl("rest/V1/products/3") ' ,
271
275
[]
272
276
],
273
- 'getUrl from API of different module ' => [
277
+ 'getUrl from API of same module without parameter ' => [
278
+ 'Magento\Catalog\SomeClass ' ,
279
+ '$this->getUrl("rest/V1/products") ' ,
280
+ []
281
+ ],
282
+ 'getUrl from API of different module with parameter ' => [
274
283
'Magento\Backend\SomeClass ' ,
275
284
'$this->getUrl("rest/V1/products/43/options") ' ,
276
285
[
@@ -281,7 +290,7 @@ public function getDependencyInfoDataCaseGetUrlDataProvider()
281
290
]
282
291
],
283
292
],
284
- 'getUrl from routeid wildcard in controller ' => [
293
+ 'getUrl from routeid wildcard ' => [
285
294
'Magento\Catalog\Controller\ControllerName\SomeClass ' ,
286
295
'$this->getUrl("*/Invalid/*") ' ,
287
296
[]
@@ -291,19 +300,112 @@ public function getDependencyInfoDataCaseGetUrlDataProvider()
291
300
'$this->getUrl("Catalog/*/View") ' ,
292
301
[]
293
302
],
294
- 'getUrl from wildcard url within ignored Block class ' => [
295
- 'Magento\Cms\Block \SomeClass ' ,
303
+ 'getUrl from wildcard url within ignored Model file ' => [
304
+ 'Magento\Cms\Model \SomeClass ' ,
296
305
'$this->getUrl("Catalog/*/View") ' ,
297
306
[]
298
307
],
299
- 'getUrl from wildcard url for ControllerName ' => [
300
- 'Magento\Catalog \Controller\Category\IGNORE ' ,
301
- '$this->getUrl("Catalog /*/View ") ' ,
308
+ 'getUrl with in admin controller for controllerName wildcard ' => [
309
+ 'Magento\Backend \Controller\Adminhtml\System\Store\DeleteStore ' ,
310
+ '$this->getUrl("adminhtml /*/deleteStorePost ") ' ,
302
311
[]
303
312
],
304
313
];
305
314
}
306
315
316
+
317
+ /**
318
+ * @param string $template
319
+ * @param string $content
320
+ * @param array $expected
321
+ * @throws \Exception
322
+ * @dataProvider getDependencyInfoDataCaseGetTemplateUrlDataProvider
323
+ */
324
+ public function testGetDependencyInfoCaseTemplateGetUrl (
325
+ string $ template ,
326
+ string $ content ,
327
+ array $ expected
328
+ ) {
329
+ $ module = $ this ->getModuleFromClass ($ template );
330
+
331
+ $ this ->assertEquals ($ expected , $ this ->model ->getDependencyInfo ($ module , 'php ' , $ template , $ content ));
332
+ }
333
+
334
+ public function getDependencyInfoDataCaseGetTemplateUrlDataProvider ()
335
+ {
336
+ return [ 'getUrl from ignore template ' => [
337
+ 'app/code/Magento/Backend/view/adminhtml/templates/dashboard/totalbar/script.phtml ' ,
338
+ '$getUrl("adminhtml/*/ajaxBlock" ' ,
339
+ []]];
340
+ }
341
+
342
+ /**
343
+ * @param string $class
344
+ * @param string $content
345
+ * @param array $expected
346
+ * @dataProvider processWildcardUrlDataProvider
347
+ */
348
+ public function testProcessWildcardUrl (
349
+ string $ class ,
350
+ string $ content ,
351
+ array $ expected
352
+ ) {
353
+ $ routeMapper = $ this ->createMock (RouteMapper::class);
354
+ $ routeMapper ->expects ($ this ->once ())
355
+ ->method ('getDependencyByRoutePath ' )
356
+ ->with (
357
+ $ this ->equalTo ($ expected ['route_id ' ]),
358
+ $ this ->equalTo ($ expected ['controller_name ' ]),
359
+ $ this ->equalTo ($ expected ['action_name ' ])
360
+ );
361
+ $ phpRule = new PhpRule (
362
+ $ this ->mapRoutes ,
363
+ $ this ->mapLayoutBlocks ,
364
+ $ this ->webApiConfigReader ,
365
+ $ this ->pluginMap ,
366
+ $ this ->whitelist ,
367
+ $ this ->classScanner ,
368
+ $ routeMapper
369
+ );
370
+ $ file = $ this ->makeMockFilepath ($ class );
371
+ $ module = $ this ->getModuleFromClass ($ class );
372
+
373
+ $ phpRule ->getDependencyInfo ($ module , 'php ' , $ file , $ content );
374
+ }
375
+
376
+ public function processWildcardUrlDataProvider ()
377
+ {
378
+ return [
379
+ 'wildcard controller route ' => [
380
+ 'Magento\SomeModule\Controller\ControllerName\SomeClass ' ,
381
+ '$this->getUrl("cms/*/index") ' ,
382
+ [
383
+ 'route_id ' => 'cms ' ,
384
+ 'controller_name ' => 'controllername ' ,
385
+ 'action_name ' => 'index '
386
+ ]
387
+ ],
388
+ 'adminhtml wildcard controller route ' => [
389
+ 'Magento\Backend\Controller\Adminhtml\System\Store\DeleteStore ' ,
390
+ '$this->getUrl("adminhtml/*/deleteStorePost") ' ,
391
+ [
392
+ 'route_id ' => 'adminhtml ' ,
393
+ 'controller_name ' => 'system_store ' ,
394
+ 'action_name ' => 'deletestorepost '
395
+ ]
396
+ ],
397
+ 'index wildcard ' => [
398
+ 'Magento\Backend\Controller\System\Store\DeleteStore ' ,
399
+ '$this->getUrl("routeid/controllername/*") ' ,
400
+ [
401
+ 'route_id ' => 'routeid ' ,
402
+ 'controller_name ' => 'controllername ' ,
403
+ 'action_name ' => 'deletestore '
404
+ ]
405
+ ]
406
+ ];
407
+ }
408
+
307
409
/**
308
410
* @param string $class
309
411
* @param string $content
@@ -427,6 +529,10 @@ private function makeWebApiConfigReaderMock()
427
529
'/V1/products/:sku/options ' => ['GET ' => ['service ' => [
428
530
'class ' => 'Magento\Catalog\Api\ProductCustomOptionRepositoryInterface ' ,
429
531
'method ' => 'getList '
532
+ ] ] ],
533
+ '/V1/products ' => ['GET ' => ['service ' => [
534
+ 'class ' => 'Magento\Catalog\Api\ProductCustomOptionRepositoryInterface ' ,
535
+ 'method ' => 'getList '
430
536
] ] ]
431
537
] ];
432
538
0 commit comments