Skip to content

Commit

Permalink
Add test for ajax payment using iats (#961)
Browse files Browse the repository at this point in the history
  • Loading branch information
jitendrapurohit authored May 6, 2024
1 parent 1051b35 commit 79fea2f
Showing 1 changed file with 56 additions and 34 deletions.
90 changes: 56 additions & 34 deletions tests/src/FunctionalJavascript/ContributionIatsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand All @@ -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']);
Expand All @@ -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']);
Expand Down Expand Up @@ -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,
]);
Expand Down Expand Up @@ -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 <div> 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);
Expand All @@ -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');
Expand All @@ -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();
Expand All @@ -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,
]);

Expand All @@ -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",
Expand All @@ -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,
]);

Expand All @@ -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', [
Expand Down

0 comments on commit 79fea2f

Please sign in to comment.