From 0c03c65e4507057e167bc11e7fbd2e2d19d8926d Mon Sep 17 00:00:00 2001 From: demeritcowboy Date: Mon, 20 Jun 2022 20:36:49 -0400 Subject: [PATCH] CIVICRM-1998 CiviCRM Events RSS feed does not output a pubDate for each Event, use the Event Start Date as the pubDate which is generally how other Event systems use RSS --- .../Smarty/plugins/modifier.crmRSSPubDate.php | 36 +++++++++ templates/CRM/Core/Calendar/Rss.tpl | 2 + .../Core/Smarty/plugins/CrmRSSPubDateTest.php | 76 +++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 CRM/Core/Smarty/plugins/modifier.crmRSSPubDate.php create mode 100644 tests/phpunit/CRM/Core/Smarty/plugins/CrmRSSPubDateTest.php diff --git a/CRM/Core/Smarty/plugins/modifier.crmRSSPubDate.php b/CRM/Core/Smarty/plugins/modifier.crmRSSPubDate.php new file mode 100644 index 0000000000..39bc9ea264 --- /dev/null +++ b/CRM/Core/Smarty/plugins/modifier.crmRSSPubDate.php @@ -0,0 +1,36 @@ +format($date::RSS); + } + catch (Exception $e) { + // fall through + } + } + return $now->format($now::RSS); +} diff --git a/templates/CRM/Core/Calendar/Rss.tpl b/templates/CRM/Core/Calendar/Rss.tpl index ffb5259b5f..ed85ad03f1 100644 --- a/templates/CRM/Core/Calendar/Rss.tpl +++ b/templates/CRM/Core/Calendar/Rss.tpl @@ -19,6 +19,8 @@ {foreach from=$events key=uid item=event} {$event.title|escape:'html'} +{* pubDate must follow RFC822 format *} +{$event.start_date|crmRSSPubDate} {crmURL p='civicrm/event/info' q="reset=1&id=`$event.event_id`" fe=1 a=1} {if $event.summary}{$event.summary|escape:'html'} diff --git a/tests/phpunit/CRM/Core/Smarty/plugins/CrmRSSPubDateTest.php b/tests/phpunit/CRM/Core/Smarty/plugins/CrmRSSPubDateTest.php new file mode 100644 index 0000000000..1b967ef809 --- /dev/null +++ b/tests/phpunit/CRM/Core/Smarty/plugins/CrmRSSPubDateTest.php @@ -0,0 +1,76 @@ + ['2021-02-03 13:14:15', 'Wed, 03 Feb 2021 13:14:15 ' . (new DateTime('2021-02-03 13:14:15'))->format('O')], + 1 => ['2021-02-03', 'Wed, 03 Feb 2021 00:00:00 ' . (new DateTime('2021-02-03'))->format('O')], + 2 => ['2021-12-13 04:05:06', 'Mon, 13 Dec 2021 04:05:06 ' . (new DateTime('2021-12-13 04:05:06'))->format('O')], + 3 => ['2021-12-13 04:05', 'Mon, 13 Dec 2021 04:05:00 ' . (new DateTime('2021-12-13 04:05'))->format('O')], + ]; + } + + /** + * @dataProvider dateList + * @param string $input + * @param string $expected + */ + public function testRSSPubDate(string $input, string $expected) { + $smarty = CRM_Core_Smarty::singleton(); + $smarty->assign('the_date', $input); + $actual = $smarty->fetch('string:{$the_date|crmRSSPubDate}'); + $this->assertEquals($expected, $actual); + } + + /** + * DataProvider for testRSSPubDateBad + * @return array + */ + public function dateListBad(): array { + $fixedDate = self::FIXED_DATE_RSS . ' ' . (new DateTime(self::FIXED_DATE))->format('O'); + // explicit indexes to make it easier to see which one failed + return [ + 0 => ['', $fixedDate], + 1 => [NULL, $fixedDate], + // smarty itself gives an error here before even getting to the modifier function, so we can't easily test this + // 2 => [[], ''], + 2 => ['nap time', $fixedDate], + 3 => ['0', $fixedDate], + 4 => [0, $fixedDate], + 5 => [1, $fixedDate], + ]; + } + + /** + * Test that invalid inputs return "today"'s date. + * + * @dataProvider dateListBad + * @param mixed $input + * @param string $expected + */ + public function testRSSPubDateBad($input, string $expected) { + putenv('TIME_FUNC=frozen'); + CRM_Utils_Time::setTime(self::FIXED_DATE); + + $smarty = CRM_Core_Smarty::singleton(); + $smarty->assign('the_date', $input); + $actual = $smarty->fetch('string:{$the_date|crmRSSPubDate}'); + $this->assertEquals($expected, $actual); + } + +} -- 2.25.1