You are here

public function MultiCustomFieldsSubmissionTest::testAnonymousSubmitWithContribution in Webform CiviCRM Integration 8.5

File

tests/src/FunctionalJavascript/MultiCustomFieldsSubmissionTest.php, line 98

Class

MultiCustomFieldsSubmissionTest
Tests submitting a Webform with CiviCRM: single contact + custom fields.

Namespace

Drupal\Tests\webform_civicrm\FunctionalJavascript

Code

public function testAnonymousSubmitWithContribution() {
  $payment_processor = $this
    ->createPaymentProcessor();
  $this->_totalMV = 1;
  $this
    ->createMultiValueCustomFields();
  $this
    ->drupalLogin($this->rootUser);
  $this
    ->drupalGet(Url::fromRoute('entity.webform.civicrm', [
    'webform' => $this->webform
      ->id(),
  ]));
  $this
    ->enableCivicrmOnWebform();
  $this
    ->enableCustomFields(1, TRUE);
  $this
    ->htmlOutput();

  //Configure Contribution tab.
  $this
    ->configureContributionTab(TRUE);
  $this
    ->getSession()
    ->getPage()
    ->checkField('Contribution Amount');
  $this
    ->assertSession()
    ->checkboxChecked('Contribution Amount');
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption('Payment Processor', $payment_processor['id']);
  $this
    ->saveCiviCRMSettings();
  $this
    ->drupalLogout();
  $this
    ->drupalGet($this->webform
    ->toUrl('canonical'));
  $this
    ->htmlOutput();
  $this
    ->assertPageNoErrorMessages();
  $params = [
    'First Name' => 'The',
    'Last Name' => 'Weeknd',
    'Email' => 'theweeknd@example.com',
    'Month' => 'January',
    'civicrm_1_contact_1_cg1_custom_2' => 200,
  ];
  $this
    ->submitWebform($params, 'Next >');
  $this
    ->htmlOutput();
  $this
    ->assertSession()
    ->elementExists('css', '#wf-crm-billing-items');
  $this
    ->getSession()
    ->getPage()
    ->fillField('Contribution Amount', 20);

  // Wait for the credit card form to load in.
  $this
    ->assertSession()
    ->waitForField('credit_card_number');
  $this
    ->assertSession()
    ->elementTextContains('css', '#wf-crm-billing-total', '20.00');
  $billingValues = [
    'first_name' => 'The',
    'last_name' => 'Weeknd',
    'street_address' => 'Raymond James Stadium',
    'city' => 'Tampa',
    'country' => '1228',
    'state_province' => '1008',
    'postal_code' => '33607',
  ];
  $this
    ->fillBillingFields($billingValues);
  $this
    ->getSession()
    ->getPage()
    ->fillField('Card Number', '4222222222222220');
  $this
    ->getSession()
    ->getPage()
    ->fillField('Security Code', '123');
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption('credit_card_exp_date[M]', '11');
  $this_year = date('Y');
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption('credit_card_exp_date[Y]', $this_year + 1);
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Submit');
  $this
    ->htmlOutput();
  $this
    ->assertSession()
    ->pageTextContains('New submission added to CiviCRM Webform Test.');
  $cid = $this->utils
    ->wf_civicrm_api('Contact', 'getsingle', [
    'first_name' => $params['First Name'],
    'last_name' => $params['Last Name'],
  ])['contact_id'];

  // Ensure contribution is created on the contact.
  $contribution = $this->utils
    ->wf_civicrm_api('Contribution', 'getsingle', [
    'contact_id' => $cid,
  ]);
  $this
    ->assertEquals($contribution["total_amount"], '20.00');
  $customValues = $this->utils
    ->wf_civicrm_api('CustomValue', 'get', [
    'entity_id' => $cid,
  ])['values'];

  // Assert only 1 multivalue record is created.
  unset($customValues[$this->_customFields['month']]['latest'], $customValues[$this->_customFields['data']]['latest']);
  $monthValueCount = array_count_values($customValues[$this->_customFields['month']]);
  $dataValueCount = array_count_values($customValues[$this->_customFields['data']]);
  $this
    ->assertEquals($monthValueCount["January"], 1);
  $this
    ->assertEquals($dataValueCount["200"], 1);
}