Source of file CheckoutTest.php
Size: 15,384 Bytes - Last Modified: 2021-12-23T10:47:54+00:00
/var/www/docs.ssmods.com/process/src/tests/CheckoutTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498 | <?php /** * Testing {@link Order} modifiers at checkout. * * Summary of tests: * ----------------- * checkout published product * unpublish product after it is in the cart cannot checkout * delete product after it is in the cart cannot checkout * add variation then disable the variation, cannot checkout * add variation to cart then delete variation cannot checkout * submit checkout without products in cart * checkout with product that has attributes, without a variation set * submit checkout without necessary details * submit checkout without specifying payment gateway * * TODO * ---- * process payment * send receipt * checkout addresses correct * when attribute deleted cannot checkout variation * when attribute deleted previous orders keep data * * @author Frank Mullenger <frankmullenger@gmail.com> * @copyright Copyright (c) 2011, Frank Mullenger * @package swipestripe * @subpackage tests */ class SWS_CheckoutTest extends SWS_Test { function setUp() { parent::setUp(); //Check that payment module is installed $this->assertTrue(class_exists('Payment'), 'Payment module is installed.'); //$this->assertTrue(class_exists('ChequePayment'), 'Cheque Payment is installed.'); //Need to publish a few pages because not using the draft site $checkoutPage = $this->objFromFixture('CheckoutPage', 'checkout'); $accountPage = $this->objFromFixture('AccountPage', 'account'); $cartPage = $this->objFromFixture('CartPage', 'cart'); $this->loginAs('admin'); $checkoutPage->doPublish(); $accountPage->doPublish(); $cartPage->doPublish(); $this->logOut(); Config::inst()->remove('PaymentProcessor', 'supported_methods'); Config::inst()->update('PaymentProcessor', 'supported_methods', array('test' => array('Cheque'))); Config::inst()->remove('PaymentGateway', 'environment'); Config::inst()->update('PaymentGateway', 'environment', 'test'); } /** * Create product and check basic attributes */ function testCheckoutWithPublishedProduct() { $productA = $this->objFromFixture('Product', 'productA'); $checkoutPage = $this->objFromFixture('CheckoutPage', 'checkout'); $this->loginAs('admin'); $productA->doPublish(); $this->logOut(); $this->assertTrue($productA->isPublished()); $buyer = $this->objFromFixture('Customer', 'buyer'); $this->loginAs($buyer); $this->get(Director::makeRelative($productA->Link())); $this->submitForm('ProductForm_ProductForm', null, array( 'Quantity' => 1 )); $order = Cart::get_current_order(); $items = $order->Items(); $this->assertEquals(1, $items->Count()); $this->assertEquals($productA->ID, $items->First()->Product()->ID); $orders = $buyer->Orders(); $this->assertEquals(1, $orders->Count()); $this->get(Director::makeRelative($checkoutPage->Link())); $this->submitForm('OrderForm_OrderForm', null, array( 'Notes' => 'New order for test buyer.' )); $orders = $buyer->Orders(); $this->assertEquals(2, $orders->Count()); } /** * Try to checkout an unpublished product */ function testCheckoutWithUnpublishedProduct() { $productA = $this->objFromFixture('Product', 'productA'); $this->loginAs('admin'); $productA->doPublish(); $this->logOut(); $this->assertTrue($productA->isPublished()); //Add product to cart, buyer has one Order existing from fixture $buyer = $this->objFromFixture('Customer', 'buyer'); $this->assertEquals(1, $buyer->Orders()->Count()); $this->loginAs($buyer); $this->get(Director::makeRelative($productA->Link())); $this->submitForm('ProductForm_ProductForm', null, array( 'Quantity' => 1 )); $order = Cart::get_current_order(); $items = $order->Items(); $this->assertEquals(1, $items->Count()); $this->assertEquals($productA->ID, $items->First()->Product()->ID); $this->logOut(); //Unpublish the product thats in the cart $this->loginAs('admin'); $productA->doUnpublish(); $this->logOut(); $this->assertEquals(false, $productA->isPublished()); $this->assertEquals(false, $order->validateForCart()->valid()); //Log in as buyer again and try to checkout $this->loginAs($this->objFromFixture('Customer', 'buyer')); $checkoutPage = DataObject::get_one('CheckoutPage'); $this->get(Director::makeRelative($checkoutPage->Link())); $this->submitForm('OrderForm_OrderForm', null, array( 'Notes' => 'This order should fail.' )); $orders = $buyer->Orders(); $this->assertEquals(1, $orders->Count()); } /** * Try to checkout a deleted product */ function testCheckoutWithDeletedProduct() { $productA = $this->objFromFixture('Product', 'productA'); $this->loginAs('admin'); $productA->doPublish(); $this->logOut(); $this->assertTrue($productA->isPublished()); //Add product to cart, buyer has one Order existing from fixture $buyer = $this->objFromFixture('Customer', 'buyer'); $this->assertEquals(1, $buyer->Orders()->Count()); $this->loginAs($buyer); $this->get(Director::makeRelative($productA->Link())); $this->submitForm('ProductForm_ProductForm', null, array( 'Quantity' => 1 )); $order = Cart::get_current_order(); $items = $order->Items(); $this->assertEquals(1, $items->Count()); $this->assertEquals($productA->ID, $items->First()->Product()->ID); $this->logOut(); //Delete the product thats in the cart $this->loginAs('admin'); $productA->delete(); $this->logOut(); $this->assertEquals(false, $productA->isInDB()); $this->assertEquals(false, $order->validateForCart()->valid()); //Log in as buyer again and try to checkout $this->loginAs($buyer); $checkoutPage = DataObject::get_one('CheckoutPage'); $this->get(Director::makeRelative($checkoutPage->Link())); $this->submitForm('OrderForm_OrderForm', null, array( 'Notes' => 'This order should fail.' )); $orders = $buyer->Orders(); $this->assertEquals(1, $orders->Count()); } /** * Try to checkout a disabled variation */ function testCheckoutWithDisabledVariation() { $shortsA = $this->objFromFixture('Product', 'shortsA'); $this->loginAs('admin'); $shortsA->doPublish(); $this->logOut(); $this->assertTrue($shortsA->isPublished()); //Add product to cart, buyer has one Order existing from fixture $buyer = $this->objFromFixture('Customer', 'buyer'); $this->assertEquals(1, $buyer->Orders()->Count()); $this->loginAs($this->objFromFixture('Customer', 'buyer')); $this->get(Director::makeRelative($shortsA->Link())); $shortsAVariation = $this->objFromFixture('Variation', 'shortsSmallRedCotton'); $this->assertEquals('Enabled', $shortsAVariation->Status); $this->submitForm('ProductForm_ProductForm', null, array( 'Quantity' => 1, 'Options[1]' => $shortsAVariation->getOptionForAttribute(1)->ID, //Small 'Options[2]' => $shortsAVariation->getOptionForAttribute(2)->ID, //Red 'Options[3]' => $shortsAVariation->getOptionForAttribute(3)->ID, //Cotton )); $order = Cart::get_current_order(); $items = $order->Items(); $this->assertEquals(1, $items->Count()); $this->assertEquals($shortsA->ID, $items->First()->Product()->ID); $this->logOut(); $this->logInAs('admin'); $shortsAVariation->Status = 'Disabled'; $shortsAVariation->write(); $this->logOut(); $this->assertEquals('Disabled', $shortsAVariation->Status); $this->assertEquals(false, $order->validateForCart()->valid()); //Log in as buyer again and try to checkout $this->loginAs($this->objFromFixture('Customer', 'buyer')); $checkoutPage = DataObject::get_one('CheckoutPage'); $this->get(Director::makeRelative($checkoutPage->Link())); $this->submitForm('OrderForm_OrderForm', null, array( 'Notes' => 'This order should fail.' )); $orders = $buyer->Orders(); $this->assertEquals(1, $orders->Count()); } /** * Try to checkout a deleted variation */ function testCheckoutWithDeletedVariation() { $shortsA = $this->objFromFixture('Product', 'shortsA'); $this->loginAs('admin'); $shortsA->doPublish(); $this->logOut(); $this->assertTrue($shortsA->isPublished()); //Add product to cart, buyer has one Order existing from fixture $buyer = $this->objFromFixture('Customer', 'buyer'); $this->assertEquals(1, $buyer->Orders()->Count()); $this->loginAs($this->objFromFixture('Customer', 'buyer')); $this->get(Director::makeRelative($shortsA->Link())); $shortsAVariation = $this->objFromFixture('Variation', 'shortsSmallRedCotton'); $this->assertEquals('Enabled', $shortsAVariation->Status); $this->submitForm('ProductForm_ProductForm', null, array( 'Quantity' => 1, 'Options[1]' => $shortsAVariation->getOptionForAttribute(1)->ID, //Small 'Options[2]' => $shortsAVariation->getOptionForAttribute(2)->ID, //Red 'Options[3]' => $shortsAVariation->getOptionForAttribute(3)->ID, //Cotton )); $order = Cart::get_current_order(); $items = $order->Items(); $this->assertEquals(1, $items->Count()); $this->assertEquals($shortsA->ID, $items->First()->Product()->ID); $this->logOut(); $this->logInAs('admin'); $shortsAVariation->delete(); $this->logOut(); $this->assertEquals(false, $shortsAVariation->isInDB()); $this->assertEquals(false, $order->validateForCart()->valid()); //Log in as buyer again and try to checkout $this->loginAs($this->objFromFixture('Customer', 'buyer')); $checkoutPage = DataObject::get_one('CheckoutPage'); $this->get(Director::makeRelative($checkoutPage->Link())); $this->submitForm('OrderForm_OrderForm', null, array( 'Notes' => 'This order should fail.' )); $orders = $buyer->Orders(); $this->assertEquals(1, $orders->Count()); } /** * Try to checkout without products added to the order */ function testCheckoutWithoutProducts() { //Add product to cart, buyer has one Order existing from fixture $buyer = $this->objFromFixture('Customer', 'buyer'); $this->assertEquals(1, $buyer->Orders()->Count()); //Log in as buyer again and try to checkout $this->loginAs($this->objFromFixture('Customer', 'buyer')); $order = Cart::get_current_order(); $items = $order->Items(); $this->assertEquals(0, $items->Count()); $this->assertEquals(false, $order->validateForCart()->valid()); $checkoutPage = DataObject::get_one('CheckoutPage'); $this->get(Director::makeRelative($checkoutPage->Link())); try { $this->submitForm('OrderForm_OrderForm', null, array( 'Notes' => 'This order should fail.' )); } catch (Exception $e) { } $orders = $buyer->Orders(); $this->assertEquals(1, $orders->Count()); } /** * Try to checkout with a product that requires a variation, without a variation in the cart */ function testCheckoutWithoutRequiredVariation() { $shortsA = $this->objFromFixture('Product', 'shortsA'); $this->loginAs('admin'); $shortsA->doPublish(); $this->logOut(); $this->assertTrue($shortsA->isPublished()); $this->assertTrue($shortsA->requiresVariation()); //Add product to cart, buyer has one Order existing from fixture $buyer = $this->objFromFixture('Customer', 'buyer'); $this->assertEquals(1, $buyer->Orders()->Count()); $this->loginAs($this->objFromFixture('Customer', 'buyer')); $this->get(Director::makeRelative($shortsA->Link())); $shortsAVariation = $this->objFromFixture('Variation', 'shortsSmallRedCotton'); $this->assertEquals('Enabled', $shortsAVariation->Status); $this->submitForm('ProductForm_ProductForm', null, array( 'Quantity' => 1, 'Options[1]' => $shortsAVariation->getOptionForAttribute(1)->ID, //Small 'Options[2]' => $shortsAVariation->getOptionForAttribute(2)->ID, //Red 'Options[3]' => $shortsAVariation->getOptionForAttribute(3)->ID, //Cotton )); $order = Cart::get_current_order(); $items = $order->Items(); $variation = $order->Items()->First()->Variation(); $this->assertEquals(1, $items->Count()); $this->assertEquals($shortsA->ID, $items->First()->Product()->ID); $this->logOut(); $this->logInAs('admin'); $variation->delete(); $this->logOut(); $this->assertEquals(false, $variation->isInDB()); $this->assertEquals(false, $order->validateForCart()->valid()); //Log in as buyer again and try to checkout $this->loginAs($this->objFromFixture('Customer', 'buyer')); $checkoutPage = DataObject::get_one('CheckoutPage'); $this->get(Director::makeRelative($checkoutPage->Link())); $this->submitForm('OrderForm_OrderForm', null, array( 'Notes' => 'This order should fail.' )); $orders = $buyer->Orders(); $this->assertEquals(1, $orders->Count()); } /** * Try to submit the checkout form without some required fields * Assumes that billing FirstName is always required */ function testCheckoutWithoutRequiredFields() { $shortsA = $this->objFromFixture('Product', 'shortsA'); $shortsAVariation = $this->objFromFixture('Variation', 'shortsSmallRedCotton'); $this->loginAs('admin'); $shortsA->doPublish(); $this->logOut(); $this->assertTrue($shortsA->isPublished()); //Add product to cart, buyer has one Order existing from fixture $buyer = $this->objFromFixture('Customer', 'buyer'); $this->assertEquals(1, $buyer->Orders()->Count()); $this->loginAs($this->objFromFixture('Customer', 'buyer')); $this->get(Director::makeRelative($shortsA->Link())); $this->submitForm('ProductForm_ProductForm', null, array( 'Quantity' => 1, 'Options[1]' => $shortsAVariation->getOptionForAttribute(1)->ID, //Small 'Options[2]' => $shortsAVariation->getOptionForAttribute(2)->ID, //Red 'Options[3]' => $shortsAVariation->getOptionForAttribute(3)->ID, //Cotton )); $order = Cart::get_current_order(); $items = $order->Items(); $variation = $order->Items()->First()->Variation(); $this->assertEquals(1, $items->Count()); $this->assertEquals($shortsA->ID, $items->First()->Product()->ID); $this->logOut(); $checkoutPage = DataObject::get_one('CheckoutPage'); $this->get(Director::makeRelative($checkoutPage->Link())); $this->submitForm('OrderForm_OrderForm', null, array( 'Billing[FirstName]' => '' )); $orders = $buyer->Orders(); $this->assertEquals(1, $orders->Count()); } /** * Try checking out an order without specifying a payment gateway */ function testCheckoutWithoutPaymentGateway() { $productA = $this->objFromFixture('Product', 'productA'); $this->loginAs('admin'); $productA->doPublish(); $this->logOut(); $buyer = $this->objFromFixture('Customer', 'buyer'); $this->assertEquals(1, $buyer->Orders()->Count()); $this->loginAs($this->objFromFixture('Customer', 'buyer')); $productALink = $productA->Link(); $this->get(Director::makeRelative($productALink)); $this->submitForm('ProductForm_ProductForm', null, array( 'Quantity' => 1 )); $order = Cart::get_current_order(); $items = $order->Items(); $this->assertEquals(1, $items->Count()); $checkoutPage = DataObject::get_one('CheckoutPage'); $this->get(Director::makeRelative($checkoutPage->Link())); //Submit the form without restrictions on what can be POST'd $data = $this->getFormData('OrderForm_OrderForm'); $data['PaymentMethod'] = ''; $this->post( Director::absoluteURL('/checkout/OrderForm'), $data ); $this->assertEquals(1, $buyer->Orders()->Count()); } } |