From cb087737e3dd7609ff26833dbdad0e2ff62a9adc Mon Sep 17 00:00:00 2001 From: Ahed Date: Sun, 3 Jan 2021 19:20:19 +0200 Subject: [PATCH] dev/core#2282 Use the proper content type for ICalendar link --- CRM/Event/ICalendar.php | 2 +- CRM/Utils/ICalendar.php | 5 +- tests/phpunit/CRM/Utils/ICalendarTest.php | 103 ++++++++++++++++++++++ 3 files changed, 106 insertions(+), 4 deletions(-) diff --git a/CRM/Event/ICalendar.php b/CRM/Event/ICalendar.php index 8a1fb937df..c68fa5481d 100644 --- a/CRM/Event/ICalendar.php +++ b/CRM/Event/ICalendar.php @@ -70,7 +70,7 @@ class CRM_Event_ICalendar extends CRM_Core_Page { CRM_Utils_ICalendar::send($calendar, 'text/xml', 'utf-8'); } else { - CRM_Utils_ICalendar::send($calendar, 'text/plain', 'utf-8'); + CRM_Utils_ICalendar::send($calendar, 'text/calendar', 'utf-8'); } } else { diff --git a/CRM/Utils/ICalendar.php b/CRM/Utils/ICalendar.php index 6f71bf8b6d..3e2d0a84e3 100644 --- a/CRM/Utils/ICalendar.php +++ b/CRM/Utils/ICalendar.php @@ -85,8 +85,7 @@ class CRM_Utils_ICalendar { /** * Send the ICalendar to the browser with the specified content type - * - 'text/calendar' : used for downloaded ics file - * - 'text/plain' : used for iCal formatted feed + * - 'text/calendar' : used for iCal formatted feed * - 'text/xml' : used for gData or rss formatted feeds * * @@ -106,7 +105,7 @@ class CRM_Utils_ICalendar { CRM_Utils_System::setHttpHeader("Content-Language", $lang); CRM_Utils_System::setHttpHeader("Content-Type", "$content_type; charset=$charset"); - if ($content_type == 'text/calendar') { + if ($fileName) { CRM_Utils_System::setHttpHeader('Content-Length', strlen($calendar)); CRM_Utils_System::setHttpHeader("Content-Disposition", "$disposition; filename=\"$fileName\""); CRM_Utils_System::setHttpHeader("Pragma", "no-cache"); diff --git a/tests/phpunit/CRM/Utils/ICalendarTest.php b/tests/phpunit/CRM/Utils/ICalendarTest.php index 8841ed546c..33eac671ed 100644 --- a/tests/phpunit/CRM/Utils/ICalendarTest.php +++ b/tests/phpunit/CRM/Utils/ICalendarTest.php @@ -38,4 +38,107 @@ class CRM_Utils_ICalendarTest extends CiviUnitTestCase { $this->assertEquals($testString, CRM_Utils_ICalendar::unformatText(CRM_Utils_ICalendar::formatText($testString))); } + /** + * @return array + */ + public function getSendParameters() { + return [ + [ + ['calendar_data', 'text/xml', 'utf-8', NULL, NULL], + [ + 'Content-Language' => 'en_US', + 'Content-Type' => 'text/xml; charset=utf-8', + ], + ], + [ + ['calendar_data', 'text/calendar', 'utf-8', NULL, NULL], + [ + 'Content-Language' => 'en_US', + 'Content-Type' => 'text/calendar; charset=utf-8', + ], + ], + ]; + } + + /** + * Test provided send parameters. + * + * @dataProvider getSendParameters + */ + public function testSendParametersWithoutAttachment($parameters, $expected) { + // we need to capture echo output + ob_start(); + CRM_Utils_ICalendar::send( + $parameters[0], + $parameters[1], + $parameters[2], + $parameters[3], + $parameters[4] + ); + ob_end_clean(); + + $headerList = \Civi::$statics['CRM_Utils_System_UnitTests']['header']; + + // Convert headers from simple array to associative array + $headers = []; + foreach ($headerList as $header) { + $headerParts = explode(': ', $header); + $headers[$headerParts[0]] = $headerParts[1]; + } + + $this->assertEquals($expected['Content-Language'], $headers['Content-Language']); + $this->assertEquals($expected['Content-Type'], $headers['Content-Type']); + $this->assertArrayNotHasKey('Content-Length', $headers); + $this->assertArrayNotHasKey('Content-Disposition', $headers); + $this->assertArrayNotHasKey('Pragma', $headers); + $this->assertArrayNotHasKey('Expires', $headers); + $this->assertArrayNotHasKey('Cache-Control', $headers); + } + + /** + * Test Send with attachment. + */ + public function testSendWithAttachment() { + $parameters = [ + 'calendar_data', 'text/calendar', 'utf-8', 'civicrm_ical.ics', 'attachment', + ]; + $expected = [ + 'Content-Language' => 'en_US', + 'Content-Type' => 'text/calendar; charset=utf-8', + 'Content-Length' => '13', + 'Content-Disposition' => 'attachment; filename="civicrm_ical.ics"', + 'Pragma' => 'no-cache', + 'Expires' => '0', + 'Cache-Control' => 'no-cache, must-revalidate', + ]; + + // we need to capture echo output + ob_start(); + CRM_Utils_ICalendar::send( + $parameters[0], + $parameters[1], + $parameters[2], + $parameters[3], + $parameters[4] + ); + ob_end_clean(); + + $headerList = \Civi::$statics['CRM_Utils_System_UnitTests']['header']; + + // Convert headers from simple array to associative array + $headers = []; + foreach ($headerList as $header) { + $headerParts = explode(': ', $header); + $headers[$headerParts[0]] = $headerParts[1]; + } + + $this->assertEquals($expected['Content-Language'], $headers['Content-Language']); + $this->assertEquals($expected['Content-Type'], $headers['Content-Type']); + $this->assertEquals($expected['Content-Length'], $headers['Content-Length']); + $this->assertEquals($expected['Content-Disposition'], $headers['Content-Disposition']); + $this->assertEquals($expected['Pragma'], $headers['Pragma']); + $this->assertEquals($expected['Expires'], $headers['Expires']); + $this->assertEquals($expected['Cache-Control'], $headers['Cache-Control']); + } + } -- 2.25.1