From 4f4fff8f845aa656a86c7a940fffec5f0956004c Mon Sep 17 00:00:00 2001 From: MeenakshiSundaram Date: Mon, 15 Nov 2021 21:54:03 +0530 Subject: [PATCH 1/4] Fix 'magento' adding in the URL when Use Web Server Rewrites set to NO using Console Command --- app/code/Magento/Store/Model/Store.php | 9 ++-- .../Store/Test/Unit/Model/StoreTest.php | 41 +++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/Store/Model/Store.php b/app/code/Magento/Store/Model/Store.php index f437d9bca0b74..933f96428c654 100644 --- a/app/code/Magento/Store/Model/Store.php +++ b/app/code/Magento/Store/Model/Store.php @@ -711,9 +711,12 @@ protected function _updatePathUseRewrites($url) if ($this->_isCustomEntryPoint()) { $indexFileName = 'index.php'; } else { - $scriptFilename = $this->_request->getServer('SCRIPT_FILENAME'); - // phpcs:ignore Magento2.Functions.DiscouragedFunction - $indexFileName = basename($scriptFilename); + $indexFileName = ''; + if ($this->_request->getOriginalPathInfo()) { + $scriptFilename = $this->_request->getServer('SCRIPT_FILENAME'); + // phpcs:ignore Magento2.Functions.DiscouragedFunction + $indexFileName = basename($scriptFilename); + } } $url .= $indexFileName . '/'; } diff --git a/app/code/Magento/Store/Test/Unit/Model/StoreTest.php b/app/code/Magento/Store/Test/Unit/Model/StoreTest.php index 59c967e79dd3f..6bc26105e330b 100644 --- a/app/code/Magento/Store/Test/Unit/Model/StoreTest.php +++ b/app/code/Magento/Store/Test/Unit/Model/StoreTest.php @@ -91,6 +91,7 @@ protected function setUp(): void 'getDistroBaseUrl', 'isSecure', 'getServer', + 'getOriginalPathInfo' ]); $this->filesystemMock = $this->getMockBuilder(Filesystem::class) @@ -401,6 +402,9 @@ public function testGetBaseUrlEntryPoint() ->willReturnCallback(function ($path, $scope, $scopeCode) use ($expectedPath) { return $expectedPath == $path ? 'http://domain.com/' . $path . '/' : null; }); + $this->requestMock->expects($this->once()) + ->method('getOriginalPathInfo') + ->willReturn('web/unsecure/base_link_url/test_script.php/'); $this->requestMock->expects($this->once()) ->method('getServer') ->with('SCRIPT_FILENAME') @@ -425,6 +429,43 @@ public function testGetBaseUrlEntryPoint() ); } + /** + * @return void + */ + public function testGetBaseUrlFromCli() + { + $expectedPath = 'web/unsecure/base_link_url'; + $expectedBaseUrl = 'http://domain.com/web/unsecure/base_link_url/'; + /** @var \Magento\Framework\App\Config\ReinitableConfigInterface $configMock */ + $configMock = $this->getMockForAbstractClass(ReinitableConfigInterface::class); + $configMock->expects($this->atLeastOnce()) + ->method('getValue') + ->willReturnCallback(function ($path, $scope, $scopeCode) use ($expectedPath) { + return $expectedPath == $path ? 'http://domain.com/' . $path . '/' : null; + }); + $this->requestMock->expects($this->once()) + ->method('getOriginalPathInfo') + ->willReturn(''); + + /** @var Store $model */ + $model = $this->objectManagerHelper->getObject( + Store::class, + [ + 'config' => $configMock, + 'isCustomEntryPoint' => false, + 'request' => $this->requestMock + ] + ); + $model->setCode('scopeCode'); + + $this->setUrlModifier($model); + + $this->assertEquals( + $expectedBaseUrl, + $model->getBaseUrl(UrlInterface::URL_TYPE_LINK, false) + ); + } + public function testGetBaseUrlWrongType() { $this->expectException(\InvalidArgumentException::class); From 782f3b05e0e94378191262be6cea37e2f6ec4800 Mon Sep 17 00:00:00 2001 From: engcom-Bravo Date: Fri, 31 Dec 2021 17:57:29 +0530 Subject: [PATCH 2/4] ISSUE-25976 Fixed Unit tests --- app/code/Magento/Store/Model/Store.php | 4 +- .../Store/Test/Unit/Model/StoreTest.php | 67 ++++++++++++++++--- 2 files changed, 59 insertions(+), 12 deletions(-) diff --git a/app/code/Magento/Store/Model/Store.php b/app/code/Magento/Store/Model/Store.php index affbbd8bed6fa..e9730d40adf02 100644 --- a/app/code/Magento/Store/Model/Store.php +++ b/app/code/Magento/Store/Model/Store.php @@ -698,7 +698,9 @@ protected function _updatePathUseRewrites($url) if ($this->_isCustomEntryPoint()) { $indexFileName = 'index.php'; } else { - $scriptFilename = $this->_request->getServer('SCRIPT_FILENAME'); + $scriptFilename = $this->_request->getOriginalPathInfo() ? + $this->_request->getServer('SCRIPT_FILENAME') : + '/server.php'; // phpcs:ignore Magento2.Functions.DiscouragedFunction $indexFileName = is_string($scriptFilename) ? basename($scriptFilename) : ''; } diff --git a/app/code/Magento/Store/Test/Unit/Model/StoreTest.php b/app/code/Magento/Store/Test/Unit/Model/StoreTest.php index 6bc26105e330b..444d564563141 100644 --- a/app/code/Magento/Store/Test/Unit/Model/StoreTest.php +++ b/app/code/Magento/Store/Test/Unit/Model/StoreTest.php @@ -404,7 +404,7 @@ public function testGetBaseUrlEntryPoint() }); $this->requestMock->expects($this->once()) ->method('getOriginalPathInfo') - ->willReturn('web/unsecure/base_link_url/test_script.php/'); + ->willReturn('test.html'); $this->requestMock->expects($this->once()) ->method('getServer') ->with('SCRIPT_FILENAME') @@ -430,20 +430,35 @@ public function testGetBaseUrlEntryPoint() } /** - * @return void + * @dataProvider getCliBaseUrlDataProvider + * + * @covers \Magento\Store\Model\Store::getBaseUrl + * @covers \Magento\Store\Model\Store::getCode + * @covers \Magento\Store\Model\Store::_updatePathUseRewrites + * @covers \Magento\Store\Model\Store::getConfig + * + * @param string $type + * @param boolean $secure + * @param boolean $isCustomEntryPoint + * @param string $expectedPath + * @param string $expectedBaseUrl */ - public function testGetBaseUrlFromCli() - { - $expectedPath = 'web/unsecure/base_link_url'; - $expectedBaseUrl = 'http://domain.com/web/unsecure/base_link_url/'; + public function testGetBaseUrlFromCli( + $type, + $secure, + $isCustomEntryPoint, + $expectedPath, + $expectedBaseUrl + ) { /** @var \Magento\Framework\App\Config\ReinitableConfigInterface $configMock */ $configMock = $this->getMockForAbstractClass(ReinitableConfigInterface::class); $configMock->expects($this->atLeastOnce()) ->method('getValue') - ->willReturnCallback(function ($path, $scope, $scopeCode) use ($expectedPath) { - return $expectedPath == $path ? 'http://domain.com/' . $path . '/' : null; + ->willReturnCallback(function ($path, $scope, $scopeCode) use ($secure, $expectedPath) { + $url = $secure ? '{{base_url}}' : 'http://domain.com/'; + return $expectedPath == $path ? $url . $path . '/' : null; }); - $this->requestMock->expects($this->once()) + $this->requestMock->expects($this->any()) ->method('getOriginalPathInfo') ->willReturn(''); @@ -452,7 +467,7 @@ public function testGetBaseUrlFromCli() Store::class, [ 'config' => $configMock, - 'isCustomEntryPoint' => false, + 'isCustomEntryPoint' => $isCustomEntryPoint, 'request' => $this->requestMock ] ); @@ -462,10 +477,40 @@ public function testGetBaseUrlFromCli() $this->assertEquals( $expectedBaseUrl, - $model->getBaseUrl(UrlInterface::URL_TYPE_LINK, false) + $model->getBaseUrl($type, $secure) ); } + /** + * @return array + */ + public function getCliBaseUrlDataProvider() + { + return [ + [ + UrlInterface::URL_TYPE_LINK, + false, + false, + 'web/unsecure/base_link_url', + 'http://domain.com/web/unsecure/base_link_url/server.php/' + ], + [ + UrlInterface::URL_TYPE_DIRECT_LINK, + false, + false, + 'web/unsecure/base_link_url', + 'http://domain.com/web/unsecure/base_link_url/server.php/' + ], + [ + UrlInterface::URL_TYPE_LINK, + true, + false, + 'web/secure/base_link_url', + 'web/secure/base_link_url/server.php/' + ], + ]; + } + public function testGetBaseUrlWrongType() { $this->expectException(\InvalidArgumentException::class); From a748e3839e114f706ffd6d3768a3b43704ee4957 Mon Sep 17 00:00:00 2001 From: engcom-Bravo Date: Fri, 31 Dec 2021 20:08:40 +0530 Subject: [PATCH 3/4] ISSUE-25976 Updated code as integration tests failing --- app/code/Magento/Store/Model/Store.php | 4 ++-- .../Magento/Store/Test/Unit/Model/StoreTest.php | 17 ++++++++--------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/app/code/Magento/Store/Model/Store.php b/app/code/Magento/Store/Model/Store.php index e9730d40adf02..7ee198b06e00b 100644 --- a/app/code/Magento/Store/Model/Store.php +++ b/app/code/Magento/Store/Model/Store.php @@ -698,9 +698,9 @@ protected function _updatePathUseRewrites($url) if ($this->_isCustomEntryPoint()) { $indexFileName = 'index.php'; } else { - $scriptFilename = $this->_request->getOriginalPathInfo() ? + $scriptFilename = $this->_request->getServer('SCRIPT_FILENAME') !== 'bin/magento' ? $this->_request->getServer('SCRIPT_FILENAME') : - '/server.php'; + ''; // phpcs:ignore Magento2.Functions.DiscouragedFunction $indexFileName = is_string($scriptFilename) ? basename($scriptFilename) : ''; } diff --git a/app/code/Magento/Store/Test/Unit/Model/StoreTest.php b/app/code/Magento/Store/Test/Unit/Model/StoreTest.php index 444d564563141..28c64469e4cab 100644 --- a/app/code/Magento/Store/Test/Unit/Model/StoreTest.php +++ b/app/code/Magento/Store/Test/Unit/Model/StoreTest.php @@ -402,10 +402,8 @@ public function testGetBaseUrlEntryPoint() ->willReturnCallback(function ($path, $scope, $scopeCode) use ($expectedPath) { return $expectedPath == $path ? 'http://domain.com/' . $path . '/' : null; }); - $this->requestMock->expects($this->once()) - ->method('getOriginalPathInfo') - ->willReturn('test.html'); - $this->requestMock->expects($this->once()) + + $this->requestMock->expects($this->exactly(2)) ->method('getServer') ->with('SCRIPT_FILENAME') ->willReturn('test_script.php'); @@ -459,8 +457,9 @@ public function testGetBaseUrlFromCli( return $expectedPath == $path ? $url . $path . '/' : null; }); $this->requestMock->expects($this->any()) - ->method('getOriginalPathInfo') - ->willReturn(''); + ->method('getServer') + ->with('SCRIPT_FILENAME') + ->willReturn('bin/magento'); /** @var Store $model */ $model = $this->objectManagerHelper->getObject( @@ -492,21 +491,21 @@ public function getCliBaseUrlDataProvider() false, false, 'web/unsecure/base_link_url', - 'http://domain.com/web/unsecure/base_link_url/server.php/' + 'http://domain.com/web/unsecure/base_link_url/' ], [ UrlInterface::URL_TYPE_DIRECT_LINK, false, false, 'web/unsecure/base_link_url', - 'http://domain.com/web/unsecure/base_link_url/server.php/' + 'http://domain.com/web/unsecure/base_link_url/' ], [ UrlInterface::URL_TYPE_LINK, true, false, 'web/secure/base_link_url', - 'web/secure/base_link_url/server.php/' + 'web/secure/base_link_url/' ], ]; } From 676b40d9d6e9c46409af0b36133b6f4b5a4470a3 Mon Sep 17 00:00:00 2001 From: engcom-Bravo Date: Sun, 2 Jan 2022 10:54:29 +0530 Subject: [PATCH 4/4] ISSUE-25976 Fixed unit test failure --- app/code/Magento/Store/Test/Unit/Model/StoreTest.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Store/Test/Unit/Model/StoreTest.php b/app/code/Magento/Store/Test/Unit/Model/StoreTest.php index 28c64469e4cab..ca70ff14e08ad 100644 --- a/app/code/Magento/Store/Test/Unit/Model/StoreTest.php +++ b/app/code/Magento/Store/Test/Unit/Model/StoreTest.php @@ -456,6 +456,10 @@ public function testGetBaseUrlFromCli( $url = $secure ? '{{base_url}}' : 'http://domain.com/'; return $expectedPath == $path ? $url . $path . '/' : null; }); + $this->requestMock->expects($this->any()) + ->method('getDistroBaseUrl') + ->willReturn('http://distro.com/'); + $this->requestMock->expects($this->any()) ->method('getServer') ->with('SCRIPT_FILENAME') @@ -505,7 +509,7 @@ public function getCliBaseUrlDataProvider() true, false, 'web/secure/base_link_url', - 'web/secure/base_link_url/' + 'http://distro.com/web/secure/base_link_url/' ], ]; }