CRM_Utils_ICalendar cleanup
authorColeman Watts <coleman@civicrm.org>
Thu, 25 Aug 2016 02:36:02 +0000 (22:36 -0400)
committerColeman Watts <coleman@civicrm.org>
Fri, 26 Aug 2016 21:02:13 +0000 (17:02 -0400)
CRM/Utils/ICalendar.php
tests/phpunit/CRM/Utils/ICalendarTest.php [new file with mode: 0644]

index d765b80f8482f45b123774a744206444083de226..5784ed9cbf1973dd43ea5ce13f24deb6a0d9917b 100644 (file)
@@ -42,23 +42,39 @@ class CRM_Utils_ICalendar {
   /**
    * Escape text elements for safe ICalendar use.
    *
-   * @param $text
+   * @param string $text
    *   Text to escape.
    *
    * @return string
-   *   Escaped text
    */
   public static function formatText($text) {
     $text = strip_tags($text);
     $text = str_replace("\"", "DQUOTE", $text);
     $text = str_replace("\\", "\\\\", $text);
-    $text = str_replace(",", "\,", $text);
-    $text = str_replace(";", "\;", $text);
+    $text = str_replace(',', '\,', $text);
+    $text = str_replace(';', '\;', $text);
     $text = str_replace(array("\r\n", "\n", "\r"), "\\n ", $text);
     $text = implode("\n ", str_split($text, 50));
     return $text;
   }
 
+  /**
+   * Restore iCal formatted text to normal.
+   *
+   * @param string $text
+   *   Text to unescape.
+   *
+   * @return string
+   */
+  public static function unformatText($text) {
+    $text = str_replace('\n ', "\n", $text);
+    $text = str_replace('\;', ';', $text);
+    $text = str_replace('\,', ',', $text);
+    $text = str_replace("\\\\", "\\", $text);
+    $text = str_replace("DQUOTE", "\"", $text);
+    return $text;
+  }
+
   /**
    * Escape date elements for safe ICalendar use.
    *
diff --git a/tests/phpunit/CRM/Utils/ICalendarTest.php b/tests/phpunit/CRM/Utils/ICalendarTest.php
new file mode 100644 (file)
index 0000000..e2f45d2
--- /dev/null
@@ -0,0 +1,55 @@
+<?php
+/*
++--------------------------------------------------------------------+
+| CiviCRM version 4.7                                                |
++--------------------------------------------------------------------+
+| Copyright CiviCRM LLC (c) 2004-2016                                |
++--------------------------------------------------------------------+
+| This file is a part of CiviCRM.                                    |
+|                                                                    |
+| CiviCRM is free software; you can copy, modify, and distribute it  |
+| under the terms of the GNU Affero General Public License           |
+| Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
+|                                                                    |
+| CiviCRM is distributed in the hope that it will be useful, but     |
+| WITHOUT ANY WARRANTY; without even the implied warranty of         |
+| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
+| See the GNU Affero General Public License for more details.        |
+|                                                                    |
+| You should have received a copy of the GNU Affero General Public   |
+| License and the CiviCRM Licensing Exception along                  |
+| with this program; if not, contact CiviCRM LLC                     |
+| at info[AT]civicrm[DOT]org. If you have questions about the        |
+| GNU Affero General Public License or the licensing of CiviCRM,     |
+| see the CiviCRM license FAQ at http://civicrm.org/licensing        |
++--------------------------------------------------------------------+
+ */
+
+/**
+ * Tests for linking to resource files
+ * @group headless
+ */
+class CRM_Utils_ICalendarTest extends CiviUnitTestCase {
+
+  /**
+   * @return array
+   */
+  public function escapeExamples() {
+    $cases = array();
+    $cases[] = array("Hello
+    this is, a test!");
+    $cases[] = array("Hello!!
+
+    this is, a \"test\"!");
+    return $cases;
+  }
+
+  /**
+   * @param string $testString
+   * @dataProvider escapeExamples
+   */
+  public function testParseStrings($testString) {
+    $this->assertEquals($testString, CRM_Utils_ICalendar::unformatText(CRM_Utils_ICalendar::formatText($testString)));
+  }
+
+}