From 0ee5ed1e998c1e67c4a0b7687977c4e2e0f29494 Mon Sep 17 00:00:00 2001
From: Andreas Gohr <andi@splitbrain.org>
Date: Sat, 12 Oct 2013 12:41:24 +0200
Subject: [PATCH] IETF's message lint service as a test for Mailer class

---
 _test/tests/inc/mailer.test.php | 56 +++++++++++++++++++++++++++++++++
 1 file changed, 56 insertions(+)

diff --git a/_test/tests/inc/mailer.test.php b/_test/tests/inc/mailer.test.php
index ef78692b3..fefb6f508 100644
--- a/_test/tests/inc/mailer.test.php
+++ b/_test/tests/inc/mailer.test.php
@@ -156,5 +156,61 @@ class mailer_test extends DokuWikiTest {
         $this->assertEquals(0, preg_match('/(^|\n)Bcc: (\n|$)/', $header), 'Bcc found in headers.');
         $this->assertEquals(0, preg_match('/(^|\n)Cc: (\n|$)/', $header), 'Bcc found in headers.');
     }
+
+    /**
+     * @group internet
+     */
+    function test_lint(){
+        // prepare a simple multipart message
+        $mail = new TestMailer();
+        $mail->to(array('Möp <moep@example.com> ',' foo <foo@example.com>'));
+        $mail->subject('This is a töst');
+        $mail->setBody('Hello Wörld,
+
+        please don\'t burn, okay?
+        ');
+        $mail->attachContent('some test data', 'text/plain', 'text.txt');
+        $msg = $mail->dump();
+        $msglines = explode("\n", $msg);
+
+        // ask message lint if it is okay
+        $html = new HTTPClient();
+        $results = $html->post('http://tools.ietf.org/tools/msglint/msglint', array('msg'=>$msg));
+        $this->assertTrue($results !== false);
+
+        // parse the result lines
+        $lines = explode("\n", $results);
+        $rows  = count($lines);
+        $i=0;
+        while(trim($lines[$i]) != '-----------' && $i<$rows) $i++; //skip preamble
+        for($i=$i+1; $i<$rows; $i++){
+            $line = trim($lines[$i]);
+            if($line == '-----------') break; //skip appendix
+
+            // get possible continuation of the line
+            while($lines[$i+1][0] == ' '){
+                $line .= ' '.trim($lines[$i+1]);
+                $i++;
+            }
+
+            // check the line for errors
+            if(substr($line,0,5) == 'ERROR'){
+                // get the context in which the error occured
+                $errorin = '';
+                if(preg_match('/line (\d+)$/', $line, $m)){
+                    $errorin .= "\n".$msglines[$m[1] - 1];
+                }
+                if(preg_match('/lines (\d+)-(\d+)$/', $line, $m)){
+                    for($x=$m[1]-1; $x<$m[2]; $x++){
+                        $errorin .= "\n".$msglines[$x];
+                    }
+                }
+
+                // raise the error
+                throw new Exception($line.$errorin);
+            }
+        }
+
+    }
 }
 //Setup VIM: ex: et ts=4 :
-- 
GitLab