Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
CiviCRM Core
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container registry
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Development
CiviCRM Core
Commits
c667c8c8
Unverified
Commit
c667c8c8
authored
3 years ago
by
colemanw
Committed by
GitHub
3 years ago
Browse files
Options
Downloads
Plain Diff
Merge pull request #21508 from eileenmcnaughton/date
Add support for CiviCRM date formats in crmDate
parents
30faff70
5f95ae68
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
CRM/Core/Smarty/plugins/modifier.crmDate.php
+24
-2
24 additions, 2 deletions
CRM/Core/Smarty/plugins/modifier.crmDate.php
tests/phpunit/CRM/Core/TokenSmartyTest.php
+34
-4
34 additions, 4 deletions
tests/phpunit/CRM/Core/TokenSmartyTest.php
with
58 additions
and
6 deletions
CRM/Core/Smarty/plugins/modifier.crmDate.php
+
24
−
2
View file @
c667c8c8
...
...
@@ -21,14 +21,36 @@
* @param string $dateString
* Date which needs to converted to human readable format.
*
* @param null $dateFormat
* @param string|null $dateFormat
* A string per https://www.php.net/manual/en/function.strftime.php or
* one of our configured formats name - eg
* - dateformatDatetime
* - dateformatFull
* - dateformatPartial
* - dateformatTime
* - dateformatYear
* - dateformatFinancialBatch
* - dateformatshortdate
*
* @param bool $onlyTime
*
* @return string
* human readable date format | invalid date message
*/
function
smarty_modifier_crmDate
(
$dateString
,
$dateFormat
=
NULL
,
$onlyTime
=
FALSE
)
{
function
smarty_modifier_crmDate
(
$dateString
,
?string
$dateFormat
=
NULL
,
bool
$onlyTime
=
FALSE
)
:
string
{
if
(
$dateString
)
{
$configuredFormats
=
[
'Datetime'
,
'Full'
,
'Partial'
,
'Time'
,
'Year'
,
'FinancialBatch'
,
'shortdate'
,
];
if
(
in_array
(
$dateFormat
,
$configuredFormats
,
TRUE
))
{
$dateFormat
=
Civi
::
settings
()
->
get
(
'dateformat'
.
$dateFormat
);
}
// this check needs to be type sensitive
// CRM-3689, CRM-2441
if
(
$dateFormat
===
0
)
{
...
...
This diff is collapsed.
Click to expand it.
tests/phpunit/CRM/Core/TokenSmartyTest.php
+
34
−
4
View file @
c667c8c8
...
...
@@ -56,7 +56,7 @@ class CRM_Core_TokenSmartyTest extends CiviUnitTestCase {
/**
* A template that specifically opts out of Smarty.
*/
public
function
testDisableSmarty
()
{
public
function
testDisableSmarty
()
:
void
{
$rendered
=
CRM_Core_TokenSmarty
::
render
(
[
'msg_subject'
=>
'First name is {contact.first_name}. ExtraFoo is {$extra.foo}.'
],
[
'contactId'
=>
$this
->
contactId
,
'smarty'
=>
FALSE
],
...
...
@@ -65,10 +65,40 @@ class CRM_Core_TokenSmartyTest extends CiviUnitTestCase {
$this
->
assertEquals
(
'First name is Bob. ExtraFoo is {$extra.foo}.'
,
$rendered
[
'msg_subject'
]);
}
/**
* Test smarty rendered dates using the setting short name.
*
* @param string $format
* @param string $expected
*
* @dataProvider getDateFormats
*/
public
function
testSmartySettingDates
(
string
$format
,
string
$expected
=
''
):
void
{
$date
=
'2010-09-19 13:34:45'
;
CRM_Core_Smarty
::
singleton
()
->
assign
(
'date'
,
$date
);
$string
=
'{$date|crmDate:'
.
$format
.
'}'
;
$this
->
assertEquals
(
$expected
,
CRM_Utils_String
::
parseOneOffStringThroughSmarty
(
$string
));
}
/**
* Get date formats to test.
*/
public
function
getDateFormats
():
array
{
return
[
[
'Full'
,
'September 19th, 2010'
],
[
'Datetime'
,
'September 19th, 2010 1:34 PM'
],
[
'Partial'
,
'September 2010'
],
[
'Time'
,
' 1:34 PM'
],
[
'Year'
,
'2010'
],
[
'FinancialBatch'
,
'09/19/2010'
],
[
'shortdate'
,
'09/19/2010'
],
];
}
/**
* Someone malicious gives cutesy expressions (via token-content) that tries to provoke extra evaluation.
*/
public
function
testCutesyTokenData
()
{
public
function
testCutesyTokenData
()
:
void
{
$cutesyContactId
=
$this
->
individualCreate
([
'first_name'
=>
'{$extra.foo}{contact.last_name}'
,
'last_name'
=>
'Roberts'
,
...
...
@@ -84,7 +114,7 @@ class CRM_Core_TokenSmartyTest extends CiviUnitTestCase {
/**
* Someone malicious gives cutesy expressions (via Smarty-content) that tries to provoke extra evaluation.
*/
public
function
testCutesySmartyData
()
{
public
function
testCutesySmartyData
()
:
void
{
$rendered
=
CRM_Core_TokenSmarty
::
render
(
[
'msg_subject'
=>
'First name is {contact.first_name}. ExtraFoo is {$extra.foo}.'
],
[
'contactId'
=>
$this
->
contactId
],
...
...
@@ -96,7 +126,7 @@ class CRM_Core_TokenSmartyTest extends CiviUnitTestCase {
/**
* The same tokens are used in multiple parts of the template - without redundant evaluation.
*/
public
function
testDataLoadCount
()
{
public
function
testDataLoadCount
()
:
void
{
// Define a token `{counter.i}` which increments whenever tokens are evaluated.
Civi
::
dispatcher
()
->
addListener
(
'civi.token.eval'
,
function
(
\Civi\Token\Event\TokenValueEvent
$e
)
{
static
$i
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment