+ public function testCaseTokens() {
+ $caseTest = new CiviCaseTestCase();
+ $caseTest->setUp();
+ // Create a contact and contactDetails array.
+ $contactId = $this->individualCreate();
+
+ // create a case for this user
+ $result = $this->callAPISuccess('Case', 'create', [
+ 'contact_id' => $contactId,
+ 'case_type_id' => '1',
+ 'subject' => "my case",
+ 'status_id' => "Open",
+ ]);
+
+ $caseId = $result['id'];
+ $html_message = "<p>This is a test case with id: {case.id} and subject: {case.subject}</p>";
+ $html_message = CRM_Utils_Token::replaceCaseTokens($caseId, $html_message);
+
+ $this->assertTrue(strpos($html_message, 'id: ' . $caseId) !== 0);
+ $this->assertTrue(strpos($html_message, 'subject: my case') !== 0);
+ $caseTest->tearDown();
+ }
+
+ public function testSendEmailWithCaseId() {
+ $caseTest = new CiviCaseTestCase();
+ $caseTest->setUp();
+ // Create a contact and contactDetails array.
+ $contactId = $this->individualCreate();
+ $contact = $this->callAPISuccess('Contact', 'get', ['id' => $contactId]);
+
+ // create a logged in USER since the code references it for sendEmail user.
+ $this->createLoggedInUser();
+ CRM_Core_Config::singleton()->userPermissionClass->permissions = ['view all contacts', 'access CiviCRM', 'access all cases and activities', 'administer CiviCase'];
+ $session = CRM_Core_Session::singleton();
+ $loggedInUser = $session->get('userID');
+
+ // create a case for this user
+ $result = $this->callAPISuccess('Case', 'create', [
+ 'contact_id' => $contactId,
+ 'case_type_id' => 1,
+ 'subject' => "my case",
+ 'status_id' => "Open",
+ ]);
+
+ $caseId = $result['id'];
+
+ $subject = __FUNCTION__ . ' subject {case.subject}';
+ $html = __FUNCTION__ . ' html {case.subject}';
+ $text = __FUNCTION__ . ' text';
+
+ $mut = new CiviMailUtils($this, TRUE);
+ list($sent, $activity_id) = $email_result = CRM_Activity_BAO_Activity::sendEmail(
+ $contact['values'],
+ $subject,
+ $text,
+ $html,
+ $contact['values'][$contactId]['email'],
+ $loggedInUser,
+ $from = __FUNCTION__ . '@example.com',
+ NULL,
+ NULL,
+ NULL,
+ [$contactId],
+ NULL,
+ NULL,
+ NULL,
+ $caseId
+ );
+ $activity = $this->callAPISuccess('Activity', 'getsingle', ['id' => $activity_id, 'return' => ['case_id']]);
+ $this->assertEquals($caseId, $activity['case_id'][0], 'Activity case_id does not match.');
+ $mut->checkMailLog(['subject my case']);
+ $mut->stop();
+ }
+
+ /**
+ * Adds a case with one activity.
+ *
+ */
+ protected function addCaseWithActivity() {
+ // case is not enabled by default do that now.
+ $enableResult = CRM_Core_BAO_ConfigSetting::enableComponent('CiviCase');
+ $this->assertTrue($enableResult, 'Cannot enable CiviCase in line ' . __LINE__);
+
+ // We need a minimal case setup.
+ $case_type_id = civicrm_api3('CaseType', 'get', ['return' => 'id', 'name' => 'test_case_type'])['id'] ?? NULL;
+ if (!$case_type_id) {
+ $params = [
+ 'name' => 'test_case_type',
+ 'title' => 'test_case_type',
+ "is_active" => "1",
+ "definition" => [
+ "activityTypes" => [
+ ["name" => "Open Case", "max_instances" => "1"],
+ ["name" => "Meeting"],
+ ],
+ "activitySets" => [
+ [
+ "name" => "standard_timeline",
+ "label" => "Standard Timeline",
+ "timeline" => "1",
+ "activityTypes" => [
+ [
+ "name" => "Open Case",
+ "status" => "Completed",
+ "label" => "Open Case",
+ "default_assignee_type" => "1",
+ ],
+ ],
+ ],
+ ],
+ "timelineActivityTypes" => [
+ [
+ "name" => "Open Case",
+ "status" => "Completed",
+ "label" => "Open Case",
+ "default_assignee_type" => "1",
+ ],
+ ],
+ "caseRoles" => [
+ [
+ "name" => "Case Coordinator",
+ "creator" => "1",
+ "manager" => "1",
+ ],
+ ],
+ ],
+ ];
+ $case_type_id = $this->callAPISuccess('CaseType', 'create', $params)['id'];
+ }
+
+ // Create a case with Contact #3 as the client.
+ $case_id = civicrm_api3('case', 'get', ['subject' => 'test case 1'])['id'] ?? NULL;
+ if (!$case_id) {
+ // Create case
+ $params = [
+ 'subject' => 'test case 1',
+ 'contact_id' => 3,
+ 'status_id' => 'Open',
+ 'case_type_id' => $case_type_id,
+ 'creator_id' => 3,
+ ];
+ $case_id = $this->callAPISuccess('case', 'create', $params)['id'];
+ }
+
+ // Create a scheduled 'Meeting' activity that belongs to this case, but is
+ // assigned to contact #9
+ $activity_id = $this->callAPISuccess('Activity', 'create', [
+ 'activity_type_id' => 'Meeting',
+ 'status_id' => 'Scheduled',
+ 'case_id' => $case_id,
+ 'source_contact_id' => 3,
+ 'assignee_id' => [9],
+ 'subject' => 'test meeting activity',
+ ])['id'] ?? NULL;
+ }
+
+ /**
+ * Change setting, and the cache of it.
+ */
+ protected function setShowCaseActivitiesInCore(bool $val) {
+ Civi::settings()->set('civicaseShowCaseActivities', $val ? 1 : 0);
+ CRM_Core_Component::getEnabledComponents();
+ Civi::$statics['CRM_Core_Component']['info']['CiviCase']->info['showActivitiesInCore'] = $val;
+ }
+