From e66b736aa6c2e00643b9b061d7064c7991503390 Mon Sep 17 00:00:00 2001 From: Joel Uckelman Date: Mon, 27 Feb 2012 05:54:36 +0100 Subject: Remove Edit marker from incoming mail, add it to outgoing mail. --- src/build_email.php | 15 ++-- src/build_post.php | 5 +- test/build_emailTest.php | 178 +++++++++++++++++++++++++++++++++++++++++++++++ test/build_postTest.php | 43 ++++++++++++ 4 files changed, 231 insertions(+), 10 deletions(-) create mode 100644 test/build_emailTest.php create mode 100644 test/build_postTest.php diff --git a/src/build_email.php b/src/build_email.php index a5bb7bd..567da0d 100644 --- a/src/build_email.php +++ b/src/build_email.php @@ -97,7 +97,7 @@ function build_email_from($name, $email) { return sprintf('%s <%s>', $qname, $email); } -function build_email_subject($forumtag, $reply, $subject) { +function build_email_subject($forumtag, $subject, $reply, $edit) { $subject = trim($subject); if ($subject == '') { $subject = '(no subject)'; @@ -109,22 +109,21 @@ function build_email_subject($forumtag, $reply, $subject) { $subject = 'Re: ' . $subject; } + if ($edit) { + $subject = 'Edit: ' . $subject; + } + return utf8_quote_non_ascii($subject); } function build_email_headers( - $userName, $userEmail, $to, $sender, $subject, $edit, - $time, $messageId, $forumURL, $inReplyTo, $references) + $userName, $userEmail, $to, $sender, $subject, $time, + $messageId, $forumURL, $inReplyTo, $references) { $from = build_email_from($userName, $userEmail); $subject = utf8_quote_non_ascii($subject); $date = date(DATE_RFC2822, $time); - if ($edit) { - $edit_header = 'Edit: '; - $subject = $edit_header . $subject; - } - $headers = array( 'To' => $to, 'From' => $from, diff --git a/src/build_post.php b/src/build_post.php index 98f6dc7..aa0cbf6 100644 --- a/src/build_post.php +++ b/src/build_post.php @@ -6,8 +6,9 @@ function build_post_subject($listtag, $forumtag, $subject) { '|' . preg_quote($forumtag, '/') . ')\\s*/'; $subj = preg_replace($tagpat, '', $subject); - // strip leading sequences of Re-equivalents - if (preg_match('/^(?:(?:RE|AW|SV|VS)(?:\\[\\d+\\])?:\\s*)+/i', $subj, $m)) { + // strip leading sequences of Re-equivalents and Edit + $re = '/^(?:(?:RE|AW|SV|VS|EDIT)(?:\\[\\d+\\])?:\\s*)+/i'; + if (preg_match($re, $subj, $m)) { $subj = substr($subj, strlen($m[0])); } diff --git a/test/build_emailTest.php b/test/build_emailTest.php new file mode 100644 index 0000000..5730873 --- /dev/null +++ b/test/build_emailTest.php @@ -0,0 +1,178 @@ +assertEquals($expected, build_email_text($text, $edit)); + } + + public function build_email_text_data() { + return array( + array('foo bar', false, 'foo bar'), + array('foo bar', true, "[This message has been edited.] + +foo bar") + ); + } + + public function test_build_email_footer() { + $this->assertEquals( + " +_______________________________________________ +Read this topic online here: +http://www.example.com/viewtopic.php?p=42#p42", + build_email_footer(42, 'http://www.example.com') + ); + } + + /** @dataProvider build_email_from_data */ + public function test_build_email_from($name, $email, $expected) { + $this->assertEquals($expected, build_email_from($name, $email)); + } + + public function build_email_from_data() { + return array( + array('Heizölrückstoßabdämpfung', 'foo@example.com', '=?UTF-8?B?SGVpesO2bHLDvGNrc3Rvw59hYmTDpG1wZnVuZw==?= '), + array('Joel Uckelman', 'uckelman@nomic.net', 'Joel Uckelman '), + array('L.Tankersley', 'leland53@comcast.net', '"L.Tankersley" ') + ); + } + + /** @dataProvider build_email_subject_data */ + public function test_build_email_subject($ftag, $subject, $reply, $edit, $expected) { + $this->assertEquals( + $expected, + build_email_subject($ftag, $subject, $reply, $edit) + ); + } + + public function build_email_subject_data() { + return array( + array('[f]', '', false, false, '[f] (no subject)'), + array('[f]', '', true, false, 'Re: [f] (no subject)'), + array('[f]', 'Subject', false, false, '[f] Subject'), + array('[f]', 'Subject', true, false, 'Re: [f] Subject'), + array('[f]', '', false, true, 'Edit: [f] (no subject)'), + array('[f]', '', true, true, 'Edit: Re: [f] (no subject)'), + array('[f]', 'Subject', false, true, 'Edit: [f] Subject'), + array('[f]', 'Subject', true, true, 'Edit: Re: [f] Subject') + ); + } + + protected $default_headers = array( + 'To' => 'messages@vassalengine.org', + 'From' => 'Joel Uckelman ', + 'Sender' => 'forum-bridge@vassalengine.org', + 'Subject' => 'Test message', + 'Date' => 'Sun, 31 Oct 2010 08:46:00 -0700', + 'Message-ID' => '<20100302094228.33F0310091@charybdis.ellipsis.cx>', + 'X-BeenThere' => 'http://www.example.com', + 'In-Reply-To' => '<1267473003.m2f.17543@www.vassalengine.org>', + 'References' => '<1267171317.m2f.17507@www.vassalengine.org> <1267473003.m2f.17543@www.vassalengine.org>' + ); + + protected $default_headers_params = array( + 'Joel Uckelman', + 'uckelman@nomic.net', + 'messages@vassalengine.org', + 'forum-bridge@vassalengine.org', + 'Test message', + 1288539960, + '<20100302094228.33F0310091@charybdis.ellipsis.cx>', + 'http://www.example.com', + '<1267473003.m2f.17543@www.vassalengine.org>', + '<1267171317.m2f.17507@www.vassalengine.org> <1267473003.m2f.17543@www.vassalengine.org>' + ); + + protected function call_build_email_headers(array $headers, array $params) { + date_default_timezone_set('America/Phoenix'); + $this->assertEquals( + $headers, + build_email_headers( + $params[0], + $params[1], + $params[2], + $params[3], + $params[4], + $params[5], + $params[6], + $params[7], + $params[8], + $params[9] + ) + ); + } + + public function test_build_email_headers() { + $headers = $this->default_headers; + $headers_params = $this->default_headers_params; + $this->call_build_email_headers($headers, $headers_params); + } + + public function test_build_email_headers_no_in_reply_to() { + $headers = $this->default_headers; + $headers_params = $this->default_headers_params; + + unset($headers['In-Reply-To']); + $headers_params[8] = null; + + $this->call_build_email_headers($headers, $headers_params); + } + + public function test_build_email_headers_no_references() { + $headers = $this->default_headers; + $headers_params = $this->default_headers_params; + + unset($headers['References']); + $headers_params[9] = null; + + $this->call_build_email_headers($headers, $headers_params); + } + + public function test_build_email_headers_utf8_subject() { + $headers = $this->default_headers; + $headers_params = $this->default_headers_params; + + $headers['Subject'] = '=?UTF-8?B?SGVpesO2bHLDvGNrc3Rvw59hYmTDpG1wZnVuZw==?='; + $headers_params[4] = 'Heizölrückstoßabdämpfung'; + + $this->call_build_email_headers($headers, $headers_params); + } + + public function test_build_email_headers_utf8_username() { + $headers = $this->default_headers; + $headers_params = $this->default_headers_params; + + $headers['From'] = '=?UTF-8?B?SGVpesO2bHLDvGNrc3Rvw59hYmTDpG1wZnVuZw==?= '; + $headers_params[0] = 'Heizölrückstoßabdämpfung'; + + $this->call_build_email_headers($headers, $headers_params); + } + + public function test_build_email_body_no_attachments() { + $headers = array(); + $text = 'This is some test text.'; + $footer = " +_______________________________________________ +Read this topic online here: +http://www.example.com/viewtopic.php?p=42#p42"; + $attachments = null; + + $body = build_email_body($headers, $text, $attachments, $footer); + + + $this->assertEquals('text/plain; charset=UTF-8; format=flowed', $headers['Content-Type']); + $this->assertEquals('8bit', $headers['Content-Transfer-Encoding']); + $this->assertEquals("$text\n$footer", $body); + } + + public function test_build_email_body_attachments() { + // FIXME: This is kind of a complex test to write, because the result + // is a Mail_mimePart object. + $this->markTestIncomplete(); + } +} + +?> diff --git a/test/build_postTest.php b/test/build_postTest.php new file mode 100644 index 0000000..be542dd --- /dev/null +++ b/test/build_postTest.php @@ -0,0 +1,43 @@ +assertEquals( + $expected, + build_post_subject($ltag, $ftag, $subject) + ); + } + + public function build_post_subject_data() { + return array( + array('[l]', '[f]', '', '(no subject)'), + array('[l]', '[f]', 'Re:', '(no subject)'), + array('[l]', '[f]', 'Subject', 'Subject'), + array('[l]', '[f]', 'Re: Subject', 'Subject'), + array('[l]', '[f]', 'Re: Re: Re: Subject', 'Subject'), + array('[l]', '[f]', '[f] Subject', 'Subject'), + array('[l]', '[f]', '[f] [f] Subject', 'Subject'), + array('[l]', '[f]', '[f] [f] Subject [f]', 'Subject'), + array('[l]', '[f]', '[l] [f] Re: Subject', 'Subject'), + array('[l]', '[f]', 'Re: [l] [f] Subject', 'Subject'), + array('[l]', '[f]', 'Re: Subject [l][f] Subject', 'Subject Subject'), + array('[l]', '[f]', 'Edit:', '(no subject)'), + array('[l]', '[f]', 'Edit: Re:', '(no subject)'), + array('[l]', '[f]', 'Edit: Subject', 'Subject'), + array('[l]', '[f]', 'Edit: Re: Subject', 'Subject'), + array('[l]', '[f]', 'Edit: Re: Re: Re: Subject', 'Subject'), + array('[l]', '[f]', 'Edit: [f] Subject', 'Subject'), + array('[l]', '[f]', 'Edit: [f] [f] Subject', 'Subject'), + array('[l]', '[f]', 'Edit: [f] [f] Subject [f]', 'Subject'), + array('[l]', '[f]', '[l] [f] Edit: Re: Subject', 'Subject'), + array('[l]', '[f]', 'Edit: Re: [l] [f] Subject', 'Subject'), + array('[l]', '[f]', 'Edit: Re: Subject [l][f] Subject', 'Subject Subject') + ); + } +} + +?> -- cgit v1.2.3