Skip to content

Commit

Permalink
MDL-48002 inbound: add tests for evolution
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitagarwal committed May 4, 2015
1 parent c712250 commit e945517
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 84 deletions.
125 changes: 125 additions & 0 deletions 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 <ankit@localhost>
To: ankit+AAAAAAAAAAIAAAAAAAAADQAAAAAAAAA3xIUdg5K043cciDEJ@localhost
Date: Tue, 28 Apr 2015 10:49:43 +0530
In-Reply-To: <77abfbf3eea680a5bd9cd88d2ca74e602b2168c7684d404d5049289f49f30e0f@localhost>
References:
<cc33da86ad1b5b11e72155da74ca23c92e5e295c534f724162172905dee7a2fb@localhost>
<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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8">
<META NAME="GENERATOR" CONTENT="GtkHTML/4.6.6">
</HEAD>
<BODY>
An ion meets his atom friend on the street and says he's lost an<BR>
electron. &quot;Are you sure?&quot; asks the atom. The ion replies, &quot;I'm positive.&quot;<BR>
On Tue, 2015-04-28 at 13:18 +0800, Admin User wrote:
<BLOCKQUOTE TYPE=CITE>
<A HREF="http://localhost/stable_master_forum/course/view.php?id=2">test</A> &#187; <A HREF="http://localhost/stable_master_forum/mod/forum/index.php?id=2">Forums</A> &#187; <A HREF="http://localhost/stable_master_forum/mod/forum/view.php?f=2">Forum</A> &#187; <A HREF="http://localhost/stable_master_forum/mod/forum/discuss.php?d=1">test</A>
</BLOCKQUOTE>
<BLOCKQUOTE TYPE=CITE>
<TABLE CELLSPACING="0" CELLPADDING="3">
<TR>
<TD WIDTH="35" VALIGN="top">
<A HREF="http://localhost/stable_master_forum/user/view.php?id=2&amp;course=2"><IMG SRC="http://localhost/stable_master_forum/theme/image.php?theme=clean&amp;component=core&amp;image=u%2Ff2&amp;svg=0" WIDTH="35" HEIGHT="35" ALIGN="bottom" ALT="Picture of Admin User"></A>
</TD>
<TD>
Re: test
by <A HREF="http://localhost/stable_master_forum/user/view.php?id=2&amp;course=2">Admin User</A> - Tuesday, 28 April 2015, 1:17 PM
</TD>
</TR>
<TR>
<TD VALIGN="top">
&nbsp;
</TD>
<TD>
Tell me a joke<BR>
<BR>
<BR>
<A HREF="http://localhost/stable_master_forum/mod/forum/discuss.php?d=1&amp;parent=1">Show parent</A> | <A HREF="http://localhost/stable_master_forum/mod/forum/post.php?reply=55">Reply</A>
<A HREF="http://localhost/stable_master_forum/mod/forum/discuss.php?d=1#p55">See this post in context</A>
</TD>
</TR>
</TABLE>

<HR ALIGN="center">

</BLOCKQUOTE>
<BLOCKQUOTE TYPE=CITE>
<A HREF="http://localhost/stable_master_forum/mod/forum/subscribe.php?id=2&amp;d=1">Unsubscribe from this discussion</A>&nbsp;<A HREF="http://localhost/stable_master_forum/mod/forum/unsubscribeall.php">Unsubscribe from all forums</A>&nbsp;<A HREF="http://localhost/stable_master_forum/mod/forum/index.php?id=2">Change your forum digest preferences</A>
</BLOCKQUOTE>
<BLOCKQUOTE TYPE=CITE>
<BR>
You can reply to this forum post via email.<BR>
<BR>
</BLOCKQUOTE>
<BR>
</BODY>
</HTML>


--=-HCcF2t8PQgHyHwzJsOEP--


38 changes: 26 additions & 12 deletions lib/tests/messageinbound_test.php
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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])) {
Expand Down Expand Up @@ -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) {}
}
72 changes: 0 additions & 72 deletions mod/forum/classes/message/inbound/reply_handler.php
Expand Up @@ -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;
}
}

0 comments on commit e945517

Please sign in to comment.