[TASK] Move breakLinesForEmail and add some unit tests
authorDimitri König <dk@cabag.ch>
Sat, 9 Jul 2011 10:08:03 +0000 (12:08 +0200)
committerXavier Perseguers <typo3@perseguers.ch>
Tue, 12 Jul 2011 11:03:19 +0000 (13:03 +0200)
Clean-up on t3lib_div by moving out method breakLinesForEmail to
t3lib_utility_Mail.

Change-Id: Icc80c3dc97ebf0af61f1abf9316c840ed53a796e
Resolves: #28028
Reviewed-on: http://review.typo3.org/3181
Reviewed-by: Dimitri Koenig
Tested-by: Dimitri Koenig
Reviewed-by: Xavier Perseguers
Tested-by: Xavier Perseguers
t3lib/class.t3lib_div.php
t3lib/utility/class.t3lib_utility_mail.php
tests/t3lib/utility/class.t3lib_utility_mailTest.php

index 1df596f..6a8c2a6 100644 (file)
@@ -383,39 +383,14 @@ final class t3lib_div {
         * Breaks up a single line of text for emails
         *
         * @param string $str The string to break up
-        * @param string $implChar The string to implode the broken lines with (default/typically \n)
-        * @param integer $charWidth The line length
-        * @return string
+        * @param string $newlineChar The string to implode the broken lines with (default/typically \n)
+        * @param integer $lineWidth The line width
+        * @return string reformatted text
+        * @deprecated since TYPO3 4.6, will be removed in TYPO3 4.8 - Use t3lib_utility_Mail::breakLinesForPlainEmail()
         */
-       public static function breakLinesForEmail($str, $implChar = LF, $charWidth = 76) {
-               $lines = array();
-               $l = $charWidth;
-               $p = 0;
-               while (strlen($str) > $p) {
-                       $substr = substr($str, $p, $l);
-                       if (strlen($substr) == $l) {
-                               $count = count(explode(' ', trim(strrev($substr))));
-                               if ($count > 1) { // OK...
-                                       $parts = explode(' ', strrev($substr), 2);
-                                       $theLine = strrev($parts[1]);
-                               } else {
-                                       $afterParts = explode(' ', substr($str, $l + $p), 2);
-                                       $theLine = $substr . $afterParts[0];
-                               }
-                               if (!strlen($theLine)) {
-                                       break;
-                               } // Error, because this would keep us in an endless loop.
-                       } else {
-                               $theLine = $substr;
-                       }
-
-                       $lines[] = trim($theLine);
-                       $p += strlen($theLine);
-                       if (!trim(substr($str, $p, $l))) {
-                               break;
-                       } // added...
-               }
-               return implode($implChar, $lines);
+       public static function breakLinesForEmail($str, $newlineChar = LF, $lineWidth = 76) {
+               self::logDeprecatedFunction();
+               return t3lib_utility_Mail::breakLinesForPlainEmail($str, $newlineChar, $lineWidth);
        }
 
        /**
index 0ffaecf..69bb034 100644 (file)
@@ -199,6 +199,53 @@ final class t3lib_utility_Mail {
 
                return $address;
        }
+
+       /**
+        * Breaks up a single line of text for emails
+        *
+        * @param string $str The string to break up
+        * @param string $newlineChar The string to implode the broken lines with (default/typically \n)
+        * @param integer $lineWidth The line width
+        * @return string reformated text
+        */
+       public static function breakLinesForEmail($str, $newlineChar = LF, $lineWidth = 76) {
+               $lines = array();
+               $substrStart = 0;
+               while (strlen($str) > $substrStart) {
+                       $substr = substr($str, $substrStart, $lineWidth);
+
+                               // has line exceeded (reached) the maximum width?
+                       if (strlen($substr) == $lineWidth) {
+                                       // find last space-char
+                               $count = count(explode(' ', trim(strrev($substr))));
+                                       // space-char found?
+                               if ($count > 1) {
+                                               // take everything up to last space-char
+                                       $parts = explode(' ', strrev($substr), 2);
+                                       $theLine = strrev($parts[1]);
+                               } else {
+                                               // search for space-char in remaining text
+                                               // makes this line longer than $lineWidth!
+                                       $afterParts = explode(' ', substr($str, $lineWidth + $substrStart), 2);
+                                       $theLine = $substr . $afterParts[0];
+                               }
+                               if (!strlen($theLine)) {
+                                               // prevent endless loop because of empty line
+                                       break;
+                               }
+                       } else {
+                               $theLine = $substr;
+                       }
+
+                       $lines[] = trim($theLine);
+                       $substrStart += strlen($theLine);
+                       if (trim(substr($str, $substrStart, $lineWidth)) === '') {
+                                       // no more text
+                               break;
+                       }
+               }
+               return implode($newlineChar, $lines);
+       }
 }
 
 ?>
\ No newline at end of file
index 2f6ca2c..c08f4d1 100644 (file)
@@ -30,7 +30,7 @@
  *
  * @author Oliver Klee <typo3-coding@oliverklee.de>
  */
-class t3lib_utility_mailTest extends tx_phpunit_testcase {
+class t3lib_utility_MailTest extends tx_phpunit_testcase {
        /**
         * backed-up TYPO3_CONF_VARS SC_OPTIONS
         *
@@ -94,5 +94,42 @@ class t3lib_utility_mailTest extends tx_phpunit_testcase {
                        $additionalParameters
                );
        }
+
+    /**
+     * @test
+     */
+    public function breakLinesForEmailReturnsEmptyStringIfEmptryStringIsGiven() {
+        $this->assertEmpty(
+            t3lib_utility_Mail::breakLinesForEmail('')
+        );
+    }
+
+    /**
+     * @test
+     */
+    public function breakLinesForEmailReturnsOneLineIfCharWithIsNotExceeded() {
+        $newlineChar = LF;
+        $lineWidth = 76;
+        $str = 'This text is not longer than 76 chars and therefore will not be broken.';
+        $returnString = t3lib_utility_Mail::breakLinesForEmail($str, $newlineChar, $lineWidth);
+        $this->assertEquals(
+            1,
+            count(explode($newlineChar, $returnString))
+        );
+    }
+
+    /**
+     * @test
+     */
+    public function breakLinesForEmailBreaksTextIfCharWithIsExceeded() {
+        $newlineChar = LF;
+        $lineWidth = 50;
+        $str = 'This text is longer than 50 chars and therefore will be broken.';
+        $returnString = t3lib_utility_Mail::breakLinesForEmail($str, $newlineChar, $lineWidth);
+        $this->assertEquals(
+            2,
+            count(explode($newlineChar, $returnString))
+        );
+    }
 }
 ?>
\ No newline at end of file