Merge pull request #24115 from kcristiano/5.52-token
[civicrm-core.git] / tests / phpunit / CRM / Contact / Form / Task / PDFLetterCommonTest.php
index cdd3d9f3ad73ac40a4f0342fc9141bc7bc7147e2..3e7fd38c65662463fe4cc9bdbb55b4b5abd5276d 100644 (file)
@@ -17,6 +17,8 @@
  */
 class CRM_Contact_Form_Task_PDFLetterCommonTest extends CiviUnitTestCase {
 
+  use CRMTraits_Custom_CustomDataTrait;
+
   /**
    * Contact ID.
    *
@@ -47,25 +49,11 @@ class CRM_Contact_Form_Task_PDFLetterCommonTest extends CiviUnitTestCase {
    * @dataProvider getFilenameCases
    */
   public function testFilenameIsAssigned(?string $pdfFileName, ?string $activitySubject, ?bool $isLiveMode, string $expectedFilename): void {
-    $_REQUEST['cid'] = $this->contactId;
-    $form = $this->getFormObject('CRM_Contact_Form_Task_PDF', [
+    $form = $this->getPDFForm([
       'pdf_file_name' => $pdfFileName,
       'subject' => $activitySubject,
-      '_contactIds' => [],
-      'document_type' => 'pdf',
-      'buttons' => [
-        '_qf_PDF_upload' => $isLiveMode,
-      ],
-    ]);
-    $fileNameAssigned = NULL;
-    $form->preProcess();
-    try {
-      $form->postProcess();
-    }
-    catch (CRM_Core_Exception_PrematureExitException $e) {
-      $fileNameAssigned = $e->errorData['fileName'];
-    }
-
+    ], [$this->contactId], $isLiveMode);
+    $fileNameAssigned = $this->submitForm($form)['fileName'];
     $this->assertEquals($expectedFilename, $fileNameAssigned);
   }
 
@@ -116,4 +104,106 @@ class CRM_Contact_Form_Task_PDFLetterCommonTest extends CiviUnitTestCase {
     ];
   }
 
+  /**
+   * @param \CRM_Core_Form $form
+   *
+   * @return int|mixed
+   */
+  protected function submitForm(CRM_Core_Form $form) {
+    $form->preProcess();
+    try {
+      $form->postProcess();
+    }
+    catch (CRM_Core_Exception_PrematureExitException $e) {
+      return $e->errorData;
+
+    }
+    $this->fail('line should be unreachable');
+  }
+
+  /**
+   * @param array $formValues
+   * @param array $contactIDs
+   * @param bool|null $isLiveMode
+   *
+   * @return \CRM_Contact_Form_Task_PDF
+   */
+  protected function getPDFForm(array $formValues, array $contactIDs, ?bool $isLiveMode = TRUE): CRM_Contact_Form_Task_PDF {
+    // pretty cludgey.
+    $_REQUEST['cid'] = $contactIDs[0];
+    /* @var CRM_Contact_Form_Task_PDF $form */
+    $form = $this->getFormObject('CRM_Contact_Form_Task_PDF', array_merge([
+      'pdf_file_name' => 'pdf_file_name',
+      'subject' => 'subject',
+      'document_type' => 'pdf',
+      '_qf_button_name' => ('_qf_PDF_upload' . (!$isLiveMode ? '_preview' : '')),
+    ], $formValues));
+    $form->_contactIds = $contactIDs;
+    return $form;
+  }
+
+  /**
+   * Test contact tokens are resolved.
+   */
+  public function testContactTokensAreResolved(): void {
+    $form = $this->getPDFForm([
+      'html_message' => '{contact.first_name}, {contact.email_greeting}',
+    ], [$this->contactId]);
+    $processedMessage = $this->submitForm($form)['html'];
+    $this->assertStringContainsString('Logged In, Dear Logged In', $processedMessage);
+  }
+
+  /**
+   * Test case tokens are resolved in pdf letter.
+   */
+  public function testCaseTokensAreResolved() : void {
+    // @todo - find a better way to set case id....
+    $_REQUEST['caseid'] = $this->getCaseID();
+    $form = $this->getPDFForm([
+      'html_message' => '{contact.first_name}, {case.case_type_id:label} {case.' . $this->getCustomFieldName('text') . '}',
+    ], [$this->contactId]);
+    $processedMessage = $this->submitForm($form)['html'];
+    $this->assertStringContainsString('Logged In, Housing Support bb', $processedMessage);
+  }
+
+  /**
+   * Get case ID.
+   *
+   * @return int
+   */
+  protected function getCaseID(): int {
+    if (!isset($this->ids['Case'][0])) {
+      CRM_Core_BAO_ConfigSetting::enableComponent('CiviCase');
+      $this->createCustomGroupWithFieldOfType(['extends' => 'Case']);
+      $this->ids['Case'][0] = $this->callAPISuccess('Case', 'create', [
+        'case_type_id' => 'housing_support',
+        'activity_subject' => 'Case Subject',
+        'client_id' => $this->getContactID(),
+        'status_id' => 1,
+        'subject' => 'Case Subject',
+        'start_date' => '2021-07-23 15:39:20',
+        // Note end_date is inconsistent with status Ongoing but for the
+        // purposes of testing tokens is ok. Creating it with status Resolved
+        // then ignores our known fixed end date.
+        'end_date' => '2021-07-26 18:07:20',
+        'medium_id' => 2,
+        'details' => 'case details',
+        'activity_details' => 'blah blah',
+        'sequential' => 1,
+        $this->getCustomFieldName('text') => 'bb',
+      ])['id'];
+    }
+    return $this->ids['Case'][0];
+  }
+
+  /**
+   * @return int
+   */
+  protected function getContactID(): int {
+    if (!isset($this->ids['Contact'][0])) {
+      $this->ids['Contact'][0] = $this->individualCreate();
+    }
+    return $this->ids['Contact'][0];
+  }
+
 }