From e945517d666306d48c82850e310c46a43043705e Mon Sep 17 00:00:00 2001 From: Ankit Agarwal Date: Tue, 28 Apr 2015 11:26:07 +0530 Subject: [PATCH] MDL-48002 inbound: add tests for evolution --- .../fixtures/messageinbound/evolution.test | 125 ++++++++++++++++++ lib/tests/messageinbound_test.php | 38 ++++-- .../classes/message/inbound/reply_handler.php | 72 ---------- 3 files changed, 151 insertions(+), 84 deletions(-) create mode 100644 lib/tests/fixtures/messageinbound/evolution.test diff --git a/lib/tests/fixtures/messageinbound/evolution.test b/lib/tests/fixtures/messageinbound/evolution.test new file mode 100644 index 0000000000000..a4dcd24f45914 --- /dev/null +++ b/lib/tests/fixtures/messageinbound/evolution.test @@ -0,0 +1,125 @@ +----EXPECTEDPLAIN---- +An ion meets his atom friend on the street and says he's lost an +electron. "Are you sure?" asks the atom. The ion replies, "I'm +positive." + +----EXPECTEDHTML---- +An ion meets his atom friend on the street and says he's lost an + electron. "Are you sure?" asks the atom. The ion replies, "I'm positive." + +----FULLSOURCE---- +Message-ID: <1430198383.10608.0.camel@jean> +Subject: Re: [DIVERTED t1@localhost] test: Re: test +From: ankit +To: ankit+AAAAAAAAAAIAAAAAAAAADQAAAAAAAAA3xIUdg5K043cciDEJ@localhost +Date: Tue, 28 Apr 2015 10:49:43 +0530 +In-Reply-To: <77abfbf3eea680a5bd9cd88d2ca74e602b2168c7684d404d5049289f49f30e0f@localhost> +References: + + <77abfbf3eea680a5bd9cd88d2ca74e602b2168c7684d404d5049289f49f30e0f@localhost> +Content-Type: multipart/alternative; boundary="=-HCcF2t8PQgHyHwzJsOEP" +X-Mailer: Evolution 3.10.4-0ubuntu2 +Mime-Version: 1.0 +X-Evolution-Identity: 1416821596.2740.0@jean +X-Evolution-Fcc: folder://local/Sent +X-Evolution-Transport: 1416821597.2740.10@jean +X-Evolution-Source-Folder: folder://1416821596.2740.2%40jean/INBOX +X-Evolution-Source-Message: 298 +X-Evolution-Source-Flags: ANSWERED SEEN +X-Evolution-Format: text/html +X-Evolution-Source: + + +--=-HCcF2t8PQgHyHwzJsOEP +Content-Type: text/plain; charset="UTF-8" +Content-Transfer-Encoding: 8bit + +An ion meets his atom friend on the street and says he's lost an +electron. "Are you sure?" asks the atom. The ion replies, "I'm +positive." +On Tue, 2015-04-28 at 13:18 +0800, Admin User wrote: +> test » Forums » Forum » test +> +> Picture of Admin User +> Re: test +> by Admin User - Tuesday, 28 April +> 2015, 1:17 PM +> +> Tell me a joke +> +> +> Show parent | Reply +> See this post in context +> +> ______________________________________________________________________ +> +> Unsubscribe from this discussion Unsubscribe from all forums Change +> your forum digest preferences +> +> +> You can reply to this forum post via email. +> + + + +--=-HCcF2t8PQgHyHwzJsOEP +Content-Type: text/html; charset="utf-8" +Content-Transfer-Encoding: 7bit + + + + + + + + +An ion meets his atom friend on the street and says he's lost an
+electron. "Are you sure?" asks the atom. The ion replies, "I'm positive."
+On Tue, 2015-04-28 at 13:18 +0800, Admin User wrote: +
+ test » Forums » Forum » test +
+
+ + + + + + + + + +
+Picture of Admin User + +Re: test +by Admin User - Tuesday, 28 April 2015, 1:17 PM +
+  + +Tell me a joke
+
+
+Show parent | Reply +See this post in context +
+ +
+ +
+
+ Unsubscribe from this discussion Unsubscribe from all forums Change your forum digest preferences +
+
+
+ You can reply to this forum post via email.
+
+
+
+ + + + +--=-HCcF2t8PQgHyHwzJsOEP-- + + diff --git a/lib/tests/messageinbound_test.php b/lib/tests/messageinbound_test.php index d367cdadd886c..7f2ffda3056f3 100644 --- a/lib/tests/messageinbound_test.php +++ b/lib/tests/messageinbound_test.php @@ -45,19 +45,23 @@ public function test_messageinbound_handler_trim($file, $source, $expectedplain, $mime = Horde_Mime_Part::parseMessage($source); if ($plainpartid = $mime->findBody('plain')) { - $messagedata = $mime->getPart($plainpartid)->getContents(); + $messagedata = new stdClass(); + $messagedata->plain = $mime->getPart($plainpartid)->getContents(); + $messagedata->html = ''; - $linecount = test_handler::get_linecount_to_remove($messagedata); - $actual = test_handler::remove_quoted_text($messagedata, $linecount); - $this->assertEquals($expectedplain, $actual); + list($message, $format) = test_handler::remove_quoted_text($messagedata); + $this->assertEquals($expectedplain, $message); + $this->assertEquals(FORMAT_PLAIN, $format); } if ($htmlpartid = $mime->findBody('html')) { - $messagedata = html_to_text($mime->getPart($htmlpartid)->getContents()); + $messagedata = new stdClass(); + $messagedata->plain = ''; + $messagedata->html = $mime->getPart($htmlpartid)->getContents(); - $linecount = test_handler::get_linecount_to_remove($messagedata); - $actual = test_handler::remove_quoted_text($messagedata, $linecount); - $this->assertEquals($expectedhtml, $actual); + list($message, $format) = test_handler::remove_quoted_text($messagedata); + $this->assertEquals($expectedhtml, $message); + $this->assertEquals(FORMAT_PLAIN, $format); } } @@ -108,9 +112,10 @@ protected function read_test_file(\SplFileInfo $file, $fixturesdir) { 'EXPECTEDHTML' => true, ); $section = null; + $data = array(); foreach ($tokens as $i => $token) { if (null === $section && empty($token)) { - continue; // skip leading blank + continue; // Skip leading blank. } if (null === $section) { if (!isset($sections[$token])) { @@ -140,13 +145,22 @@ protected function read_test_file(\SplFileInfo $file, $fixturesdir) { } } -class test_handler extends \mod_forum\message\inbound\reply_handler { +/** + * Class test_handler + */ +class test_handler extends \core\message\inbound\handler { - public static function remove_quoted_text($text, $linecount = 1) { - return parent::remove_quoted_text($text, $linecount); + public static function remove_quoted_text($messagedata) { + return parent::remove_quoted_text($messagedata); } public static function get_linecount_to_remove($messagedata) { return parent::get_linecount_to_remove($messagedata); } + + public function get_name() {} + + public function get_description() {} + + public function process_message(stdClass $record, stdClass $messagedata) {} } diff --git a/mod/forum/classes/message/inbound/reply_handler.php b/mod/forum/classes/message/inbound/reply_handler.php index 7baab87c5f04e..e033498ca9a16 100644 --- a/mod/forum/classes/message/inbound/reply_handler.php +++ b/mod/forum/classes/message/inbound/reply_handler.php @@ -303,76 +303,4 @@ public function get_success_message(\stdClass $messagedata, $handlerresult) { $message->html = get_string('postbymailsuccess_html', 'mod_forum', $a); return $message; } - - /** - * Remove quoted message string from the text message. - * - * @param string $text text message. - * @param int $linecount number of lines to remove before quoted text. - * @return mixed|string - */ - protected static function remove_quoted_text($text, $linecount = 1) { - $splitted = preg_split("/\n|\r/", $text); - if (empty($splitted)) { - return $text; - } - - // Remove extra line. "Xyz wrote on...". - $count = 0; - $i = 0; - foreach ($splitted as $i => $element) { - if (stripos($element, ">") === 0) { - // Remove 2 non empty line before this. - for ($j = $i - 1; ($j >= 0); $j--) { - $element = $splitted[$j]; - if (!empty($element)) { - unset($splitted[$j]); - $count++; - } - if ($count == $linecount) { - break; - } - } - break; - } - } - $k = $i - $linecount; // Where to start the chopping process. - - // Remove quoted text. - $splitted = array_slice($splitted, 0, $k); - - // Strip out empty lines towards the end, since a lot of clients add a huge chunk of empty lines. - $reverse = array_reverse($splitted); - foreach ($reverse as $i => $line) { - if (empty($line)) { - unset($reverse[$i]); - } else { - // Non empty line found. - break; - } - } - - $replaced = implode(PHP_EOL, array_reverse($reverse)); - return trim($replaced); - } - - /** - * Try to guess how many lines to remove from the email to delete "xyz wrote on" text. Hard coded numbers for various email - * clients. - * Gmail uses two - * Evolution uses one - * Thunderbird uses one - * - * @param \stdClass $messagedata The Inbound Message record - * - * @return int number of lines to chop off before the start of quoted text. - */ - protected static function get_linecount_to_remove($messagedata) { - $linecount = 1; - if (!empty($messagedata->html) && stripos($messagedata->html, 'gmail_quote') !== false) { - // Gmail uses two lines. - $linecount = 2; - } - return $linecount; - } }