Skip to content

Commit

Permalink
Merge pull request #9575 from jitendrapurohit/CRM-19761
Browse files Browse the repository at this point in the history
CRM-19761: Add unit test and some fixes
  • Loading branch information
monishdeb authored Jan 2, 2017
2 parents 0a1c3e0 + 993a5b9 commit e2b0438
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 12 deletions.
7 changes: 7 additions & 0 deletions CRM/Contribute/Form/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,13 @@ public static function preProcessCommon(&$form, $useTable = FALSE) {
}
}

/**
* 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
Expand Down
2 changes: 1 addition & 1 deletion CRM/Contribute/Form/Task/PDFLetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public function buildQuickForm() {

$this->addButtons(array(
array(
'type' => 'submit',
'type' => 'upload',
'name' => ts('Make Thank-you Letters'),
'isDefault' => TRUE,
),
Expand Down
11 changes: 9 additions & 2 deletions CRM/Contribute/Form/Task/PDFLetterCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -108,6 +111,10 @@ public static function postProcess(&$form) {
}
}
}

if (!empty($formValues['is_unit_test'])) {
return $html;
}
//createActivities requires both $form->_contactIds and $contacts -
//@todo - figure out why
$form->_contactIds = array_keys($contacts);
Expand Down
7 changes: 0 additions & 7 deletions CRM/Contribute/Form/Task/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,6 @@ public function preProcess() {
$this->assign('single', $this->_single);
}

/**
* Sets contribution Ids for unit test.
*/
public function setContributionIds($contributionIds) {
$this->_contributionIds = $contributionIds;
}

/**
* Build the form object.
*/
Expand Down
4 changes: 2 additions & 2 deletions CRM/Utils/PDF/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ public static function printDoc($phpWord, $ext, $fileName) {
'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]);
Expand Down
53 changes: 53 additions & 0 deletions tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -82,4 +92,47 @@ public function hookTokenValues(&$details, $contactIDs, $jobID, $tokens, $classN
}
}

/**
* 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);
}
}
}

}
Binary file not shown.
Binary file not shown.

0 comments on commit e2b0438

Please sign in to comment.