You are here

public function MultiCustomFieldsSubmissionTest::testSubmitWebform in Webform CiviCRM Integration 8.5

Test submitting Custom Fields

File

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

Class

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

Namespace

Drupal\Tests\webform_civicrm\FunctionalJavascript

Code

public function testSubmitWebform() {
  $this->_totalMV = 5;
  $this
    ->createMultiValueCustomFields();
  $this
    ->drupalLogin($this->rootUser);
  $this
    ->drupalGet(Url::fromRoute('entity.webform.civicrm', [
    'webform' => $this->webform
      ->id(),
  ]));
  $this
    ->enableCivicrmOnWebform();
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption("number_of_contacts", $this->_totalMV);
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->htmlOutput();
  $this
    ->enableCustomFields(1);
  $this
    ->htmlOutput();
  $this
    ->getSession()
    ->getPage()
    ->clickLink('Contact 2');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->getSession()
    ->getPage()
    ->checkField('civicrm_2_contact_1_contact_existing');
  $this
    ->assertSession()
    ->checkboxChecked('civicrm_2_contact_1_contact_existing');
  $this
    ->enableCustomFields(2);
  $this
    ->getSession()
    ->getPage()
    ->clickLink('Contact 3');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->getSession()
    ->getPage()
    ->checkField('civicrm_3_contact_1_contact_existing');
  $this
    ->assertSession()
    ->checkboxChecked('civicrm_3_contact_1_contact_existing');
  $this
    ->enableCustomFields(3);
  $this
    ->saveCiviCRMSettings();
  $this
    ->drupalGet($this->webform
    ->toUrl('edit-form'));
  $editContact = [
    'selector' => 'edit-webform-ui-elements-civicrm-2-contact-1-contact-existing-operations',
    'widget' => 'Autocomplete',
    'default' => '- None -',
  ];
  $this
    ->editContactElement($editContact);
  $this
    ->drupalGet($this->webform
    ->toUrl('edit-form'));
  $editContact = [
    'selector' => 'edit-webform-ui-elements-civicrm-3-contact-1-contact-existing-operations',
    'widget' => 'Autocomplete',
    'default' => '- None -',
  ];
  $this
    ->editContactElement($editContact);

  //Create 2 contacts to fill on the webform.
  $this->_contact1 = $this
    ->createIndividual();
  $this->_contact2 = $this
    ->createIndividual();
  $this
    ->drupalGet($this->webform
    ->toUrl('canonical'));
  $this
    ->htmlOutput();
  $this
    ->assertPageNoErrorMessages();

  //Enter values for the custom fields and save.
  $months = [
    'Jan',
    'Feb',
    'March',
    'April',
    'May',
  ];
  $data = [
    100,
    200,
  ];
  $params = [];
  for ($c = 1; $c <= $this->_totalMV; $c++) {
    if ($c < 4) {
      for ($i = 1; $i <= $this->_totalMV; $i++) {
        $params["civicrm_{$c}_contact_{$i}_cg1_custom_1"] = $months[array_rand($months)];
        $params["civicrm_{$c}_contact_{$i}_cg1_custom_2"] = $data[array_rand($data)];
      }
    }
    else {
      $params["civicrm_{$c}_contact_1_contact_first_name"] = substr(sha1(rand()), 0, 7);
      $params["civicrm_{$c}_contact_1_contact_last_name"] = substr(sha1(rand()), 0, 7);
    }
  }
  $this
    ->submitWebform($params);
  $this
    ->verifyCustomValues($params);

  //Visit the webform again.
  $this
    ->drupalGet($this->webform
    ->toUrl('canonical'));
  $this
    ->htmlOutput();
  $this
    ->assertPageNoErrorMessages();

  // Verify if field values are loaded on the webform.
  foreach ($params as $key => $val) {
    if (strpos($key, 'civicrm_1_contact') !== false) {
      if (strpos($key, 'custom_1') !== false) {
        $this
          ->assertSession()
          ->fieldValueEquals($key, $val);
      }
      elseif (strpos($key, 'custom_2') !== false) {
        $this
          ->assertSession()
          ->elementExists('css', '[name="' . $key . '"][value=' . $val . ']')
          ->isChecked();
      }
    }
  }

  //Update values for the custom fields and save.
  $months = [
    'JanEdited',
    'FebEdited',
    'MarchEdited',
    'AprilEdited',
    'MayEdited',
  ];
  $data = [
    100,
    200,
  ];
  $params = [];
  for ($c = 1; $c <= $this->_totalMV; $c++) {
    if ($c < 4) {
      for ($i = 1; $i <= $this->_totalMV; $i++) {
        $params["civicrm_{$c}_contact_{$i}_cg1_custom_1"] = $months[array_rand($months)];
        $params["civicrm_{$c}_contact_{$i}_cg1_custom_2"] = $data[array_rand($data)];
      }
    }
    else {
      $params["civicrm_{$c}_contact_1_contact_first_name"] = substr(sha1(rand()), 0, 7);
      $params["civicrm_{$c}_contact_1_contact_last_name"] = substr(sha1(rand()), 0, 7);
    }
  }
  $this
    ->submitWebform($params);

  // Check if updated values are stored on the contact.
  $this
    ->verifyCustomValues($params);
}