config = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock(); $this->fileManager = new FileManager(); $this->metadata = $this->getMockBuilder(Metadata::class)->disableOriginalConstructor()->getMock(); $this->dataCache = $this->getMockBuilder(DataCache::class)->disableOriginalConstructor()->getMock(); $this->pathProvider = $this->createMock(PathProvider::class); $this->route = new Route( $this->config, $this->metadata, $this->fileManager, $this->dataCache, $this->pathProvider ); } private function initPathProvider(string $folder): void { $this->pathProvider ->method('getCustom') ->willReturn($this->filesPath . '/' . $folder . '/custom/Espo/Custom/Resources/'); $this->pathProvider ->method('getCore') ->willReturn($this->filesPath . '/' . $folder . '/application/Espo/Resources/'); $this->pathProvider ->method('getModule') ->willReturnCallback( function (?string $moduleName) use ($folder): string { $path = $this->filesPath . '/' . $folder . '/application/Espo/Modules/{*}/Resources/'; if ($moduleName === null) { return $path; } return str_replace('{*}', $moduleName, $path); } ); } public function testUnifyCase1CustomRoutes() { $this->initPathProvider('testCase1'); $this->metadata ->expects($this->once()) ->method('getModuleList') ->willReturn( ['Crm'] ); $expected = [ [ 'adjustedRoute' => '/Custom/{scope}/{id}/{name}', 'route' => '/Custom/:scope/:id/:name', 'method' => 'get', 'params' => [ 'controller' => 'Custom', 'action' => 'list', 'scope' => ':scope', 'id' => ':id', 'name' => ':name', ], ], [ 'adjustedRoute' => '/Activities/{scope}/{id}/{name}', 'route' => '/Activities/:scope/:id/:name', 'method' => 'get', 'params' => [ 'controller' => 'Activities', 'action' => 'list', 'scope' => ':scope', 'id' => ':id', 'name' => ':name', ], ], [ 'adjustedRoute' => '/Activities', 'route' => '/Activities', 'method' => 'get', 'params' => [ 'controller' => 'Activities', 'action' => 'listCalendarEvents', ], ], [ 'adjustedRoute' => '/App/user', 'route' => '/App/user', 'method' => 'get', 'params' => [ 'controller' => 'App', 'action' => 'user', ], ], [ 'adjustedRoute' => '/Metadata', 'route' => '/Metadata', 'method' => 'get', 'params' => [ 'controller' => 'Metadata', ], ], [ 'adjustedRoute' => '/{controller}/action/{action}', 'route' => '/:controller/action/:action', 'method' => 'post', 'params' => [ 'controller' => ':controller', 'action' => ':action', ], ], [ 'adjustedRoute' => '/{controller}/action/{action}', 'route' => '/:controller/action/:action', 'method' => 'get', 'params' => [ 'controller' => ':controller', 'action' => ':action', ], ], ]; $expectedItemList = array_map( function (array $item) { return new RouteItem( $item['method'], $item['route'], $item['adjustedRoute'], $item['params'] ?? [], $item['noAuth'] ?? false, null ); }, $expected ); $this->assertEquals($expectedItemList, $this->route->getFullList()); } public function testUnifyCase2ModuleRoutes() { $this->initPathProvider('testCase2'); $this->metadata ->expects($this->once()) ->method('getModuleList') ->willReturn( ['Crm', 'Test'] ); $expected = [ [ 'adjustedRoute' => '/Activities/{scope}/{id}/{name}', 'route' => '/Activities/:scope/:id/:name', 'method' => 'get', 'params' => [ 'controller' => 'Activities', 'action' => 'list', 'scope' => ':scope', 'id' => ':id', 'name' => ':name', ], ], [ 'adjustedRoute' => '/Activities', 'route' => '/Activities', 'method' => 'get', 'params' => [ 'controller' => 'Activities', 'action' => 'listCalendarEvents', ], ], [ 'adjustedRoute' => '/Test', 'route' => '/Test', 'method' => 'get', 'params' => [ 'controller' => 'Test', 'action' => 'listCalendarEvents', ], ], [ 'adjustedRoute' => '/App/user', 'route' => '/App/user', 'method' => 'get', 'params' => [ 'controller' => 'App', 'action' => 'user', ], ], [ 'adjustedRoute' => '/Metadata', 'route' => '/Metadata', 'method' => 'get', 'params' => [ 'controller' => 'Metadata', ], ], [ 'adjustedRoute' => '/{controller}/action/{action}', 'route' => '/:controller/action/:action', 'method' => 'post', 'params' => [ 'controller' => ':controller', 'action' => ':action', ], ], [ 'adjustedRoute' => '/{controller}/action/{action}', 'route' => '/:controller/action/:action', 'method' => 'get', 'params' => [ 'controller' => ':controller', 'action' => ':action', ], ], ]; $expectedItemList = array_map( function (array $item) { return new RouteItem( $item['method'], $item['route'], $item['adjustedRoute'], $item['params'] ?? [], $item['noAuth'] ?? false, null ); }, $expected ); $this->assertEquals($expectedItemList, $this->route->getFullList()); } public function testUnifyCase3ModuleRoutesWithRewrites() { $this->initPathProvider('testCase3'); $this->metadata ->expects($this->once()) ->method('getModuleList') ->willReturn( ['Crm', 'Test'] ); $expected = [ [ 'adjustedRoute' => '/Activities/{scope}/{id}/{name}', 'route' => '/Activities/:scope/:id/:name', 'method' => 'get', 'params' => [ 'controller' => 'Test', 'action' => 'list', 'scope' => ':scope', 'id' => ':id', 'name' => ':name', ], ], [ 'adjustedRoute' => '/Activities', 'route' => '/Activities', 'method' => 'get', 'params' => [ 'controller' => 'Activities', 'action' => 'listCalendarEvents', ], ], [ 'adjustedRoute' => '/Test', 'route' => '/Test', 'method' => 'get', 'params' => [ 'controller' => 'Test', 'action' => 'listCalendarEvents', ], ], [ 'adjustedRoute' => '/App/user', 'route' => '/App/user', 'method' => 'get', 'params' => [ 'controller' => 'App', 'action' => 'user', ], ], [ 'adjustedRoute' => '/Metadata', 'route' => '/Metadata', 'method' => 'get', 'params' => [ 'controller' => 'Metadata', ], ], [ 'adjustedRoute' => '/{controller}/action/{action}', 'route' => '/:controller/action/:action', 'method' => 'post', 'params' => [ 'controller' => ':controller', 'action' => ':action', ], ], [ 'adjustedRoute' => '/{controller}/action/{action}', 'route' => '/:controller/action/:action', 'method' => 'get', 'params' => [ 'controller' => ':controller', 'action' => ':action', ], ], ]; $expectedItemList = array_map( function (array $item) { return new RouteItem( $item['method'], $item['route'], $item['adjustedRoute'], $item['params'] ?? [], $item['noAuth'] ?? false, false ); }, $expected ); $this->assertEquals($expectedItemList, $this->route->getFullList()); } public function testUnifyCase4ModuleRoutesWithRewrites() { $this->initPathProvider('testCase4'); $this->metadata ->expects($this->once()) ->method('getModuleList') ->willReturn( ['Crm', 'Test', 'TestExt'] ); $expected = [ [ 'adjustedRoute' => '/Activities/{scope}/{id}/{name}', 'route' => '/Activities/:scope/:id/:name', 'method' => 'get', 'params' => [ 'controller' => 'TestExt', 'action' => 'list', 'scope' => ':scope', 'id' => ':id', 'name' => ':name', ], ], [ 'adjustedRoute' => '/Activities', 'route' => '/Activities', 'method' => 'get', 'params' => [ 'controller' => 'TestExt', 'action' => 'testExtListCalendarEvents' ], ], [ 'adjustedRoute' => '/Product', 'route' => '/Product', 'method' => 'get', 'params' => [ 'controller' => 'Product', 'action' => 'listProduct' ], ], [ 'adjustedRoute' => '/Test', 'route' => '/Test', 'method' => 'get', 'params' => [ 'controller' => 'Test', 'action' => 'testAction' ], ], ]; $expectedItemList = array_map( function (array $item) { return new RouteItem( $item['method'], $item['route'], $item['adjustedRoute'], $item['params'] ?? [], $item['noAuth'] ?? false, false ); }, $expected ); $this->assertEquals($expectedItemList, $this->route->getFullList()); } }