public function testLoadSpecificUrl() { $searchResult = new RewritingUrl(); $searchResult->setUrl('foo.html'); $retrieverQuery = $this->getMock('\\Thelia\\Model\\RewritingUrlQuery', array('getSpecificUrlQuery')); $retrieverQuery->expects($this->any())->method('getSpecificUrlQuery')->with('view', 'fr_FR', 1, array('foo0' => 'bar0', 'foo1' => 'bar1'))->will($this->returnValue($searchResult)); $retriever = new RewritingRetriever(); $rewritingUrlQuery = $this->getProperty('rewritingUrlQuery'); $rewritingUrlQuery->setValue($retriever, $retrieverQuery); $retriever->loadSpecificUrl('view', 'fr_FR', 1, array('foo0' => 'bar0', 'foo1' => 'bar1')); $this->assertEquals('foo.html', $retriever->rewrittenUrl); $this->assertEquals(URL::getInstance()->viewUrl('view', array('foo0' => 'bar0', 'foo1' => 'bar1', 'lang' => 'fr_FR', 'view_id' => 1)), $retriever->url); }
/** * Set the rewritten URL for the given locale * * @param string $locale a valid locale (e.g. en_US) * @param $url * @return $this * @throws UrlRewritingException * @throws \Thelia\Exception\UrlRewritingException */ public function setRewrittenUrl($locale, $url) { $currentUrl = $this->getRewrittenUrl($locale); if ($currentUrl == $url || null === $url) { /* no url update */ return $this; } try { $resolver = new RewritingResolver($url); /* we can reassign old url */ if (null === $resolver->redirectedToUrl) { /* else ... */ if ($resolver->view == $this->getRewrittenUrlViewName() && $resolver->viewId == $this->getId()) { /* it's an url related to the current object */ if ($resolver->locale != $locale) { /* it is an url related to this product for another locale */ throw new UrlRewritingException(Translator::getInstance()->trans('URL_ALREADY_EXISTS'), UrlRewritingException::URL_ALREADY_EXISTS); } if (count($resolver->otherParameters) > 0) { /* it is an url related to this product but with more arguments */ throw new UrlRewritingException(Translator::getInstance()->trans('URL_ALREADY_EXISTS'), UrlRewritingException::URL_ALREADY_EXISTS); } /* here it must be a deprecated url */ } else { /* already related to another object */ throw new UrlRewritingException(Translator::getInstance()->trans('URL_ALREADY_EXISTS'), UrlRewritingException::URL_ALREADY_EXISTS); } } } catch (UrlRewritingException $e) { /* It's all good if URL is not found */ if ($e->getCode() !== UrlRewritingException::URL_NOT_FOUND) { throw $e; } } /* set the new URL */ if (isset($resolver)) { /* erase the old one */ $rewritingUrl = RewritingUrlQuery::create()->findOneByUrl($url); $rewritingUrl->setView($this->getRewrittenUrlViewName())->setViewId($this->getId())->setViewLocale($locale)->setRedirected(null)->save(); /* erase additional arguments if any : only happens in case it erases a deprecated url */ RewritingArgumentQuery::create()->filterByRewritingUrl($rewritingUrl)->deleteAll(); } else { /* just create it */ $rewritingUrl = new RewritingUrl(); $rewritingUrl->setUrl($url)->setView($this->getRewrittenUrlViewName())->setViewId($this->getId())->setViewLocale($locale)->save(); } /* deprecate the old one if needed */ if (null !== ($oldRewritingUrl = RewritingUrlQuery::create()->findOneByUrl($currentUrl))) { $oldRewritingUrl->setRedirected($rewritingUrl->getId())->save(); } return $this; }
/** * @param string $url * @param string $view * @param int $id * @return RewritingUrl */ public function insertUrl($url, $view, $id) { //test if exist url principal $findUrlPrincipal = RewritingUrlQuery::create()->filterByView($view)->filterByViewId($id)->filterByRedirected(null)->findOne(); if ($findUrlPrincipal !== null) { $redirected = $findUrlPrincipal->getId(); } else { $redirected = null; } $local = LangQuery::create()->findOneByByDefault(1); $find = RewritingUrlQuery::create()->findOneByUrl($url); if ($find === null) { //insert RewritingUrl $insert = new RewritingUrl(); $insert->setUrl($url)->setView($view)->setViewId($id)->setRedirected($redirected)->setViewLocale($local->getLocale())->save(); } else { $insert = $find; } return $insert; }