You are here

public function ContactSubmissionTest::testSelectContactElement in Webform CiviCRM Integration 8.5

Test select contact widget for existingcontact element.

File

tests/src/FunctionalJavascript/ContactSubmissionTest.php, line 58

Class

ContactSubmissionTest
Tests submitting a Webform with CiviCRM and a single contact.

Namespace

Drupal\Tests\webform_civicrm\FunctionalJavascript

Code

public function testSelectContactElement() {

  // Create sample contacts.
  $this
    ->createGroupWithContacts();
  $this
    ->drupalLogin($this->rootUser);
  $this
    ->drupalGet(Url::fromRoute('entity.webform.civicrm', [
    'webform' => $this->webform
      ->id(),
  ]));
  $this
    ->enableCivicrmOnWebform();
  $this
    ->saveCiviCRMSettings();
  $this
    ->drupalGet($this->webform
    ->toUrl('edit-form'));

  // Edit contact element and enable select widget.
  $editContact = [
    'selector' => 'edit-webform-ui-elements-civicrm-1-contact-1-contact-existing-operations',
    'widget' => 'Select List',
    'filter' => [
      'group' => $this->group['id'],
    ],
  ];
  $this
    ->editContactElement($editContact);
  $this
    ->drupalGet($this->webform
    ->toUrl('canonical'));
  $this
    ->assertPageNoErrorMessages();

  // Check if no autocomplete is present on the page.
  $this
    ->assertSession()
    ->elementNotExists('css', '.token-input-list');

  // Asset if select element is rendered for contact element.
  $this
    ->assertSession()
    ->elementExists('css', 'select#edit-civicrm-1-contact-1-contact-existing');

  // Check if expected contacts are loaded in the select element.
  $loadedContacts = $this
    ->getOptions('Existing Contact');
  foreach ($this->contacts as $k => $value) {
    if ($k == 5) {
      $this
        ->assertArrayNotHasKey($value['id'], $loadedContacts, 'Unexpected contact loaded on the select element.');
    }
    else {
      $this
        ->assertArrayHasKey($value['id'], $loadedContacts, 'Expected contact not loaded on the select element.');
    }
  }
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption('Existing Contact', $this->contacts[1]['id']);
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();

  // Check if we can replace/overwrite the currently loaded values for First Name and Last Name
  $this
    ->addFieldValue('First Name', 'Jann');
  $this
    ->addFieldValue('Last Name', 'Arden');
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Submit');
  $this
    ->assertSession()
    ->pageTextContains('New submission added to CiviCRM Webform Test.');

  // Verify if the modified value is updated for the contact.
  $contact_result = $this->utils
    ->wf_civicrm_api('contact', 'get', [
    'sequential' => 1,
    'id' => $this->contacts[1]['id'],
  ]);
  $result_debug = var_export($contact_result, TRUE);
  $this
    ->assertEquals(1, $contact_result['count'], $result_debug);
  $this
    ->assertEquals('Jann', $contact_result['values'][0]['first_name'], $result_debug);
  $this
    ->assertEquals('Arden', $contact_result['values'][0]['last_name'], $result_debug);
}