test rewriting

This commit is contained in:
Etienne Roudeix
2013-12-09 13:01:39 +01:00
parent 1e8fee99cb
commit 090b201fea
8 changed files with 141 additions and 5 deletions

View File

@@ -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);

View File

@@ -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());

View File

@@ -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);

View File

@@ -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)

View File

@@ -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())
;

View File

@@ -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)
;

View File

@@ -0,0 +1,99 @@
<?php
namespace Thelia\Tests\Action;
use Propel\Runtime\ActiveQuery\Criteria;
use Thelia\Exception\UrlRewritingException;
use Thelia\Model\Base\RewritingUrlQuery;
use Thelia\Model\ConfigQuery;
use Thelia\Rewriting\RewritingResolver;
use Thelia\Tools\URL;
trait RewrittenUrlTestTrait
{
abstract public function getUpdateEvent(&$object);
abstract public function processUpdateAction($event);
public function setUp()
{
new URL(null);
}
/**
* @expectedException \Thelia\Form\Exception\FormValidationException
* @expectedExceptionCode 100
*/
public function testUpdateEmptyUrl()
{
$object = null;
$event = $this->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
}
}

View File

@@ -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.
*