Files
DomFramework/Tests/MailTest.php
2022-11-25 21:21:30 +01:00

235 lines
9.1 KiB
PHP

<?php
/** DomFramework - Tests
* @package domframework
* @author Dominique Fournier <dominique@fournier38.fr>
* @license BSD
*/
namespace Domframework\Tests;
use Domframework\Mail;
/** Test the Mail.php file */
class MailTest extends \PHPUnit_Framework_TestCase
{
public function testSetBodyText1()
{
$mail = new Mail();
$mail->setBodyText("TEST");
$res = $mail->getMail();
$this->assertRegExp("#TEST\n$#", $res);
}
public function testSetBodyHTML1()
{
$mail = new Mail();
$mail->setBodyHTML("TEST");
$res = $mail->getMail();
$this->assertRegExp("#^\r\nTEST\n$#m", $res);
}
public function testSetGetBodyText1()
{
$mail = new Mail();
$mail->setBodyText("TEST");
$res = $mail->getBodyText();
$this->assertSame("TEST\n", $res);
}
public function testSetGetBodyHtml1()
{
$mail = new Mail();
$mail->setBodyHtml("TEST");
$res = $mail->getBodyHtml();
$this->assertSame("TEST\n", $res);
}
public function testSetFrom1()
{
$mail = new Mail();
$mail->setFrom("test@example.com", "Test Exa1mple Com");
$res = $mail->getFrom();
$this->assertSame("Test Exa1mple Com <test@example.com>\r\n", $res);
}
public function testAddTo1()
{
$mail = new Mail();
$mail->addTo("test@example.com", "Test Exa1mple Com");
$res = $mail->getTo();
$this->assertSame("Test Exa1mple Com <test@example.com>\r\n", $res);
}
public function testAddTo2()
{
$mail = new Mail();
$mail->addTo("test@example.com", "Test Exa1mple Com");
$res = $mail->getMail();
$this->assertRegexp("#^To: Test Exa1mple Com <test@example.com>\r$#m", $res);
}
public function testSetDateTimestamp1()
{
$mail = new Mail();
$time = time();
$mail->setDateTimestamp($time);
$res = $mail->getDateTimestamp();
$this->assertSame($res, $time);
}
public function testSetDateTimestamp2()
{
$mail = new Mail();
$time = time();
$mail->setDateTimestamp($time);
$res = $mail->getMail();
$this->assertRegexp("#^Date: " . preg_quote(date("r", $time)) . "\r$#m", $res);
}
public function testConvertPeopleToArray1()
{
$mail = new Mail();
$res = $mail->convertPeopleToArray("toto toto <toto@toto.com>");
$this->assertSame($res, array (array ("name" => "toto toto",
"mail" => "toto@toto.com")));
}
public function testConvertPeopleToArray2()
{
$mail = new Mail();
$res = $mail->convertPeopleToArray("<toto@toto.com>");
$this->assertSame($res, array (array ("name" => "",
"mail" => "toto@toto.com")));
}
public function testConvertPeopleToArray3()
{
$mail = new Mail();
$res = $mail->convertPeopleToArray("toto@toto.com,titi@titi.com");
$this->assertSame($res, array (array ("name" => "",
"mail" => "toto@toto.com"),
array ("name" => "",
"mail" => "titi@titi.com")));
}
public function testConvertPeopleToArray4()
{
$mail = new Mail();
$res = $mail->convertPeopleToArray("toto@toto.com");
$this->assertSame($res, array (array ("name" => "",
"mail" => "toto@toto.com")));
}
public function testConvertPeopleToArray5()
{
$mail = new Mail();
$res = $mail->convertPeopleToArray(" <toto@toto.com> , <titi@titi.com>");
$this->assertSame($res, array (array ("name" => "",
"mail" => "toto@toto.com"),
array ("name" => "",
"mail" => "titi@titi.com")));
}
public function testConvertPeopleToArray6()
{
$mail = new Mail();
$res = $mail->convertPeopleToArray("ToTo <toto@toto.com> , <titi@titi.com>");
$this->assertSame($res, array (array ("name" => "ToTo",
"mail" => "toto@toto.com"),
array ("name" => "",
"mail" => "titi@titi.com")));
}
/** Read all the headers and return them as JSON object
*/
public function testHeadersToJSON1()
{
$mail = new Mail();
$mail->readMail( // {{{
"Return-Path: <dominique.fournier@grenoble.cnrs.fr>
Delivered-To: dominique@fournier38.fr
Received: from mail-postfix-mx.fournier38.fr ([192.168.1.103])
by mail-dovecot.fournier38.fr with LMTP
id z706BqQt7V15IAAAA28uxw
(envelope-from <dominique.fournier@grenoble.cnrs.fr>)
for <dominique@fournier38.fr>; Sun, 08 Dec 2019 18:06:44 +0100
Received: from mailgw-out1.grenoble.cnrs.fr (mailgw-out1.grenoble.cnrs.fr [147.173.1.68])
(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
(No client certificate requested)
by mail-postfix-mx.fournier38.fr (Postfix) with ESMTPS id 8A2DF820733
for <dominique@fournier38.fr>; Sun, 8 Dec 2019 18:06:43 +0100 (CET)
DMARC-Filter: OpenDMARC Filter v1.3.2 mail-postfix-mx.fournier38.fr 8A2DF820733
Delivered-To: dominique.fournier@grenoble.cnrs.fr
Received: from [192.168.1.154] (82-64-55-197.subs.proxad.net [82.64.55.197])
(using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits))
(No client certificate requested)
by mailgw-out1.grenoble.cnrs.fr (Postfix) with ESMTPSA id 458EDBFCE4
for <dominique@fournier38.fr>; Sun, 8 Dec 2019 18:08:06 +0100 (CET)
From: Dominique Fournier <dominique.fournier@grenoble.cnrs.fr>
Subject: Analyse de texte
To: Dominique Fournier / Fournier38 <dominique@fournier38.fr>
Message-ID: <81d7982f-7fee-96b4-078f-a6365234356f@grenoble.cnrs.fr>
Date: Sun, 8 Dec 2019 18:08:06 +0100
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101
Thunderbird/68.2.2
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-US
Content-Transfer-Encoding: 7bit
Authentication-Results: mail-postfix-mx.fournier38.fr;
dkim=none;
dmarc=none;
spf=pass (mail-postfix-mx.fournier38.fr: domain of dominique.fournier@grenoble.cnrs.fr designates 147.173.1.68 as permitted sender) smtp.mailfrom=dominique.fournier@grenoble.cnrs.fr
X-Spamd-Bar: --------------------------------------------------------------------------------------------------
https://openclassrooms.com/fr/courses/4470541-analysez-vos-donnees-textuelles/4854971-nettoyez-et-normalisez-les-donnees
http://www.nltk.org/nltk_data/
http://www.cs.cmu.edu/~ralf/langid.html
"
);
// }}}
$this->assertSame($mail->getHeaders(), array (
["Return-Path" => "<dominique.fournier@grenoble.cnrs.fr>\n"],
["Delivered-To" => "dominique@fournier38.fr\n"],
["Received" => "from mail-postfix-mx.fournier38.fr ([192.168.1.103])
by mail-dovecot.fournier38.fr with LMTP
id z706BqQt7V15IAAAA28uxw
(envelope-from <dominique.fournier@grenoble.cnrs.fr>)
for <dominique@fournier38.fr>; Sun, 08 Dec 2019 18:06:44 +0100\n"],
["Received" => "from mailgw-out1.grenoble.cnrs.fr (mailgw-out1.grenoble.cnrs.fr [147.173.1.68])
(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
(No client certificate requested)
by mail-postfix-mx.fournier38.fr (Postfix) with ESMTPS id 8A2DF820733
for <dominique@fournier38.fr>; Sun, 8 Dec 2019 18:06:43 +0100 (CET)\n"],
["DMARC-Filter" => "OpenDMARC Filter v1.3.2 mail-postfix-mx.fournier38.fr 8A2DF820733\n"],
["Delivered-To" => "dominique.fournier@grenoble.cnrs.fr\n"],
["Received" => "from [192.168.1.154] (82-64-55-197.subs.proxad.net [82.64.55.197])
(using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits))
(No client certificate requested)
by mailgw-out1.grenoble.cnrs.fr (Postfix) with ESMTPSA id 458EDBFCE4
for <dominique@fournier38.fr>; Sun, 8 Dec 2019 18:08:06 +0100 (CET)\n"],
["From" => "Dominique Fournier <dominique.fournier@grenoble.cnrs.fr>\n"],
["Subject" => "Analyse de texte\n"],
["To" => "Dominique Fournier / Fournier38 <dominique@fournier38.fr>\n"],
["Message-ID" => "<81d7982f-7fee-96b4-078f-a6365234356f@grenoble.cnrs.fr>\n"],
["Date" => "Sun, 8 Dec 2019 18:08:06 +0100\n"],
["User-Agent" => "Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101
Thunderbird/68.2.2\n"],
["MIME-Version" => "1.0\n"],
["Content-Type" => "text/plain; charset=utf-8; format=flowed\n"],
["Content-Language" => "en-US\n"],
["Content-Transfer-Encoding" => "7bit\n"],
["Authentication-Results" => "mail-postfix-mx.fournier38.fr;
dkim=none;
dmarc=none;
spf=pass (mail-postfix-mx.fournier38.fr: domain of dominique.fournier@grenoble.cnrs.fr designates 147.173.1.68 as permitted sender) smtp.mailfrom=dominique.fournier@grenoble.cnrs.fr\n"],
["X-Spamd-Bar" => "--------------------------------------------------------------------------------------------------\n"],
));
}
}