CRM-19443 - Fix date tokens in Print/Merge Task
authorjitendrapurohit <jitendra.purohit@webaccessglobal.com>
Thu, 13 Oct 2016 13:29:22 +0000 (18:59 +0530)
committerjitendrapurohit <jitendra.purohit@webaccessglobal.com>
Fri, 14 Oct 2016 06:20:06 +0000 (11:50 +0530)
CRM/Utils/Token.php
tests/phpunit/CRM/Member/Form/Task/PDFLetterCommonTest.php [new file with mode: 0644]

index 63d6cbf82306a953209cad9ac2062056492aac6d..c40c0da6ab113a6dbb0a09bcedb8258baec67f7e 100644 (file)
@@ -753,6 +753,9 @@ class CRM_Utils_Token {
           $value = CRM_Core_PseudoConstant::getLabel('CRM_Contact_BAO_Contact', $token, $value);
         }
       }
+      elseif ($value && CRM_Utils_String::endsWith($token, '_date')) {
+        $value = CRM_Utils_Date::customFormat($value);
+      }
     }
 
     if (!$html) {
@@ -1406,9 +1409,9 @@ class CRM_Utils_Token {
    */
   public static function getMembershipTokenDetails($membershipIDs) {
     $memberships = civicrm_api3('membership', 'get', array(
-        'options' => array('limit' => 200000),
-        'membership_id' => array('IN' => (array) $membershipIDs),
-      ));
+      'options' => array('limit' => 0),
+      'membership_id' => array('IN' => (array) $membershipIDs),
+    ));
     return $memberships['values'];
   }
 
@@ -1798,6 +1801,9 @@ class CRM_Utils_Token {
       default:
         if (in_array($token, self::$_tokens[$entity])) {
           $value = $membership[$token];
+          if (CRM_Utils_String::endsWith($token, '_date')) {
+            $value = CRM_Utils_Date::customFormat($value);
+          }
         }
         else {
           // ie unchanged
diff --git a/tests/phpunit/CRM/Member/Form/Task/PDFLetterCommonTest.php b/tests/phpunit/CRM/Member/Form/Task/PDFLetterCommonTest.php
new file mode 100644 (file)
index 0000000..33ca159
--- /dev/null
@@ -0,0 +1,128 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 4.7                                                |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2016                                |
+ +--------------------------------------------------------------------+
+ | This file is a part of CiviCRM.                                    |
+ |                                                                    |
+ | CiviCRM is free software; you can copy, modify, and distribute it  |
+ | under the terms of the GNU Affero General Public License           |
+ | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
+ |                                                                    |
+ | CiviCRM is distributed in the hope that it will be useful, but     |
+ | WITHOUT ANY WARRANTY; without even the implied warranty of         |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
+ | See the GNU Affero General Public License for more details.        |
+ |                                                                    |
+ | You should have received a copy of the GNU Affero General Public   |
+ | License and the CiviCRM Licensing Exception along                  |
+ | with this program; if not, contact CiviCRM LLC                     |
+ | at info[AT]civicrm[DOT]org. If you have questions about the        |
+ | GNU Affero General Public License or the licensing of CiviCRM,     |
+ | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ * @package CiviCRM_APIv3
+ * @subpackage API_Contribution
+ * @group headless
+ */
+class CRM_Member_Form_Task_PDFLetterCommonTest extends CiviUnitTestCase {
+
+  /**
+   * Clean up after each test.
+   */
+  public function tearDown() {
+    $this->quickCleanUpFinancialEntities();
+    CRM_Utils_Hook::singleton()->reset();
+  }
+
+  /**
+   * Test token replacement for Print/Merge Task
+   */
+  public function testMembershipTokenReplacementInPDF() {
+    $membershipIds = $returnProperties = $categories = $expected = array();
+    list($tokens, $htmlMessage) = self::getSampleHTML();
+
+    $membershipDates = array(
+      date('Y-m-d'),
+      date('Y-m-d', strtotime('-1 month')),
+    );
+    // Create sample memberships with different dates.
+    foreach ($membershipDates as $date) {
+      $contactId = $this->individualCreate();
+      $membershipTypeID = $this->membershipTypeCreate(array(
+        'minimum_fee' => '100.00',
+        'member_of_contact_id' => $contactId,
+      ));
+      $params = array(
+        'contact_id' => $contactId,
+        'membership_type_id' => $membershipTypeID,
+        'join_date' => $date,
+        'start_date' => $date,
+        'end_date' => date('Y-m-d', strtotime("{$date} +1 year")),
+      );
+      $result = $this->callAPISuccess('membership', 'create', $params);
+      $membershipIds[] = $result['id'];
+      $params = array_merge($params,
+        array(
+          'fee' => '100.00',
+          'type' => 'General',
+          'status' => 'New',
+        )
+      );
+
+      // Form an expected array replacing tokens for each contact.
+      foreach ($tokens as $key => $val) {
+        if (CRM_Utils_String::endsWith($val, '_date')) {
+          $formattedDate = CRM_Utils_Date::customFormat($params[$val]);
+          $expected[$contactId][$val] = "{$key} - {$formattedDate}";
+        }
+        else {
+          $expected[$contactId][$val] = $params[$val];
+        }
+      }
+    }
+    $messageToken = CRM_Utils_Token::getTokens($htmlMessage);
+    $testHTML = CRM_Member_Form_Task_PDFLetterCommon::generateHTML($membershipIds,
+      $returnProperties,
+      NULL,
+      NULL,
+      $messageToken,
+      $htmlMessage,
+      $categories
+    );
+
+    // Assert all membership tokens are replaced correctly.
+    $expected = array_values($expected);
+    foreach ($expected as $key => $dateVal) {
+      foreach ($tokens as $text => $token) {
+        $this->assertContains($dateVal[$token], $testHTML[$key]);
+      }
+    }
+  }
+
+  /**
+   * Generate sample HTML for testing.
+   */
+  public static function getSampleHTML() {
+    $tokens = array(
+      'Test Fee' => 'fee',
+      'Test Type' => 'type',
+      'Test Status' => 'status',
+      'Test Join Date' => 'join_date',
+      'Test Start Date' => 'start_date',
+      'Test End Date' => 'end_date',
+    );
+
+    $html = '';
+    foreach ($tokens as $key => $val) {
+      $html .= "<p>{$key} - {membership.{$val}}</p>";
+    }
+    return array($tokens, $html);
+  }
+
+}