Skip to content

Commit

Permalink
Merge pull request #11820 from seanmadsen/CRM-21264
Browse files Browse the repository at this point in the history
 CRM-21264 - Fix tabular formatting of contributions in merged letters grouped by contact
  • Loading branch information
eileenmcnaughton authored Mar 19, 2018
2 parents e7abfc3 + 5aece37 commit 1142ce8
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 7 deletions.
14 changes: 7 additions & 7 deletions CRM/Contribute/Form/Task/PDFLetterCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,17 +188,17 @@ public static function isValidHTMLWithTableSeparator($tokens, $html) {
* Check that the token only appears in a table cell. The '</td><td>' separator cannot otherwise work
* Calculate the number of times it appears IN the cell & the number of times it appears - should be the same!
*
* @param $token
* @param $entity
* @param $textToSearch
* @param string $token
* @param string $entity
* @param string $textToSearch
*
* @return bool
*/
public static function isHtmlTokenInTableCell($token, $entity, $textToSearch) {
$tokenToMatch = $entity . '.' . $token;
$dontCare = array();
$within = preg_match_all("|<td.+?{" . $tokenToMatch . "}.+?</td|si", $textToSearch, $dontCare);
$total = preg_match_all("|{" . $tokenToMatch . "}|", $textToSearch, $dontCare);
$tokenToMatch = $entity . '\.' . $token;
$pattern = '|<td(?![\w-])((?!</td>).)*\{' . $tokenToMatch . '\}.*?</td>|si';
$within = preg_match_all($pattern, $textToSearch);
$total = preg_match_all("|{" . $tokenToMatch . "}|", $textToSearch);
return ($within == $total);
}

Expand Down
74 changes: 74 additions & 0 deletions tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,4 +376,78 @@ public function hook_aggregateTokenValues(&$values, $contactIDs, $job = NULL, $t
}
}

/**
* @param string $token
* @param string $entity
* @param string $textToSearch
* @param bool $expected
*
* @dataProvider isHtmlTokenInTableCellProvider
*/
public function testIsHtmlTokenInTableCell($token, $entity, $textToSearch, $expected) {
$this->assertEquals($expected,
CRM_Contribute_Form_Task_PDFLetterCommon::isHtmlTokenInTableCell($token, $entity, $textToSearch)
);
}

public function isHtmlTokenInTableCellProvider() {
return [

'simplest TRUE' => [
'token',
'entity',
'<td>{entity.token}</td>',
TRUE,
],

'simplest FALSE' => [
'token',
'entity',
'{entity.token}',
FALSE,
],

'token between two tables' => [
'token',
'entity',
' <table><tr><td>Top</td></tr></table>
{entity.token}
<table><tr><td>Bottom</td></tr></table>',
FALSE,
],

'token in two tables' => [
'token',
'entity',
' <table><tr><td>{entity.token}</td></tr><tr><td>foo</td></tr></table>
<table><tr><td>{entity.token}</td></tr><tr><td>foo</td></tr></table>',
TRUE,
],

'token outside of table and inside of table' => [
'token',
'entity',
' {entity.token}
<table><tr><td>{entity.token}</td></tr><tr><td>foo</td></tr></table>',
FALSE,
],

'token inside more complicated table' => [
'token',
'entity',
' <table><tr><td class="foo"><em>{entity.token}</em></td></tr></table>',
TRUE,
],

'token inside something that looks like table cell' => [
'token',
'entity',
' <tdata>{entity.token}</tdata>
<table><tr><td>Bottom</td></tr></table>',
FALSE,
],

];
}

}

0 comments on commit 1142ce8

Please sign in to comment.