CRM-19761: Add unit test and some fixes
authorjitendrapurohit <jitendra.purohit@webaccessglobal.com>
Fri, 23 Dec 2016 12:57:31 +0000 (18:27 +0530)
committerjitendrapurohit <jitendra.purohit@webaccessglobal.com>
Fri, 23 Dec 2016 13:05:59 +0000 (18:35 +0530)
CRM/Contribute/Form/Task.php
CRM/Contribute/Form/Task/PDFLetter.php
CRM/Contribute/Form/Task/PDFLetterCommon.php
CRM/Contribute/Form/Task/Status.php
CRM/Utils/PDF/Document.php
tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php
tests/phpunit/CRM/Contribute/Form/Task/sample_documents/Template.docx [new file with mode: 0644]
tests/phpunit/CRM/Contribute/Form/Task/sample_documents/Template.odt [new file with mode: 0644]

index df277b28545da16b3e858c3004b6cfe9a9568bee..0e141796678642d6c638eeef47b31ce1cfd3c064 100644 (file)
@@ -184,6 +184,13 @@ class CRM_Contribute_Form_Task extends CRM_Core_Form {
     }
   }
 
+  /**
+   * Sets contribution Ids for unit test.
+   */
+  public function setContributionIds($contributionIds) {
+    $this->_contributionIds = $contributionIds;
+  }
+
   /**
    * Given the contribution id, compute the contact id
    * since its used for things like send email
index 49c52dbc1e21bbf3f07c8dd444a3f689f6747ac2..810ae851768ca3dd118b55daf9dcd63481bbb256 100644 (file)
@@ -140,7 +140,7 @@ class CRM_Contribute_Form_Task_PDFLetter extends CRM_Contribute_Form_Task {
 
     $this->addButtons(array(
         array(
-          'type' => 'submit',
+          'type' => 'upload',
           'name' => ts('Make Thank-you Letters'),
           'isDefault' => TRUE,
         ),
index b93bd533a0306774a6aaa8b9678bd2fb8bd4b496..696d5f018ece2f26e48de24b2ce6ccb74ca149c0 100644 (file)
@@ -10,9 +10,12 @@ class CRM_Contribute_Form_Task_PDFLetterCommon extends CRM_Contact_Form_Task_PDF
    * Process the form after the input has been submitted and validated.
    *
    * @param CRM_Contribute_Form_Task $form
+   * @param array $formValues
    */
-  public static function postProcess(&$form) {
-    $formValues = $form->controller->exportValues($form->getName());
+  public static function postProcess(&$form, $formValues = NULL) {
+    if (empty($formValues)) {
+      $formValues = $form->controller->exportValues($form->getName());
+    }
     list($formValues, $categories, $html_message, $messageToken, $returnProperties) = self::processMessageTemplate($formValues);
     $isPDF = FALSE;
     $emailParams = array();
@@ -108,6 +111,10 @@ class CRM_Contribute_Form_Task_PDFLetterCommon extends CRM_Contact_Form_Task_PDF
         }
       }
     }
+
+    if (!empty($formValues['is_unit_test'])) {
+      return $html;
+    }
     //createActivities requires both $form->_contactIds and $contacts -
     //@todo - figure out why
     $form->_contactIds = array_keys($contacts);
index 26aefacf3c60911efd07409efaf505614ed1afcb..462a5b9fee7a77c58be39106b80af2bfa180a4b7 100644 (file)
@@ -82,13 +82,6 @@ AND    {$this->_componentClause}";
     $this->assign('single', $this->_single);
   }
 
-  /**
-   * Sets contribution Ids for unit test.
-   */
-  public function setContributionIds($contributionIds) {
-    $this->_contributionIds = $contributionIds;
-  }
-
   /**
    * Build the form object.
    */
index b2ecd6aede177f4d5f8eab9cc2c86ec193ba57ad..49a29f06328aaa5afb2942b727373d3cba0c9a81 100644 (file)
@@ -106,8 +106,8 @@ class CRM_Utils_PDF_Document {
       'pdf' => 'PDF',
     );
 
-    if (realpath($phpWord)) {
-      $phpWord = \PhpOffice\PhpWord\IOFactory::load($phpWord, $formats[$ext]);
+    if (realpath($fileName)) {
+      $phpWord = \PhpOffice\PhpWord\IOFactory::load($fileName, $formats[$ext]);
     }
 
     $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, $formats[$ext]);
index 297057b91ca85ac41febf6750a76c97766d588a9..b882a4d8c55165ace28fb06f95999058eb3b1ac1 100644 (file)
@@ -39,6 +39,16 @@ class CRM_Contribute_Form_Task_PDFLetterCommonTest extends CiviUnitTestCase {
    */
   protected $_individualId;
 
+  protected $_docTypes = NULL;
+
+  protected $_contactIds = NULL;
+
+  protected function setUp() {
+    parent::setUp();
+    $this->_individualId = $this->individualCreate(array('first_name' => 'Anthony', 'last_name' => 'Collins'));
+    $this->_docTypes = CRM_Core_SelectValues::documentApplicationType();
+  }
+
   /**
    * Clean up after each test.
    */
@@ -82,4 +92,47 @@ class CRM_Contribute_Form_Task_PDFLetterCommonTest extends CiviUnitTestCase {
     }
   }
 
+  /**
+   * Test contribution token replacement in
+   * html returned by postProcess function.
+   */
+  public function testPostProcess() {
+    $this->_individualId = $this->individualCreate();
+    foreach (array('docx', 'odt') as $docType) {
+      $formValues = array(
+        'is_unit_test' => TRUE,
+        'group_by' => NULL,
+        'document_file' => array(
+          'name' => __DIR__ . "/sample_documents/Template.$docType",
+          'type' => $this->_docTypes[$docType],
+        ),
+      );
+
+      $contributionParams = array(
+        'contact_id' => $this->_individualId,
+        'total_amount' => 100,
+        'financial_type_id' => 'Donation',
+      );
+      $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
+      $contributionId = $contribution['id'];
+      $form = new CRM_Contribute_Form_Task_PDFLetter();
+      $form->setContributionIds(array($contributionId));
+      $format = Civi::settings()->get('dateformatFull');
+      $date = CRM_Utils_Date::getToday();
+      $displayDate = CRM_Utils_Date::customFormat($date, $format);
+
+      $html = CRM_Contribute_Form_Task_PDFLetterCommon::postProcess($form, $formValues);
+      $expectedValues = array(
+        'Hello Anthony Collins',
+        '$ 100.00',
+        $displayDate,
+        'Donation'
+      );
+
+      foreach ($expectedValues as $val) {
+        $this->assertTrue(strpos($html[$contributionId], $val) !== 0);
+      }
+    }
+  }
+
 }
diff --git a/tests/phpunit/CRM/Contribute/Form/Task/sample_documents/Template.docx b/tests/phpunit/CRM/Contribute/Form/Task/sample_documents/Template.docx
new file mode 100644 (file)
index 0000000..f030b0e
Binary files /dev/null and b/tests/phpunit/CRM/Contribute/Form/Task/sample_documents/Template.docx differ
diff --git a/tests/phpunit/CRM/Contribute/Form/Task/sample_documents/Template.odt b/tests/phpunit/CRM/Contribute/Form/Task/sample_documents/Template.odt
new file mode 100644 (file)
index 0000000..ffd70a7
Binary files /dev/null and b/tests/phpunit/CRM/Contribute/Form/Task/sample_documents/Template.odt differ