More phpcbs code cleanups
[civicrm-core.git] / tests / phpunit / CRM / Case / XMLProcessor / ProcessTest.php
index bc24f4c670c6113c20b4a761e597b4551f44ea90..c83fcfa9769ec9608a797ff499175f28b1d4f540 100644 (file)
@@ -7,7 +7,7 @@ require_once 'CiviTest/CiviCaseTestCase.php';
  */
 class CRM_Case_XMLProcessor_ProcessTest extends CiviCaseTestCase {
 
-  public function setUp() {
+  public function setUp(): void {
     parent::setUp();
 
     $this->defaultAssigneeOptionsValues = [];
@@ -21,7 +21,7 @@ class CRM_Case_XMLProcessor_ProcessTest extends CiviCaseTestCase {
     $this->process = new CRM_Case_XMLProcessor_Process();
   }
 
-  public function tearDown() {
+  public function tearDown(): void {
     $this->deleteMoreRelationshipTypes();
 
     parent::tearDown();
@@ -178,6 +178,7 @@ class CRM_Case_XMLProcessor_ProcessTest extends CiviCaseTestCase {
     $this->activityTypeXml = new SimpleXMLElement($activityTypeXml);
     $this->activityParams = [
       'activity_date_time' => date('Ymd'),
+      // @todo This seems wrong, it just happens to work out because both caseId and caseTypeId equal 1 in the stock setup here.
       'caseID' => $this->caseTypeId,
       'clientID' => $this->contacts['ana'],
       'creatorID' => $this->_loggedInUser,
@@ -197,6 +198,97 @@ class CRM_Case_XMLProcessor_ProcessTest extends CiviCaseTestCase {
     $this->assertActivityAssignedToContactExists($this->contacts['beto']);
   }
 
+  /**
+   * Test the creation of activities where the default assignee should not
+   * end up being a contact from another case where it has the same client
+   * and relationship.
+   */
+  public function testCreateActivityWithDefaultContactByRelationshipTwoCases() {
+    /*
+    At this point the stock setup looks like this:
+    Case 1: no roles assigned
+    Non-case relationship with ana as pupil of beto
+    Non-case relationship with ana as spouse of carlos
+
+    So we want to:
+    Make another case for the same client ana.
+    Add a pupil role on that new case with some other person.
+    Make an activity on the first case.
+
+    Since there is a non-case relationship of that type for the
+    right person we do want it to take that one even though there is no role
+    on the first case, i.e. it SHOULD fall back to non-case relationships.
+    So this is test 1.
+
+    Then we want to get rid of the non-case relationship and try again. In
+    this situation it should not make any assignment, i.e. it should not
+    take the other person from the other case. The original bug was that it
+    would assign the activity to that other person from the other case. This
+    is test 2.
+     */
+
+    $relationship = $this->relationships['ana_is_pupil_of_beto'];
+
+    // Make another case and add a case role with the same relationship we
+    // want, but a different person.
+    $caseObj = $this->createCase($this->contacts['ana'], $this->_loggedInUser);
+    $this->callAPISuccess('Relationship', 'create', [
+      'contact_id_a' => $this->contacts['ana'],
+      'contact_id_b' => $this->contacts['carlos'],
+      'relationship_type_id' => $relationship['type_id'],
+      'case_id' => $caseObj->id,
+    ]);
+
+    $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);
+
+    // We can't use assertActivityAssignedToContactExists because it assumes
+    // there's only one activity in the database, but we have several from the
+    // second case. We want the one we just created on the first case.
+    $result = $this->callAPISuccess('Activity', 'get', [
+      'case_id' => $this->activityParams['caseID'],
+      'return' => ['assignee_contact_id'],
+    ])['values'];
+    $this->assertCount(1, $result);
+    foreach ($result as $activity) {
+      // Note the first parameter is turned into an array to match the second.
+      $this->assertEquals([$this->contacts['beto']], $activity['assignee_contact_id']);
+    }
+
+    // Now remove the non-case relationship.
+    $result = $this->callAPISuccess('Relationship', 'get', [
+      'case_id' => ['IS NULL' => 1],
+      'relationship_type_id' => $relationship['type_id'],
+      'contact_id_a' => $this->contacts['ana'],
+      'contact_id_b' => $this->contacts['beto'],
+    ])['values'];
+    $this->assertCount(1, $result);
+    foreach ($result as $activity) {
+      $result = $this->callAPISuccess('Relationship', 'delete', ['id' => $activity['id']]);
+    }
+
+    // Create another activity on the first case. Make it a different activity
+    // type so we can find it better.
+    $activityXml = '<activity-type><name>Follow up</name></activity-type>';
+    $activityXmlElement = new SimpleXMLElement($activityXml);
+    $activityXmlElement->default_assignee_type = $this->defaultAssigneeOptionsValues['BY_RELATIONSHIP'];
+    $activityXmlElement->default_assignee_relationship = "{$relationship['type_id']}_b_a";
+    $this->process->createActivity($activityXmlElement, $this->activityParams);
+
+    $result = $this->callAPISuccess('Activity', 'get', [
+      'case_id' => $this->activityParams['caseID'],
+      'activity_type_id' => 'Follow up',
+      'return' => ['assignee_contact_id'],
+    ])['values'];
+    $this->assertCount(1, $result);
+    foreach ($result as $activity) {
+      // It should be empty, not the contact from the second case.
+      $this->assertEmpty($activity['assignee_contact_id']);
+    }
+  }
+
   /**
    * Tests when the default assignee relationship exists, but in the other direction only.
    * Ana is a pupil, but has no pupils related to her.
@@ -315,8 +407,8 @@ class CRM_Case_XMLProcessor_ProcessTest extends CiviCaseTestCase {
    *   unique for each entry in the dataprovider since want to test a given
    *   relationship type against multiple xml strings. It's not a test
    *   identifier, it's an array key to use to look up something.
-   * @param $xmlString string
-   * @param $expected array
+   * @param string $xmlString
+   * @param array $expected
    * @param $dontcare array We're re-using the data provider for two tests and
    *   we don't care about those expected values.
    *
@@ -339,10 +431,10 @@ class CRM_Case_XMLProcessor_ProcessTest extends CiviCaseTestCase {
    *   unique for each entry in the dataprovider since want to test a given
    *   relationship type against multiple xml strings. It's not a test
    *   identifier, it's an array key to use to look up something.
-   * @param $xmlString string
+   * @param string $xmlString
    * @param $dontcare array We're re-using the data provider for two tests and
    *   we don't care about those expected values.
-   * @param $expected array
+   * @param array $expected
    *
    * @dataProvider xmlCaseRoleDataProvider
    */