From 977d1c851cc9b01fb9a0d15f1f4c0167647f948a Mon Sep 17 00:00:00 2001 From: Mathieu Lutfy Date: Tue, 21 Jun 2022 09:54:05 -0400 Subject: [PATCH] dev/core#1541 Fix ICalendar random invalid utf8 (PHP <7.4 compat) and tests --- CRM/Utils/ICalendar.php | 5 ++++- tests/phpunit/CRM/Utils/ICalendarTest.php | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CRM/Utils/ICalendar.php b/CRM/Utils/ICalendar.php index b23f24f3c1..170dbef162 100644 --- a/CRM/Utils/ICalendar.php +++ b/CRM/Utils/ICalendar.php @@ -38,7 +38,9 @@ class CRM_Utils_ICalendar { $text = str_replace(',', '\,', $text); $text = str_replace(';', '\;', $text); $text = str_replace(["\r\n", "\n", "\r"], "\\n ", $text); - $text = implode("\n ", mb_str_split($text, 50)); + // Remove this check after PHP 7.4 becomes a minimum requirement + $str_split = function_exists('mb_str_split') ? 'mb_str_split' : 'str_split'; + $text = implode("\n ", $str_split($text, 50)); return $text; } @@ -51,6 +53,7 @@ class CRM_Utils_ICalendar { * @return string */ public static function unformatText($text) { + $text = str_replace("\n ", "", $text); $text = str_replace('\n ', "\n", $text); $text = str_replace('\;', ';', $text); $text = str_replace('\,', ',', $text); diff --git a/tests/phpunit/CRM/Utils/ICalendarTest.php b/tests/phpunit/CRM/Utils/ICalendarTest.php index 33eac671ed..180e94b6c4 100644 --- a/tests/phpunit/CRM/Utils/ICalendarTest.php +++ b/tests/phpunit/CRM/Utils/ICalendarTest.php @@ -27,6 +27,8 @@ class CRM_Utils_ICalendarTest extends CiviUnitTestCase { this is, a \"test\"!", ]; + $cases[] = ["one, two, three; aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaahh ahh ahhh"]; + $cases[] = ["Bonjour! éèçô, этому скромному разработчику не нравится война на Украине 💓 💔 🌈 💕 💖"]; return $cases; } -- 2.25.1