diff --git a/core/lib/Thelia/Action/Order.php b/core/lib/Thelia/Action/Order.php index 3b1ceb5da..f93733da3 100755 --- a/core/lib/Thelia/Action/Order.php +++ b/core/lib/Thelia/Action/Order.php @@ -144,6 +144,7 @@ class Order extends BaseAction implements EventSubscriberInterface ->setAddress3($deliveryAddress->getAddress3()) ->setZipcode($deliveryAddress->getZipcode()) ->setCity($deliveryAddress->getCity()) + ->setPhone($deliveryAddress->getPhone()) ->setCountryId($deliveryAddress->getCountryId()) ->save($con) ; @@ -159,6 +160,7 @@ class Order extends BaseAction implements EventSubscriberInterface ->setAddress3($invoiceAddress->getAddress3()) ->setZipcode($invoiceAddress->getZipcode()) ->setCity($invoiceAddress->getCity()) + ->setPhone($invoiceAddress->getPhone()) ->setCountryId($invoiceAddress->getCountryId()) ->save($con) ; @@ -284,20 +286,6 @@ class Order extends BaseAction implements EventSubscriberInterface /* @todo */ } - /** - * @param \Thelia\Core\Event\OrderEvent $event - */ - public function setReference(OrderEvent $event) - { - $event->getOrder()->setRef($this->generateRef()); - } - - public function generateRef() - { - /* order addresses are unique */ - return uniqid('ORD', true); - } - /** * Returns an array of event names this subscriber wants to listen to. * @@ -326,7 +314,6 @@ class Order extends BaseAction implements EventSubscriberInterface TheliaEvents::ORDER_SET_INVOICE_ADDRESS => array("setInvoiceAddress", 128), TheliaEvents::ORDER_SET_PAYMENT_MODULE => array("setPaymentModule", 128), TheliaEvents::ORDER_PAY => array("create", 128), - TheliaEvents::ORDER_BEFORE_CREATE => array("setReference", 128), TheliaEvents::ORDER_BEFORE_PAYMENT => array("sendOrderEmail", 128), ); } diff --git a/core/lib/Thelia/Model/Order.php b/core/lib/Thelia/Model/Order.php index 5249a1366..fd5164513 100755 --- a/core/lib/Thelia/Model/Order.php +++ b/core/lib/Thelia/Model/Order.php @@ -26,6 +26,8 @@ class Order extends BaseOrder */ public function preInsert(ConnectionInterface $con = null) { + $this->setRef($this->generateRef()); + $this->dispatchEvent(TheliaEvents::ORDER_BEFORE_CREATE, new OrderEvent($this)); return true; @@ -39,6 +41,12 @@ class Order extends BaseOrder $this->dispatchEvent(TheliaEvents::ORDER_AFTER_CREATE, new OrderEvent($this)); } + public function generateRef() + { + /* order addresses are unique */ + return uniqid('ORD', true); + } + /** * calculate the total amount * diff --git a/core/lib/Thelia/Tests/Action/OrderTest.php b/core/lib/Thelia/Tests/Action/OrderTest.php new file mode 100644 index 000000000..3802b362f --- /dev/null +++ b/core/lib/Thelia/Tests/Action/OrderTest.php @@ -0,0 +1,353 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Tests\Action; +use Propel\Runtime\ActiveQuery\Criteria; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; +use Thelia\Core\Event\OrderEvent; +use Thelia\Core\HttpFoundation\Request; +use Thelia\Core\HttpFoundation\Session\Session; +use Thelia\Core\Security\SecurityContext; +use Thelia\Model\AddressQuery; +use Thelia\Model\Base\OrderProductQuery; +use Thelia\Model\OrderStatus; +use Thelia\Model\ProductSaleElementsQuery; +use Thelia\Model\Cart; +use Thelia\Model\CartItem; +use Thelia\Model\CurrencyQuery; +use Thelia\Model\CustomerQuery; +use Thelia\Model\ModuleQuery; +use Thelia\Model\Order as OrderModel; +use Thelia\Model\Customer as CustomerModel; +use Thelia\Action\Order; +use Thelia\Model\ProductQuery; +use Thelia\Module\BaseModule; + +/** + * Class CustomerTest + * @package Thelia\Tests\Action + * @author Etienne Roudeix + */ +class OrderTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var ContainerBuilder $container + */ + protected $container; + + /** + * @var Order $orderAction + */ + protected $orderAction; + + /** + * @var OrderEvent $orderEvent + */ + protected $orderEvent; + + /** + * @var CustomerModel $customer + */ + protected $customer; + + /** + * @var Cart $customer + */ + protected $cart; + + /** + * @var CartItem[] + */ + protected $cartItems; + + public function setUp() + { + $container = new ContainerBuilder(); + + $session = new Session(new MockArraySessionStorage()); + $request = new Request(); + $dispatcher = $this->getMock("Symfony\Component\EventDispatcher\EventDispatcherInterface"); + + $request->setSession($session); + + $container->set("event_dispatcher", $dispatcher); + $container->set('request', $request); + $container->set('thelia.securityContext', new SecurityContext($request)); + + $this->container = $container; + + $this->orderEvent = new OrderEvent(new OrderModel()); + + $this->orderAction = new Order($this->container); + + /* load customer */ + $this->customer = $this->loadCustomer(); + if(null === $this->customer) { + return; + } + + /* fill cart */ + $this->cart = $this->fillCart(); + + } + + public function loadCustomer() + { + $customer = CustomerQuery::create()->findOne(); + if(null === $customer) { + return null; + } + + $this->container->get('thelia.securityContext')->setCustomerUser($customer); + + return $customer; + } + + public function fillCart() + { + $currency = CurrencyQuery::create()->findOne(); + + //create a fake cart in database; + $cart = new Cart(); + $cart->setToken(uniqid("createorder", true)) + ->setCustomer($this->customer) + ->setCurrency($currency) + ->save(); + + /* add 3 items */ + $productList = array(); + for($i=0; $i<3; $i++) { + $pse = ProductSaleElementsQuery::create() + ->filterByProduct( + ProductQuery::create() + ->filterByVisible(1) + ->filterById($productList, Criteria::NOT_IN) + ->find() + ) + ->filterByQuantity(5, Criteria::GREATER_EQUAL) + ->joinProductPrice('pp', Criteria::INNER_JOIN) + ->addJoinCondition('pp', 'currency_id = ?', $currency->getId(), null, \PDO::PARAM_INT) + ->withColumn('`pp`.price', 'price_PRICE') + ->withColumn('`pp`.promo_price', 'price_PROMO_PRICE') + ->findOne(); + + $productList[] = $pse->getProductId(); + + $cartItem = new CartItem(); + $cartItem + ->setCart($cart) + ->setProduct($pse->getProduct()) + ->setProductSaleElements($pse) + ->setQuantity($i) + ->setPrice($pse->getPrice()) + ->setPromoPrice($pse->getPromoPrice()) + ->setPromo($pse->getPromo()) + ->setPriceEndOfLife(time() + 60*60*24*30) + ->save(); + $this->cartItems[] = $cartItem; + } + + $this->container->get('request')->getSession()->setCart($cart->getId()); + + return $cart; + } + + public function testSetDeliveryAddress() + { + //$validAddressId = AddressQuery::create()->findOneByCustomerId($this->customer->getId()); + + $this->orderEvent->setDeliveryAddress(321); + + $this->orderAction->setDeliveryAddress($this->orderEvent); + + $this->assertEquals( + 321, + $this->orderEvent->getOrder()->chosenDeliveryAddress + ); + } + + public function testSetinvoiceAddress() + { + $this->orderEvent->setInvoiceAddress(654); + + $this->orderAction->setInvoiceAddress($this->orderEvent); + + $this->assertEquals( + 654, + $this->orderEvent->getOrder()->chosenInvoiceAddress + ); + } + + public function testSetDeliveryModule() + { + $this->orderEvent->setDeliveryModule(123); + + $this->orderAction->setDeliveryModule($this->orderEvent); + + $this->assertEquals( + 123, + $this->orderEvent->getOrder()->getDeliveryModuleId() + ); + } + + public function testSetPaymentModule() + { + $this->orderEvent->setPaymentModule(456); + + $this->orderAction->setPaymentModule($this->orderEvent); + + $this->assertEquals( + 456, + $this->orderEvent->getOrder()->getPaymentModuleId() + ); + } + + public function testCreate() + { + $validDeliveryAddress = AddressQuery::create()->findOneByCustomerId($this->customer->getId()); + $validInvoiceAddress = AddressQuery::create()->filterById($validDeliveryAddress->getId(), Criteria::NOT_EQUAL)->findOneByCustomerId($this->customer->getId()); + + $deliveryModule = ModuleQuery::create() + ->filterByType(BaseModule::DELIVERY_MODULE_TYPE) + ->filterByActivate(1) + ->findOne(); + + if(null === $deliveryModule) { + return; + } + + $paymentModule = ModuleQuery::create() + ->filterByType(BaseModule::PAYMENT_MODULE_TYPE) + ->filterByActivate(1) + ->findOne(); + + if(null === $paymentModule) { + return; + } + + $this->orderEvent->getOrder()->chosenDeliveryAddress = $validDeliveryAddress->getId(); + $this->orderEvent->getOrder()->chosenInvoiceAddress = $validInvoiceAddress->getId(); + $this->orderEvent->getOrder()->setDeliveryModuleId($deliveryModule->getId()); + $this->orderEvent->getOrder()->setPostage(20); + $this->orderEvent->getOrder()->setPaymentModuleId($paymentModule->getId()); + + /* memorize current stocks */ + $itemsStock = array(); + foreach($this->cartItems as $index => $cartItem) { + $itemsStock[$index] = $cartItem->getProductSaleElements()->getQuantity(); + } + + $this->orderAction->create($this->orderEvent); + + $placedOrder = $this->orderEvent->getPlacedOrder(); + + $this->assertNotNull($placedOrder); + $this->assertNotNull($placedOrder->getId()); + + /* check customer */ + $this->assertEquals($this->customer->getId(), $placedOrder->getCustomerId(), 'customer i does not match'); + + /* check delivery address */ + $deliveryOrderAddress = $placedOrder->getOrderAddressRelatedByDeliveryOrderAddressId(); + $this->assertEquals($validDeliveryAddress->getCustomerTitle()->getId(), $deliveryOrderAddress->getCustomerTitleId(), 'delivery address title does not match'); + $this->assertEquals($validDeliveryAddress->getCompany(), $deliveryOrderAddress->getCompany(), 'delivery address company does not match'); + $this->assertEquals($validDeliveryAddress->getFirstname(), $deliveryOrderAddress->getFirstname(), 'delivery address fistname does not match'); + $this->assertEquals($validDeliveryAddress->getLastname(), $deliveryOrderAddress->getLastname(), 'delivery address lastname does not match'); + $this->assertEquals($validDeliveryAddress->getAddress1(), $deliveryOrderAddress->getAddress1(), 'delivery address address1 does not match'); + $this->assertEquals($validDeliveryAddress->getAddress2(), $deliveryOrderAddress->getAddress2(), 'delivery address address2 does not match'); + $this->assertEquals($validDeliveryAddress->getAddress3(), $deliveryOrderAddress->getAddress3(), 'delivery address address3 does not match'); + $this->assertEquals($validDeliveryAddress->getZipcode(), $deliveryOrderAddress->getZipcode(), 'delivery address zipcode does not match'); + $this->assertEquals($validDeliveryAddress->getCity(), $deliveryOrderAddress->getCity(), 'delivery address city does not match'); + $this->assertEquals($validDeliveryAddress->getPhone(), $deliveryOrderAddress->getPhone(), 'delivery address phone does not match'); + $this->assertEquals($validDeliveryAddress->getCountryId(), $deliveryOrderAddress->getCountryId(), 'delivery address country does not match'); + + /* check invoice address */ + $invoiceOrderAddress = $placedOrder->getOrderAddressRelatedByInvoiceOrderAddressId(); + $this->assertEquals($validInvoiceAddress->getCustomerTitle()->getId(), $invoiceOrderAddress->getCustomerTitleId(), 'invoice address title does not match'); + $this->assertEquals($validInvoiceAddress->getCompany(), $invoiceOrderAddress->getCompany(), 'invoice address company does not match'); + $this->assertEquals($validInvoiceAddress->getFirstname(), $invoiceOrderAddress->getFirstname(), 'invoice address fistname does not match'); + $this->assertEquals($validInvoiceAddress->getLastname(), $invoiceOrderAddress->getLastname(), 'invoice address lastname does not match'); + $this->assertEquals($validInvoiceAddress->getAddress1(), $invoiceOrderAddress->getAddress1(), 'invoice address address1 does not match'); + $this->assertEquals($validInvoiceAddress->getAddress2(), $invoiceOrderAddress->getAddress2(), 'invoice address address2 does not match'); + $this->assertEquals($validInvoiceAddress->getAddress3(), $invoiceOrderAddress->getAddress3(), 'invoice address address3 does not match'); + $this->assertEquals($validInvoiceAddress->getZipcode(), $invoiceOrderAddress->getZipcode(), 'invoice address zipcode does not match'); + $this->assertEquals($validInvoiceAddress->getCity(), $invoiceOrderAddress->getCity(), 'invoice address city does not match'); + $this->assertEquals($validInvoiceAddress->getPhone(), $invoiceOrderAddress->getPhone(), 'invoice address phone does not match'); + $this->assertEquals($validInvoiceAddress->getCountryId(), $invoiceOrderAddress->getCountryId(), 'invoice address country does not match'); + + /* check currency */ + $this->assertEquals($this->cart->getCurrencyId(), $placedOrder->getCurrencyId(), 'currency id does not match'); + $this->assertEquals($this->cart->getCurrency()->getRate(), $placedOrder->getCurrencyRate(), 'currency rate does not match'); + + /* check delivery module */ + $this->assertEquals(20, $placedOrder->getPostage(), 'postage does not match'); + $this->assertEquals($deliveryModule->getId(), $placedOrder->getDeliveryModuleId(), 'delivery module does not match'); + + /* check payment module */ + $this->assertEquals($paymentModule->getId(), $placedOrder->getPaymentModuleId(), 'payment module does not match'); + + /* check status */ + $this->assertEquals(OrderStatus::CODE_NOT_PAID, $placedOrder->getOrderStatus()->getCode(), 'status does not match'); + + /* check lang */ + $this->assertEquals($this->container->get('request')->getSession()->getLang()->getId(), $placedOrder->getLangId(), 'lang does not match'); + + /* check ordered product */ + foreach($this->cartItems as $index => $cartItem) { + $orderProduct = OrderProductQuery::create() + ->filterByOrderId($placedOrder->getId()) + ->filterByProductRef($cartItem->getProduct()->getRef()) + ->filterByProductSaleElementsRef($cartItem->getProductSaleElements()->getRef()) + ->filterByQuantity($cartItem->getQuantity()) + ->filterByPrice($cartItem->getPrice(), Criteria::LIKE) + ->filterByPromoPrice($cartItem->getPromoPrice(), Criteria::LIKE) + ->filterByWasNew($cartItem->getProductSaleElements()->getNewness()) + ->filterByWasInPromo($cartItem->getPromo()) + ->filterByWeight($cartItem->getProductSaleElements()->getWeight()) + ->findOne(); + + $this->assertNotNull($orderProduct); + + /* check attribute combinations */ + $this->assertEquals( + $cartItem->getProductSaleElements()->getAttributeCombinations()->count(), + $orderProduct->getOrderProductAttributeCombinations()->count() + ); + + /* check stock decrease */ + $this->assertEquals( + $itemsStock[$index] - $orderProduct->getQuantity(), + $cartItem->getProductSaleElements()->getQuantity() + ); + + /* check tax */ + $orderProductTaxList = $orderProduct->getOrderProductTaxes(); + foreach($cartItem->getProduct()->getTaxRule()->getTaxDetail($validDeliveryAddress->getCountry(), $cartItem->getPromo() == 1 ? $cartItem->getPromoPrice() : $cartItem->getPrice()) as $index => $tax) { + $orderProductTax = $orderProductTaxList[$index]; + $this->assertEquals($tax->getAmount(), $orderProductTax->getAmount()); + } + } + + } +} \ No newline at end of file diff --git a/core/lib/Thelia/Tests/Form/OrderDeliveryTest.php b/core/lib/Thelia/Tests/Form/OrderDeliveryTest.php new file mode 100755 index 000000000..980c17d88 --- /dev/null +++ b/core/lib/Thelia/Tests/Form/OrderDeliveryTest.php @@ -0,0 +1,32 @@ +. */ +/* */ +/*************************************************************************************/ +namespace Thelia\Tests\Form; + +class OrderDeliveryTest extends \PHPUnit_Framework_TestCase +{ + + public function testOrderDelivery() + { + + } +} diff --git a/install/faker.php b/install/faker.php index eed6db11a..60232e2ea 100755 --- a/install/faker.php +++ b/install/faker.php @@ -160,6 +160,24 @@ try { "test@thelia.net", "azerty" ); + for ($j = 0; $j <= 3; $j++) { + $address = new Thelia\Model\Address(); + $address->setLabel($faker->text(20)) + ->setTitleId(rand(1,3)) + ->setFirstname($faker->firstname) + ->setLastname($faker->lastname) + ->setAddress1($faker->streetAddress) + ->setAddress2($faker->streetAddress) + ->setAddress3($faker->streetAddress) + ->setCellphone($faker->phoneNumber) + ->setPhone($faker->phoneNumber) + ->setZipcode($faker->postcode) + ->setCity($faker->city) + ->setCountryId(64) + ->setCustomer($customer) + ->save() + ; + } for($i = 0; $i < 50; $i++) { $customer = new Thelia\Model\Customer();