From 50212c55cf50a55f702b863fec3d5cac3fb83f59 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Tue, 17 Sep 2013 17:50:05 +0200 Subject: [PATCH 1/4] remove old model classes: --- core/lib/Thelia/Model/Rewriting.php | 9 -------- core/lib/Thelia/Model/RewritingQuery.php | 20 ---------------- .../Thelia/Model/Tools/UrlRewritingTrait.php | 23 ++++++++++++++++++- 3 files changed, 22 insertions(+), 30 deletions(-) delete mode 100644 core/lib/Thelia/Model/Rewriting.php delete mode 100644 core/lib/Thelia/Model/RewritingQuery.php diff --git a/core/lib/Thelia/Model/Rewriting.php b/core/lib/Thelia/Model/Rewriting.php deleted file mode 100644 index 8d6f75fab..000000000 --- a/core/lib/Thelia/Model/Rewriting.php +++ /dev/null @@ -1,9 +0,0 @@ -generateRewritenUrl($this->getRewritenUrlViewName(), $this->getId(), $locale, $this->getTitle()); + // Borrowed from http://stackoverflow.com/questions/2668854/sanitizing-strings-to-make-them-url-and-filename-safe + + $this->setLocale($locale); + // Replace all weird characters with dashes + $string = preg_replace('/[^\w\-~_\.]+/u', '-', $this->getTitle()); + + // Only allow one dash separator at a time (and make string lowercase) + $cleanString = mb_strtolower(preg_replace('/--+/u', '-', $string), 'UTF-8'); + + $urlFilePart = $cleanString . ".html"; + + // TODO : + // check if URL url already exists, and add a numeric suffix, or the like + try{ + URL::getInstance()->resolve($urlFilePart); + } catch (UrlRewritingException $e) { + + } + // insert the URL in the rewriting table + //URL::getInstance()->generateRewritenUrl($this->getRewritenUrlViewName(), $this->getId(), $locale, $this->getTitle()); } /** From 60d09dc76e86c96f7690815850f7db727a848aaa Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Tue, 17 Sep 2013 19:11:44 +0200 Subject: [PATCH 2/4] refactor rewrriten method name --- core/lib/Thelia/Model/Category.php | 4 +- core/lib/Thelia/Model/Content.php | 4 +- core/lib/Thelia/Model/Folder.php | 4 +- core/lib/Thelia/Model/Product.php | 4 +- core/lib/Thelia/Model/RewritingUrl.php | 17 +++++ .../Thelia/Model/Tools/UrlRewritingTrait.php | 63 +++++++++++++++---- 6 files changed, 76 insertions(+), 20 deletions(-) diff --git a/core/lib/Thelia/Model/Category.php b/core/lib/Thelia/Model/Category.php index 042864de0..91b32eec7 100755 --- a/core/lib/Thelia/Model/Category.php +++ b/core/lib/Thelia/Model/Category.php @@ -28,7 +28,7 @@ class Category extends BaseCategory /** * {@inheritDoc} */ - protected function getRewritenUrlViewName() { + protected function getRewrittenUrlViewName() { return 'category'; } @@ -69,7 +69,7 @@ class Category extends BaseCategory { $this->setPosition($this->getNextPosition()); - $this->generateRewritenUrl($this->getLocale()); + $this->generateRewrittenUrl($this->getLocale()); $this->dispatchEvent(TheliaEvents::BEFORE_CREATECATEGORY, new CategoryEvent($this)); diff --git a/core/lib/Thelia/Model/Content.php b/core/lib/Thelia/Model/Content.php index 79660f93a..d626bfb10 100755 --- a/core/lib/Thelia/Model/Content.php +++ b/core/lib/Thelia/Model/Content.php @@ -17,7 +17,7 @@ class Content extends BaseContent /** * {@inheritDoc} */ - protected function getRewritenUrlViewName() { + protected function getRewrittenUrlViewName() { return 'content'; } @@ -37,7 +37,7 @@ class Content extends BaseContent { $this->setPosition($this->getNextPosition()); - $this->generateRewritenUrl($this->getLocale()); + $this->generateRewrittenUrl($this->getLocale()); return true; } diff --git a/core/lib/Thelia/Model/Folder.php b/core/lib/Thelia/Model/Folder.php index 1e73e7e23..318edb7ac 100755 --- a/core/lib/Thelia/Model/Folder.php +++ b/core/lib/Thelia/Model/Folder.php @@ -17,7 +17,7 @@ class Folder extends BaseFolder /** * {@inheritDoc} */ - protected function getRewritenUrlViewName() { + protected function getRewrittenUrlViewName() { return 'folder'; } @@ -67,7 +67,7 @@ class Folder extends BaseFolder { $this->setPosition($this->getNextPosition()); - $this->generateRewritenUrl($this->getLocale()); + $this->generateRewrittenUrl($this->getLocale()); return true; } diff --git a/core/lib/Thelia/Model/Product.php b/core/lib/Thelia/Model/Product.php index 87b414dd9..d227cc9a2 100755 --- a/core/lib/Thelia/Model/Product.php +++ b/core/lib/Thelia/Model/Product.php @@ -21,7 +21,7 @@ class Product extends BaseProduct /** * {@inheritDoc} */ - protected function getRewritenUrlViewName() { + protected function getRewrittenUrlViewName() { return 'product'; } @@ -106,7 +106,7 @@ class Product extends BaseProduct { $this->setPosition($this->getNextPosition()); - $this->generateRewritenUrl($this->getLocale()); + $this->generateRewrittenUrl($this->getLocale()); $this->dispatchEvent(TheliaEvents::BEFORE_CREATEPRODUCT, new ProductEvent($this)); diff --git a/core/lib/Thelia/Model/RewritingUrl.php b/core/lib/Thelia/Model/RewritingUrl.php index 58dde9388..9dd826d01 100644 --- a/core/lib/Thelia/Model/RewritingUrl.php +++ b/core/lib/Thelia/Model/RewritingUrl.php @@ -2,8 +2,25 @@ namespace Thelia\Model; +use Propel\Runtime\Connection\ConnectionInterface; use Thelia\Model\Base\RewritingUrl as BaseRewritingUrl; +use Thelia\Model\RewritingUrlQuery; class RewritingUrl extends BaseRewritingUrl { + public function preSave(ConnectionInterface $con = null) + { + if($this->getRedirected() == 0) { + //check if rewriting url alredy exists and put redirect to 1 + RewritingUrlQuery::create() + ->filterByView($this->getView()) + ->filterByViewId($this->getViewId()) + ->filterByViewLocale($this->getViewLocale()) + ->update(array( + "redirect" => 1 + )); + } + + return true; + } } diff --git a/core/lib/Thelia/Model/Tools/UrlRewritingTrait.php b/core/lib/Thelia/Model/Tools/UrlRewritingTrait.php index f50da56d5..c9154a0db 100644 --- a/core/lib/Thelia/Model/Tools/UrlRewritingTrait.php +++ b/core/lib/Thelia/Model/Tools/UrlRewritingTrait.php @@ -24,7 +24,8 @@ namespace Thelia\Model\Tools; use Thelia\Exception\UrlRewritingException; -use Thelia\Model\Rewriting; +use Thelia\Model\RewritingUrlQuery; +use Thelia\Model\RewritingUrl; use Thelia\Tools\URL; /** * A trait for managing Rewriten URLs from model classes @@ -34,7 +35,7 @@ trait UrlRewritingTrait { /** * @returns string the view name of the rewriten object (e.g., 'category', 'product') */ - protected abstract function getRewritenUrlViewName(); + protected abstract function getRewrittenUrlViewName(); /** * Get the object URL for the given locale, rewriten if rewriting is enabled. @@ -43,7 +44,7 @@ trait UrlRewritingTrait { */ public function getUrl($locale) { - return URL::getInstance()->retrieve($this->getRewritenUrlViewName(), $this->getId(), $locale)->toString(); + return URL::getInstance()->retrieve($this->getRewrittenUrlViewName(), $this->getId(), $locale)->toString(); } /** @@ -51,13 +52,22 @@ trait UrlRewritingTrait { * * @param string $locale a valid locale (e.g. en_US) */ - public function generateRewritenUrl($locale) + public function generateRewrittenUrl($locale) { + if ($this->isNew()) { + throw new \RuntimeException(sprintf('Object %s must be save before generating url', $this->getRewrittenUrlViewName())); + } // Borrowed from http://stackoverflow.com/questions/2668854/sanitizing-strings-to-make-them-url-and-filename-safe $this->setLocale($locale); + + $title = $this->getTitle(); + + if (null === $title) { + throw new \RuntimeException(sprintf('Impossible to generate url if title does not exists for the locale %s', $locale)); + } // Replace all weird characters with dashes - $string = preg_replace('/[^\w\-~_\.]+/u', '-', $this->getTitle()); + $string = preg_replace('/[^\w\-~_\.]+/u', '-', $title); // Only allow one dash separator at a time (and make string lowercase) $cleanString = mb_strtolower(preg_replace('/--+/u', '-', $string), 'UTF-8'); @@ -67,30 +77,59 @@ trait UrlRewritingTrait { // TODO : // check if URL url already exists, and add a numeric suffix, or the like try{ - URL::getInstance()->resolve($urlFilePart); + $i=0; + while(URL::getInstance()->resolve($urlFilePart)) { + $i++; + $urlFilePart = sprintf("%s-%d.html",$cleanString, $i); + } } catch (UrlRewritingException $e) { - + $rewritingUrl = new RewritingUrl(); + $rewritingUrl->setUrl($urlFilePart) + ->setView($this->getRewrittenUrlViewName()) + ->setViewId($this->getId()) + ->setViewLocale($locale) + ->setRedirected(0) + ->save() + ; } - // insert the URL in the rewriting table - //URL::getInstance()->generateRewritenUrl($this->getRewritenUrlViewName(), $this->getId(), $locale, $this->getTitle()); + + return $urlFilePart; + } /** * return the rewriten URL for the given locale * * @param string $locale a valid locale (e.g. en_US) + * @return null */ - public function getRewritenUrl($locale) + public function getRewrittenUrl($locale) { - return "fake url - TODO"; + $rewritingUrl = RewritingUrlQuery::create() + ->filterByViewLocale($locale) + ->filterByView($this->getRewrittenUrlViewName()) + ->filterByViewId($this->getId()) + ->filterByRedirected(0) + ->findOne() + ; + + if($rewritingUrl) { + $url = $rewritingUrl->getUrl(); + } else { + $url = null; + } + + return $url; } /** * Set the rewriten URL for the given locale * * @param string $locale a valid locale (e.g. en_US) + * @param $url the wanted url + * @return $this */ - public function setRewritenUrl($locale, $url) + public function setRewrittenUrl($locale, $url) { // TODO - code me ! From e1e383707b04f59ee33eb80fdb0b86e0b23bbf49 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Tue, 17 Sep 2013 19:19:52 +0200 Subject: [PATCH 3/4] template is not mandatory in product table. --- core/lib/Thelia/Model/Base/ProductQuery.php | 4 +- core/lib/Thelia/Model/Base/Template.php | 9 +- core/lib/Thelia/Model/Base/TemplateQuery.php | 4 +- core/lib/Thelia/Model/Category.php | 3 +- core/lib/Thelia/Model/Content.php | 7 +- core/lib/Thelia/Model/Folder.php | 6 +- core/lib/Thelia/Model/Map/ProductTableMap.php | 2 +- .../Model/Map/ProductVersionTableMap.php | 2 +- core/lib/Thelia/Model/Product.php | 3 +- install/thelia.sql | 4 +- local/config/schema.xml | 2398 ++++++++--------- 11 files changed, 1225 insertions(+), 1217 deletions(-) diff --git a/core/lib/Thelia/Model/Base/ProductQuery.php b/core/lib/Thelia/Model/Base/ProductQuery.php index 9b1710f03..9c3b0759c 100644 --- a/core/lib/Thelia/Model/Base/ProductQuery.php +++ b/core/lib/Thelia/Model/Base/ProductQuery.php @@ -857,7 +857,7 @@ abstract class ProductQuery extends ModelCriteria * * @return ChildProductQuery The current query, for fluid interface */ - public function joinTemplate($relationAlias = null, $joinType = Criteria::INNER_JOIN) + public function joinTemplate($relationAlias = null, $joinType = Criteria::LEFT_JOIN) { $tableMap = $this->getTableMap(); $relationMap = $tableMap->getRelation('Template'); @@ -892,7 +892,7 @@ abstract class ProductQuery extends ModelCriteria * * @return \Thelia\Model\TemplateQuery A secondary query class using the current class as primary query */ - public function useTemplateQuery($relationAlias = null, $joinType = Criteria::INNER_JOIN) + public function useTemplateQuery($relationAlias = null, $joinType = Criteria::LEFT_JOIN) { return $this ->joinTemplate($relationAlias, $joinType) diff --git a/core/lib/Thelia/Model/Base/Template.php b/core/lib/Thelia/Model/Base/Template.php index 77efa7ba0..08bb0f3a0 100644 --- a/core/lib/Thelia/Model/Base/Template.php +++ b/core/lib/Thelia/Model/Base/Template.php @@ -864,9 +864,10 @@ abstract class Template implements ActiveRecordInterface if ($this->productsScheduledForDeletion !== null) { if (!$this->productsScheduledForDeletion->isEmpty()) { - \Thelia\Model\ProductQuery::create() - ->filterByPrimaryKeys($this->productsScheduledForDeletion->getPrimaryKeys(false)) - ->delete($con); + foreach ($this->productsScheduledForDeletion as $product) { + // need to save related object because we set the relation to null + $product->save($con); + } $this->productsScheduledForDeletion = null; } } @@ -1553,7 +1554,7 @@ abstract class Template implements ActiveRecordInterface $this->productsScheduledForDeletion = clone $this->collProducts; $this->productsScheduledForDeletion->clear(); } - $this->productsScheduledForDeletion[]= clone $product; + $this->productsScheduledForDeletion[]= $product; $product->setTemplate(null); } diff --git a/core/lib/Thelia/Model/Base/TemplateQuery.php b/core/lib/Thelia/Model/Base/TemplateQuery.php index 506d7d943..98c588dd8 100644 --- a/core/lib/Thelia/Model/Base/TemplateQuery.php +++ b/core/lib/Thelia/Model/Base/TemplateQuery.php @@ -395,7 +395,7 @@ abstract class TemplateQuery extends ModelCriteria * * @return ChildTemplateQuery The current query, for fluid interface */ - public function joinProduct($relationAlias = null, $joinType = Criteria::INNER_JOIN) + public function joinProduct($relationAlias = null, $joinType = Criteria::LEFT_JOIN) { $tableMap = $this->getTableMap(); $relationMap = $tableMap->getRelation('Product'); @@ -430,7 +430,7 @@ abstract class TemplateQuery extends ModelCriteria * * @return \Thelia\Model\ProductQuery A secondary query class using the current class as primary query */ - public function useProductQuery($relationAlias = null, $joinType = Criteria::INNER_JOIN) + public function useProductQuery($relationAlias = null, $joinType = Criteria::LEFT_JOIN) { return $this ->joinProduct($relationAlias, $joinType) diff --git a/core/lib/Thelia/Model/Category.php b/core/lib/Thelia/Model/Category.php index 91b32eec7..204896674 100755 --- a/core/lib/Thelia/Model/Category.php +++ b/core/lib/Thelia/Model/Category.php @@ -69,8 +69,6 @@ class Category extends BaseCategory { $this->setPosition($this->getNextPosition()); - $this->generateRewrittenUrl($this->getLocale()); - $this->dispatchEvent(TheliaEvents::BEFORE_CREATECATEGORY, new CategoryEvent($this)); return true; @@ -81,6 +79,7 @@ class Category extends BaseCategory */ public function postInsert(ConnectionInterface $con = null) { + //$this->generateRewrittenUrl($this->getLocale()); $this->dispatchEvent(TheliaEvents::AFTER_CREATECATEGORY, new CategoryEvent($this)); } diff --git a/core/lib/Thelia/Model/Content.php b/core/lib/Thelia/Model/Content.php index d626bfb10..4d9cf9f3a 100755 --- a/core/lib/Thelia/Model/Content.php +++ b/core/lib/Thelia/Model/Content.php @@ -37,8 +37,11 @@ class Content extends BaseContent { $this->setPosition($this->getNextPosition()); - $this->generateRewrittenUrl($this->getLocale()); - return true; } + + public function postInsert(ConnectionInterface $con = null) + { + //$this->generateRewrittenUrl($this->getLocale()); + } } diff --git a/core/lib/Thelia/Model/Folder.php b/core/lib/Thelia/Model/Folder.php index 318edb7ac..e32505d9d 100755 --- a/core/lib/Thelia/Model/Folder.php +++ b/core/lib/Thelia/Model/Folder.php @@ -67,8 +67,12 @@ class Folder extends BaseFolder { $this->setPosition($this->getNextPosition()); - $this->generateRewrittenUrl($this->getLocale()); return true; } + + public function postInsert(ConnectionInterface $con = null) + { + //$this->generateRewrittenUrl($this->getLocale()); + } } diff --git a/core/lib/Thelia/Model/Map/ProductTableMap.php b/core/lib/Thelia/Model/Map/ProductTableMap.php index 17c4585f0..59ac236ac 100644 --- a/core/lib/Thelia/Model/Map/ProductTableMap.php +++ b/core/lib/Thelia/Model/Map/ProductTableMap.php @@ -189,7 +189,7 @@ class ProductTableMap extends TableMap $this->addColumn('REF', 'Ref', 'VARCHAR', true, 255, null); $this->addColumn('VISIBLE', 'Visible', 'TINYINT', true, null, 0); $this->addColumn('POSITION', 'Position', 'INTEGER', true, null, null); - $this->addForeignKey('TEMPLATE_ID', 'TemplateId', 'INTEGER', 'template', 'ID', true, null, null); + $this->addForeignKey('TEMPLATE_ID', 'TemplateId', 'INTEGER', 'template', 'ID', false, null, null); $this->addColumn('CREATED_AT', 'CreatedAt', 'TIMESTAMP', false, null, null); $this->addColumn('UPDATED_AT', 'UpdatedAt', 'TIMESTAMP', false, null, null); $this->addColumn('VERSION', 'Version', 'INTEGER', false, null, 0); diff --git a/core/lib/Thelia/Model/Map/ProductVersionTableMap.php b/core/lib/Thelia/Model/Map/ProductVersionTableMap.php index 4e84b4db8..14d38a764 100644 --- a/core/lib/Thelia/Model/Map/ProductVersionTableMap.php +++ b/core/lib/Thelia/Model/Map/ProductVersionTableMap.php @@ -180,7 +180,7 @@ class ProductVersionTableMap extends TableMap $this->addColumn('REF', 'Ref', 'VARCHAR', true, 255, null); $this->addColumn('VISIBLE', 'Visible', 'TINYINT', true, null, 0); $this->addColumn('POSITION', 'Position', 'INTEGER', true, null, null); - $this->addColumn('TEMPLATE_ID', 'TemplateId', 'INTEGER', true, null, null); + $this->addColumn('TEMPLATE_ID', 'TemplateId', 'INTEGER', false, null, null); $this->addColumn('CREATED_AT', 'CreatedAt', 'TIMESTAMP', false, null, null); $this->addColumn('UPDATED_AT', 'UpdatedAt', 'TIMESTAMP', false, null, null); $this->addPrimaryKey('VERSION', 'Version', 'INTEGER', true, null, 0); diff --git a/core/lib/Thelia/Model/Product.php b/core/lib/Thelia/Model/Product.php index 2be99958c..be534298e 100755 --- a/core/lib/Thelia/Model/Product.php +++ b/core/lib/Thelia/Model/Product.php @@ -107,7 +107,7 @@ class Product extends BaseProduct { $this->setPosition($this->getNextPosition()); - $this->generateRewrittenUrl($this->getLocale()); + $this->dispatchEvent(TheliaEvents::BEFORE_CREATEPRODUCT, new ProductEvent($this)); @@ -119,6 +119,7 @@ class Product extends BaseProduct */ public function postInsert(ConnectionInterface $con = null) { + //$this->generateRewrittenUrl($this->getLocale()); $this->dispatchEvent(TheliaEvents::AFTER_CREATEPRODUCT, new ProductEvent($this)); } diff --git a/install/thelia.sql b/install/thelia.sql index 3dc5d0454..b49840d9b 100755 --- a/install/thelia.sql +++ b/install/thelia.sql @@ -36,7 +36,7 @@ CREATE TABLE `product` `ref` VARCHAR(255) NOT NULL, `visible` TINYINT DEFAULT 0 NOT NULL, `position` INTEGER NOT NULL, - `template_id` INTEGER NOT NULL, + `template_id` INTEGER, `created_at` DATETIME, `updated_at` DATETIME, `version` INTEGER DEFAULT 0, @@ -2167,7 +2167,7 @@ CREATE TABLE `product_version` `ref` VARCHAR(255) NOT NULL, `visible` TINYINT DEFAULT 0 NOT NULL, `position` INTEGER NOT NULL, - `template_id` INTEGER NOT NULL, + `template_id` INTEGER, `created_at` DATETIME, `updated_at` DATETIME, `version` INTEGER DEFAULT 0 NOT NULL, diff --git a/local/config/schema.xml b/local/config/schema.xml index a43cfda56..86e313cdf 100755 --- a/local/config/schema.xml +++ b/local/config/schema.xml @@ -1,1199 +1,1199 @@ - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - -
- - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - -
- - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - -
- - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - -
- - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - -
- - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - -
- - - - - -
- - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - -
- - - - - - - - - - - - - - -
- - - - - - - - - - - -
- - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - -
- - - - - - - - -
- - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - -
- - - - - - - -
-
+ + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + +
+ + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + +
+ + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + +
+ + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + +
+ + + + + +
+ + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + +
+ + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + +
+ + + + + + + +
+
From da7f9e07c407f3d70f2aea053270bebbddb1ea80 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Tue, 17 Sep 2013 20:15:28 +0200 Subject: [PATCH 4/4] create test for UrlRewritingTrait --- core/lib/Thelia/Model/RewritingUrl.php | 12 +-- .../Thelia/Model/Tools/UrlRewritingTrait.php | 9 +- .../Tests/Rewriting/ProductRewriteTest.php | 88 +++++++++++++++++++ 3 files changed, 96 insertions(+), 13 deletions(-) create mode 100644 core/lib/Thelia/Tests/Rewriting/ProductRewriteTest.php diff --git a/core/lib/Thelia/Model/RewritingUrl.php b/core/lib/Thelia/Model/RewritingUrl.php index 9dd826d01..a13777c41 100644 --- a/core/lib/Thelia/Model/RewritingUrl.php +++ b/core/lib/Thelia/Model/RewritingUrl.php @@ -2,25 +2,25 @@ namespace Thelia\Model; +use Propel\Runtime\ActiveQuery\Criteria; use Propel\Runtime\Connection\ConnectionInterface; use Thelia\Model\Base\RewritingUrl as BaseRewritingUrl; use Thelia\Model\RewritingUrlQuery; class RewritingUrl extends BaseRewritingUrl { - public function preSave(ConnectionInterface $con = null) + public function postInsert(ConnectionInterface $con = null) { - if($this->getRedirected() == 0) { - //check if rewriting url alredy exists and put redirect to 1 + if(null !== $this->getRedirected()) { + //check if rewriting url alredy exists and put redirect to the new one RewritingUrlQuery::create() ->filterByView($this->getView()) ->filterByViewId($this->getViewId()) ->filterByViewLocale($this->getViewLocale()) + ->filterByRedirected($this->getId(), Criteria::NOT_IN) ->update(array( - "redirect" => 1 + "Redirected" => $this->getId() )); } - - return true; } } diff --git a/core/lib/Thelia/Model/Tools/UrlRewritingTrait.php b/core/lib/Thelia/Model/Tools/UrlRewritingTrait.php index c9154a0db..1b9087626 100644 --- a/core/lib/Thelia/Model/Tools/UrlRewritingTrait.php +++ b/core/lib/Thelia/Model/Tools/UrlRewritingTrait.php @@ -55,17 +55,13 @@ trait UrlRewritingTrait { public function generateRewrittenUrl($locale) { if ($this->isNew()) { - throw new \RuntimeException(sprintf('Object %s must be save before generating url', $this->getRewrittenUrlViewName())); + throw new \RuntimeException(sprintf('Object %s must be saved before generating url', $this->getRewrittenUrlViewName())); } // Borrowed from http://stackoverflow.com/questions/2668854/sanitizing-strings-to-make-them-url-and-filename-safe $this->setLocale($locale); - $title = $this->getTitle(); - - if (null === $title) { - throw new \RuntimeException(sprintf('Impossible to generate url if title does not exists for the locale %s', $locale)); - } + $title = $this->getTitle() ?: $this->getRef(); // Replace all weird characters with dashes $string = preg_replace('/[^\w\-~_\.]+/u', '-', $title); @@ -88,7 +84,6 @@ trait UrlRewritingTrait { ->setView($this->getRewrittenUrlViewName()) ->setViewId($this->getId()) ->setViewLocale($locale) - ->setRedirected(0) ->save() ; } diff --git a/core/lib/Thelia/Tests/Rewriting/ProductRewriteTest.php b/core/lib/Thelia/Tests/Rewriting/ProductRewriteTest.php new file mode 100644 index 000000000..c38773814 --- /dev/null +++ b/core/lib/Thelia/Tests/Rewriting/ProductRewriteTest.php @@ -0,0 +1,88 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Tests\Rewriting; +use Thelia\Model\Product; +use Thelia\Model\ProductQuery; + + +/** + * Class ProductRewriteTest + * @package Thelia\Tests\Rewriting + * @author Manuel Raynaud + */ +class ProductRewriteTest extends \PHPUnit_Framework_TestCase +{ + protected static $productId; + + public static function setUpBeforeClass() + { + $product = new Product(); + $product->setRef(sprintf("TestRewrittenProduct%s",uniqid())) + ->setPosition(1) + ->setVisible(1) + ->setLocale('en_US') + ->setTitle('My english super Title') + ->setLocale('fr_FR') + ->setTitle('Mon super titre en français') + ->save(); + + self::$productId = $product->getId(); + } + + /** + * @covers Thelia\Model\Tools\UrlRewritingTrait::generateRewrittenUrl + */ + public function testFrenchRewrittenUrl() + { + $product = ProductQuery::create()->findPk(self::$productId); + + $rewrittenUrl = $product->generateRewrittenUrl('fr_FR'); + $this->assertNotNull($rewrittenUrl, "rewritten url can not be null"); + $this->assertRegExp('/^mon-super-titre-en-français(-[0-9]+)?\.html$/', $rewrittenUrl); + //mon-super-titre-en-français-2.html + } + + /** + * @covers Thelia\Model\Tools\UrlRewritingTrait::generateRewrittenUrl + */ + public function testEnglishRewrittenUrl() + { + $product = ProductQuery::create()->findPk(self::$productId); + + $rewrittenUrl = $product->generateRewrittenUrl('en_US'); + $this->assertNotNull($rewrittenUrl, "rewritten url can not be null"); + $this->assertRegExp('/^my-english-super-title(-[0-9]+)?\.html$/', $rewrittenUrl); + } + + /** + * @expectedException \RuntimeException + * @expectedExceptionMessage Object product must be saved before generating url + */ + public function testOnNotSavedProduct() + { + $product = new Product(); + + $product->generateRewrittenUrl('fr_FR'); + } +} \ No newline at end of file