You are here

public function ContactSubmissionTest::testStaticAndAutocompleteOnContactElement in Webform CiviCRM Integration 8.5

Test contact submission using static and autocomplete widget.

File

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

Class

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

Namespace

Drupal\Tests\webform_civicrm\FunctionalJavascript

Code

public function testStaticAndAutocompleteOnContactElement() {
  $contact = $this
    ->createIndividual();
  $this
    ->drupalLogin($this->rootUser);
  $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
    ->enableCivicrmOnWebform();
  $this
    ->getSession()
    ->getPage()
    ->clickLink('Additional Settings');
  $this
    ->assertSession()
    ->elementTextContains('css', '#edit-checksum-text', 'To have this form auto-filled for anonymous users, enable the "Existing Contact" field for Contact 1 and send the following link from CiviMail');
  $this
    ->saveCiviCRMSettings();
  $this
    ->drupalGet($this->webform
    ->toUrl('canonical', [
    'query' => [
      'cid1' => $contact['id'],
    ],
  ]));
  $this
    ->assertPageNoErrorMessages();

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

  // Check if name fields are pre populated with existing values.
  $this
    ->assertSession()
    ->fieldValueEquals('First Name', $contact['first_name']);
  $this
    ->assertSession()
    ->fieldValueEquals('Last Name', $contact['last_name']);

  // Update the name to some other value.
  $this
    ->addFieldValue('First Name', 'Alanis');
  $this
    ->addFieldValue('Last Name', 'Morissette');
  $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' => $contact['id'],
  ]);
  $result_debug = var_export($contact_result, TRUE);
  $this
    ->assertArrayHasKey('count', $contact_result, $result_debug);
  $this
    ->assertEquals(1, $contact_result['count'], $result_debug);
  $this
    ->assertEquals('Alanis', $contact_result['values'][0]['first_name'], $result_debug);
  $this
    ->assertEquals('Morissette', $contact_result['values'][0]['last_name'], $result_debug);

  // Enable Autocomplete on the contact Element.
  $this
    ->drupalGet($this->webform
    ->toUrl('edit-form'));

  // Assert CiviCRM elements are not loaded on Add Element form.
  $this
    ->assertSession()
    ->elementExists('css', '#webform-ui-add-element')
    ->click();
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->htmlOutput();
  $this
    ->assertSession()
    ->elementNotExists('css', '[data-drupal-selector="edit-elements-civicrm-contact"]');
  $this
    ->assertSession()
    ->elementNotExists('css', '[data-drupal-selector="edit-elements-civicrm-options"]');
  $this
    ->assertSession()
    ->elementNotExists('css', '[data-drupal-selector="edit-elements-civicrm-select"]');
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Close');
  $contactElementEdit = $this
    ->assertSession()
    ->elementExists('css', '[data-drupal-selector="edit-webform-ui-elements-civicrm-1-contact-1-contact-existing-operations"] a.webform-ajax-link');
  $contactElementEdit
    ->click();
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->htmlOutput();
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->elementExists('css', '[data-drupal-selector="edit-form"]')
    ->click();
  $this
    ->assertSession()
    ->waitForField('properties[widget]');
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption('Form Widget', 'Autocomplete');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->waitForElementVisible('css', '[data-drupal-selector="edit-properties-search-prompt"]');
  $this
    ->addFieldValue('Search Prompt', '- Select Contact -');
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Save');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->pageTextContains('Existing Contact has been updated');
  $this
    ->drupalGet($this->webform
    ->toUrl('canonical'));
  $this
    ->assertPageNoErrorMessages();

  // Check if autocomplete is present on the page.
  $this
    ->assertSession()
    ->elementExists('css', '.token-input-list');
  $currentUserUF = $this
    ->getUFMatchRecord($this->rootUser
    ->id());
  $currentUserDisplayName = $this->utils
    ->wf_civicrm_api('contact', 'getvalue', [
    'id' => $currentUserUF['contact_id'],
    'return' => "display_name",
  ]);
  $this
    ->assertSession()
    ->elementTextContains('css', '.token-input-token', $currentUserDisplayName);

  // Clear the existing selection.
  $this
    ->assertSession()
    ->elementExists('css', '.token-input-delete-token')
    ->click();
  $this
    ->fillContactAutocomplete('token-input-edit-civicrm-1-contact-1-contact-existing', $contact_result['values'][0]['first_name']);
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertFieldValue('edit-civicrm-1-contact-1-contact-first-name', $contact_result['values'][0]['first_name']);
  $this
    ->assertFieldValue('edit-civicrm-1-contact-1-contact-last-name', $contact_result['values'][0]['last_name']);

  // Update the name to some other value.
  $this
    ->addFieldValue('First Name', 'Frederick-Edited');
  $this
    ->addFieldValue('Last Name', 'Pabst-Edited');
  $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_result2 = $this->utils
    ->wf_civicrm_api('contact', 'get', [
    'sequential' => 1,
    'id' => $contact_result['id'],
  ]);
  $result_debug = var_export($contact_result2, TRUE);
  $this
    ->assertArrayHasKey('count', $contact_result2, $result_debug);
  $this
    ->assertEquals(1, $contact_result2['count'], $result_debug);
  $this
    ->assertEquals('Frederick-Edited', $contact_result2['values'][0]['first_name'], $result_debug);
  $this
    ->assertEquals('Pabst-Edited', $contact_result2['values'][0]['last_name'], $result_debug);
}