Merge pull request #12290 from eileenmcnaughton/export
[civicrm-core.git] / tests / phpunit / CRM / Case / XMLProcessor / ProcessTest.php
index 52413195309f63d32a79955912a86a7301420c43..2d30c0bc82f2af71c45f5cdcf512701b48839a2a 100644 (file)
@@ -11,29 +11,31 @@ class CRM_Case_XMLProcessor_ProcessTest extends CiviCaseTestCase {
     parent::setUp();
 
     $this->defaultAssigneeOptionsValues = [];
-    $this->assigneeContactId = $this->individualCreate();
-    $this->targetContactId = $this->individualCreate();
 
-    $this->setUpDefaultAssigneeOptions();
-    $this->setUpRelationship();
-
-    $activityTypeXml = '<activity-type><name>Open Case</name></activity-type>';
-    $this->activityTypeXml = new SimpleXMLElement($activityTypeXml);
-    $this->params = [
-      'activity_date_time' => date('Ymd'),
-      'caseID' => $this->caseTypeId,
-      'clientID' => $this->targetContactId,
-      'creatorID' => $this->_loggedInUser,
-    ];
+    $this->setupContacts();
+    $this->setupDefaultAssigneeOptions();
+    $this->setupRelationships();
+    $this->setupActivityDefinitions();
 
     $this->process = new CRM_Case_XMLProcessor_Process();
   }
 
+  /**
+   * Creates sample contacts.
+   */
+  protected function setUpContacts() {
+    $this->contacts = [
+      'ana' => $this->individualCreate(),
+      'beto' => $this->individualCreate(),
+      'carlos' => $this->individualCreate(),
+    ];
+  }
+
   /**
    * Adds the default assignee group and options to the test database.
    * It also stores the IDs of the options in an index.
    */
-  protected function setUpDefaultAssigneeOptions() {
+  protected function setupDefaultAssigneeOptions() {
     $options = [
       'NONE', 'BY_RELATIONSHIP', 'SPECIFIC_CONTACT', 'USER_CREATING_THE_CASE'
     ];
@@ -56,47 +58,114 @@ class CRM_Case_XMLProcessor_ProcessTest extends CiviCaseTestCase {
   /**
    * Adds a relationship between the activity's target contact and default assignee.
    */
-  protected function setUpRelationship() {
-    $this->assignedRelationshipType = 'Instructor of';
-    $this->unassignedRelationshipType = 'Employer of';
-
-    $assignedRelationshipTypeId = $this->relationshipTypeCreate([
-      'contact_type_a' => 'Individual',
-      'contact_type_b' => 'Individual',
-      'name_a_b' => 'Pupil of',
-      'name_b_a' => $this->assignedRelationshipType,
-    ]);
-    $this->relationshipTypeCreate([
-      'name_a_b' => 'Employee of',
-      'name_b_a' => $this->unassignedRelationshipType,
-    ]);
-    $this->callAPISuccess('Relationship', 'create', [
-      'contact_id_a' => $this->assigneeContactId,
-      'contact_id_b' => $this->targetContactId,
-      'relationship_type_id' => $assignedRelationshipTypeId
-    ]);
+  protected function setupRelationships() {
+    $this->relationships = [
+      'ana_is_pupil_of_beto' => [
+        'type_id' => NULL,
+        'name_a_b' => 'Pupil of',
+        'name_b_a' => 'Instructor',
+        'contact_id_a' => $this->contacts['ana'],
+        'contact_id_b' => $this->contacts['beto']
+      ],
+      'ana_is_spouse_of_carlos' => [
+        'type_id' => NULL,
+        'name_a_b' => 'Spouse of',
+        'name_b_a' => 'Spouse of',
+        'contact_id_a' => $this->contacts['ana'],
+        'contact_id_b' => $this->contacts['carlos']
+      ],
+      'unassigned_employee' => [
+        'type_id' => NULL,
+        'name_a_b' => 'Employee of',
+        'name_b_a' => 'Employer'
+      ],
+    ];
+
+    foreach ($this->relationships as $name => &$relationship) {
+      $relationship['type_id'] = $this->relationshipTypeCreate([
+        'contact_type_a' => 'Individual',
+        'contact_type_b' => 'Individual',
+        'name_a_b' => $relationship['name_a_b'],
+        'label_a_b' => $relationship['name_a_b'],
+        'name_b_a' => $relationship['name_b_a'],
+        'label_b_a' => $relationship['name_b_a']
+      ]);
+
+      if (isset($relationship['contact_id_a'])) {
+        $this->callAPISuccess('Relationship', 'create', [
+          'contact_id_a' => $relationship['contact_id_a'],
+          'contact_id_b' => $relationship['contact_id_b'],
+          'relationship_type_id' => $relationship['type_id'],
+        ]);
+      }
+    }
   }
 
   /**
-   * Tests the creation of activities with default assignee by relationship.
+   * Defines the the activity parameters and XML definitions. These can be used
+   * to create the activity.
+   */
+  protected function setupActivityDefinitions() {
+    $activityTypeXml = '<activity-type><name>Open Case</name></activity-type>';
+    $this->activityTypeXml = new SimpleXMLElement($activityTypeXml);
+    $this->activityParams = [
+      'activity_date_time' => date('Ymd'),
+      'caseID' => $this->caseTypeId,
+      'clientID' => $this->contacts['ana'],
+      'creatorID' => $this->_loggedInUser,
+    ];
+  }
+
+  /**
+   * Tests the creation of activities where the default assignee should be the
+   * target contact's instructor. Beto is the instructor for Ana.
    */
   public function testCreateActivityWithDefaultContactByRelationship() {
+    $relationship = $this->relationships['ana_is_pupil_of_beto'];
+    $this->activityTypeXml->default_assignee_type = $this->defaultAssigneeOptionsValues['BY_RELATIONSHIP'];
+    $this->activityTypeXml->default_assignee_relationship = "{$relationship['type_id']}_b_a";
+
+    $this->process->createActivity($this->activityTypeXml, $this->activityParams);
+    $this->assertActivityAssignedToContactExists($this->contacts['beto']);
+  }
+
+  /**
+   * Tests when the default assignee relationship exists, but in the other direction only.
+   * Ana is a pupil, but has no pupils related to her.
+   */
+  public function testCreateActivityWithDefaultContactByRelationshipMissing() {
+    $relationship = $this->relationships['ana_is_pupil_of_beto'];
+    $this->activityTypeXml->default_assignee_type = $this->defaultAssigneeOptionsValues['BY_RELATIONSHIP'];
+    $this->activityTypeXml->default_assignee_relationship = "{$relationship['type_id']}_a_b";
+
+    $this->process->createActivity($this->activityTypeXml, $this->activityParams);
+    $this->assertActivityAssignedToContactExists(NULL);
+  }
+
+  /**
+   * Tests when the the default assignee relationship exists and is a bidirectional
+   * relationship. Ana and Carlos are spouses.
+   */
+  public function testCreateActivityWithDefaultContactByRelationshipBidirectional() {
+    $relationship = $this->relationships['ana_is_spouse_of_carlos'];
+    $this->activityParams['clientID'] = $this->contacts['carlos'];
     $this->activityTypeXml->default_assignee_type = $this->defaultAssigneeOptionsValues['BY_RELATIONSHIP'];
-    $this->activityTypeXml->default_assignee_relationship = $this->assignedRelationshipType;
+    $this->activityTypeXml->default_assignee_relationship = "{$relationship['type_id']}_a_b";
 
-    $this->process->createActivity($this->activityTypeXml, $this->params);
-    $this->assertActivityAssignedToContactExists($this->assigneeContactId);
+    $this->process->createActivity($this->activityTypeXml, $this->activityParams);
+    $this->assertActivityAssignedToContactExists($this->contacts['ana']);
   }
 
   /**
-   * Tests the creation of activities with default assignee by relationship,
-   * but the target contact doesn't have any relationship of the selected type.
+   * Tests when the default assignee relationship does not exist. Ana is not an
+   * employee for anyone.
    */
   public function testCreateActivityWithDefaultContactByRelationButTheresNoRelationship() {
+    $relationship = $this->relationships['unassigned_employee'];
     $this->activityTypeXml->default_assignee_type = $this->defaultAssigneeOptionsValues['BY_RELATIONSHIP'];
-    $this->activityTypeXml->default_assignee_relationship = $this->unassignedRelationshipType;
+    $this->activityTypeXml->default_assignee_relationship = "{$relationship['type_id']}_b_a";
 
-    $this->process->createActivity($this->activityTypeXml, $this->params);
+    $this->process->createActivity($this->activityTypeXml, $this->activityParams);
     $this->assertActivityAssignedToContactExists(NULL);
   }
 
@@ -105,10 +174,10 @@ class CRM_Case_XMLProcessor_ProcessTest extends CiviCaseTestCase {
    */
   public function testCreateActivityAssignedToSpecificContact() {
     $this->activityTypeXml->default_assignee_type = $this->defaultAssigneeOptionsValues['SPECIFIC_CONTACT'];
-    $this->activityTypeXml->default_assignee_contact = $this->assigneeContactId;
+    $this->activityTypeXml->default_assignee_contact = $this->contacts['carlos'];
 
-    $this->process->createActivity($this->activityTypeXml, $this->params);
-    $this->assertActivityAssignedToContactExists($this->assigneeContactId);
+    $this->process->createActivity($this->activityTypeXml, $this->activityParams);
+    $this->assertActivityAssignedToContactExists($this->contacts['carlos']);
   }
 
   /**
@@ -119,7 +188,7 @@ class CRM_Case_XMLProcessor_ProcessTest extends CiviCaseTestCase {
     $this->activityTypeXml->default_assignee_type = $this->defaultAssigneeOptionsValues['SPECIFIC_CONTACT'];
     $this->activityTypeXml->default_assignee_contact = 987456321;
 
-    $this->process->createActivity($this->activityTypeXml, $this->params);
+    $this->process->createActivity($this->activityTypeXml, $this->activityParams);
     $this->assertActivityAssignedToContactExists(NULL);
   }
 
@@ -130,7 +199,7 @@ class CRM_Case_XMLProcessor_ProcessTest extends CiviCaseTestCase {
   public function testCreateActivityAssignedToUserCreatingTheCase() {
     $this->activityTypeXml->default_assignee_type = $this->defaultAssigneeOptionsValues['USER_CREATING_THE_CASE'];
 
-    $this->process->createActivity($this->activityTypeXml, $this->params);
+    $this->process->createActivity($this->activityTypeXml, $this->activityParams);
     $this->assertActivityAssignedToContactExists($this->_loggedInUser);
   }
 
@@ -140,7 +209,7 @@ class CRM_Case_XMLProcessor_ProcessTest extends CiviCaseTestCase {
   public function testCreateActivityAssignedNoUser() {
     $this->activityTypeXml->default_assignee_type = $this->defaultAssigneeOptionsValues['NONE'];
 
-    $this->process->createActivity($this->activityTypeXml, $this->params);
+    $this->process->createActivity($this->activityTypeXml, $this->activityParams);
     $this->assertActivityAssignedToContactExists(NULL);
   }
 
@@ -148,7 +217,7 @@ class CRM_Case_XMLProcessor_ProcessTest extends CiviCaseTestCase {
    * Tests the creation of activities when the default assignee is set to NONE.
    */
   public function testCreateActivityWithNoDefaultAssigneeOption() {
-    $this->process->createActivity($this->activityTypeXml, $this->params);
+    $this->process->createActivity($this->activityTypeXml, $this->activityParams);
     $this->assertActivityAssignedToContactExists(NULL);
   }
 
@@ -161,7 +230,7 @@ class CRM_Case_XMLProcessor_ProcessTest extends CiviCaseTestCase {
   protected function assertActivityAssignedToContactExists($assigneeContactId) {
     $expectedContact = $assigneeContactId === NULL ? [] : [$assigneeContactId];
     $result = $this->callAPISuccess('Activity', 'get', [
-      'target_contact_id' => $this->targetContactId,
+      'target_contact_id' => $this->activityParams['clientID'],
       'return' => ['assignee_contact_id']
     ]);
     $activity = CRM_Utils_Array::first($result['values']);