This commit is contained in:
Jim Jagielski 2018-11-05 08:15:17 -05:00
commit cf58579543
1 changed files with 123 additions and 22 deletions

View File

@ -12,6 +12,7 @@
namespace PHPMailer\Test;
use PHPMailer\PHPMailer\OAuth;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\POP3;
use PHPUnit\Framework\TestCase;
@ -659,6 +660,8 @@ final class PHPMailerTest extends TestCase
$this->assertFalse(PHPMailer::validateAddress('test@example.com.', 'pcre'));
$this->assertTrue(PHPMailer::validateAddress('test@example.com', 'pcre8'));
$this->assertFalse(PHPMailer::validateAddress('test@example.com.', 'pcre8'));
$this->assertTrue(PHPMailer::validateAddress('test@example.com', 'html5'));
$this->assertFalse(PHPMailer::validateAddress('test@example.com.', 'html5'));
$this->assertTrue(PHPMailer::validateAddress('test@example.com', 'php'));
$this->assertFalse(PHPMailer::validateAddress('test@example.com.', 'php'));
$this->assertTrue(PHPMailer::validateAddress('test@example.com', 'noregex'));
@ -939,6 +942,37 @@ EOT;
$this->assertNotContains("\r\n\r\nMIME-Version:", $msg, 'Incorrect MIME headers');
}
/**
* createBody test of switch case
*/
public function testCreateBody()
{
$PHPMailer = new PHPMailer();
$reflection = new \ReflectionClass($PHPMailer);
$property = $reflection->getProperty('message_type');
$property->setAccessible(true);
$property->setValue($PHPMailer, 'inline');
$this->assertInternalType('string', $PHPMailer->createBody());
$property->setValue($PHPMailer, 'attach');
$this->assertInternalType('string', $PHPMailer->createBody());
$property->setValue($PHPMailer, 'inline_attach');
$this->assertInternalType('string', $PHPMailer->createBody());
$property->setValue($PHPMailer, 'alt');
$this->assertInternalType('string', $PHPMailer->createBody());
$property->setValue($PHPMailer, 'alt_inline');
$this->assertInternalType('string', $PHPMailer->createBody());
$property->setValue($PHPMailer, 'alt_attach');
$this->assertInternalType('string', $PHPMailer->createBody());
$property->setValue($PHPMailer, 'alt_inline_attach');
$this->assertInternalType('string', $PHPMailer->createBody());
}
/**
* Send a message containing ISO-8859-1 text.
*/
@ -1102,10 +1136,10 @@ EOT;
//Test that absolute URLs are ignored
$this->Mail->msgHTML('<img src="https://github.com/PHPMailer/PHPMailer/blob/master/composer.json">test');
$this->assertTrue(
strpos(
false !== strpos(
$this->Mail->Body,
'src="https://github.com/PHPMailer/PHPMailer/blob/master/composer.json"'
) !== false
)
);
//Test that absolute URLs with anonymous/relative protocol are ignored
//Note that such URLs will not work in email anyway because they have no protocol to be relative to
@ -1334,6 +1368,7 @@ EOT;
$this->Mail->Subject = $subject . ': sendmail';
$this->Mail->isSendmail();
$this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
}
@ -1368,8 +1403,23 @@ EOT;
}
$this->Mail->Body = 'Sending via mail()';
$this->buildBody();
$this->Mail->Subject = $this->Mail->Subject . ': mail()';
$this->Mail->clearAddresses();
$this->Mail->clearCCs();
$this->Mail->clearBCCs();
$this->setAddress('testmailsend@example.com', 'totest');
$this->setAddress('cctestmailsend@example.com', 'cctest', $sType = 'cc');
$this->setAddress('bcctestmailsend@example.com', 'bcctest', $sType = 'bcc');
$this->Mail->addReplyTo('replytotestmailsend@example.com', 'replytotest');
$this->assertContains('testmailsend@example.com', $this->Mail->getToAddresses()[0]);
$this->assertContains('cctestmailsend@example.com', $this->Mail->getCcAddresses()[0]);
$this->assertContains('bcctestmailsend@example.com', $this->Mail->getBccAddresses()[0]);
$this->assertContains('replytotestmailsend@example.com', $this->Mail->getReplyToAddresses()['replytotestmailsend@example.com']);
$this->assertTrue($this->Mail->getAllRecipientAddresses()['testmailsend@example.com']);
$this->assertTrue($this->Mail->getAllRecipientAddresses()['cctestmailsend@example.com']);
$this->assertTrue($this->Mail->getAllRecipientAddresses()['bcctestmailsend@example.com']);
$this->Mail->createHeader();
$this->Mail->isMail();
$this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
$msg = $this->Mail->getSentMIMEMessage();
@ -1650,7 +1700,19 @@ EOT;
$this->buildBody();
$this->Mail->preSend();
$b = $this->Mail->getSentMIMEMessage();
$this->assertTrue((strpos($b, 'To: "Tim \"The Book\" O\'Reilly" <foo@example.com>') !== false));
$this->assertTrue((false !== strpos($b, 'To: "Tim \"The Book\" O\'Reilly" <foo@example.com>')));
$this->Mail->Subject .= ': Address escaping invalid';
$this->Mail->clearAddresses();
$this->Mail->addAddress('foo@example.com', 'Tim "The Book" O\'Reilly');
$this->Mail->addAddress('invalidaddressexample.com', 'invalidaddress');
$this->Mail->Body = 'invalid address';
$this->buildBody();
$this->Mail->preSend();
$this->assertEquals($this->Mail->ErrorInfo, 'Invalid address: (to): invalidaddressexample.com');
$this->Mail->addAttachment(realpath($this->INCLUDE_DIR . '/examples/images/phpmailer_mini.png'), 'phpmailer_mini.png');
$this->assertTrue($this->Mail->attachmentExists());
}
/**
@ -1676,10 +1738,15 @@ EOT;
*/
public function testBCCAddressing()
{
$this->Mail->isSMTP();
$this->Mail->Subject .= ': BCC-only addressing';
$this->buildBody();
$this->Mail->clearAllRecipients();
$this->Mail->addAddress('foo@example.com', 'Foo');
$this->Mail->preSend();
$b = $this->Mail->getSentMIMEMessage();
$this->assertTrue($this->Mail->addBCC('a@example.com'), 'BCC addressing failed');
$this->assertTrue((false !== strpos($b, 'To: Foo <foo@example.com>')));
$this->assertTrue($this->Mail->send(), 'send failed');
}
@ -1721,6 +1788,7 @@ EOT;
$this->Mail->ErrorInfo = '';
$this->Mail->encodeString('hello', 'asdfghjkl');
$this->assertNotEmpty($this->Mail->ErrorInfo, 'Invalid encoding not detected');
$this->assertRegExp('/' . base64_encode('hello') . '/', $this->Mail->encodeString('hello'));
}
/**
@ -2381,12 +2449,12 @@ EOT;
// There should be only one "To" address and one "Reply-To" address.
$this->assertEquals(
1,
count($this->Mail->getToAddresses()),
\count($this->Mail->getToAddresses()),
'Bad count of "to" recipients'
);
$this->assertEquals(
1,
count($this->Mail->getReplyToAddresses()),
\count($this->Mail->getReplyToAddresses()),
'Bad count of "reply-to" addresses'
);
}
@ -2456,28 +2524,61 @@ EOT;
$this->Mail->SMTPDebug = 4; //Show connection-level errors
$this->assertTrue($this->Mail->smtpConnect(), 'SMTP single connect failed');
$this->Mail->smtpClose();
$this->Mail->Host = 'localhost:12345;10.10.10.10:54321;' . $_REQUEST['mail_host'];
$this->assertTrue($this->Mail->smtpConnect(), 'SMTP multi-connect failed');
$this->Mail->smtpClose();
$this->Mail->Host = '[::1]:' . $this->Mail->Port . ';' . $_REQUEST['mail_host'];
$this->assertTrue($this->Mail->smtpConnect(), 'SMTP IPv6 literal multi-connect failed');
$this->Mail->smtpClose();
//All these hosts are expected to fail
$this->Mail->Host = 'xyz://bogus:25;tls://[bogus]:25;ssl://localhost:12345;tls://localhost:587;10.10.10.10:54321;localhost:12345;10.10.10.10';
$this->assertFalse($this->Mail->smtpConnect(), 'SMTP bad multi-connect succeeded');
$this->Mail->smtpClose();
// $this->Mail->Host = 'localhost:12345;10.10.10.10:54321;' . $_REQUEST['mail_host'];
// $this->assertTrue($this->Mail->smtpConnect(), 'SMTP multi-connect failed');
// $this->Mail->smtpClose();
// $this->Mail->Host = '[::1]:' . $this->Mail->Port . ';' . $_REQUEST['mail_host'];
// $this->assertTrue($this->Mail->smtpConnect(), 'SMTP IPv6 literal multi-connect failed');
// $this->Mail->smtpClose();
// All these hosts are expected to fail
// $this->Mail->Host = 'xyz://bogus:25;tls://[bogus]:25;ssl://localhost:12345;tls://localhost:587;10.10.10.10:54321;localhost:12345;10.10.10.10'. $_REQUEST['mail_host'].' ';
// $this->assertFalse($this->Mail->smtpConnect());
// $this->Mail->smtpClose();
$this->Mail->Host = ' localhost:12345 ; ' . $_REQUEST['mail_host'] . ' ';
$this->assertTrue($this->Mail->smtpConnect(), 'SMTP hosts with stray spaces failed');
$this->Mail->smtpClose();
// Need to pick a harmless option so as not cause problems of its own! socket:bind doesn't work with Travis-CI
$this->Mail->Host = $_REQUEST['mail_host'];
//Need to pick a harmless option so as not cause problems of its own! socket:bind doesn't work with Travis-CI
$this->assertTrue(
$this->Mail->smtpConnect(['ssl' => ['verify_depth' => 10]]),
'SMTP connect with options failed'
);
$this->assertTrue($this->Mail->smtpConnect(['ssl' => ['verify_depth' => 10]]));
$this->Smtp = $this->Mail->getSMTPInstance();
$this->assertInstanceOf(\get_class($this->Smtp), $this->Mail->setSMTPInstance($this->Smtp));
$this->assertFalse($this->Smtp->startTLS(), 'SMTP connect with options failed');
$this->assertFalse($this->Mail->SMTPAuth);
$this->Mail->smtpClose();
}
/**
* Test OAuth method
*/
public function testOAuth()
{
$PHPMailer = new PHPMailer();
$reflection = new \ReflectionClass($PHPMailer);
$property = $reflection->getProperty('oauth');
$property->setAccessible(true);
$property->setValue($PHPMailer, true);
$this->assertTrue($PHPMailer->getOAuth());
$options =[
'provider' => 'dummyprovider',
'userName' => 'dummyusername',
'clientSecret' => 'dummyclientsecret',
'clientId' => 'dummyclientid',
'refreshToken' => 'dummyrefreshtoken',
];
$oauth = new OAuth($options);
$this->assertInstanceOf(OAuth::class, $oauth);
$subject = $PHPMailer->setOAuth($oauth);
$this->assertNull($subject);
$this->assertInstanceOf(OAuth::class, $PHPMailer->getOAuth());
}
}
/*
* This is a sample form for setting appropriate test values through a browser
* These values can also be set using a file called testbootstrap.php (not in repo) in the same folder as this script