public function testGetDirectUrl() { $requestMock = $this->getRequestMock(); $routeConfigMock = $this->getMock('Magento\Framework\App\Route\ConfigInterface'); $model = $this->getUrlModel( [ 'scopeResolver' => $this->scopeResolverMock, 'routeParamsResolverFactory' => $this->getRouteParamsResolverFactory(), 'queryParamsResolver' => $this->queryParamsResolverMock, 'request' => $requestMock, 'routeConfig' => $routeConfigMock, 'routeParamsPreprocessor' => $this->routeParamsPreprocessorMock ] ); $baseUrl = 'http://localhost/index.php/'; $urlType = \Magento\Framework\UrlInterface::URL_TYPE_LINK; $this->scopeMock->expects($this->once())->method('getBaseUrl')->will($this->returnValue($baseUrl)); $this->scopeResolverMock->expects($this->any()) ->method('getScope') ->will($this->returnValue($this->scopeMock)); $this->routeParamsResolverMock->expects($this->any())->method('getType')->will($this->returnValue($urlType)); $this->routeParamsPreprocessorMock->expects($this->once()) ->method('execute') ->willReturnArgument(2); $requestMock->expects($this->once())->method('isDirectAccessFrontendName')->will($this->returnValue(true)); $url = $model->getDirectUrl('direct-url'); $this->assertEquals('http://localhost/index.php/direct-url', $url); }
/** * Build and cache url by requested path and parameters * * @param string|null $routePath * @param array|null $routeParams * @return string * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function getUrl($routePath = null, $routeParams = null) { if (filter_var($routePath, FILTER_VALIDATE_URL)) { return $routePath; } $routeParams = $this->routeParamsPreprocessor ->execute($this->_scopeResolver->getAreaCode(), $routePath, $routeParams); $isCached = true; $isArray = is_array($routeParams); if ($isArray) { array_walk_recursive( $routeParams, function ($item) use (&$isCached) { if (is_object($item)) { $isCached = false; } } ); } if(!$isCached) { return $this->getUrlModifier()->execute( $this->createUrl($routePath, $routeParams), $routeParams ); } $cashedParams = $routeParams; if ($isArray) { ksort($cashedParams); } $cacheKey = md5($routePath . serialize($cashedParams)); if (!isset($this->cacheUrl[$cacheKey])) { $this->cacheUrl[$cacheKey] = $this->getUrlModifier()->execute( $this->createUrl($routePath, $routeParams), $routeParams ); } return $this->cacheUrl[$cacheKey]; }