diff --git a/app/code/Magento/Catalog/Test/Unit/Block/Widget/LinkTest.php b/app/code/Magento/Catalog/Test/Unit/Block/Widget/LinkTest.php index aab5685b64a7e..0fbda39ed35d7 100644 --- a/app/code/Magento/Catalog/Test/Unit/Block/Widget/LinkTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Block/Widget/LinkTest.php @@ -195,10 +195,17 @@ public function testStoreCodeShouldBeIncludedInURLOnlyIfItIsConfiguredSo( [ [Store::XML_PATH_USE_REWRITES, ReinitableConfigInterface::SCOPE_TYPE_DEFAULT, null, true], [Store::XML_PATH_UNSECURE_BASE_LINK_URL, ScopeConfigInterface::SCOPE_TYPE_DEFAULT, null, ''], + ] + ); + $config->expects($this->any()) + ->method('isSetFlag') + ->willReturnMap( + [ [ Store::XML_PATH_STORE_IN_URL, ReinitableConfigInterface::SCOPE_TYPE_DEFAULT, - null, $includeStoreCode + null, + $includeStoreCode ] ] ); diff --git a/app/code/Magento/Store/App/Request/StorePathInfoValidator.php b/app/code/Magento/Store/App/Request/StorePathInfoValidator.php index e22742992d786..0a353813866ac 100644 --- a/app/code/Magento/Store/App/Request/StorePathInfoValidator.php +++ b/app/code/Magento/Store/App/Request/StorePathInfoValidator.php @@ -77,17 +77,16 @@ public function __construct( */ public function getValidStoreCode(Http $request, string $pathInfo = '') : ?string { - $useStoreCodeInUrl = (bool) $this->config->getValue(Store::XML_PATH_STORE_IN_URL); - if (!$useStoreCodeInUrl) { + if (!$this->config->isSetFlag(Store::XML_PATH_STORE_IN_URL)) { return null; } - if (empty($pathInfo)) { + if ($pathInfo === '') { $pathInfo = $this->pathInfo->getPathInfo($request->getRequestUri(), $request->getBaseUrl()); } $storeCode = $this->getStoreCode($pathInfo); - if (empty($storeCode) || $storeCode === Store::ADMIN_CODE || !$this->storeCodeValidator->isValid($storeCode)) { + if ($storeCode === '' || $storeCode === Store::ADMIN_CODE || !$this->storeCodeValidator->isValid($storeCode)) { return null; } @@ -98,14 +97,9 @@ public function getValidStoreCode(Http $request, string $pathInfo = '') : ?strin try { $this->storeRepository->getActiveStoreByCode($storeCode); - $this->validatedStoreCodes[$storeCode] = $storeCode; - return $storeCode; - } catch (NoSuchEntityException $e) { - $this->validatedStoreCodes[$storeCode] = null; - return null; - } catch (StoreIsInactiveException $e) { - $this->validatedStoreCodes[$storeCode] = null; - return null; + return $this->validatedStoreCodes[$storeCode] = $storeCode; + } catch (NoSuchEntityException|StoreIsInactiveException) { + return $this->validatedStoreCodes[$storeCode] = null; } } diff --git a/app/code/Magento/Store/Model/Store.php b/app/code/Magento/Store/Model/Store.php index becdfa8e9e614..05c762cce5164 100644 --- a/app/code/Magento/Store/Model/Store.php +++ b/app/code/Magento/Store/Model/Store.php @@ -764,8 +764,8 @@ protected function _updatePathUseStoreView($url) public function isUseStoreInUrl() { return !($this->hasDisableStoreInUrl() && $this->getDisableStoreInUrl()) - && !$this->getConfig(StoreManager::XML_PATH_SINGLE_STORE_MODE_ENABLED) - && $this->getConfig(self::XML_PATH_STORE_IN_URL); + && !$this->_config->isSetFlag(StoreManager::XML_PATH_SINGLE_STORE_MODE_ENABLED) + && $this->_config->isSetFlag(self::XML_PATH_STORE_IN_URL); } /** diff --git a/app/code/Magento/Store/Test/Unit/App/Request/StorePathInfoValidatorTest.php b/app/code/Magento/Store/Test/Unit/App/Request/StorePathInfoValidatorTest.php index 399b1ae184d84..a2ecbc101bf95 100644 --- a/app/code/Magento/Store/Test/Unit/App/Request/StorePathInfoValidatorTest.php +++ b/app/code/Magento/Store/Test/Unit/App/Request/StorePathInfoValidatorTest.php @@ -80,7 +80,7 @@ public function testGetValidStoreCodeWithoutStoreInUrl(): void ->willReturn(true); $this->configMock->expects($this->once()) - ->method('getValue') + ->method('isSetFlag') ->with(Store::XML_PATH_STORE_IN_URL) ->willReturn(false); $this->storeRepositoryMock->expects($this->never()) @@ -95,7 +95,7 @@ public function testGetValidStoreCodeWithoutPathInfo(): void $storeCode = 'store1'; $this->configMock->expects($this->once()) - ->method('getValue') + ->method('isSetFlag') ->with(Store::XML_PATH_STORE_IN_URL) ->willReturn(true); $this->pathInfoMock->expects($this->once()) @@ -118,7 +118,7 @@ public function testGetValidStoreCodeWithoutPathInfo(): void public function testGetValidStoreCodeWithEmptyPathInfo(): void { $this->configMock->expects($this->once()) - ->method('getValue') + ->method('isSetFlag') ->with(Store::XML_PATH_STORE_IN_URL) ->willReturn(true); $this->pathInfoMock->expects($this->once()) @@ -139,7 +139,7 @@ public function testGetValidStoreCodeWithEmptyPathInfo(): void */ public function testGetValidStoreCodeThrowsException(\Throwable $exception): void { - $this->configMock->method('getValue') + $this->configMock->method('isSetFlag') ->with(Store::XML_PATH_STORE_IN_URL) ->willReturn(true); $this->storeCodeValidatorMock->method('isValid') @@ -169,7 +169,7 @@ public static function getValidStoreCodeExceptionDataProvider(): array */ public function testGetValidStoreCode(string $pathInfo, bool $isStoreCodeValid, ?string $expectedResult): void { - $this->configMock->method('getValue') + $this->configMock->method('isSetFlag') ->with(Store::XML_PATH_STORE_IN_URL) ->willReturn(true); $this->pathInfoMock->method('getPathInfo') @@ -195,7 +195,7 @@ public function testGetValidStoreCodeResultIsCached( bool $isStoreCodeValid, ?string $expectedResult ): void { - $this->configMock->method('getValue') + $this->configMock->method('isSetFlag') ->with(Store::XML_PATH_STORE_IN_URL) ->willReturn(true); $this->pathInfoMock->method('getPathInfo') diff --git a/app/code/Magento/Store/Test/Unit/Url/Plugin/RouteParamsResolverTest.php b/app/code/Magento/Store/Test/Unit/Url/Plugin/RouteParamsResolverTest.php index 9c1639b642da5..691e0282881fb 100644 --- a/app/code/Magento/Store/Test/Unit/Url/Plugin/RouteParamsResolverTest.php +++ b/app/code/Magento/Store/Test/Unit/Url/Plugin/RouteParamsResolverTest.php @@ -1,7 +1,7 @@ storeMock->expects($this->any())->method('getCode')->willReturn('custom_store'); $this->storeManagerMock = $this->getMockForAbstractClass(StoreManagerInterface::class); - $this->storeManagerMock - ->expects($this->once()) - ->method('getStore') - ->willReturn($this->storeMock); + $this->storeManagerMock->method('getStore')->willReturn($this->storeMock); $this->queryParamsResolverMock = $this->getMockForAbstractClass(QueryParamsResolverInterface::class); $this->model = new RouteParamsResolver( @@ -74,12 +71,8 @@ public function testBeforeSetRouteParamsScopeInParams() $this->scopeConfigMock ->expects($this->once()) - ->method('getValue') - ->with( - Store::XML_PATH_STORE_IN_URL, - ScopeInterface::SCOPE_STORE, - $storeCode - ) + ->method('isSetFlag') + ->with(Store::XML_PATH_STORE_IN_URL) ->willReturn(false); $this->storeManagerMock->expects($this->any())->method('hasSingleStore')->willReturn(false); @@ -106,15 +99,11 @@ public function testBeforeSetRouteParamsScopeUseStoreInUrl() $this->scopeConfigMock ->expects($this->once()) - ->method('getValue') - ->with( - Store::XML_PATH_STORE_IN_URL, - ScopeInterface::SCOPE_STORE, - $storeCode - ) + ->method('isSetFlag') + ->with(Store::XML_PATH_STORE_IN_URL) ->willReturn(true); - $this->storeManagerMock->expects($this->any())->method('hasSingleStore')->willReturn(false); + $this->storeManagerMock->expects($this->never())->method('hasSingleStore'); /** @var MockObject $routeParamsResolverMock */ $routeParamsResolverMock = $this->getMockBuilder(\Magento\Framework\Url\RouteParamsResolver::class) @@ -122,14 +111,11 @@ public function testBeforeSetRouteParamsScopeUseStoreInUrl() ->disableOriginalConstructor() ->getMock(); $routeParamsResolverMock->expects($this->once())->method('setScope')->with($storeCode); - $routeParamsResolverMock->expects($this->once())->method('getScope')->willReturn($storeCode); + $routeParamsResolverMock->expects($this->never())->method('getScope'); $this->queryParamsResolverMock->expects($this->never())->method('setQueryParam')->with('___store', $storeCode); - $this->model->beforeSetRouteParams( - $routeParamsResolverMock, - $data - ); + $this->model->beforeSetRouteParams($routeParamsResolverMock, $data); } public function testBeforeSetRouteParamsSingleStore() @@ -139,14 +125,10 @@ public function testBeforeSetRouteParamsSingleStore() $this->scopeConfigMock ->expects($this->once()) - ->method('getValue') - ->with( - Store::XML_PATH_STORE_IN_URL, - ScopeInterface::SCOPE_STORE, - $storeCode - ) + ->method('isSetFlag') + ->with(Store::XML_PATH_STORE_IN_URL) ->willReturn(false); - $this->storeManagerMock->expects($this->any())->method('hasSingleStore')->willReturn(true); + $this->storeManagerMock->expects($this->once())->method('hasSingleStore')->willReturn(true); /** @var MockObject $routeParamsResolverMock */ $routeParamsResolverMock = $this->getMockBuilder(\Magento\Framework\Url\RouteParamsResolver::class) @@ -154,7 +136,7 @@ public function testBeforeSetRouteParamsSingleStore() ->disableOriginalConstructor() ->getMock(); $routeParamsResolverMock->expects($this->once())->method('setScope')->with($storeCode); - $routeParamsResolverMock->expects($this->once())->method('getScope')->willReturn($storeCode); + $routeParamsResolverMock->expects($this->never())->method('getScope'); $this->queryParamsResolverMock->expects($this->never())->method('setQueryParam'); @@ -171,15 +153,11 @@ public function testBeforeSetRouteParamsNoScopeInParams() $this->scopeConfigMock ->expects($this->once()) - ->method('getValue') - ->with( - Store::XML_PATH_STORE_IN_URL, - ScopeInterface::SCOPE_STORE, - $storeCode - ) + ->method('isSetFlag') + ->with(Store::XML_PATH_STORE_IN_URL) ->willReturn(true); - $this->storeManagerMock->expects($this->any())->method('hasSingleStore')->willReturn(false); + $this->storeManagerMock->expects($this->never())->method('hasSingleStore'); /** @var MockObject $routeParamsResolverMock */ $routeParamsResolverMock = $this->getMockBuilder(\Magento\Framework\Url\RouteParamsResolver::class) @@ -187,7 +165,7 @@ public function testBeforeSetRouteParamsNoScopeInParams() ->disableOriginalConstructor() ->getMock(); $routeParamsResolverMock->expects($this->never())->method('setScope'); - $routeParamsResolverMock->expects($this->once())->method('getScope')->willReturn(false); + $routeParamsResolverMock->expects($this->never())->method('getScope'); $this->queryParamsResolverMock->expects($this->never())->method('setQueryParam')->with('___store', $storeCode); diff --git a/app/code/Magento/Store/Url/Plugin/RouteParamsResolver.php b/app/code/Magento/Store/Url/Plugin/RouteParamsResolver.php index 9c9d1e6023af0..84a8509454688 100644 --- a/app/code/Magento/Store/Url/Plugin/RouteParamsResolver.php +++ b/app/code/Magento/Store/Url/Plugin/RouteParamsResolver.php @@ -1,49 +1,37 @@ scopeConfig = $scopeConfig; - $this->storeManager = $storeManager; - $this->queryParamsResolver = $queryParamsResolver; } /** @@ -56,29 +44,19 @@ public function __construct( * * @return array */ - public function beforeSetRouteParams( - \Magento\Framework\Url\RouteParamsResolver $subject, - array $data, - $unsetOldParams = true - ) { + public function beforeSetRouteParams(UrlRouteParamsResolver $subject, array $data, $unsetOldParams = true) + { if (isset($data['_scope'])) { $subject->setScope($data['_scope']); unset($data['_scope']); } if (isset($data['_scope_to_url']) && (bool)$data['_scope_to_url'] === true) { - /** @var StoreInterface $currentScope */ - $currentScope = $subject->getScope(); - $storeCode = $currentScope && $currentScope instanceof StoreInterface ? - $currentScope->getCode() : - $this->storeManager->getStore()->getCode(); - - $useStoreInUrl = $this->scopeConfig->getValue( - Store::XML_PATH_STORE_IN_URL, - StoreScopeInterface::SCOPE_STORE, - $storeCode - ); - + $useStoreInUrl = $this->scopeConfig->isSetFlag(Store::XML_PATH_STORE_IN_URL); if (!$useStoreInUrl && !$this->storeManager->hasSingleStore()) { + $currentScope = $subject->getScope(); + $storeCode = $currentScope instanceof StoreInterface + ? $currentScope->getCode() + : $this->storeManager->getStore()->getCode(); $this->queryParamsResolver->setQueryParam('___store', $storeCode); } } diff --git a/dev/tests/integration/testsuite/Magento/Store/Model/StoreTest.php b/dev/tests/integration/testsuite/Magento/Store/Model/StoreTest.php index ba483136898db..0e14c830d4def 100644 --- a/dev/tests/integration/testsuite/Magento/Store/Model/StoreTest.php +++ b/dev/tests/integration/testsuite/Magento/Store/Model/StoreTest.php @@ -1,8 +1,9 @@ get(\Magento\Framework\App\Config\MutableScopeConfigInterface::class) - ->setValue(Store::XML_PATH_STORE_IN_URL, $useStoreCode, ScopeInterface::SCOPE_STORE); + ->setValue(Store::XML_PATH_STORE_IN_URL, $useStoreCode); $actual = $this->model->getBaseUrl($type); $this->assertEquals($expected, $actual); @@ -220,7 +221,7 @@ public function testGetBaseUrlForCustomEntryPoint($type, $useCustomEntryPoint, $ \Magento\TestFramework\Helper\Bootstrap::getObjectManager() ->get(\Magento\Framework\App\Config\MutableScopeConfigInterface::class) - ->setValue(Store::XML_PATH_STORE_IN_URL, $useStoreCode, ScopeInterface::SCOPE_STORE); + ->setValue(Store::XML_PATH_STORE_IN_URL, $useStoreCode); // emulate custom entry point $_SERVER['SCRIPT_FILENAME'] = 'custom_entry.php'; @@ -294,7 +295,7 @@ public function testGetCurrentUrl() { $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); $objectManager->get(\Magento\Framework\App\Config\MutableScopeConfigInterface::class) - ->setValue('web/url/use_store', true, ScopeInterface::SCOPE_STORE, 'secondstore'); + ->setValue('web/url/use_store', 1); $this->model->load('admin'); $this->model @@ -346,7 +347,7 @@ public function testGetCurrentUrlWithUseStoreInUrlFalse() { $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); $objectManager->get(\Magento\Framework\App\Config\ReinitableConfigInterface::class) - ->setValue('web/url/use_store', false, ScopeInterface::SCOPE_STORE, 'default'); + ->setValue('web/url/use_store', 0); /** @var \Magento\Store\Model\Store $secondStore */ $secondStore = $objectManager->get(StoreRepositoryInterface::class)->get('secondstore'); @@ -461,14 +462,12 @@ public function testIsUseStoreInUrl($storeInUrl, $disableStoreInUrl, $singleStor ->create(\Magento\Framework\Model\Context::class, ['appState' => $appStateMock]); $configMock - ->method('getValue') + ->method('isSetFlag') ->willReturnCallback( - function ($arg1) use ($singleStoreModeEnabled, $storeInUrl) { - if ($arg1 == StoreManager::XML_PATH_SINGLE_STORE_MODE_ENABLED) { - return $singleStoreModeEnabled; - } elseif ($arg1 == Store::XML_PATH_STORE_IN_URL) { - return $storeInUrl; - } + static fn($arg1) => match ($arg1) { + StoreManager::XML_PATH_SINGLE_STORE_MODE_ENABLED => $singleStoreModeEnabled, + Store::XML_PATH_STORE_IN_URL => $storeInUrl, + default => null } );