Source of file DMSDocumentCartControllerTest.php
Size: 11,083 Bytes - Last Modified: 2021-12-23T10:29:29+00:00
/var/www/docs.ssmods.com/process/src/tests/controllers/DMSDocumentCartControllerTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 | <?php /** * Class DMSDocumentCartControllerTest contains all the tests for {@link DMSDocumentCartController} */ class DMSDocumentCartControllerTest extends FunctionalTest { protected static $fixture_file = 'dms-cart/tests/DMSDocumentCartTest.yml'; /** * @var DMSDocumentCartController */ protected $controller; /** * @var DMSDocumentCart */ protected $cart; public function setUp() { parent::setUp(); DMSDocumentCartController::add_extension('StubDMSDocumentCheckoutPageExtension'); $this->controller = DMSDocumentCartController::create(); $this->cart = $this->controller->getCart(); $this->cart->emptyCart(); } /** * Test the items method of the controller */ public function testItems() { /** @var DMSDocument $doc1 */ $doc1 = $this->objFromFixture('DMSDocument', 'doc1'); /** @var DMSDocument $doc2 */ $doc2 = $this->objFromFixture('DMSDocument', 'doc2'); /** @var DMSRequestItem $item */ $item1 = DMSRequestItem::create()->setDocument($doc1)->setQuantity(2); $item2 = DMSRequestItem::create()->setDocument($doc2)->setQuantity(5); $this->cart->addItem($item1); $this->cart->addItem($item2); $this->assertInstanceOf( 'ArrayList', $this->controller->items(), 'DMSDocumentCartController->Items() returned an ArrayList' ); $this->assertCount( 2, $this->controller->items(), 'DMSDocumentCartController->Items()->count() returned the requisite number of items' ); } public function testReceiverInfo() { // Now add some info $this->cart->setReceiverInfo(array('Name' => 'Joe', 'Surname' => 'Soap')); $this->assertCount(2, $this->controller->getReceiverInfo()); } public function testIsCartEmpty() { $this->assertTrue($this->controller->getIsCartEmpty()); /** @var DMSDocument $doc */ $doc1 = $this->objFromFixture('DMSDocument', 'doc1'); /** @var DMSRequestItem $item */ $item1 = DMSRequestItem::create()->setDocument($doc1)->setQuantity(2); $this->cart->addItem($item1); $this->assertFalse($this->controller->getIsCartEmpty()); } public function testAdd() { $this->logInWithPermission(); $doc1 = $this->objFromFixture('DMSDocument', 'doc1'); // Check cart is initially empty $this->assertTrue($this->controller->getIsCartEmpty()); // Now call controller add $request = new SS_HTTPRequest('POST', '', array(), array('quantity' => 5)); $request->setRouteParams(array('ID' => $doc1->ID)); $this->controller->add($request); $this->assertFalse($this->controller->getIsCartEmpty()); // Do it again and assert quantity was updated $request = new SS_HTTPRequest('POST', '', array(), array('quantity' => 7)); $request->setRouteParams(array('ID' => $doc1->ID)); $this->controller->add($request); // Test ajax $request = new SS_HTTPRequest('POST', '', array(), array('quantity' => 7, 'ajax' => 1)); $request->setRouteParams(array('ID' => $doc1->ID)); $response = $this->controller->add($request); $this->assertTrue($request->isAjax()); $this->assertJson($response, 'Confirmed that an ajax call to add() responded with json JSON'); $item = $this->cart->getItem($doc1->ID); $this->assertEquals(19, $item->getQuantity()); } public function testDeduct() { $doc1 = $this->objFromFixture('DMSDocument', 'doc1'); // Check catty is initially empty $this->assertTrue($this->controller->getIsCartEmpty()); // Now call controller add $request = new SS_HTTPRequest('POST', '', array(), array('quantity'=>5)); $request->setRouteParams(array('ID' => $doc1->ID)); $this->controller->deduct($request); // Assert cart still empty because item doesn't exist $this->assertTrue($this->controller->getIsCartEmpty()); // Now add item $this->controller->add($request); $this->assertFalse($this->controller->getIsCartEmpty()); // Now try and deduct 2 from the items $request = new SS_HTTPRequest('POST', '', array(), array('quantity' => -2)); $request->setRouteParams(array('ID' => $doc1->ID)); $this->controller->deduct($request); $item = $this->cart->getItem($doc1->ID); $this->assertEquals(3, $item->getQuantity()); // Test ajax $request = new SS_HTTPRequest('POST', '', array(), array('quantity' => 7, 'ajax' => 1)); $request->setRouteParams(array('ID' => $doc1->ID)); $response = $this->controller->deduct($request); $this->assertTrue($request->isAjax()); $this->assertJson($response, 'Confirmed that an ajax call to deduct() method responded with JSON'); } public function testRemove() { $doc1 = $this->objFromFixture('DMSDocument', 'doc1'); // Check catty is initially empty $this->assertTrue($this->controller->getIsCartEmpty()); // Now call controller add $request = new SS_HTTPRequest('POST', '', array(), array('quantity'=>5)); $request->setRouteParams(array('ID' => $doc1->ID)); // Now add item $this->controller->add($request); $this->assertFalse($this->controller->getIsCartEmpty()); $this->controller->remove($request); $this->assertTrue($this->controller->getIsCartEmpty()); // Test ajax $request = new SS_HTTPRequest('POST', '', array(), array('quantity' => 7, 'ajax' => 1)); $request->setRouteParams(array('ID' => $doc1->ID)); $response = $this->controller->remove($request); $this->assertTrue($request->isAjax()); $this->assertJson($response, 'Confirmed that an ajax call to remove() method responded with JSON'); } /** * Ensure that a validation error is shown when requesting to add more of a document that is allowed */ public function testCannotAddMoreThanSuggestedQuantityOfItem() { $document = $this->objFromFixture('DMSDocument', 'limited_supply'); $result = $this->get('/documentcart/add/' . $document->ID . '?quantity=5&ajax=1'); $this->assertContains( 'Maximum of 3 documents exceeded for \"Doc3\", please select a lower quantity.', (string) $result->getBody() ); } /** * Ensure that multiple validation errors are returned in the failure message, if any */ public function testMultipleValidationErrorsReturned() { $document1 = $this->objFromFixture('DMSDocument', 'limited_supply'); $document2 = $this->objFromFixture('DMSDocument', 'very_limited_supply'); $this->cart ->addItem(DMSRequestItem::create($document1)) ->addItem(DMSRequestItem::create($document2)); $input = array('ItemQuantity' => array( $document1->ID => 15000, $document2->ID => 12000 )); $form = Form::create($this->controller, '', new FieldList, new FieldList); $result = $this->controller->updateCartItems($input, $form, new SS_HTTPRequest('POST', '/')); $this->assertContains('Maximum of 3 documents exceeded for "Doc3"', $form->Message()); $this->assertContains('Maximum of 2 documents exceeded for "Doc5"', $form->Message()); } /** * Ensure that when a document that cannot be added to the cart is added to the cart, a validation error is * returned */ public function testValidationErrorReturnedOnInvalidAdd() { $document = $this->objFromFixture('DMSDocument', 'not_allowed_in_cart'); $result = $this->get('/documentcart/add/' . $document->ID . '?ajax=1'); $this->assertContains('You are not allowed to add this document', (string) $result->getBody()); } /** * Tests whether the cart items are updated from the controller * * @param array $quantity * @param int|string $expectedCount * @dataProvider updateCartItemsProvider */ public function testUpdateCartItems($quantity, $expectedCount) { $document = $this->objFromFixture('DMSDocument', 'doc1'); $item = DMSRequestItem::create()->setDocument($document)->setQuantity(2); $this->cart->addItem($item); $quantities = array('ItemQuantity' => array($document->ID => $quantity)); $form = Form::create($this->controller, 'Test', FieldList::create(), FieldList::create()); $request = new SS_HTTPRequest('POST', ''); $response = $this->controller->updateCartItems($quantities, $form, $request); $item = $this->cart->getItem($item->getItemId()); if (is_numeric($quantity) && $quantity <= 0) { $this->assertFalse($item); } else { $this->assertEquals($expectedCount, $item->getQuantity()); } } /** * @return array[] */ public function updateCartItemsProvider() { return array( // Invalid quantity, leave it unchanged array('non-numeric', 2), // Existing quantity remains the same array(2, 2), // Valid quantity increase array(5, 5), // Zero, remove it array(0, 0), // Negative, treat as zero array(-10, 0) ); } /** * Tests DMSCartEditForm form has a FieldList */ public function testDMSCartEditForm() { $form = $this->controller->DMSCartEditForm(); $this->assertInstanceOf('FieldList', $form->Fields()); } /** * Tests if the DMSCartEditForm is extensible */ public function testDMSCartEditFormIsExtensible() { $controller = $this->controller; $form = $controller->DMSCartEditForm(); $this->assertNotNull( $form->Fields()->fieldByName('NewTextField'), 'DMSDocumentRequestForm() is extensible as it included the field from the extension' ); } /** * Tests that the cart summary view is returned. */ public function testView() { $result = $this->get('documentcart/view'); $this->assertInstanceOf('SS_HTTPResponse', $result); $this->assertContains('Updating cart items', $result->getBody()); } /** * Ensure the link is "friendly", not a class name */ public function testLink() { $this->assertSame('documentcart', $this->controller->Link()); $this->assertSame('documentcart/view', $this->controller->Link('view')); } /** * Ensure that the user is notified when the cart is empty */ public function testEmptyCartShowsNoticeOnCheckout() { $result = $this->get('checkout'); $this->assertContains('Your cart is currently empty.', $result->getBody()); } } |