Example #1
0
 /**
  * @dataProvider getUrlDataProvider
  * @covers Magento\Catalog\Model\Product\Url::getUrl
  * @covers Magento\Catalog\Model\Product\Url::getUrlInStore
  * @covers Magento\Catalog\Model\Product\Url::getProductUrl
  *
  * @param $getUrlMethod
  * @param $routePath
  * @param $requestPathProduct
  * @param $storeId
  * @param $categoryId
  * @param $routeParams
  * @param $routeParamsUrl
  * @param $entityId
  * @param $idPath
  * @param $requestPathUrlRewrite
  * @param $productId
  * @param $productUrlKey
  * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  */
 public function testGetUrl($getUrlMethod, $routePath, $requestPathProduct, $storeId, $categoryId, $routeParams, $routeParamsUrl, $entityId, $idPath, $requestPathUrlRewrite, $productId, $productUrlKey)
 {
     $product = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->setMethods(['getStoreId', 'getEntityId', 'getId', 'getUrlKey', 'setRequestPath', 'hasUrlDataObject', 'getRequestPath', 'getCategoryId', 'getDoNotUseCategoryId', '__wakeup'])->getMock();
     $product->expects($this->any())->method('getStoreId')->will($this->returnValue($storeId));
     $product->expects($this->any())->method('getCategoryId')->will($this->returnValue($categoryId));
     $product->expects($this->any())->method('getRequestPath')->will($this->returnValue($requestPathProduct));
     $product->expects($this->any())->method('getEntityId')->will($this->returnValue($entityId));
     $product->expects($this->any())->method('setRequestPath')->with($requestPathUrlRewrite)->will($this->returnSelf());
     $product->expects($this->any())->method('getId')->will($this->returnValue($productId));
     $product->expects($this->any())->method('getUrlKey')->will($this->returnValue($productUrlKey));
     $this->url->expects($this->any())->method('setScope')->with($storeId)->will($this->returnSelf());
     $this->url->expects($this->any())->method('getUrl')->with($routePath, $routeParamsUrl)->will($this->returnValue($requestPathProduct));
     $this->urlRewrite->expects($this->any())->method('setStoreId')->with($storeId)->will($this->returnSelf());
     $this->urlRewrite->expects($this->any())->method('loadByIdPath')->with($idPath)->will($this->returnSelf());
     $this->urlRewrite->expects($this->any())->method('getId')->will($this->returnSelf());
     $this->urlRewrite->expects($this->any())->method('getRequestPath')->will($this->returnValue($requestPathUrlRewrite));
     switch ($getUrlMethod) {
         case 'getUrl':
             $this->assertEquals($requestPathProduct, $this->model->getUrl($product, $routeParams));
             break;
         case 'getUrlInStore':
             $this->assertEquals($requestPathProduct, $this->model->getUrlInStore($product, $routeParams));
             break;
         case 'getProductUrl':
             $this->assertEquals($requestPathProduct, $this->model->getProductUrl($product, true));
             $this->sidResolver->expects($this->once())->method('getUseSessionInUrl')->will($this->returnValue(true));
             $this->assertEquals($requestPathProduct, $this->model->getProductUrl($product, null));
             break;
     }
 }
Example #2
0
 /**
  * Get URL rewrite from request
  *
  * @return \Magento\UrlRewrite\Model\UrlRewrite
  */
 protected function _getUrlRewrite()
 {
     if (!$this->_urlRewrite) {
         $this->_urlRewrite = $this->_objectManager->create('Magento\\UrlRewrite\\Model\\UrlRewrite');
         $urlRewriteId = (int) $this->getRequest()->getParam('id', 0);
         if ($urlRewriteId) {
             $this->_urlRewrite->load($urlRewriteId);
         }
     }
     return $this->_urlRewrite;
 }
Example #3
0
 /**
  * Load rewrite information for request
  * If $path is array - we must load all possible records and choose one matching earlier record in array
  *
  * @param   \Magento\UrlRewrite\Model\UrlRewrite $object
  * @param   array|string $path
  * @return  $this
  */
 public function loadByRequestPath(\Magento\UrlRewrite\Model\UrlRewrite $object, $path)
 {
     if (!is_array($path)) {
         $path = array($path);
     }
     $pathBind = array();
     foreach ($path as $key => $url) {
         $pathBind['path' . $key] = $url;
     }
     // Form select
     $adapter = $this->_getReadAdapter();
     $select = $adapter->select()->from($this->getMainTable())->where('request_path IN (:' . implode(', :', array_flip($pathBind)) . ')')->where('store_id IN(?)', array(\Magento\Store\Model\Store::DEFAULT_STORE_ID, (int) $object->getStoreId()));
     $items = $adapter->fetchAll($select, $pathBind);
     // Go through all found records and choose one with lowest penalty - earlier path in array, concrete store
     $mapPenalty = array_flip(array_values($path));
     // we got mapping array(path => index), lower index - better
     $currentPenalty = null;
     $foundItem = null;
     foreach ($items as $item) {
         if (!array_key_exists($item['request_path'], $mapPenalty)) {
             continue;
         }
         $penalty = $mapPenalty[$item['request_path']] << 1 + ($item['store_id'] ? 0 : 1);
         if (!$foundItem || $currentPenalty > $penalty) {
             $foundItem = $item;
             $currentPenalty = $penalty;
             if (!$currentPenalty) {
                 // Found best matching item with zero penalty, no reason to continue
                 break;
             }
         }
     }
     // Set data and finish loading
     if ($foundItem) {
         $object->setData($foundItem);
     }
     // Finish
     $this->unserializeFields($object);
     $this->_afterLoad($object);
     return $this;
 }
Example #4
0
 /**
  * Override URL rewrite data, basing on current CMS page
  *
  * @param \Magento\UrlRewrite\Model\UrlRewrite $model
  * @return void
  */
 private function _handleCmsPageUrlRewrite($model)
 {
     $cmsPage = $this->_getCmsPage();
     if ($cmsPage->getId()) {
         if ($model->isObjectNew()) {
             $model->setEntityType(self::ENTITY_TYPE_CMS_PAGE)->setEntityId($cmsPage->getId());
         }
         if ($model->getRedirectType() && !$model->getIsAutogenerated()) {
             $targetPath = $this->cmsPageUrlPathGenerator->getUrlPath($cmsPage);
         } else {
             $targetPath = $this->cmsPageUrlPathGenerator->getCanonicalUrlPath($cmsPage);
         }
         $model->setTargetPath($targetPath);
     }
 }
Example #5
0
 /**
  * Get category instance applicable for generatePath
  *
  * @param \Magento\UrlRewrite\Model\UrlRewrite $model
  * @return Category|null
  */
 protected function _getInitializedCategory($model)
 {
     /** @var $category Category */
     $category = $this->_getCategory();
     if ($category->getId()) {
         $model->setCategoryId($category->getId());
     } else {
         $category = null;
     }
     return $category;
 }
Example #6
0
 public function testGetStoreId()
 {
     $id = 42;
     $this->assertEquals($id, $this->model->setStoreId($id)->getStoreId());
 }
Example #7
0
 /**
  * @magentoDbIsolation enabled
  *
  * @throws \Exception
  */
 public function testCRUD()
 {
     $this->model->setStoreId($this->objectManager->get('Magento\\Store\\Model\\StoreManagerInterface')->getDefaultStoreView()->getId())->setRequestPath('fancy/url.html')->setTargetPath('catalog/product/view')->setIsSystem(1)->setOptions('RP');
     $crud = new \Magento\TestFramework\Entity($this->model, ['request_path' => 'fancy/url2.html']);
     $crud->testCrud();
 }
Example #8
0
 /**
  * Has redirect options set
  *
  * @param \Magento\UrlRewrite\Model\UrlRewrite $urlRewrite
  * @return bool
  */
 public function hasRedirectOptions($urlRewrite)
 {
     return in_array($urlRewrite->getOptions(), $this->_urlrewrite->getRedirectOptions());
 }