Skip to content

Commit b336df7

Browse files
committed
MC-24057: Implement static dependency analysis for wildcard and API urls
- Added further test cases
1 parent 525cd6b commit b336df7

File tree

2 files changed

+135
-27
lines changed
  • dev/tests/static/framework

2 files changed

+135
-27
lines changed

dev/tests/static/framework/Magento/TestFramework/Dependency/PhpRule.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,22 +106,24 @@ class PhpRule implements RuleInterface
106106
* @param array $pluginMap
107107
* @param array $whitelists
108108
* @param ClassScanner|null $classScanner
109+
* @param RouteMapper|null $routeMapper
109110
*/
110111
public function __construct(
111112
array $mapRouters,
112113
array $mapLayoutBlocks,
113114
ConfigReader $configReader,
114115
array $pluginMap = [],
115116
array $whitelists = [],
116-
ClassScanner $classScanner = null
117+
ClassScanner $classScanner = null,
118+
RouteMapper $routeMapper = null
117119
) {
118120
$this->_mapRouters = $mapRouters;
119121
$this->_mapLayoutBlocks = $mapLayoutBlocks;
120122
$this->configReader = $configReader;
121123
$this->pluginMap = $pluginMap ?: null;
122-
$this->routeMapper = new RouteMapper();
123124
$this->whitelists = $whitelists;
124125
$this->classScanner = $classScanner ?? new ClassScanner();
126+
$this->routeMapper = $routeMapper ?? new RouteMapper();
125127
}
126128

127129
/**
@@ -388,9 +390,9 @@ private function processWildcardUrl(string $urlPath, string $filePath)
388390
}
389391

390392
return $this->routeMapper->getDependencyByRoutePath(
391-
$routeId,
392-
$controllerName,
393-
$actionName
393+
strtolower($routeId),
394+
strtolower($controllerName),
395+
strtolower($actionName)
394396
);
395397
}
396398

dev/tests/static/framework/tests/unit/testsuite/Magento/TestFramework/Dependency/PhpRuleTest.php

Lines changed: 128 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Framework\Exception\LocalizedException;
1111
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
1212
use Magento\TestFramework\Dependency\Reader\ClassScanner;
13+
use Magento\TestFramework\Dependency\Route\RouteMapper;
1314

1415
/**
1516
* Test for PhpRule dependency check
@@ -36,34 +37,36 @@ class PhpRuleTest extends \PHPUnit\Framework\TestCase
3637
*/
3738
private $webApiConfigReader;
3839

40+
private $pluginMap;
41+
private $mapRoutes;
42+
private $mapLayoutBlocks;
43+
private $whitelist;
44+
3945
/**
4046
* @inheritDoc
4147
* @throws \Exception
4248
*/
4349
protected function setUp(): void
4450
{
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 = [
4854
'Magento\Module1\Plugin1' => 'Magento\Module1\Subject',
4955
'Magento\Module1\Plugin2' => 'Magento\Module2\Subject',
5056
];
51-
$whitelist = [];
57+
$this->whitelist = [];
5258

5359
$this->objectManagerHelper = new ObjectManagerHelper($this);
5460
$this->classScanner = $this->createMock(ClassScanner::class);
5561
$this->webApiConfigReader = $this->makeWebApiConfigReaderMock();
5662

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
6770
);
6871
}
6972

@@ -225,6 +228,7 @@ public function getDependencyInfoDataProvider()
225228
];
226229
}
227230

231+
228232
/**
229233
* @param string $class
230234
* @param string $content
@@ -265,12 +269,17 @@ public function getDependencyInfoDataCaseGetUrlDataProvider()
265269
]
266270
]
267271
],
268-
'getUrl from API of same module' => [
272+
'getUrl from API of same module with parameter' => [
269273
'Magento\Catalog\SomeClass',
270274
'$this->getUrl("rest/V1/products/3")',
271275
[]
272276
],
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' => [
274283
'Magento\Backend\SomeClass',
275284
'$this->getUrl("rest/V1/products/43/options")',
276285
[
@@ -281,7 +290,7 @@ public function getDependencyInfoDataCaseGetUrlDataProvider()
281290
]
282291
],
283292
],
284-
'getUrl from routeid wildcard in controller' => [
293+
'getUrl from routeid wildcard' => [
285294
'Magento\Catalog\Controller\ControllerName\SomeClass',
286295
'$this->getUrl("*/Invalid/*")',
287296
[]
@@ -291,19 +300,112 @@ public function getDependencyInfoDataCaseGetUrlDataProvider()
291300
'$this->getUrl("Catalog/*/View")',
292301
[]
293302
],
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',
296305
'$this->getUrl("Catalog/*/View")',
297306
[]
298307
],
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")',
302311
[]
303312
],
304313
];
305314
}
306315

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+
307409
/**
308410
* @param string $class
309411
* @param string $content
@@ -427,6 +529,10 @@ private function makeWebApiConfigReaderMock()
427529
'/V1/products/:sku/options' => ['GET' => ['service' => [
428530
'class' => 'Magento\Catalog\Api\ProductCustomOptionRepositoryInterface',
429531
'method' => 'getList'
532+
] ] ],
533+
'/V1/products' => ['GET' => ['service' => [
534+
'class' => 'Magento\Catalog\Api\ProductCustomOptionRepositoryInterface',
535+
'method' => 'getList'
430536
] ] ]
431537
] ];
432538

0 commit comments

Comments
 (0)