diff --git a/src/WebformCivicrmPreProcess.php b/src/WebformCivicrmPreProcess.php index 6364c89a4..8b8831af0 100644 --- a/src/WebformCivicrmPreProcess.php +++ b/src/WebformCivicrmPreProcess.php @@ -781,8 +781,15 @@ private function findExistingActivities() { 'is_deleted' => '0', 'is_current_revision' => '1', 'options' => ['limit' => 1], + 'activity_type_id' => $this->data['activity'][$n]['activity'][1]['activity_type_id'], ]; - $params['activity_type_id'] = $this->data['activity'][$n]['activity'][1]['activity_type_id']; + $caseType = $this->data['activity'][$n]['case_type_id'] ?? NULL; + if (!empty($caseType) && $caseType[0] === '#') { + $case_num = substr($caseType, 1); + if (!empty($this->ent['case'][$case_num]['id'])) { + $params['case_id'] = $this->ent['case'][$case_num]['id']; + } + } $items = $this->utils->wf_crm_apivalues('activity', 'get', $params); if (isset($items[0]['id'])) { $this->ent['activity'][$n] = ['id' => $items[0]['id']]; diff --git a/tests/src/FunctionalJavascript/CaseSubmissionTest.php b/tests/src/FunctionalJavascript/CaseSubmissionTest.php index 850ce2756..c98622878 100644 --- a/tests/src/FunctionalJavascript/CaseSubmissionTest.php +++ b/tests/src/FunctionalJavascript/CaseSubmissionTest.php @@ -30,9 +30,70 @@ protected function setUp(): void { $this->getSession()->getPage()->checkField('Case Subject'); $this->getSession()->getPage()->checkField('Case Start Date'); + // Configure Activity Tab + $this->getSession()->getPage()->clickLink('Activities'); + $this->getSession()->getPage()->selectFieldOption('activity_number_of_activity', 1); + $this->assertSession()->waitForField('civicrm_1_activity_1_activity_subject'); + $this->htmlOutput(); + $this->getSession()->getPage()->selectFieldOption('Update Existing Activity', 'Scheduled'); + $this->getSession()->getPage()->selectFieldOption('File On Case', 'Case 1'); + $this->assertSession()->assertWaitOnAjaxRequest(); + + $this->getSession()->getPage()->checkField('Activity Subject'); + $this->getSession()->getPage()->selectFieldOption('Activity Type', 'Follow up'); + $this->assertSession()->assertWaitOnAjaxRequest(); + $this->saveCiviCRMSettings(); } + /** + * Verify correct Case and related Activity + * is loaded on the webform by default. + */ + public function testCaseActivityDefaults() { + $cases = [ + [ + 'subject' => 'Case 1 subject', + 'status' => 'Closed', + 'activity_subject' => 'Followup Activity from Case 1', + ], + [ + 'subject' => 'Case 2 subject', + 'status' => 'Open', + 'activity_subject' => 'Followup Activity from Case 2', + ], + ]; + // Create case 1 with status=Resolved + // Create case 2 with status=Ongoing + foreach ($cases as $params) { + $case = \Civi\Api4\CiviCase::create(TRUE) + ->addValue('case_type_id.name', 'housing_support') + ->addValue('status_id:name', $params['status']) + ->addValue('contact_id', $this->rootUserCid) + ->addValue('subject', $params['subject']) + ->addValue('creator_id', $this->rootUserCid) + ->execute() + ->first(); + + // Add subject to the followup activity + \Civi\Api4\Activity::update(TRUE) + ->addValue('subject', $params['activity_subject']) + ->addWhere('case_id', '=', $case['id']) + ->addWhere('activity_type_id:name', '=', 'Follow up') + ->execute() + ->first(); + } + + // Load the webform + $this->drupalGet($this->webform->toUrl('canonical')); + $this->htmlOutput(); + $this->assertPageNoErrorMessages(); + + // Ensure case subject and activity subject is populated correctly. + $this->assertSession()->fieldValueEquals('civicrm_1_case_1_case_subject', 'Case 2 subject'); + $this->assertSession()->fieldValueEquals('civicrm_1_activity_1_activity_subject', 'Followup Activity from Case 2'); + } + /** * Test Case Submission. */