diff --git a/app/code/Magento/Store/Model/Store.php b/app/code/Magento/Store/Model/Store.php index affbbd8bed6fa..7ee198b06e00b 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->getServer('SCRIPT_FILENAME') !== 'bin/magento' ? + $this->_request->getServer('SCRIPT_FILENAME') : + ''; // 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 59c967e79dd3f..ca70ff14e08ad 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,7 +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()) + + $this->requestMock->expects($this->exactly(2)) ->method('getServer') ->with('SCRIPT_FILENAME') ->willReturn('test_script.php'); @@ -425,6 +427,93 @@ public function testGetBaseUrlEntryPoint() ); } + /** + * @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( + $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 ($secure, $expectedPath) { + $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') + ->willReturn('bin/magento'); + + /** @var Store $model */ + $model = $this->objectManagerHelper->getObject( + Store::class, + [ + 'config' => $configMock, + 'isCustomEntryPoint' => $isCustomEntryPoint, + 'request' => $this->requestMock + ] + ); + $model->setCode('scopeCode'); + + $this->setUrlModifier($model); + + $this->assertEquals( + $expectedBaseUrl, + $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/' + ], + [ + UrlInterface::URL_TYPE_DIRECT_LINK, + false, + false, + 'web/unsecure/base_link_url', + 'http://domain.com/web/unsecure/base_link_url/' + ], + [ + UrlInterface::URL_TYPE_LINK, + true, + false, + 'web/secure/base_link_url', + 'http://distro.com/web/secure/base_link_url/' + ], + ]; + } + public function testGetBaseUrlWrongType() { $this->expectException(\InvalidArgumentException::class);