From 8eaf4245ce7e8e742726f596fa5431fe9fc936f8 Mon Sep 17 00:00:00 2001 From: hitesh-kumar-metacube <100181288+hitesh-kumar-metacube@users.noreply.github.com> Date: Tue, 10 Oct 2023 16:27:01 +0530 Subject: [PATCH] CC-17181 Fixed login page redirection issue in Merchant Portal. (#9655) CC-17181 Fixed login page redirection issue in Merchant Portal. --- architecture-baseline.json | 56 --------- .../RouterResource/AbstractRouterResource.php | 7 +- .../BackofficeRouterResource.php | 14 ++- .../MerchantPortalRouterResource.php | 52 ++++++++ src/Spryker/Zed/Router/RouterConfig.php | 20 +++ .../Shared/Router/_support/RouterTester.php | 2 +- .../Yves/Router/_support/RouterYvesTester.php | 2 +- .../BackofficeRouterResourceTest.php | 89 +++++++++++++ .../MerchantPortalRouterResourceTest.php | 117 ++++++++++++++++++ .../Router/_support/RouterBusinessTester.php | 29 +++++ .../_support/RouterCommunicationTester.php | 2 +- .../Zed/Router/_support/RouterRootTester.php | 2 +- tests/SprykerTest/Zed/Router/codeception.yml | 8 ++ 13 files changed, 338 insertions(+), 62 deletions(-) create mode 100644 tests/SprykerTest/Zed/Router/Business/Router/RouterResource/BackofficeRouterResourceTest.php create mode 100644 tests/SprykerTest/Zed/Router/Business/Router/RouterResource/MerchantPortalRouterResourceTest.php create mode 100644 tests/SprykerTest/Zed/Router/_support/RouterBusinessTester.php diff --git a/architecture-baseline.json b/architecture-baseline.json index 6eeb08f..1f465fb 100644 --- a/architecture-baseline.json +++ b/architecture-baseline.json @@ -1,60 +1,4 @@ [ - { - "fileName": "src/Spryker/Glue/Router/Resolver/RequestRequestValueResolver.php", - "description": "Method `Spryker\\Glue\\Router\\Resolver\\RequestRequestValueResolver::supports()` must have return type.", - "rule": "ExternalMethodExtensionReturnTypeRule", - "ruleset": "Spryker", - "priority": "1" - }, - { - "fileName": "src/Spryker/Glue/Router/Resolver/RequestRequestValueResolver.php", - "description": "Method `Spryker\\Glue\\Router\\Resolver\\RequestRequestValueResolver::resolve()` must have return type.", - "rule": "ExternalMethodExtensionReturnTypeRule", - "ruleset": "Spryker", - "priority": "1" - }, - { - "fileName": "src/Spryker/Yves/Router/Generator/UrlGenerator.php", - "description": "Method `Spryker\\Yves\\Router\\Generator\\UrlGenerator::generate()` must have return type.", - "rule": "ExternalMethodExtensionReturnTypeRule", - "ruleset": "Spryker", - "priority": "1" - }, - { - "fileName": "src/Spryker/Yves/Router/Resolver/RequestRequestValueResolver.php", - "description": "Method `Spryker\\Yves\\Router\\Resolver\\RequestRequestValueResolver::supports()` must have return type.", - "rule": "ExternalMethodExtensionReturnTypeRule", - "ruleset": "Spryker", - "priority": "1" - }, - { - "fileName": "src/Spryker/Yves/Router/Resolver/RequestRequestValueResolver.php", - "description": "Method `Spryker\\Yves\\Router\\Resolver\\RequestRequestValueResolver::resolve()` must have return type.", - "rule": "ExternalMethodExtensionReturnTypeRule", - "ruleset": "Spryker", - "priority": "1" - }, - { - "fileName": "src/Spryker/Yves/Router/Router/Router.php", - "description": "Method `Spryker\\Yves\\Router\\Router\\Router::getGenerator()` must have return type.", - "rule": "ExternalMethodExtensionReturnTypeRule", - "ruleset": "Spryker", - "priority": "1" - }, - { - "fileName": "src/Spryker/Zed/Router/Business/Generator/UrlGenerator.php", - "description": "Method `Spryker\\Zed\\Router\\Business\\Generator\\UrlGenerator::generate()` must have return type.", - "rule": "ExternalMethodExtensionReturnTypeRule", - "ruleset": "Spryker", - "priority": "1" - }, - { - "fileName": "src/Spryker/Zed/Router/Business/Router/Router.php", - "description": "Method `Spryker\\Zed\\Router\\Business\\Router\\Router::getGenerator()` must have return type.", - "rule": "ExternalMethodExtensionReturnTypeRule", - "ruleset": "Spryker", - "priority": "1" - }, { "fileName": "src/Spryker/Zed/Router/Business/RouterFacade.php", "description": "Zed Business Layer - Facade: The method `Spryker\\Zed\\Router\\Business\\RouterFacade::getRouter()()` is using an invalid return type `\\Spryker\\Zed\\Router\\Business\\Router\\ChainRouter` which violates the rule \"Every Facade should only return native types or transfer objects.\"", diff --git a/src/Spryker/Zed/Router/Business/Router/RouterResource/AbstractRouterResource.php b/src/Spryker/Zed/Router/Business/Router/RouterResource/AbstractRouterResource.php index e57f44b..78c2863 100644 --- a/src/Spryker/Zed/Router/Business/Router/RouterResource/AbstractRouterResource.php +++ b/src/Spryker/Zed/Router/Business/Router/RouterResource/AbstractRouterResource.php @@ -34,6 +34,11 @@ abstract class AbstractRouterResource implements ResourceInterface */ public const CONTROLLER_NAME_POSITION = 5; + /** + * @var string + */ + protected const APPLICATION_MODULE_NAME = 'application'; + /** * @var \Spryker\Zed\Router\RouterConfig */ @@ -208,7 +213,7 @@ protected function getPathCandidates(string $module, string $controller, string $pathCandidates[] = sprintf('/%s', $module); } - if ($module === 'application' && $controller === 'index' && $action === 'index') { + if ($module === static::APPLICATION_MODULE_NAME && $controller === 'index' && $action === 'index') { $pathCandidates[] = '/'; } diff --git a/src/Spryker/Zed/Router/Business/Router/RouterResource/BackofficeRouterResource.php b/src/Spryker/Zed/Router/Business/Router/RouterResource/BackofficeRouterResource.php index 1e2d673..363f480 100644 --- a/src/Spryker/Zed/Router/Business/Router/RouterResource/BackofficeRouterResource.php +++ b/src/Spryker/Zed/Router/Business/Router/RouterResource/BackofficeRouterResource.php @@ -7,6 +7,7 @@ namespace Spryker\Zed\Router\Business\Router\RouterResource; +use SplFileInfo; use Symfony\Component\Finder\Finder; class BackofficeRouterResource extends AbstractRouterResource @@ -20,7 +21,18 @@ protected function getFinder(): Finder $finder->files() ->in($this->config->getControllerDirectories()) ->name('*Controller.php') - ->notName('GatewayController.php'); + ->notName('GatewayController.php') + ->filter(function (SplFileInfo $splFileInfo) { + $pathToController = $splFileInfo->getPathname(); + foreach ($this->config->getNotAllowedBackofficeControllerDirectories() as $notAllowedBackofficeControllerDirectory) { + $notAllowedBackofficeControllerDirectoryPath = sprintf('%s%s%s', DIRECTORY_SEPARATOR, $notAllowedBackofficeControllerDirectory, DIRECTORY_SEPARATOR); + if (strpos($pathToController, $notAllowedBackofficeControllerDirectoryPath) !== false) { + return false; + } + } + + return true; + }); return $finder; } diff --git a/src/Spryker/Zed/Router/Business/Router/RouterResource/MerchantPortalRouterResource.php b/src/Spryker/Zed/Router/Business/Router/RouterResource/MerchantPortalRouterResource.php index 56ec406..a24d82c 100644 --- a/src/Spryker/Zed/Router/Business/Router/RouterResource/MerchantPortalRouterResource.php +++ b/src/Spryker/Zed/Router/Business/Router/RouterResource/MerchantPortalRouterResource.php @@ -7,7 +7,10 @@ namespace Spryker\Zed\Router\Business\Router\RouterResource; +use ReflectionMethod; use SplFileInfo; +use Spryker\Zed\Router\Business\Route\Route; +use Spryker\Zed\Router\Business\Route\RouteCollection; use Symfony\Component\Finder\Finder; class MerchantPortalRouterResource extends AbstractRouterResource @@ -17,6 +20,21 @@ class MerchantPortalRouterResource extends AbstractRouterResource */ protected const MERCHANT_PORTAL_FOLDER_FILTER = 'MerchantPortal'; + /** + * @var string + */ + protected const APPLICATION_MODULE_NAME = 'merchant-portal-application'; + + /** + * @var string + */ + protected const ROUTE_NAME_SECURITY_MERCHANT_PORTAL_GUI_LOGIN = 'security-merchant-portal-gui:login'; + + /** + * @var string + */ + protected const ROUTE_NAME_SECURITY_GUI_LOGIN = 'security-gui:login'; + /** * @return \Symfony\Component\Finder\Finder */ @@ -33,4 +51,38 @@ protected function getFinder(): Finder return $finder; } + + /** + * @param \ReflectionMethod $method + * @param \Spryker\Zed\Router\Business\Route\RouteCollection $routeCollection + * @param string $pathCandidate + * @param string $controllerClassName + * @param string $template + * + * @return \Spryker\Zed\Router\Business\Route\RouteCollection + */ + protected function addRouteToCollection( + ReflectionMethod $method, + RouteCollection $routeCollection, + string $pathCandidate, + string $controllerClassName, + string $template + ): RouteCollection { + $route = new Route($pathCandidate); + + $route->addDefaults([ + '_controller' => [$controllerClassName, $method->getName()], + '_template' => $template, + ]); + + $routeName = str_replace('/', ':', trim($pathCandidate, '/')); + + $routeCollection->add($routeName, $route, 0); + + if ($routeName === static::ROUTE_NAME_SECURITY_MERCHANT_PORTAL_GUI_LOGIN) { + $routeCollection->add(static::ROUTE_NAME_SECURITY_GUI_LOGIN, $route, 0); + } + + return $routeCollection; + } } diff --git a/src/Spryker/Zed/Router/RouterConfig.php b/src/Spryker/Zed/Router/RouterConfig.php index 56a325b..8b63983 100644 --- a/src/Spryker/Zed/Router/RouterConfig.php +++ b/src/Spryker/Zed/Router/RouterConfig.php @@ -18,6 +18,11 @@ */ class RouterConfig extends AbstractBundleConfig { + /** + * @var string + */ + protected const DIRECTORY_NAME_MERCHANT_PORTAL_APPLICATION = 'MerchantPortalApplication'; + /** * Specification: * - Returns a Router configuration which makes use of a Router cache. @@ -288,4 +293,19 @@ public function getSslExcludedRouteNames(): array { return $this->get(RouterConstants::ZED_SSL_EXCLUDED_ROUTE_NAMES, []); } + + /** + * Specification: + * - Returns a list of directories that are not allowed for Backoffice controllers. + * + * @api + * + * @return list + */ + public function getNotAllowedBackofficeControllerDirectories(): array + { + return [ + static::DIRECTORY_NAME_MERCHANT_PORTAL_APPLICATION, + ]; + } } diff --git a/tests/SprykerTest/Shared/Router/_support/RouterTester.php b/tests/SprykerTest/Shared/Router/_support/RouterTester.php index e3667bf..0559141 100644 --- a/tests/SprykerTest/Shared/Router/_support/RouterTester.php +++ b/tests/SprykerTest/Shared/Router/_support/RouterTester.php @@ -26,7 +26,7 @@ * @method void comment($description) * @method void pause() * - * @SuppressWarnings(PHPMD) + * @SuppressWarnings(\SprykerTest\Shared\Router\PHPMD) */ class RouterTester extends Actor { diff --git a/tests/SprykerTest/Yves/Router/_support/RouterYvesTester.php b/tests/SprykerTest/Yves/Router/_support/RouterYvesTester.php index d2003dc..c6a7ab3 100644 --- a/tests/SprykerTest/Yves/Router/_support/RouterYvesTester.php +++ b/tests/SprykerTest/Yves/Router/_support/RouterYvesTester.php @@ -21,7 +21,7 @@ * @method void comment($description) * @method \Codeception\Lib\Friend haveFriend($name, $actorClass = null) * - * @SuppressWarnings(PHPMD) + * @SuppressWarnings(\SprykerTest\Yves\Router\PHPMD) */ class RouterYvesTester extends Actor { diff --git a/tests/SprykerTest/Zed/Router/Business/Router/RouterResource/BackofficeRouterResourceTest.php b/tests/SprykerTest/Zed/Router/Business/Router/RouterResource/BackofficeRouterResourceTest.php new file mode 100644 index 0000000..ac0ca0e --- /dev/null +++ b/tests/SprykerTest/Zed/Router/Business/Router/RouterResource/BackofficeRouterResourceTest.php @@ -0,0 +1,89 @@ + $notAllowedBackofficeControllerDirectories + * @param int $expectedCount + * + * @return void + */ + public function testGetFinder(array $notAllowedBackofficeControllerDirectories, int $expectedCount): void + { + // Arrange + $this->tester->mockConfigMethod('getControllerDirectories', [codecept_data_dir('Fixtures/Controller/')]); + + /** @var \Spryker\Zed\Router\RouterConfig $routerConfig */ + $routerConfig = $this->tester->mockConfigMethod('getNotAllowedBackofficeControllerDirectories', $notAllowedBackofficeControllerDirectories); + + // Act + $finder = $this->createBackofficeRouterResource($routerConfig)->getFinder(); + + // Assert + $this->assertCount($expectedCount, $finder); + } + + /** + * @return array|int>> + */ + protected function getGetFinderDataProvider(): array + { + return [ + 'Should not filter out files when all directories are allowed.' => [[], 1], + 'Should not filter out files when a directory does not exist.' => [['Mock'], 1], + 'Should filter out files when a directory does not exist.' => [['Fixtures'], 0], + ]; + } + + /** + * @param \Spryker\Zed\Router\RouterConfig $config + * + * @return \Spryker\Zed\Router\Business\RouterResource\ResourceInterface + */ + protected function createBackofficeRouterResource(RouterConfig $config): ResourceInterface + { + return new class ($config) extends BackofficeRouterResource + { + /** + * @return \Symfony\Component\Finder\Finder + */ + public function getFinder(): Finder + { + return parent::getFinder(); + } + }; + } +} diff --git a/tests/SprykerTest/Zed/Router/Business/Router/RouterResource/MerchantPortalRouterResourceTest.php b/tests/SprykerTest/Zed/Router/Business/Router/RouterResource/MerchantPortalRouterResourceTest.php new file mode 100644 index 0000000..caa6d94 --- /dev/null +++ b/tests/SprykerTest/Zed/Router/Business/Router/RouterResource/MerchantPortalRouterResourceTest.php @@ -0,0 +1,117 @@ + $expectedPathCandidates + * + * @return void + */ + public function testGetPathCandidates(string $module, string $controller, string $action, array $expectedPathCandidates): void + { + // Arrange + /** @var \Spryker\Zed\Router\RouterConfig $routerConfig */ + $routerConfig = $this->tester->getModuleConfig(); + + // Act + $pathCandidates = $this->createMerchantPortalRouterResource($routerConfig) + ->getPathCandidates($module, $controller, $action); + + // Assert + $this->assertSame($expectedPathCandidates, $pathCandidates); + } + + /** + * @return array>> + */ + protected function getControllerPathDataProvider(): array + { + return [ + [ + 'MockModule', 'Edit', 'edit', ['/mock-module/edit/edit'], + ], + [ + 'MockModule', 'Edit', 'index', ['/mock-module/edit/index', '/mock-module/edit'], + ], + [ + 'MockModule', 'Index', 'index', ['/mock-module/index/index', '/mock-module/index', '/mock-module'], + ], + [ + static::APPLICATION_MODULE_NAME, + 'Index', + 'index', + [ + sprintf('/%s/index/index', static::APPLICATION_MODULE_NAME), + sprintf('/%s/index', static::APPLICATION_MODULE_NAME), + sprintf('/%s', static::APPLICATION_MODULE_NAME), + '/', + ], + ], + ]; + } + + /** + * @param \Spryker\Zed\Router\RouterConfig $config + * + * @return \Spryker\Zed\Router\Business\RouterResource\ResourceInterface + */ + protected function createMerchantPortalRouterResource(RouterConfig $config): ResourceInterface + { + return new class ($config) extends MerchantPortalRouterResource + { + /** + * @param string $module + * @param string $controller + * @param string $action + * + * @return list + */ + public function getPathCandidates(string $module, string $controller, string $action): array + { + return parent::getPathCandidates($module, $controller, $action); + } + }; + } +} diff --git a/tests/SprykerTest/Zed/Router/_support/RouterBusinessTester.php b/tests/SprykerTest/Zed/Router/_support/RouterBusinessTester.php new file mode 100644 index 0000000..fc9b2eb --- /dev/null +++ b/tests/SprykerTest/Zed/Router/_support/RouterBusinessTester.php @@ -0,0 +1,29 @@ +