From 6986e4f1b74c75796a95e3e6e3b218f68649c44b Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 10 Aug 2020 16:12:02 +1200 Subject: [PATCH] [NFC] [Test] Intial testing on event payment forms. I'm trying to work on improving our testing of payment forms. I started with 3 event forms but there were enough challenges that for this commit I'm just adding tests on one form. As noted in the code comments it makes sense for this test to be in the extension but I want to work through the challenges on the other forms before finalising any helper functions to the point where they are available to extensions. This is actually a bit of a break through as it's the first time we have testing on a form flow - ie submitting the first form and then the second. It was quite painful --- .../CRM/Core/Payment/AuthorizeNetTrait.php | 75 +++++++++++++ .../CRM/Financial/Form/PaymentFormsTest.php | 102 ++++++++++++++++++ tests/phpunit/CiviTest/CiviUnitTestCase.php | 15 ++- 3 files changed, 191 insertions(+), 1 deletion(-) create mode 100644 tests/phpunit/CRM/Core/Payment/AuthorizeNetTrait.php create mode 100644 tests/phpunit/CRM/Financial/Form/PaymentFormsTest.php diff --git a/tests/phpunit/CRM/Core/Payment/AuthorizeNetTrait.php b/tests/phpunit/CRM/Core/Payment/AuthorizeNetTrait.php new file mode 100644 index 0000000000..87ff897d41 --- /dev/null +++ b/tests/phpunit/CRM/Core/Payment/AuthorizeNetTrait.php @@ -0,0 +1,75 @@ +processor = Civi\Payment\System::singleton()->getById($id); + } + $response = $this->isRecur ? $this->getExpectedRecurResponse() : $this->getExpectedSinglePaymentResponse(); + $this->createMockHandler([$response]); + $this->setUpClientWithHistoryContainer(); + $this->processor->setGuzzleClient($this->getGuzzleClient()); + } + + /** + * Get a successful response to setting up a recurring. + * + * @return string + */ + public function getExpectedRecurResponse() { + return '8d468ca1b1dd5c2b56c7OkI00001Successful.663205215120232801512027350'; + } + +} diff --git a/tests/phpunit/CRM/Financial/Form/PaymentFormsTest.php b/tests/phpunit/CRM/Financial/Form/PaymentFormsTest.php new file mode 100644 index 0000000000..29b271cadb --- /dev/null +++ b/tests/phpunit/CRM/Financial/Form/PaymentFormsTest.php @@ -0,0 +1,102 @@ +paymentProcessorAuthorizeNetCreate(['is_test' => FALSE])]; + $this->setupMockHandler($processors[0]); + $eventID = $this->eventCreatePaid([ + 'end_date' => '+ 1 month', + 'registration_end_date' => '+ 1 month', + 'payment_processor' => $processors, + ])['id']; + $this->createLoggedInUser(); + + $forms = [ + 'CRM_Event_Cart_Form_Checkout_ParticipantsAndPrices' => [ + 'forms' => ['CRM_Event_Cart_Form_Checkout_ParticipantsAndPrices', 'CRM_Event_Cart_Form_Checkout_Payment'], + 'controller' => [], + 'submitValues' => [ + 'event' => [$eventID => ['participant' => [1 => ['email' => 'bob@example.com']]]], + 'event_' . $eventID . '_price_' . $this->_ids['price_field'][0] => $this->_ids['price_field_value'][0], + ], + 'REQUEST' => [], + ], + ]; + $genericParams = [ + 'credit_card_number' => 4111111111111111, + 'processor_id' => $processors[0], + 'cvv2' => '123', + 'credit_card_exp_date' => [ + 'M' => '1', + 'Y' => date('Y') + 1, + ], + 'credit_card_type' => 'Visa', + 'billing_contact_email' => 'bobby@example.com', + 'billing_first_name' => 'John', + 'billing_middle_name' => '', + 'billing_last_name' => "O'Connor", + 'billing_street_address-5' => '8 Hobbitton Road', + 'billing_city-5' => 'The Shire', + 'billing_state_province_id-5' => 1012, + 'billing_postal_code-5' => 5010, + 'billing_country_id-5' => 1228, + ]; + + $cart = CRM_Event_Cart_BAO_Cart::find_or_create_for_current_session(); + $cart->add_event($eventID); + + foreach ($forms as $values) { + $_REQUEST = $values['REQUEST']; + $qfKey = NULL; + foreach ($values['forms'] as $formName) { + $formValues = array_merge($genericParams, $values['submitValues'], ['qfKey' => $qfKey]); + $form = $this->getFormObject($formName, $formValues); + $form->preProcess(); + $form->buildQuickForm(); + $form->postProcess(); + $qfKey = $form->controller->_key; + } + $this->callAPISuccessGetSingle('Participant', ['participant_status_id' => 'Registered']); + $request = explode('&', $this->getRequestBodies()[0]); + // This is stand in for now just to check a request happened. We can improve later. + $this->assertContains('x_card_num=4111111111111111', $request); + } + } + +} diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 34bba898b1..7d05958487 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -3234,9 +3234,22 @@ VALUES * @throws \CRM_Core_Exception */ public function getFormObject($class, $formValues = [], $pageName = '') { + $_POST = $formValues; $form = new $class(); $_SERVER['REQUEST_METHOD'] = 'GET'; - $form->controller = new CRM_Core_Controller(); + switch ($class) { + case 'CRM_Event_Cart_Form_Checkout_Payment': + case 'CRM_Event_Cart_Form_Checkout_ParticipantsAndPrices': + $form->controller = new CRM_Event_Cart_Controller_Checkout(); + break; + + default: + $form->controller = new CRM_Core_Controller(); + } + if (!$pageName) { + $formParts = explode('_', $class); + $pageName = array_pop($formParts); + } $form->controller->setStateMachine(new CRM_Core_StateMachine($form->controller)); $_SESSION['_' . $form->controller->_name . '_container']['values'][$pageName] = $formValues; return $form; -- 2.25.1