diff --git a/core/lib/Thelia/Action/Cart.php b/core/lib/Thelia/Action/Cart.php index 22001f592..b040634f9 100755 --- a/core/lib/Thelia/Action/Cart.php +++ b/core/lib/Thelia/Action/Cart.php @@ -50,8 +50,7 @@ class Cart extends BaseAction implements EventSubscriberInterface /** * * add an article in the current cart - * - * @param \Thelia\Core\Event\ActionEvent $event + * @param \Thelia\Core\Event\CartEvent $event */ public function addArticle(CartEvent $event) { @@ -85,7 +84,6 @@ class Cart extends BaseAction implements EventSubscriberInterface $this->updateQuantity($cartItem, $quantity); } - $this->redirect($cartAdd->getSuccessUrl($request->getUriAddingParameters(array("addCart" => 1)))); } catch (PropelException $e) { \Thelia\Log\Tlog::getInstance()->error(sprintf("Failed to add item to cart with message : %s", $e->getMessage())); $message = "Failed to add this article to your cart, please try again"; diff --git a/core/lib/Thelia/Cart/CartTrait.php b/core/lib/Thelia/Cart/CartTrait.php index a5ecd84a9..0cd0a2c6e 100644 --- a/core/lib/Thelia/Cart/CartTrait.php +++ b/core/lib/Thelia/Cart/CartTrait.php @@ -32,7 +32,7 @@ use Thelia\Model\ConfigQuery; use Thelia\Model\Customer; use Symfony\Component\HttpFoundation\Request; use Thelia\Core\HttpFoundation\Session\Session; -use Thelia\Core\Event\CartEvent; +use Thelia\Core\Event\Internal\CartEvent; use Thelia\Core\Event\TheliaEvents; trait CartTrait { diff --git a/core/lib/Thelia/Config/Resources/routing/front.xml b/core/lib/Thelia/Config/Resources/routing/front.xml index ff050f35d..4c5ff6859 100644 --- a/core/lib/Thelia/Config/Resources/routing/front.xml +++ b/core/lib/Thelia/Config/Resources/routing/front.xml @@ -14,4 +14,7 @@ connexion + + Thelia\Controller\Front\CartController::addArticle + \ No newline at end of file diff --git a/core/lib/Thelia/Controller/Front/CartController.php b/core/lib/Thelia/Controller/Front/CartController.php index cedf73508..3a0a499b2 100644 --- a/core/lib/Thelia/Controller/Front/CartController.php +++ b/core/lib/Thelia/Controller/Front/CartController.php @@ -31,9 +31,10 @@ class CartController extends BaseFrontController public function addArticle() { - $cart = $this->getCart($this->getRequest); + $request = $this->getRequest(); + $cart = $this->getCart($request); - $cartEvent = new CartEvent($this->getRequest(), "action.addArticle", $cart); + $cartEvent = new CartEvent($request, "action.addArticle", $cart); $this->dispatch("action.addArticle", $cartEvent); } diff --git a/core/lib/Thelia/Tests/Cart/CartTraitTest.php b/core/lib/Thelia/Tests/Cart/CartTraitTest.php index e7f49fd12..e09f60294 100644 --- a/core/lib/Thelia/Tests/Cart/CartTraitTest.php +++ b/core/lib/Thelia/Tests/Cart/CartTraitTest.php @@ -41,233 +41,241 @@ use Thelia\Model\ProductSaleElementsQuery; class CartTraitTest extends \PHPUnit_Framework_TestCase { - public function testNoError() + protected $session; + + protected $request; + + protected $cartTrait; + + protected $uniqid; + + public function getContainer() { - //fake assertion - $this->assertTrue(true); + $container = new \Symfony\Component\DependencyInjection\ContainerBuilder(); + + $dispatcher = $this->getMock("Symfony\Component\EventDispatcher\EventDispatcherInterface"); + + $container->set("event_dispatcher", $dispatcher); + + return $container; + } + + public function setUp() + { + $this->session = new Session(new MockArraySessionStorage()); + $this->request = new Request(); + + $this->request->setSession($this->session); + + $this->uniqid = uniqid('', true); + + $this->cartTrait = new MockCartTrait($this->uniqid, $this->getContainer()); + } + + /** + * no cart present in session and cart_id no yet exists in cookies. + * + * In this case, a new cart instance must be create + */ + public function testGetCartWithoutCustomerAndWithoutExistingCart() + { + $cartTrait = $this->cartTrait; + + $cart = $cartTrait->getCart($this->request); + + $this->assertInstanceOf("Thelia\Model\Cart", $cart, '$cart must be an instance of cart model Thelia\Model\Cart'); + $this->assertNull($cart->getCustomerId()); + $this->assertNull($cart->getAddressDeliveryId()); + $this->assertNull($cart->getAddressInvoiceId()); + $this->assertEquals($this->uniqid, $cart->getToken()); + + } + + /** + * Customer is connected but his cart does not exists yet + * + * Cart must be created and associated to the current connected Customer + */ + public function testGetCartWithCustomerAndWithoutExistingCart() + { + $cartTrait = $this->cartTrait; + + $request = $this->request; + + //create a fake customer just for test. If not persists test fails ! + $customer = new Customer(); + $customer->setFirstname("john"); + $customer->setLastname("doe"); + $customer->setTitleId(1); + $customer->save(); + + $request->getSession()->setCustomerUser($customer); + + $cart = $cartTrait->getCart($request); + $this->assertInstanceOf("Thelia\Model\Cart", $cart, '$cart must be an instance of cart model Thelia\Model\Cart'); + $this->assertNotNull($cart->getCustomerId()); + $this->assertEquals($customer->getId(), $cart->getCustomerId()); + $this->assertNull($cart->getAddressDeliveryId()); + $this->assertNull($cart->getAddressInvoiceId()); + $this->assertEquals($this->uniqid, $cart->getToken()); + + } + + /** + * Cart exists and his id put in cookies. + * + * Must return the same cart instance + */ + public function testGetCartWithoutCustomerAndWithExistingCart() + { + $cartTrait = $this->cartTrait; + + $request = $this->request; + $uniqid = uniqid("test1", true); + //create a fake cart in database; + $cart = new Cart(); + $cart->setToken($uniqid); + $cart->save(); + + $request->cookies->set("thelia_cart", $uniqid); + + $getCart = $cartTrait->getCart($request); + $this->assertInstanceOf("Thelia\Model\Cart", $getCart, '$cart must be an instance of cart model Thelia\Model\Cart'); + $this->assertNull($getCart->getCustomerId()); + $this->assertNull($getCart->getAddressDeliveryId()); + $this->assertNull($getCart->getAddressInvoiceId()); + $this->assertEquals($cart->getToken(), $getCart->getToken()); + } + + /** + * a cart id exists in cookies but this id does not exists yet in databases + * + * a new cart must be created (different token) + */ + public function testGetCartWithExistingCartButNotGoodCookies() + { + $cartTrait = $this->cartTrait; + + $request = $this->request; + + $token = "WrongToken"; + $request->cookies->set("thelia_cart", $token); + + $cart = $cartTrait->getCart($request); + $this->assertInstanceOf("Thelia\Model\Cart", $cart, '$cart must be an instance of cart model Thelia\Model\Cart'); + $this->assertNull($cart->getCustomerId()); + $this->assertNull($cart->getAddressDeliveryId()); + $this->assertNull($cart->getAddressInvoiceId()); + $this->assertNotEquals($token, $cart->getToken()); + } + + /** + * cart and customer already exists. Cart and customer are linked. + * + * cart in session must be return + */ + public function testGetCartWithExistingCartAndCustomer() + { + $cartTrait = $this->cartTrait; + + $request = $this->request; + + + //create a fake customer just for test. If not persists test fails ! + $customer = new Customer(); + $customer->setFirstname("john"); + $customer->setLastname("doe"); + $customer->setTitleId(1); + $customer->save(); + + $uniqid = uniqid("test2", true); + //create a fake cart in database; + $cart = new Cart(); + $cart->setToken($uniqid); + $cart->setCustomer($customer); + $cart->save(); + + $request->cookies->set("thelia_cart", $uniqid); + + $request->getSession()->setCustomerUser($customer); + + $getCart = $cartTrait->getCart($request); + $this->assertInstanceOf("Thelia\Model\Cart", $getCart, '$cart must be an instance of cart model Thelia\Model\Cart'); + $this->assertNotNull($getCart->getCustomerId()); + $this->assertNull($getCart->getAddressDeliveryId()); + $this->assertNull($getCart->getAddressInvoiceId()); + $this->assertEquals($cart->getToken(), $getCart->getToken(), "token must be the same"); + $this->assertEquals($customer->getId(), $getCart->getCustomerId()); + } + + /** + * Customer is connected but cart not associated to him + * + * A new cart must be created (duplicated) containing customer id + */ + public function testGetCartWithExistingCartAndCustomerButNotSameCustomerId() + { + $cartTrait = $this->cartTrait; + + $request = $this->request; + + + //create a fake customer just for test. If not persists test fails ! + $customer = new Customer(); + $customer->setFirstname("john"); + $customer->setLastname("doe"); + $customer->setTitleId(1); + $customer->save(); + + $uniqid = uniqid("test3", true); + //create a fake cart in database; + $cart = new Cart(); + $cart->setToken($uniqid); + + $cart->save(); + + $request->cookies->set("thelia_cart", $uniqid); + + $request->getSession()->setCustomerUser($customer); + + $getCart = $cartTrait->getCart($request); + $this->assertInstanceOf("Thelia\Model\Cart", $getCart, '$cart must be an instance of cart model Thelia\Model\Cart'); + $this->assertNotNull($getCart->getCustomerId()); + $this->assertNull($getCart->getAddressDeliveryId()); + $this->assertNull($getCart->getAddressInvoiceId()); + $this->assertNotEquals($cart->getToken(), $getCart->getToken(), "token must be different"); + $this->assertEquals($customer->getId(), $getCart->getCustomerId()); + } + +} + +/** + * Only way to mock a trait before phpunit 3.8 + * + * Class MockCartTrait + * @package Thelia\Tests\Cart\CartTraitTest + */ +class MockCartTrait +{ + use \Thelia\Cart\CartTrait; + + public $uniqid; + public $container; + + public function __construct($uniqid, $container) + { + $this->uniqid = $uniqid; + $this->container = $container; + } + + public function generateCookie() + { + return $this->uniqid; + } + + public function getDispatcher() + { + return $this->container->get("event_dispatcher"); } -// protected $session; -// -// protected $request; -// -// protected $actionCart; -// -// protected $uniqid; -// -// -// public function getContainer() -// { -// $container = new \Symfony\Component\DependencyInjection\ContainerBuilder(); -// -// $dispatcher = $this->getMock("Symfony\Component\EventDispatcher\EventDispatcherInterface"); -// -// $container->set("event_dispatcher", $dispatcher); -// -// return $container; -// } -// -// public function setUp() -// { -// $this->session = new Session(new MockArraySessionStorage()); -// $this->request = new Request(); -// -// $this->request->setSession($this->session); -// -// $this->uniqid = uniqid('', true); -// -// $container = $this->getContainer(); -// -// -// -// $this->actionCart = $this->getMockForTrait("\Thelia\Cart\CartTrait"); -// -// -// -// $this->actionCart -// ->expects($this->any()) -// ->method("generateCookie") -// ->will($this->returnValue($this->uniqid)); -// -// $this->actionCart -// ->expects($this->any()) -// ->method("redirect") -// ->will($this->returnValue(true)) -// ; -// } -// -// /** -// * no cart present in session and cart_id no yet exists in cookies. -// * -// * In this case, a new cart instance must be create -// */ -// public function testGetCartWithoutCustomerAndWithoutExistingCart() -// { -// $actionCart = $this->actionCart; -// -// $cart = $actionCart->getCart($this->request); -// -// $this->assertInstanceOf("Thelia\Model\Cart", $cart, '$cart must be an instance of cart model Thelia\Model\Cart'); -// $this->assertNull($cart->getCustomerId()); -// $this->assertNull($cart->getAddressDeliveryId()); -// $this->assertNull($cart->getAddressInvoiceId()); -// $this->assertEquals($this->uniqid, $cart->getToken()); -// -// } -// -// /** -// * Customer is connected but his cart does not exists yet -// * -// * Cart must be created and associated to the current connected Customer -// */ -// public function testGetCartWithCustomerAndWithoutExistingCart() -// { -// $actionCart = $this->actionCart; -// -// $request = $this->request; -// -// //create a fake customer just for test. If not persists test fails ! -// $customer = new Customer(); -// $customer->setFirstname("john"); -// $customer->setLastname("doe"); -// $customer->setTitleId(1); -// $customer->save(); -// -// $request->getSession()->setCustomerUser($customer); -// -// $cart = $actionCart->getCart($request); -// $this->assertInstanceOf("Thelia\Model\Cart", $cart, '$cart must be an instance of cart model Thelia\Model\Cart'); -// $this->assertNotNull($cart->getCustomerId()); -// $this->assertEquals($customer->getId(), $cart->getCustomerId()); -// $this->assertNull($cart->getAddressDeliveryId()); -// $this->assertNull($cart->getAddressInvoiceId()); -// $this->assertEquals($this->uniqid, $cart->getToken()); -// -// } -// -// /** -// * Cart exists and his id put in cookies. -// * -// * Must return the same cart instance -// */ -// public function testGetCartWithoutCustomerAndWithExistingCart() -// { -// $actionCart = $this->actionCart; -// -// $request = $this->request; -// $uniqid = uniqid("test1", true); -// //create a fake cart in database; -// $cart = new Cart(); -// $cart->setToken($uniqid); -// $cart->save(); -// -// $request->cookies->set("thelia_cart", $uniqid); -// -// $getCart = $actionCart->getCart($request); -// $this->assertInstanceOf("Thelia\Model\Cart", $getCart, '$cart must be an instance of cart model Thelia\Model\Cart'); -// $this->assertNull($getCart->getCustomerId()); -// $this->assertNull($getCart->getAddressDeliveryId()); -// $this->assertNull($getCart->getAddressInvoiceId()); -// $this->assertEquals($cart->getToken(), $getCart->getToken()); -// } -// -// /** -// * a cart id exists in cookies but this id does not exists yet in databases -// * -// * a new cart must be created (different token) -// */ -// public function testGetCartWithExistingCartButNotGoodCookies() -// { -// $actionCart = $this->actionCart; -// -// $request = $this->request; -// -// $token = "WrongToken"; -// $request->cookies->set("thelia_cart", $token); -// -// $cart = $actionCart->getCart($request); -// $this->assertInstanceOf("Thelia\Model\Cart", $cart, '$cart must be an instance of cart model Thelia\Model\Cart'); -// $this->assertNull($cart->getCustomerId()); -// $this->assertNull($cart->getAddressDeliveryId()); -// $this->assertNull($cart->getAddressInvoiceId()); -// $this->assertNotEquals($token, $cart->getToken()); -// } -// -// /** -// * cart and customer already exists. Cart and customer are linked. -// * -// * cart in session must be return -// */ -// public function testGetCartWithExistingCartAndCustomer() -// { -// $actionCart = $this->actionCart; -// -// $request = $this->request; -// -// -// //create a fake customer just for test. If not persists test fails ! -// $customer = new Customer(); -// $customer->setFirstname("john"); -// $customer->setLastname("doe"); -// $customer->setTitleId(1); -// $customer->save(); -// -// $uniqid = uniqid("test2", true); -// //create a fake cart in database; -// $cart = new Cart(); -// $cart->setToken($uniqid); -// $cart->setCustomer($customer); -// $cart->save(); -// -// $request->cookies->set("thelia_cart", $uniqid); -// -// $request->getSession()->setCustomerUser($customer); -// -// $getCart = $actionCart->getCart($request); -// $this->assertInstanceOf("Thelia\Model\Cart", $getCart, '$cart must be an instance of cart model Thelia\Model\Cart'); -// $this->assertNotNull($getCart->getCustomerId()); -// $this->assertNull($getCart->getAddressDeliveryId()); -// $this->assertNull($getCart->getAddressInvoiceId()); -// $this->assertEquals($cart->getToken(), $getCart->getToken(), "token must be the same"); -// $this->assertEquals($customer->getId(), $getCart->getCustomerId()); -// } -// -// /** -// * Customer is connected but cart not associated to him -// * -// * A new cart must be created (duplicated) containing customer id -// */ -// public function testGetCartWithExistingCartAndCustomerButNotSameCustomerId() -// { -// $actionCart = $this->actionCart; -// -// $request = $this->request; -// -// -// //create a fake customer just for test. If not persists test fails ! -// $customer = new Customer(); -// $customer->setFirstname("john"); -// $customer->setLastname("doe"); -// $customer->setTitleId(1); -// $customer->save(); -// -// $uniqid = uniqid("test3", true); -// //create a fake cart in database; -// $cart = new Cart(); -// $cart->setToken($uniqid); -// -// $cart->save(); -// -// $request->cookies->set("thelia_cart", $uniqid); -// -// $request->getSession()->setCustomerUser($customer); -// -// $getCart = $actionCart->getCart($request); -// $this->assertInstanceOf("Thelia\Model\Cart", $getCart, '$cart must be an instance of cart model Thelia\Model\Cart'); -// $this->assertNotNull($getCart->getCustomerId()); -// $this->assertNull($getCart->getAddressDeliveryId()); -// $this->assertNull($getCart->getAddressInvoiceId()); -// $this->assertNotEquals($cart->getToken(), $getCart->getToken(), "token must be different"); -// $this->assertEquals($customer->getId(), $getCart->getCustomerId()); -// } } \ No newline at end of file diff --git a/templates/default/cart.html b/templates/default/cart.html index 92de9838e..2413ac957 100644 --- a/templates/default/cart.html +++ b/templates/default/cart.html @@ -9,9 +9,7 @@ {form name="thelia.cart.add" } {* We use $INDEX_PAGE as form action to avoid mixing post and get data *} -
- - + {* The form error status and the form error messages are defined in Customer action,