From 79fea2f9523b1a35f205dd3e2a69f841b5f25bc2 Mon Sep 17 00:00:00 2001 From: Jitendra Purohit Date: Mon, 6 May 2024 20:29:57 +0530 Subject: [PATCH] Add test for ajax payment using iats (#961) --- .../ContributionIatsTest.php | 90 ++++++++++++------- 1 file changed, 56 insertions(+), 34 deletions(-) diff --git a/tests/src/FunctionalJavascript/ContributionIatsTest.php b/tests/src/FunctionalJavascript/ContributionIatsTest.php index f4b549f3b..c437dec2f 100644 --- a/tests/src/FunctionalJavascript/ContributionIatsTest.php +++ b/tests/src/FunctionalJavascript/ContributionIatsTest.php @@ -39,8 +39,7 @@ protected function setUp(): void { 'payment_type' => 1, 'payment_instrument_id' => 'Credit Card', ]; - $utils = \Drupal::service('webform_civicrm.utils'); - $result = $utils->wf_civicrm_api('payment_processor', 'create', $params); + $result = $this->utils->wf_civicrm_api('payment_processor', 'create', $params); $this->assertEquals(0, $result['is_error']); $this->assertEquals(1, $result['count']); $this->payment_processor_legacy = current($result['values']); @@ -63,8 +62,7 @@ protected function setUp(): void { 'payment_type' => 1, 'payment_instrument_id' => 'Debit Card', ]; - $utils = \Drupal::service('webform_civicrm.utils'); - $result = $utils->wf_civicrm_api('payment_processor', 'create', $params); + $result = $this->utils->wf_civicrm_api('payment_processor', 'create', $params); $this->assertEquals(0, $result['is_error']); $this->assertEquals(1, $result['count']); $this->payment_processor_legacy_acheft = current($result['values']); @@ -87,8 +85,7 @@ protected function setUp(): void { 'payment_type' => 1, 'payment_instrument_id' => 'Credit Card', ]; - $utils = \Drupal::service('webform_civicrm.utils'); - $result = $utils->wf_civicrm_api('payment_processor', 'create', $params); + $result = $this->utils->wf_civicrm_api('payment_processor', 'create', $params); $this->assertEquals(0, $result['is_error']); $this->assertEquals(1, $result['count']); $this->payment_processor_faps = current($result['values']); @@ -156,8 +153,7 @@ protected function setUp(): void { $this->assertPageNoErrorMessages(); // ToDo: load the Contribution and check the values - $utils = \Drupal::service('webform_civicrm.utils'); - $api_result = $utils->wf_civicrm_api('contribution', 'get', [ + $api_result = $this->utils->wf_civicrm_api('contribution', 'get', [ 'sequential' => 1, ]); @@ -200,29 +196,23 @@ private function filliATSCryptogram() { $this->getSession()->switchToIFrame(); } - public function testSubmitContribution() { - $financialAccount = $this->setupSalesTax(2, $accountParams = []); - + /** + * Create a webform with contribution. + */ + public function configureWebform() { + $this->setupSalesTax(2); $this->drupalLogin($this->adminUser); $this->drupalGet(Url::fromRoute('entity.webform.civicrm', [ 'webform' => $this->webform->id(), ])); // The label has a
in it which can cause weird failures here. - $this->assertSession()->waitForText('Enable CiviCRM Processing'); - $this->assertSession()->waitForField('nid'); - $this->getSession()->getPage()->checkField('nid'); - $this->getSession()->getPage()->clickLink('Contribution'); - $this->getSession()->getPage()->selectFieldOption('civicrm_1_contribution_1_contribution_enable_contribution', 1); - $this->assertSession()->assertWaitOnAjaxRequest(); - $this->assertSession()->pageTextContains('You must enable an email field for Contact 1 in order to process transactions.'); - $this->getSession()->getPage()->pressButton('Enable It'); - $this->assertSession()->assertWaitOnAjaxRequest(); + $this->enableCivicrmOnWebform(); + $params = [ + 'payment_processor_id' => $this->payment_processor_legacy['id'], + ]; + $this->configureContributionTab($params); $this->getSession()->getPage()->checkField('Contribution Amount'); - $this->getSession()->getPage()->selectFieldOption('Currency', 'USD'); - $this->getSession()->getPage()->selectFieldOption('Financial Type', 1); - $this->assertCount(5, $this->getOptions('Payment Processor')); - $this->getSession()->getPage()->selectFieldOption('Payment Processor', $this->payment_processor_legacy['id']); $this->enableBillingSection(); $this->getSession()->getPage()->selectFieldOption('lineitem_1_number_of_lineitem', 2); @@ -235,9 +225,13 @@ public function testSubmitContribution() { // Set the Financial Type for the second line item to Member Dues (which has Sales Tax on it). $this->getSession()->getPage()->selectFieldOption('civicrm_1_lineitem_2_contribution_financial_type_id', 2); - $this->getSession()->getPage()->pressButton('Save Settings'); - $this->assertSession()->pageTextContains('Saved CiviCRM settings'); + $this->saveCiviCRMSettings(); + } + /** + * Submit the form using iATS card details. + */ + public function submitWebForm() { $this->drupalGet($this->webform->toUrl('canonical')); $this->assertPageNoErrorMessages(); $this->getSession()->getPage()->fillField('First Name', 'Frederick'); @@ -247,6 +241,7 @@ public function testSubmitContribution() { $this->getSession()->getPage()->fillField('Line Item Amount 2', '5.00'); $this->getSession()->getPage()->pressButton('Next >'); + $this->assertSession()->waitForField('Contribution Amount'); $this->getSession()->getPage()->fillField('Contribution Amount', '3.00'); $this->assertSession()->elementExists('css', '#wf-crm-billing-items'); $this->htmlOutput(); @@ -271,14 +266,17 @@ public function testSubmitContribution() { $this->fillBillingFields($billingValues); $this->getSession()->getPage()->pressButton('Submit'); // throw new \Exception(var_export($this->htmlOutputDirectory, TRUE)); - $this->createScreenshot($this->htmlOutputDirectory . '/legacy289.png'); $this->htmlOutput(); $this->assertPageNoErrorMessages(); - $this->assertSession()->pageTextContains('New submission added to CiviCRM Webform Test.'); + $this->assertSession()->waitForText('New submission added to CiviCRM Webform Test.'); + } - $utils = \Drupal::service('webform_civicrm.utils'); - $api_result = $utils->wf_civicrm_api('contribution', 'get', [ + /** + * Verify payment values in CiviCRM. + */ + public function verifyResults() { + $api_result = $this->utils->wf_civicrm_api('contribution', 'get', [ 'sequential' => 1, ]); @@ -294,12 +292,12 @@ public function testSubmitContribution() { $this->assertEquals('USD', $contribution['currency']); // Also retrieve tax_amount (have to ask for it to be returned): - $api_result = $utils->wf_civicrm_api('contribution', 'get', [ + $api_result = $this->utils->wf_civicrm_api('contribution', 'get', [ 'sequential' => 1, 'return' => ['tax_amount', 'payment_instrument_id'], ]); $contribution = reset($api_result['values']); - $creditCardID = $utils->wf_civicrm_api('OptionValue', 'getvalue', [ + $creditCardID = $this->utils->wf_civicrm_api('OptionValue', 'getvalue', [ 'return' => "value", 'label' => "Credit Card", 'option_group_id' => "payment_instrument", @@ -308,13 +306,13 @@ public function testSubmitContribution() { $this->assertEquals($creditCardID, $contribution['payment_instrument_id']); $tax_total_amount = $contribution['tax_amount']; - $contriPriceFieldID = $utils->wf_civicrm_api('PriceField', 'get', [ + $contriPriceFieldID = $this->utils->wf_civicrm_api('PriceField', 'get', [ 'sequential' => 1, 'price_set_id' => 'default_contribution_amount', 'options' => ['limit' => 1], ])['id'] ?? NULL; - $api_result = $utils->wf_civicrm_api('line_item', 'get', [ + $api_result = $this->utils->wf_civicrm_api('line_item', 'get', [ 'sequential' => 1, ]); @@ -337,6 +335,30 @@ public function testSubmitContribution() { $this->assertEquals($contribution_total_amount, $sum_line_total + $sum_tax_amount); } + /** + * Test Payment using iATS processor. + */ + public function testSubmitContribution() { + $this->configureWebform(); + $this->submitWebForm(); + $this->verifyResults(); + } + + /** + * Test Payment on AJAX webform. + */ + public function testSubmitContributionAjaxEnabled() { + $this->configureWebform(); + // Enable AJAX on the form. + $this->drupalGet($this->webform->toUrl('settings')); + $this->htmlOutput(); + $this->getSession()->getPage()->checkField('Use Ajax'); + $this->getSession()->getPage()->pressButton('Save'); + + $this->submitWebForm(); + $this->verifyResults(); + } + public function testSubmitACHEFTContribution() { $this->drupalLogin($this->adminUser); $this->drupalGet(Url::fromRoute('entity.webform.civicrm', [