Merge pull request #18148 from civicrm/5.29
[civicrm-core.git] / tests / phpunit / CRM / Financial / Form / PaymentFormsTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Test various payment forms.
14 *
15 * This class is intended to be a place to build out testing of various forms - with the
16 * hope being to ensure all payment forms are consistently tested and to refine
17 * helper functions into a trait that could be available to
18 * extensions for testing - notably the eventcart which ideally should interact with core
19 * through approved interfaces - ideally even in tests.
20 *
21 * An approved interface would sit in the Civi directory and would at minimum support some functions
22 * to support using our processors in tests so we are testing a broader swath than just Dummy.
23 * Currently Authorize.net is also testable (uses Guzzle). At some point PaypalPro & Std should also be testable
24 * - allowing us to easily check payment forms work with the core processors which cover a reasonable amount of the
25 * expectations held by non-core processors .
26 *
27 * Note that this tests eventcart but is not in eventcart because I want to be sure about whether the
28 * traits supporting it make sense before making them available to extensions.
29 */
30 class CRM_Financial_Form_PaymentFormsTest extends CiviUnitTestCase {
31
32 use CRM_Core_Payment_AuthorizeNetTrait;
33
34 /**
35 * Generic test on event payment forms to make sure they submit without error with payment processing.
36 *
37 * @throws \CRM_Core_Exception
38 * @throws \CiviCRM_API3_Exception
39 */
40 public function testEventPaymentForms() {
41 $processors = [$this->paymentProcessorAuthorizeNetCreate(['is_test' => FALSE])];
42 $this->setupMockHandler($processors[0]);
43 $eventID = $this->eventCreatePaid([
44 'end_date' => '+ 1 month',
45 'registration_end_date' => '+ 1 month',
46 'payment_processor' => $processors,
47 ])['id'];
48 $this->createLoggedInUser();
49
50 $forms = [
51 'CRM_Event_Cart_Form_Checkout_ParticipantsAndPrices' => [
52 'forms' => ['CRM_Event_Cart_Form_Checkout_ParticipantsAndPrices', 'CRM_Event_Cart_Form_Checkout_Payment'],
53 'controller' => [],
54 'submitValues' => [
55 'event' => [$eventID => ['participant' => [1 => ['email' => 'bob@example.com']]]],
56 'event_' . $eventID . '_price_' . $this->_ids['price_field'][0] => $this->_ids['price_field_value'][0],
57 ],
58 'REQUEST' => [],
59 ],
60 ];
61 $genericParams = [
62 'credit_card_number' => 4111111111111111,
63 'processor_id' => $processors[0],
64 'cvv2' => '123',
65 'credit_card_exp_date' => [
66 'M' => '1',
67 'Y' => date('Y') + 1,
68 ],
69 'credit_card_type' => 'Visa',
70 'billing_contact_email' => 'bobby@example.com',
71 'billing_first_name' => 'John',
72 'billing_middle_name' => '',
73 'billing_last_name' => "O'Connor",
74 'billing_street_address-5' => '8 Hobbitton Road',
75 'billing_city-5' => 'The Shire',
76 'billing_state_province_id-5' => 1012,
77 'billing_postal_code-5' => 5010,
78 'billing_country_id-5' => 1228,
79 ];
80
81 $cart = CRM_Event_Cart_BAO_Cart::find_or_create_for_current_session();
82 $cart->add_event($eventID);
83
84 foreach ($forms as $values) {
85 $_REQUEST = $values['REQUEST'];
86 $qfKey = NULL;
87 foreach ($values['forms'] as $formName) {
88 $formValues = array_merge($genericParams, $values['submitValues'], ['qfKey' => $qfKey]);
89 $form = $this->getFormObject($formName, $formValues);
90 $form->preProcess();
91 $form->buildQuickForm();
92 $form->postProcess();
93 $qfKey = $form->controller->_key;
94 }
95 $this->callAPISuccessGetSingle('Participant', ['participant_status_id' => 'Registered']);
96 $request = explode('&', $this->getRequestBodies()[0]);
97 // This is stand in for now just to check a request happened. We can improve later.
98 $this->assertContains('x_card_num=4111111111111111', $request);
99 }
100 }
101
102 }