View source  
  <?php
namespace Drupal\Tests\webform_civicrm\FunctionalJavascript;
use Drupal\Core\Url;
final class ContactDedupeTest extends WebformCivicrmTestBase {
  private function createContactSubtype() {
    $params = [
      'name' => "Student",
      'is_active' => 1,
      'parent_id' => "Individual",
    ];
    $utils = \Drupal::service('webform_civicrm.utils');
    $result = $utils
      ->wf_civicrm_api('ContactType', 'create', $params);
    $this
      ->assertEquals(0, $result['is_error']);
    $this
      ->assertEquals(1, $result['count']);
  }
  
  public function testSubmitWebform() {
    $this
      ->createContactSubtype();
    $this
      ->drupalLogin($this->adminUser);
    $this
      ->drupalGet(Url::fromRoute('entity.webform.civicrm', [
      'webform' => $this->webform
        ->id(),
    ]));
    
    $this
      ->assertSession()
      ->waitForText('Enable CiviCRM Processing');
    $this
      ->assertSession()
      ->waitForField('nid');
    $this
      ->getSession()
      ->getPage()
      ->checkField('nid');
    $this
      ->getSession()
      ->getPage()
      ->selectFieldOption('civicrm_1_contact_1_contact_contact_sub_type[]', 'Student');
    $this
      ->assertSession()
      ->assertWaitOnAjaxRequest();
    
    $this
      ->getSession()
      ->getPage()
      ->selectFieldOption('contact_1_number_of_email', 1);
    $this
      ->assertSession()
      ->assertWaitOnAjaxRequest();
    $this
      ->assertSession()
      ->checkboxChecked("civicrm_1_contact_1_email_email");
    $this
      ->getSession()
      ->getPage()
      ->selectFieldOption('civicrm_1_contact_1_email_location_type_id', 'Main');
    $this
      ->htmlOutput();
    $this
      ->getSession()
      ->getPage()
      ->pressButton('Save Settings');
    $this
      ->assertSession()
      ->pageTextContains('Saved CiviCRM settings');
    $this
      ->assertPageNoErrorMessages();
    $this
      ->drupalLogout();
    $this
      ->drupalGet($this->webform
      ->toUrl('canonical'));
    $this
      ->assertPageNoErrorMessages();
    $this
      ->assertSession()
      ->waitForField('First Name');
    $this
      ->getSession()
      ->getPage()
      ->fillField('First Name', 'Frederick');
    $this
      ->getSession()
      ->getPage()
      ->fillField('Last Name', 'Pabst');
    $this
      ->getSession()
      ->getPage()
      ->fillField('Email', 'frederick@pabst.io');
    $this
      ->getSession()
      ->getPage()
      ->pressButton('Submit');
    $this
      ->assertPageNoErrorMessages();
    $this
      ->assertSession()
      ->pageTextContains('New submission added to CiviCRM Webform Test.');
    
    $utils = \Drupal::service('webform_civicrm.utils');
    $api_result = $utils
      ->wf_civicrm_api('Contact', 'get', [
      'sequential' => 1,
      'first_name' => 'Frederick',
      'last_name' => 'Pabst',
    ]);
    $this
      ->assertEquals(1, $api_result['count']);
    $contact = reset($api_result['values']);
    $this
      ->assertEquals('Student', implode($contact['contact_sub_type']));
    $api_result = $utils
      ->wf_civicrm_api('Email', 'get', [
      'contact_id' => $contact['id'],
      'sequential' => 1,
    ]);
    $email = reset($api_result['values']);
    $this
      ->assertEquals('frederick@pabst.io', $email['email']);
    
    $this
      ->drupalGet($this->webform
      ->toUrl('canonical'));
    $this
      ->assertPageNoErrorMessages();
    $this
      ->assertSession()
      ->waitForField('First Name');
    $this
      ->getSession()
      ->getPage()
      ->fillField('First Name', 'Frederick');
    $this
      ->getSession()
      ->getPage()
      ->fillField('Last Name', 'Pabsted');
    $this
      ->getSession()
      ->getPage()
      ->fillField('Email', 'frederick@pabst.io');
    $this
      ->getSession()
      ->getPage()
      ->pressButton('Submit');
    $this
      ->assertPageNoErrorMessages();
    $this
      ->assertSession()
      ->pageTextContains('New submission added to CiviCRM Webform Test.');
    
    $api_result = $utils
      ->wf_civicrm_api('Contact', 'get', [
      'sequential' => 1,
      'contact_id' => $contact['id'],
    ]);
    $contact = reset($api_result['values']);
    $this
      ->assertEquals('Pabsted', $contact['last_name']);
    
    $this
      ->assertEquals('Frederick', $contact['first_name']);
    $this
      ->assertEquals('Student', implode($contact['contact_sub_type']));
    $api_result = $utils
      ->wf_civicrm_api('Email', 'get', [
      'contact_id' => $contact['id'],
      'sequential' => 1,
    ]);
    $email = reset($api_result['values']);
    $this
      ->assertEquals('frederick@pabst.io', $email['email']);
  }
}