You are here

public function ContactRelationshipTest::testSubmitWebform in Webform CiviCRM Integration 8.5

Test submitting Contact - Matching Rule

File

tests/src/FunctionalJavascript/ContactRelationshipTest.php, line 160

Class

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

Namespace

Drupal\Tests\webform_civicrm\FunctionalJavascript

Code

public function testSubmitWebform() {
  $this
    ->createContactSubtype();
  $this
    ->createRelationshipType();
  $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
    ->enableCivicrmOnWebform();
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption('number_of_contacts', 2);
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->htmlOutput();

  // Configuring Contact 1 - Student
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption('civicrm_1_contact_1_contact_contact_sub_type[]', 'Student');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->htmlOutput();

  // Configuring Contact 2 - School (Organization)
  $this
    ->getSession()
    ->getPage()
    ->clickLink('2. Contact 2');
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption('2_contact_type', 'Organization');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->htmlOutput();
  $this
    ->getSession()
    ->getPage()
    ->checkField("civicrm_2_contact_1_contact_existing");
  $this
    ->getSession()
    ->getPage()
    ->checkField("civicrm_2_contact_1_contact_organization_name");
  $this
    ->assertSession()
    ->checkboxChecked("civicrm_2_contact_1_contact_organization_name");
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption('contact_2_number_of_relationship', 'Yes');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->htmlOutput();
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption('civicrm_2_contact_1_relationship_relationship_type_id[]', 'School is');
  $this
    ->saveCiviCRMSettings();
  $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('Organization Name', 'Western Canada High');
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Submit');
  $this
    ->assertPageNoErrorMessages();
  $this
    ->assertSession()
    ->pageTextContains('New submission added to CiviCRM Webform Test.');

  // Note: Frederick is contact_id=3 (1=default org; 2=the drupal user) and Western Canada High tis contact_id=4
  $api_result = $this->utils
    ->wf_civicrm_api('Contact', 'get', [
    'sequential' => 1,
    'first_name' => 'Frederick',
    'last_name' => 'Pabst',
  ]);
  $this
    ->assertEquals(1, $api_result['count']);
  $student = reset($api_result['values']);
  $this
    ->assertEquals('Student', implode($student['contact_sub_type']));

  // Check that the relationship is created:
  $api_result = $this->utils
    ->wf_civicrm_api('Relationship', 'get', [
    'sequential' => 1,
    'contact_id_b' => $student['contact_id'],
  ]);
  $relationship = reset($api_result['values']);

  // throw new \Exception(var_export($relationship, TRUE));
  // This is the school:
  $api_result = $this->utils
    ->wf_civicrm_api('Contact', 'get', [
    'sequential' => 1,
    'contact_id' => $relationship['contact_id_a'],
  ]);
  $contact = reset($api_result['values']);
  $this
    ->assertEquals('Western Canada High', $contact['organization_name']);

  // This is the relationship type:
  $api_result = $this->utils
    ->wf_civicrm_api('RelationshipType', 'get', [
    'sequential' => 1,
    'id' => $relationship['relationship_type_id'],
  ]);

  // throw new \Exception(var_export($api_result, TRUE));
  $relationshipType = reset($api_result['values']);

  // throw new \Exception(var_export($relationshipType, TRUE));
  $this
    ->assertEquals('Student of', $relationshipType['label_b_a']);
  $this
    ->drupalLogin($this->adminUser);

  // Edit Contact Element and enable select widget.
  $this
    ->drupalGet($this->webform
    ->toUrl('edit-form'));
  $contactElementEdit = $this
    ->assertSession()
    ->elementExists('css', '[data-drupal-selector="edit-webform-ui-elements-civicrm-2-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-contact-defaults"]')
    ->click();
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption('Set default contact from', 'Relationship to...');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $loadedRelationshipTypes = $this
    ->getOptions('Specify Relationship(s)');
  $type = array_search('School is Contact 1', $loadedRelationshipTypes);
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption('Specify Relationship(s)', $type);
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Save');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->drupalGet($this->webform
    ->toUrl('canonical', [
    'query' => [
      'cid1' => $student['contact_id'],
    ],
  ]));
  $this
    ->assertPageNoErrorMessages();

  // Check if School name is pre-populated.
  $this
    ->assertSession()
    ->fieldValueEquals('Organization Name', 'Western Canada High');
}