add test for editing activity subject to file on case
authordemeritcowboy <demeritcowboy@hotmail.com>
Mon, 15 Aug 2022 13:54:40 +0000 (09:54 -0400)
committerdemeritcowboy <demeritcowboy@hotmail.com>
Mon, 15 Aug 2022 13:54:40 +0000 (09:54 -0400)
tests/phpunit/CRM/Case/BAO/CaseTest.php
tests/phpunit/api/v3/ActivityCaseTest.php

index 4315091d5af3090ac4cf332bc54bf0e772ef57a4..5e4e9b044b7a70be94b695afdb07c4af9d8398f3 100644 (file)
@@ -1275,6 +1275,48 @@ class CRM_Case_BAO_CaseTest extends CiviUnitTestCase {
     );
   }
 
+  /**
+   * Same as testFileOnCaseBySubject but editing an existing non-case activity
+   */
+  public function testFileOnCaseByEditingSubject() {
+    $loggedInUserId = $this->createLoggedInUser();
+    $clientId = $this->individualCreate();
+    $caseObj = $this->createCase($clientId, $loggedInUserId);
+    $activity = $this->callAPISuccess('Activity', 'create', [
+      'source_contact_id' => $loggedInUserId,
+      'target_contact_id' => $clientId,
+      'activity_type_id' => 1,
+      'subject' => 'Starting as non-case activity',
+    ]);
+    $subject = 'Now should be a case activity [case #' . $caseObj->id . ']';
+    $form = $this->getFormObject('CRM_Activity_Form_Activity', [
+      'id' => $activity['id'],
+      'source_contact_id' => $loggedInUserId,
+      'target_contact_id' => $clientId,
+      'subject' => $subject,
+      'activity_date_time' => date('Y-m-d H:i:s'),
+      'activity_type_id' => 1,
+    ]);
+    $form->postProcess();
+
+    $activity = $this->callAPISuccess('Activity', 'getsingle', [
+      'id' => $activity['id'],
+      'return' => ['case_id'],
+    ]);
+    // Note it's an array
+    $this->assertEquals([$caseObj->id], $activity['case_id']);
+
+    // Double-check
+    $queryParams = [1 => [$activity['id'], 'Integer']];
+    $this->assertEquals(
+      $caseObj->id,
+      CRM_Core_DAO::singleValueQuery('SELECT ca.case_id
+        FROM civicrm_case_activity ca
+        INNER JOIN civicrm_activity a ON ca.activity_id = a.id
+        WHERE a.id = %1', $queryParams)
+    );
+  }
+
   /**
    * Basic case create test with an Org client
    */
index 98243164e6b8b900a836942d8f465c61d7758a3a..26d914b9534f7aab458412a48740463f5198cba7 100644 (file)
@@ -71,6 +71,39 @@ class api_v3_ActivityCaseTest extends CiviCaseTestCase {
     }
   }
 
+  /**
+   * Same as testActivityCreateOnCase but editing an existing non-case activity
+   */
+  public function testActivityEditAddingCaseIdInSubject() {
+    $activity = $this->callAPISuccess('Activity', 'create', [
+      'source_contact_id' => $this->_cid,
+      'activity_type_id' => 'Meeting',
+      'subject' => 'Starting as non-case activity 1',
+    ]);
+    $hash = substr(sha1(CIVICRM_SITE_KEY . $this->_case['id']), 0, 7);
+    // edit activity and put hash in the subject
+    $activity = $this->callAPISuccess('Activity', 'create', [
+      'id' => $activity['id'],
+      'subject' => "Now should be a case activity 1 [case #{$hash}]",
+    ]);
+    $case = $this->callAPISuccessGetSingle('Activity', ['return' => ['case_id'], 'id' => $activity['id']]);
+    // It should be filed on the case now
+    $this->assertEquals($this->_case['id'], $case['case_id'][0]);
+
+    // Now same thing but just with the id not the hash
+    $activity = $this->callAPISuccess('Activity', 'create', [
+      'source_contact_id' => $this->_cid,
+      'activity_type_id' => 'Meeting',
+      'subject' => 'Starting as non-case activity 2',
+    ]);
+    $activity = $this->callAPISuccess('Activity', 'create', [
+      'id' => $activity['id'],
+      'subject' => "Now should be a case activity 2 [case #{$this->_case['id']}]",
+    ]);
+    $case = $this->callAPISuccessGetSingle('Activity', ['return' => ['case_id'], 'id' => $activity['id']]);
+    $this->assertEquals($this->_case['id'], $case['case_id'][0]);
+  }
+
   public function testGet() {
     $this->assertTrue(is_numeric($this->_case['id']));
     $this->assertTrue(is_numeric($this->_otherActivity['id']));