You are here

function MembershipSubmissionTest::testSubmitMembershipAutoRenew in Webform CiviCRM Integration 8.5

File

tests/src/FunctionalJavascript/MembershipSubmissionTest.php, line 30

Class

MembershipSubmissionTest
Tests submitting a Webform with CiviCRM: Contact with Membership (Free)

Namespace

Drupal\Tests\webform_civicrm\FunctionalJavascript

Code

function testSubmitMembershipAutoRenew() {
  $this
    ->createMembershipType(1, TRUE);
  $payment_processor = $this
    ->createPaymentProcessor();
  $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('Memberships');

  // Configure Membership tab.
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption('membership_1_number_of_membership', 1);
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption('civicrm_1_membership_1_membership_membership_type_id', '- User Select -');
  $this
    ->htmlOutput();

  // $this->createScreenshot($this->htmlOutputDirectory . '/membership_page_settings.png');
  // Configure Contribution tab and enable recurring.
  $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
    ->getSession()
    ->getPage()
    ->selectFieldOption('Currency', 'USD');
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption('Financial Type', 1);
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption('Frequency of Installments', 'year');
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption('Payment Processor', $payment_processor['id']);

  // $this->createScreenshot($this->htmlOutputDirectory . '/membership_page_settings_before_save.png');
  $this
    ->enableBillingSection();
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Save Settings');
  $this
    ->assertSession()
    ->pageTextContains('Saved CiviCRM settings');
  $this
    ->drupalGet($this->webform
    ->toUrl('canonical'));
  $this
    ->assertPageNoErrorMessages();

  // $this->createScreenshot($this->htmlOutputDirectory . '/membership_page1.png');
  $this
    ->getSession()
    ->getPage()
    ->fillField('First Name', 'Frederick');
  $this
    ->getSession()
    ->getPage()
    ->fillField('Last Name', 'Pabst');
  $this
    ->getSession()
    ->getPage()
    ->fillField('Email', 'fred@example.com');
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption('civicrm_1_membership_1_membership_membership_type_id', '1');
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Next >');
  $this
    ->assertSession()
    ->elementExists('css', '#wf-crm-billing-items');
  $this
    ->htmlOutput();
  $this
    ->assertSession()
    ->elementTextContains('css', '#wf-crm-billing-total', '1.00');

  // Wait for the credit card form to load in.
  $this
    ->assertSession()
    ->waitForField('credit_card_number');
  $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);
  $billingValues = [
    'first_name' => 'Frederick',
    'last_name' => 'Pabst',
    'street_address' => '123 Milwaukee Ave',
    'city' => 'Milwaukee',
    'country' => '1228',
    'state_province' => '1048',
    'postal_code' => '53177',
  ];
  $this
    ->fillBillingFields($billingValues);
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Submit');
  $this
    ->htmlOutput();
  $this
    ->assertSession()
    ->pageTextContains('New submission added to CiviCRM Webform Test.');
  $this
    ->assertPageNoErrorMessages();

  // Assert if recur is attached to the created membership.
  $utils = \Drupal::service('webform_civicrm.utils');
  $api_result = $utils
    ->wf_civicrm_api('membership', 'get', [
    'sequential' => 1,
    'return' => 'contribution_recur_id',
  ]);
  $membership = reset($api_result['values']);
  $this
    ->assertNotEmpty($membership['contribution_recur_id']);

  // Let's make sure we have a Contribution by ensuring we have a Transaction ID
  $api_result = $utils
    ->wf_civicrm_api('contribution', 'get', [
    'sequential' => 1,
  ]);
  $contribution = reset($api_result['values']);
  $this
    ->assertNotEmpty($contribution['trxn_id']);
  $this
    ->assertEquals('1.00', $contribution['total_amount']);
}