From 090b201fea46bcbf6f489c671109d80038111e81 Mon Sep 17 00:00:00 2001 From: Etienne Roudeix Date: Mon, 9 Dec 2013 13:01:39 +0100 Subject: [PATCH] test rewriting --- core/lib/Thelia/Action/Category.php | 2 +- core/lib/Thelia/Action/Content.php | 2 +- core/lib/Thelia/Action/Folder.php | 2 +- core/lib/Thelia/Action/Product.php | 2 +- core/lib/Thelia/Tests/Action/ContentTest.php | 31 ++++++ core/lib/Thelia/Tests/Action/FolderTest.php | 1 + .../Tests/Action/RewrittenUrlTestTrait.php | 99 +++++++++++++++++++ core/lib/Thelia/Tools/URL.php | 7 +- 8 files changed, 141 insertions(+), 5 deletions(-) create mode 100644 core/lib/Thelia/Tests/Action/RewrittenUrlTestTrait.php diff --git a/core/lib/Thelia/Action/Category.php b/core/lib/Thelia/Action/Category.php index b20ed0d19..ac68baa6a 100755 --- a/core/lib/Thelia/Action/Category.php +++ b/core/lib/Thelia/Action/Category.php @@ -96,7 +96,7 @@ class Category extends BaseAction implements EventSubscriberInterface try { $category->setRewrittenUrl($event->getLocale(), $event->getUrl()); } catch(UrlRewritingException $e) { - throw new FormValidationException($e->getMessage()); + throw new FormValidationException($e->getMessage(), $e->getCode()); } $event->setCategory($category); diff --git a/core/lib/Thelia/Action/Content.php b/core/lib/Thelia/Action/Content.php index 61291556a..d3dbdbba8 100644 --- a/core/lib/Thelia/Action/Content.php +++ b/core/lib/Thelia/Action/Content.php @@ -85,7 +85,7 @@ class Content extends BaseAction implements EventSubscriberInterface try { $content->setRewrittenUrl($event->getLocale(), $event->getUrl()); } catch(UrlRewritingException $e) { - throw new FormValidationException($e->getMessage()); + throw new FormValidationException($e->getMessage(), $e->getCode()); } $content->updateDefaultFolder($event->getDefaultFolder()); diff --git a/core/lib/Thelia/Action/Folder.php b/core/lib/Thelia/Action/Folder.php index a6ae543bf..df849898c 100644 --- a/core/lib/Thelia/Action/Folder.php +++ b/core/lib/Thelia/Action/Folder.php @@ -62,7 +62,7 @@ class Folder extends BaseAction implements EventSubscriberInterface try { $folder->setRewrittenUrl($event->getLocale(), $event->getUrl()); } catch(UrlRewritingException $e) { - throw new FormValidationException($e->getMessage()); + throw new FormValidationException($e->getMessage(), $e->getCode()); } $event->setFolder($folder); diff --git a/core/lib/Thelia/Action/Product.php b/core/lib/Thelia/Action/Product.php index 90176b8ba..ff32921b6 100644 --- a/core/lib/Thelia/Action/Product.php +++ b/core/lib/Thelia/Action/Product.php @@ -119,7 +119,7 @@ class Product extends BaseAction implements EventSubscriberInterface try { $product->setRewrittenUrl($event->getLocale(), $event->getUrl()); } catch(UrlRewritingException $e) { - throw new FormValidationException($e->getMessage()); + throw new FormValidationException($e->getMessage(), $e->getCode()); } // Update default category (ifd required) diff --git a/core/lib/Thelia/Tests/Action/ContentTest.php b/core/lib/Thelia/Tests/Action/ContentTest.php index f527026bf..58baea153 100644 --- a/core/lib/Thelia/Tests/Action/ContentTest.php +++ b/core/lib/Thelia/Tests/Action/ContentTest.php @@ -43,6 +43,36 @@ use Thelia\Model\FolderQuery; */ class ContentTest extends BaseAction { + use RewrittenUrlTestTrait; + + public function getUpdateEvent(&$content) + { + if(!$content instanceof \Thelia\Model\Content) { + $content = $this->getRandomContent(); + } + + $event = new ContentUpdateEvent($content->getId()); + $event + ->setVisible(1) + ->setLocale($content->getLocale()) + ->setTitle($content->getTitle()) + ->setChapo($content->getChapo()) + ->setDescription($content->getDescription()) + ->setPostscriptum($content->getPostscriptum()) + ->setDefaultFolder($content->getDefaultFolderId()) + ; + + return $event; + } + + public function processUpdateAction($event) + { + $contentAction = new Content($this->getContainer()); + $contentAction->update($event); + + return $event->getContent(); + } + public function testCreateContent() { $folder = $this->getRandomFolder(); @@ -80,6 +110,7 @@ class ContentTest extends BaseAction ->setChapo('test update content short description') ->setDescription('test update content description') ->setPostscriptum('test update content postscriptum') + ->setUrl($content->getRewrittenUrl('en_US')) ->setDefaultFolder($folder->getId()) ; diff --git a/core/lib/Thelia/Tests/Action/FolderTest.php b/core/lib/Thelia/Tests/Action/FolderTest.php index c433411ee..fe24cc994 100644 --- a/core/lib/Thelia/Tests/Action/FolderTest.php +++ b/core/lib/Thelia/Tests/Action/FolderTest.php @@ -82,6 +82,7 @@ class FolderTest extends BaseAction ->setChapo('test folder update chapo') ->setDescription('update folder description') ->setPostscriptum('update folder postscriptum') + ->setUrl($folder->getRewrittenUrl('en_US')) ->setParent(0) ; diff --git a/core/lib/Thelia/Tests/Action/RewrittenUrlTestTrait.php b/core/lib/Thelia/Tests/Action/RewrittenUrlTestTrait.php new file mode 100644 index 000000000..c57259783 --- /dev/null +++ b/core/lib/Thelia/Tests/Action/RewrittenUrlTestTrait.php @@ -0,0 +1,99 @@ +getUpdateEvent($object); + + $event->setUrl(''); + + $updatedObject = $this->processUpdateAction($event); + } + + /** + * @expectedException \Thelia\Form\Exception\FormValidationException + * @expectedExceptionCode 100 + */ + public function testUpdateExistingUrl() + { + $object = null; + $event = $this->getUpdateEvent($object); + + /* get an existing url */ + $existingUrl = RewritingUrlQuery::create() + ->filterByViewId($object->getId(), Criteria::NOT_EQUAL) + ->filterByRedirected(null) + ->filterByView(ConfigQuery::getObsoleteRewrittenUrlView(), Criteria::NOT_EQUAL) + ->findOne(); + + if(null === $existingUrl) { + $this->fail('use fixtures before launching test, there is not enough rewritten url'); + } + + $event->setUrl($existingUrl->getUrl()); + + $updatedObject = $this->processUpdateAction($event); + } + + public function testUpdateUrl() + { + $object = null; + $event = $this->getUpdateEvent($object); + + $currentUrl = $object->getRewrittenUrl($object->getLocale()); + + /* get a brand new URL */ + $exist = true; + while(true === $exist) { + $newUrl = md5(rand(1, 999999)) . ".html"; + try { + new RewritingResolver($newUrl); + } catch(UrlRewritingException $e) { + if($e->getCode() === UrlRewritingException::URL_NOT_FOUND) { + /* It's all good if URL is not found */ + $exist = false; + } else { + throw $e; + } + } + } + + $event->setUrl($newUrl); + + $updatedObject = $this->processUpdateAction($event); + + /* new URL is updated */ + $this->assertEquals($newUrl, $updatedObject->getRewrittenUrl($object->getLocale())); + + /* old url must be redirected to the new one */ + $newUrlEntry = RewritingUrlQuery::create()->findOneByUrl($newUrl); + $oldUrlEntry = RewritingUrlQuery::create()->findOneByUrl($currentUrl); + + $this->assertEquals($oldUrlEntry->getRedirected(), $newUrlEntry->getId()); + + /* we can reassign old Url to another object */ + //@todo + } +} \ No newline at end of file diff --git a/core/lib/Thelia/Tools/URL.php b/core/lib/Thelia/Tools/URL.php index 85f5289cd..228e5a3ed 100755 --- a/core/lib/Thelia/Tools/URL.php +++ b/core/lib/Thelia/Tools/URL.php @@ -51,12 +51,17 @@ class URL self::$instance = $this; if ($container !== null) - $this->requestContext = $container->get('router.admin')->getContext(); + $this->requestContext = $this->getContext($container); $this->retriever = new RewritingRetriever(); $this->resolver = new RewritingResolver(); } + public function getContext($container) + { + return $container->get('router.admin')->getContext(); + } + /** * Return this class instance, only once instanciated. *