You are here

public function ContactRelationshipTest::testRelationshipRemoval in Webform CiviCRM Integration 8.5

Test removal of relationships.

File

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

Class

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

Namespace

Drupal\Tests\webform_civicrm\FunctionalJavascript

Code

public function testRelationshipRemoval() {
  $this
    ->drupalLogin($this->adminUser);
  $this
    ->drupalGet(Url::fromRoute('entity.webform.civicrm', [
    'webform' => $this->webform
      ->id(),
  ]));
  $this
    ->enableCivicrmOnWebform();
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption('number_of_contacts', 2);
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->htmlOutput();
  $this
    ->getSession()
    ->getPage()
    ->clickLink('2. Contact 2');
  $this
    ->getSession()
    ->getPage()
    ->checkField("civicrm_2_contact_1_contact_existing");
  $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[]', 'create_civicrm_webform_element');
  $this
    ->saveCiviCRMSettings();
  $this
    ->drupalGet($this->webform
    ->toUrl('canonical'));
  $this
    ->assertPageNoErrorMessages();
  $this
    ->getSession()
    ->getPage()
    ->fillField('civicrm_1_contact_1_contact_first_name', 'Frederick');
  $this
    ->getSession()
    ->getPage()
    ->fillField('civicrm_1_contact_1_contact_last_name', 'Pabst');
  $this
    ->getSession()
    ->getPage()
    ->fillField('civicrm_2_contact_1_contact_first_name', 'Mark');
  $this
    ->getSession()
    ->getPage()
    ->fillField('civicrm_2_contact_1_contact_last_name', 'Anthony');
  $this
    ->getSession()
    ->getPage()
    ->checkField("Child of");
  $this
    ->getSession()
    ->getPage()
    ->checkField("Partner of");
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Submit');
  $this
    ->assertPageNoErrorMessages();
  $this
    ->assertSession()
    ->pageTextContains('New submission added to CiviCRM Webform Test.');

  //Assert if relationship was created.
  $contact1 = $this->utils
    ->wf_civicrm_api('Contact', 'get', [
    'sequential' => 1,
    'first_name' => 'Frederick',
    'last_name' => 'Pabst',
  ]);
  $this
    ->assertEquals(1, $contact1['count']);
  $contact2 = $this->utils
    ->wf_civicrm_api('Contact', 'get', [
    'sequential' => 1,
    'first_name' => 'Mark',
    'last_name' => 'Anthony',
  ]);
  $this
    ->assertEquals(1, $contact2['count']);
  $contact1 = reset($contact1['values']);
  $contact2 = reset($contact2['values']);
  $relationships = $this->utils
    ->wf_civicrm_api('Relationship', 'get', [
    'sequential' => 1,
    'contact_id_b' => $contact1['contact_id'],
    'is_active' => 1,
  ]);

  //Check only 2 relationships are created b/w the contacts.
  $this
    ->assertEquals(2, $relationships['count']);
  $relationships = $relationships['values'];
  $partnerTypeID = $this->utils
    ->wf_civicrm_api('RelationshipType', 'getvalue', [
    'return' => "id",
    'name_a_b' => "Partner of",
  ]);
  $childTypeID = $this->utils
    ->wf_civicrm_api('RelationshipType', 'getvalue', [
    'return' => "id",
    'name_a_b' => "Child of",
  ]);
  $contactRelTypes = array_column($relationships, 'relationship_type_id');
  $this
    ->assertEquals($contact2['id'], $relationships[0]['contact_id_a']);
  $this
    ->assertEquals($contact2['id'], $relationships[1]['contact_id_a']);
  $this
    ->assertTrue(in_array($childTypeID, $contactRelTypes));
  $this
    ->assertTrue(in_array($partnerTypeID, $contactRelTypes));

  // Visit the webform with cid2 id in the url.
  $this
    ->drupalGet($this->webform
    ->toUrl('canonical', [
    'query' => [
      'cid1' => $contact1['id'],
      'cid2' => $contact2['id'],
    ],
  ]));
  $this
    ->assertSession()
    ->waitForField('First Name');
  $this
    ->createScreenshot($this->htmlOutputDirectory . '/relationship_selection.png');

  //Make sure the checkbox are enabled by default.
  $this
    ->assertSession()
    ->checkboxChecked("Child of");
  $this
    ->assertSession()
    ->checkboxChecked("Partner of");

  // Remove Partner of relationship with the contact.
  $this
    ->getSession()
    ->getPage()
    ->uncheckField("Partner of");
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Submit');
  $this
    ->assertPageNoErrorMessages();
  $this
    ->assertSession()
    ->pageTextContains('New submission added to CiviCRM Webform Test.');
  $relationships = $this->utils
    ->wf_civicrm_api('Relationship', 'get', [
    'sequential' => 1,
    'contact_id_b' => $contact1['contact_id'],
    'options' => [
      'sort' => "is_active DESC",
    ],
  ]);
  $this
    ->assertEquals(2, $relationships['count']);
  $relationships = $relationships['values'];
  $this
    ->assertEquals($contact2['id'], $relationships[0]['contact_id_a']);
  $this
    ->assertEquals($childTypeID, $relationships[0]['relationship_type_id']);
  $this
    ->assertEquals(1, $relationships[0]['is_active']);

  // Check if Partner relationship is expired.
  $this
    ->assertEquals($contact2['id'], $relationships[1]['contact_id_a']);
  $this
    ->assertEquals($partnerTypeID, $relationships[1]['relationship_type_id']);
  $this
    ->assertEquals(0, $relationships[1]['is_active']);
}