/**
  * {@inheritdoc}
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function setRouteParams(array $data, $unsetOldParams = true)
 {
     if (isset($data['_type'])) {
         $this->setType($data['_type']);
         unset($data['_type']);
     }
     if (isset($data['_forced_secure'])) {
         $this->setSecure((bool) $data['_forced_secure']);
         $this->setSecureIsForced(true);
         unset($data['_forced_secure']);
     } elseif (isset($data['_secure'])) {
         $this->setSecure((bool) $data['_secure']);
         unset($data['_secure']);
     }
     if (isset($data['_absolute'])) {
         unset($data['_absolute']);
     }
     if ($unsetOldParams) {
         $this->unsetData('route_params');
     }
     if (isset($data['_current'])) {
         if (is_array($data['_current'])) {
             foreach ($data['_current'] as $key) {
                 if (array_key_exists($key, $data) || !$this->request->getUserParam($key)) {
                     continue;
                 }
                 $data[$key] = $this->request->getUserParam($key);
             }
         } elseif ($data['_current']) {
             foreach ($this->request->getUserParams() as $key => $value) {
                 if (array_key_exists($key, $data) || $this->getRouteParam($key)) {
                     continue;
                 }
                 $data[$key] = $value;
             }
             foreach ($this->request->getQuery() as $key => $value) {
                 $this->queryParamsResolver->setQueryParam($key, $value);
             }
         }
         unset($data['_current']);
     }
     if (isset($data['_use_rewrite'])) {
         unset($data['_use_rewrite']);
     }
     foreach ($data as $key => $value) {
         $this->setRouteParam($key, $value);
     }
     return $this;
 }
 /**
  * Process scope query parameters.
  *
  * @param \Magento\Framework\Url\RouteParamsResolver $subject
  * @param callable $proceed
  * @param array $data
  * @param bool $unsetOldParams
  * @return \Magento\Framework\Url\RouteParamsResolver
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function aroundSetRouteParams(\Magento\Framework\Url\RouteParamsResolver $subject, \Closure $proceed, 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) {
         $storeCode = $subject->getScope() ?: $this->storeManager->getStore()->getCode();
         $useStoreInUrl = $this->scopeConfig->getValue(Store::XML_PATH_STORE_IN_URL, StoreScopeInterface::SCOPE_STORE, $storeCode);
         if (!$useStoreInUrl && !$this->storeManager->hasSingleStore()) {
             $this->queryParamsResolver->setQueryParam('___store', $storeCode);
         }
     }
     unset($data['_scope_to_url']);
     return $proceed($data, $unsetOldParams);
 }
Example #3
0
 /**
  * {@inheritdoc}
  */
 public function setRouteParams(array $data, $unsetOldParams = true)
 {
     if (isset($data['_type'])) {
         $this->setType($data['_type']);
         unset($data['_type']);
     }
     if (isset($data['_scope'])) {
         $this->setScope($data['_scope']);
         unset($data['_scope']);
     }
     if (isset($data['_forced_secure'])) {
         $this->setSecure((bool) $data['_forced_secure']);
         $this->setSecureIsForced(true);
         unset($data['_forced_secure']);
     } elseif (isset($data['_secure'])) {
         $this->setSecure((bool) $data['_secure']);
         unset($data['_secure']);
     }
     if (isset($data['_absolute'])) {
         unset($data['_absolute']);
     }
     if ($unsetOldParams) {
         $this->unsetData('route_params');
     }
     if (isset($data['_current'])) {
         if (is_array($data['_current'])) {
             foreach ($data['_current'] as $key) {
                 if (array_key_exists($key, $data) || !$this->_request->getUserParam($key)) {
                     continue;
                 }
                 $data[$key] = $this->_request->getUserParam($key);
             }
         } elseif ($data['_current']) {
             foreach ($this->_request->getUserParams() as $key => $value) {
                 if (array_key_exists($key, $data) || $this->getRouteParam($key)) {
                     continue;
                 }
                 $data[$key] = $value;
             }
             foreach ($this->_request->getQuery() as $key => $value) {
                 $this->_queryParamsResolver->setQueryParam($key, $value);
             }
         }
         unset($data['_current']);
     }
     if (isset($data['_use_rewrite'])) {
         unset($data['_use_rewrite']);
     }
     if (isset($data['_scope_to_url']) && (bool) $data['_scope_to_url'] === true) {
         $store = $this->getScope() ?: $this->_storeManager->getStore();
         if (!$this->_scopeConfig->getValue(\Magento\Store\Model\Store::XML_PATH_STORE_IN_URL, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $this->getScope()) && !$this->_storeManager->hasSingleStore()) {
             $this->_queryParamsResolver->setQueryParam('___store', $store->getCode());
         }
     }
     unset($data['_scope_to_url']);
     foreach ($data as $key => $value) {
         $this->setRouteParam($key, $value);
     }
     return $this;
 }
Example #4
0
 public function testAddSessionParam()
 {
     $model = $this->getUrlModel(['session' => $this->sessionMock, 'sidResolver' => $this->sidResolverMock, 'queryParamsResolver' => $this->queryParamsResolverMock]);
     $this->sidResolverMock->expects($this->once())->method('getSessionIdQueryParam')->with($this->sessionMock)->will($this->returnValue('sid'));
     $this->sessionMock->expects($this->once())->method('getSessionId')->will($this->returnValue('session-id'));
     $this->queryParamsResolverMock->expects($this->once())->method('setQueryParam')->with('sid', 'session-id');
     $model->addSessionParam();
 }
 /**
  * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  */
 public function testAroundSetRouteParamsNoScopeInParams()
 {
     $storeCode = 'custom_store';
     $this->scopeConfigMock->expects($this->once())->method('getValue')->with(\Magento\Store\Model\Store::XML_PATH_STORE_IN_URL, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeCode)->will($this->returnValue(false));
     $this->storeManagerMock->expects($this->any())->method('hasSingleStore')->willReturn(false);
     /** @var \PHPUnit_Framework_MockObject_MockObject| $routeParamsResolverMock */
     $storeMock = $this->getMockBuilder('Magento\\Store\\Model\\Store')->setMethods(['getCode'])->disableOriginalConstructor()->getMock();
     $storeMock->expects($this->any())->method('getCode')->willReturn($storeCode);
     $this->storeManagerMock->expects($this->any())->method('getStore')->willReturn($storeMock);
     $data = ['_scope_to_url' => true];
     /** @var \PHPUnit_Framework_MockObject_MockObject $routeParamsResolverMock */
     $routeParamsResolverMock = $this->getMockBuilder('Magento\\Framework\\Url\\RouteParamsResolver')->setMethods(['setScope', 'getScope'])->disableOriginalConstructor()->getMock();
     $routeParamsResolverMock->expects($this->never())->method('setScope');
     $routeParamsResolverMock->expects($this->once())->method('getScope')->willReturn(false);
     $this->queryParamsResolverMock->expects($this->once())->method('setQueryParam')->with('___store', $storeCode);
     $this->model->aroundSetRouteParams($routeParamsResolverMock, function ($data, $unsetOldParams) {
         $this->assertArrayNotHasKey('_scope_to_url', $data, 'This data item should have been unset.');
     }, $data);
 }
Example #6
0
    /**
     * Build url by requested path and parameters
     *
     * @param   string|null $routePath
     * @param   array|null $routeParams
     * @return  string
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     * @SuppressWarnings(PHPMD.NPathComplexity)
     */
    private function createUrl($routePath = null, array $routeParams = null)
    {
        $escapeQuery = false;

        /**
         * All system params should be unset before we call getRouteUrl
         * this method has condition for adding default controller and action names
         * in case when we have params
         */
        $this->getRouteParamsResolver()->unsetData('secure');
        $fragment = null;
        if (isset($routeParams['_fragment'])) {
            $fragment = $routeParams['_fragment'];
            unset($routeParams['_fragment']);
        }

        if (isset($routeParams['_escape'])) {
            $escapeQuery = $routeParams['_escape'];
            unset($routeParams['_escape']);
        }

        $query = null;
        if (isset($routeParams['_query'])) {
            $this->_queryParamsResolver->setQueryParams([]);
            $query = $routeParams['_query'];
            unset($routeParams['_query']);
        }

        $noSid = null;
        if (isset($routeParams['_nosid'])) {
            $noSid = (bool)$routeParams['_nosid'];
            unset($routeParams['_nosid']);
        }
        $url = $this->getRouteUrl($routePath, $routeParams);
        /**
         * Apply query params, need call after getRouteUrl for rewrite _current values
         */
        if ($query !== null) {
            if (is_string($query)) {
                $this->_setQuery($query);
            } elseif (is_array($query)) {
                $this->addQueryParams($query, !empty($routeParams['_current']));
            }
            if ($query === false) {
                $this->addQueryParams([]);
            }
        }

        if ($noSid !== true) {
            $this->_prepareSessionUrl($url);
        }

        $query = $this->_getQuery($escapeQuery);
        if ($query) {
            $mark = strpos($url, '?') === false ? '?' : ($escapeQuery ? '&' : '&');
            $url .= $mark . $query;
            $this->_queryParamsResolver->unsetData('query');
            $this->_queryParamsResolver->unsetData('query_params');
        }

        if (!is_null($fragment)) {
            $url .= '#' . $fragment;
        }
        $this->getRouteParamsResolver()->unsetData('secure');

        return $this->escape($url);
    }