From 1e15142283019420d75c4aa622bddfa766f23170 Mon Sep 17 00:00:00 2001 From: franck Date: Tue, 17 Sep 2013 20:18:53 +0200 Subject: [PATCH 1/2] Added product creation --- core/lib/Thelia/Model/Product.php | 48 +++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/core/lib/Thelia/Model/Product.php b/core/lib/Thelia/Model/Product.php index 541668ddb..ef000e820 100755 --- a/core/lib/Thelia/Model/Product.php +++ b/core/lib/Thelia/Model/Product.php @@ -10,6 +10,8 @@ use Propel\Runtime\Connection\ConnectionInterface; use Thelia\Core\Event\TheliaEvents; use Thelia\Core\Event\ProductEvent; use Propel\Runtime\ActiveQuery\Criteria; +use Propel\Runtime\Propel; +use Thelia\Model\Map\ProductTableMap; class Product extends BaseProduct { @@ -19,6 +21,8 @@ class Product extends BaseProduct use \Thelia\Model\Tools\UrlRewritingTrait; + protected $defaultCategory = null; + /** * {@inheritDoc} */ @@ -45,7 +49,7 @@ class Product extends BaseProduct } /** - * @return the current default category for this product + * @return the current default category ID for this product */ public function getDefaultCategoryId() { @@ -84,6 +88,42 @@ class Product extends BaseProduct return $this; } + /** + * Create a new product, along with the default category ID + * + * @param int $defaultCategoryId the default category ID of this product + */ + public function create($defaultCategoryId) { + + $con = Propel::getWriteConnection(ProductTableMap::DATABASE_NAME); + + $con->beginTransaction(); + + try { + // Create the product + $this->save($con); + + // Add the default category + $pc = new ProductCategory(); + + $pc + ->setProduct($this) + ->setCategoryId($defaultCategoryId) + ->setDefaultCategory(true) + ->save($con) + ; + + // Store all the stuff ! + $con->commit(); + } + catch(PropelException $ex) { + + $con->rollback(); + + throw $ex; + } + } + /** * Calculate next position relative to our default category */ @@ -105,8 +145,6 @@ class Product extends BaseProduct */ public function preInsert(ConnectionInterface $con = null) { - $this->setPosition($this->getNextPosition()); - $this->generateRewritenUrl($this->getLocale()); $this->dispatchEvent(TheliaEvents::BEFORE_CREATEPRODUCT, new ProductEvent($this)); @@ -119,6 +157,10 @@ class Product extends BaseProduct */ public function postInsert(ConnectionInterface $con = null) { + $this->setPosition($this->getNextPosition()); + + $this->save(); + $this->dispatchEvent(TheliaEvents::AFTER_CREATEPRODUCT, new ProductEvent($this)); } From 48e2be57c6ade1fdc743ee38e8536e20c387f923 Mon Sep 17 00:00:00 2001 From: franck Date: Wed, 18 Sep 2013 01:24:37 +0200 Subject: [PATCH 2/2] Fixed some unit tests --- core/lib/Thelia/Core/Template/Loop/Image.php | 9 ++- .../lib/Thelia/Core/Template/Loop/TaxRule.php | 9 ++- .../Core/Template/Element/BaseLoopTestor.php | 5 +- .../Tests/Core/Template/Loop/DocumentTest.php | 13 ++-- .../Tests/Core/Template/Loop/ImageTest.php | 13 ++-- .../Tests/Core/Template/Loop/ProductTest.php | 3 +- .../Tests/Core/Template/Loop/TaxRuleTest.php | 60 +++++++++++++++++++ .../Thelia/Tests/TaxEngine/CalculatorTest.php | 2 +- 8 files changed, 84 insertions(+), 30 deletions(-) create mode 100644 core/lib/Thelia/Tests/Core/Template/Loop/TaxRuleTest.php diff --git a/core/lib/Thelia/Core/Template/Loop/Image.php b/core/lib/Thelia/Core/Template/Loop/Image.php index c85d41fc7..8d0c99400 100755 --- a/core/lib/Thelia/Core/Template/Loop/Image.php +++ b/core/lib/Thelia/Core/Template/Loop/Image.php @@ -175,7 +175,7 @@ class Image extends BaseI18nLoop $source_id = $this->getSourceId(); $id = $this->getId(); - //echo "source = ".$this->getSource()."source_id=$source_id, id=$id
"; + //echo "source = ".$this->getSourceId()."source_id=$source_id, id=$id
"; if (is_null($source_id) && is_null($id)) { throw new \InvalidArgumentException("If 'source' argument is specified, 'id' or 'source_id' argument should be specified"); @@ -214,6 +214,7 @@ class Image extends BaseI18nLoop */ public function exec(&$pagination) { + // Select the proper query to use, and get the object type $object_type = $object_id = null; @@ -262,13 +263,14 @@ class Image extends BaseI18nLoop } - //echo "sql=".$search->toString(); + // echo "sql=".$search->toString(); $results = $this->search($search, $pagination); $loopResult = new LoopResult($results); foreach ($results as $result) { + // Create image processing event $event = new ImageEvent($this->request); @@ -315,7 +317,8 @@ class Image extends BaseI18nLoop ; $loopResult->addRow($loopResultRow); - } catch (\Exception $ex) { + } + catch (\Exception $ex) { // Ignore the result and log an error Tlog::getInstance()->addError("Failed to process image in image loop: ", $this->args); } diff --git a/core/lib/Thelia/Core/Template/Loop/TaxRule.php b/core/lib/Thelia/Core/Template/Loop/TaxRule.php index a8ba3bf4a..5851f631d 100644 --- a/core/lib/Thelia/Core/Template/Loop/TaxRule.php +++ b/core/lib/Thelia/Core/Template/Loop/TaxRule.php @@ -31,11 +31,9 @@ use Thelia\Core\Template\Element\LoopResultRow; use Thelia\Core\Template\Loop\Argument\ArgumentCollection; use Thelia\Core\Template\Loop\Argument\Argument; -use Thelia\Model\Base\CategoryQuery; -use Thelia\Model\Base\ProductCategoryQuery; -use Thelia\Model\Base\TaxRuleQuery; use Thelia\Type\TypeCollection; use Thelia\Type; +use Thelia\Model\TaxRuleQuery; /** * @@ -57,7 +55,8 @@ class TaxRule extends BaseI18nLoop { return new ArgumentCollection( Argument::createIntListTypeArgument('id'), - new Argument( + Argument::createIntListTypeArgument('exclude'), + new Argument( 'order', new TypeCollection( new Type\EnumListType(array('id', 'id_reverse', 'alpha', 'alpha_reverse')) @@ -77,7 +76,7 @@ class TaxRule extends BaseI18nLoop $search = TaxRuleQuery::create(); /* manage translations */ - $locale = $this->configureI18nProcessing($search, 'TITLE', 'DESCRIPTION'); + $locale = $this->configureI18nProcessing($search, array('TITLE', 'DESCRIPTION')); $id = $this->getId(); diff --git a/core/lib/Thelia/Tests/Core/Template/Element/BaseLoopTestor.php b/core/lib/Thelia/Tests/Core/Template/Element/BaseLoopTestor.php index 2b31d265a..eb271d4c1 100755 --- a/core/lib/Thelia/Tests/Core/Template/Element/BaseLoopTestor.php +++ b/core/lib/Thelia/Tests/Core/Template/Element/BaseLoopTestor.php @@ -132,7 +132,7 @@ abstract class BaseLoopTestor extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('\Thelia\Core\Template\Element\LoopResult', $methodReturn); } - public function baseTestSearchById($id) + public function baseTestSearchById($id, $other_args = array()) { $this->instance->initializeArgs(array_merge( $this->getMandatoryArguments(), @@ -140,7 +140,8 @@ abstract class BaseLoopTestor extends \PHPUnit_Framework_TestCase "type" => "foo", "name" => "foo", "id" => $id, - ) + ), + $other_args )); $dummy = null; diff --git a/core/lib/Thelia/Tests/Core/Template/Loop/DocumentTest.php b/core/lib/Thelia/Tests/Core/Template/Loop/DocumentTest.php index 04e41b6f8..2b7019879 100644 --- a/core/lib/Thelia/Tests/Core/Template/Loop/DocumentTest.php +++ b/core/lib/Thelia/Tests/Core/Template/Loop/DocumentTest.php @@ -58,32 +58,27 @@ class DocumentTest extends BaseLoopTestor { $document = ProductDocumentQuery::create()->findOne(); - $this->baseTestSearchById($document->getId()); + $this->baseTestSearchById($document->getId(), array('source' => 'product')); } public function testSearchByFolderId() { $document = FolderDocumentQuery::create()->findOne(); - $this->baseTestSearchById($document->getId()); + $this->baseTestSearchById($document->getId(), array('source' => 'folder')); } public function testSearchByContentId() { $document = ContentDocumentQuery::create()->findOne(); - $this->baseTestSearchById($document->getId()); + $this->baseTestSearchById($document->getId(), array('source' => 'content')); } public function testSearchByCategoryId() { $document = CategoryDocumentQuery::create()->findOne(); - $this->baseTestSearchById($document->getId()); - } - - public function testSearchLimit() - { - $this->baseTestSearchWithLimit(1); + $this->baseTestSearchById($document->getId(), array('source' => 'category')); } } diff --git a/core/lib/Thelia/Tests/Core/Template/Loop/ImageTest.php b/core/lib/Thelia/Tests/Core/Template/Loop/ImageTest.php index 3d2517c0a..ba4bfadc5 100644 --- a/core/lib/Thelia/Tests/Core/Template/Loop/ImageTest.php +++ b/core/lib/Thelia/Tests/Core/Template/Loop/ImageTest.php @@ -58,32 +58,27 @@ class ImageTest extends BaseLoopTestor { $image = ProductImageQuery::create()->findOne(); - $this->baseTestSearchById($image->getId()); + $this->baseTestSearchById($image->getId(), array('source' => 'product')); } public function testSearchByFolderId() { $image = FolderImageQuery::create()->findOne(); - $this->baseTestSearchById($image->getId()); + $this->baseTestSearchById($image->getId(), array('source' => 'folder')); } public function testSearchByContentId() { $image = ContentImageQuery::create()->findOne(); - $this->baseTestSearchById($image->getId()); + $this->baseTestSearchById($image->getId(), array('source' => 'content')); } public function testSearchByCategoryId() { $image = CategoryImageQuery::create()->findOne(); - $this->baseTestSearchById($image->getId()); - } - - public function testSearchLimit() - { - $this->baseTestSearchWithLimit(1); + $this->baseTestSearchById($image->getId(), array('source' => 'category')); } } diff --git a/core/lib/Thelia/Tests/Core/Template/Loop/ProductTest.php b/core/lib/Thelia/Tests/Core/Template/Loop/ProductTest.php index 1b307c5b5..07e179cbd 100755 --- a/core/lib/Thelia/Tests/Core/Template/Loop/ProductTest.php +++ b/core/lib/Thelia/Tests/Core/Template/Loop/ProductTest.php @@ -27,6 +27,7 @@ use Thelia\Model\ProductQuery; use Thelia\Tests\Core\Template\Element\BaseLoopTestor; use Thelia\Core\Template\Loop\Product; +use Propel\Runtime\ActiveQuery\Criteria; /** * @@ -52,7 +53,7 @@ class ProductTest extends BaseLoopTestor public function testSearchById() { - $product = ProductQuery::create()->findOne(); + $product = ProductQuery::create()->orderById(Criteria::ASC)->findOne(); $this->baseTestSearchById($product->getId()); } diff --git a/core/lib/Thelia/Tests/Core/Template/Loop/TaxRuleTest.php b/core/lib/Thelia/Tests/Core/Template/Loop/TaxRuleTest.php new file mode 100644 index 000000000..20523bfa5 --- /dev/null +++ b/core/lib/Thelia/Tests/Core/Template/Loop/TaxRuleTest.php @@ -0,0 +1,60 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Tests\Core\Template\Loop; + +use Thelia\Tests\Core\Template\Element\BaseLoopTestor; + +use Thelia\Core\Template\Loop\TaxRule; +use Thelia\Model\TaxRuleQuery; + +/** + * + * @author Etienne Roudeix + * + */ +class TaxRuleTest extends BaseLoopTestor +{ + public function getTestedClassName() + { + return 'Thelia\Core\Template\Loop\TaxRule'; + } + + public function getTestedInstance() + { + return new TaxRule($this->container); + } + + public function getMandatoryArguments() + { + return array(); + } + + public function testSearchById() + { + $tr = TaxRuleQuery::create()->findOne(); + + $this->baseTestSearchById($tr->getId()); + } + +} diff --git a/core/lib/Thelia/Tests/TaxEngine/CalculatorTest.php b/core/lib/Thelia/Tests/TaxEngine/CalculatorTest.php index e0443c5ba..502b14c7e 100755 --- a/core/lib/Thelia/Tests/TaxEngine/CalculatorTest.php +++ b/core/lib/Thelia/Tests/TaxEngine/CalculatorTest.php @@ -78,7 +78,7 @@ class CalculatorTest extends \PHPUnit_Framework_TestCase public function testLoad() { - $productQuery = ProductQuery::create()->findOneById(1); + $productQuery = ProductQuery::create()->findOne(); $countryQuery = CountryQuery::create()->findOneById(64); $calculator = new Calculator();