Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/PHPMailer/PHPMailer
Browse files Browse the repository at this point in the history
  • Loading branch information
jimjag committed Nov 5, 2018
2 parents cb4e2ae + c482b20 commit cf58579
Showing 1 changed file with 123 additions and 22 deletions.
145 changes: 123 additions & 22 deletions test/PHPMailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace PHPMailer\Test;

use PHPMailer\PHPMailer\OAuth;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\POP3;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -659,6 +660,8 @@ public function testValidate()
$this->assertFalse(PHPMailer::validateAddress('[email protected].', 'pcre'));
$this->assertTrue(PHPMailer::validateAddress('[email protected]', 'pcre8'));
$this->assertFalse(PHPMailer::validateAddress('[email protected].', 'pcre8'));
$this->assertTrue(PHPMailer::validateAddress('[email protected]', 'html5'));
$this->assertFalse(PHPMailer::validateAddress('[email protected].', 'html5'));
$this->assertTrue(PHPMailer::validateAddress('[email protected]', 'php'));
$this->assertFalse(PHPMailer::validateAddress('[email protected].', 'php'));
$this->assertTrue(PHPMailer::validateAddress('[email protected]', 'noregex'));
Expand Down Expand Up @@ -939,6 +942,37 @@ public function testHtml()
$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.
*/
Expand Down Expand Up @@ -1102,10 +1136,10 @@ function ($html) {
//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
Expand Down Expand Up @@ -1334,6 +1368,7 @@ public function testSendmailSend()

$this->Mail->Subject = $subject . ': sendmail';
$this->Mail->isSendmail();

$this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
}

Expand Down Expand Up @@ -1368,8 +1403,23 @@ public function testMailSend()
}
$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('[email protected]', 'totest');
$this->setAddress('[email protected]', 'cctest', $sType = 'cc');
$this->setAddress('[email protected]', 'bcctest', $sType = 'bcc');
$this->Mail->addReplyTo('[email protected]', 'replytotest');
$this->assertContains('[email protected]', $this->Mail->getToAddresses()[0]);
$this->assertContains('[email protected]', $this->Mail->getCcAddresses()[0]);
$this->assertContains('[email protected]', $this->Mail->getBccAddresses()[0]);
$this->assertContains('[email protected]', $this->Mail->getReplyToAddresses()['[email protected]']);
$this->assertTrue($this->Mail->getAllRecipientAddresses()['[email protected]']);
$this->assertTrue($this->Mail->getAllRecipientAddresses()['[email protected]']);
$this->assertTrue($this->Mail->getAllRecipientAddresses()['[email protected]']);

$this->Mail->createHeader();
$this->Mail->isMail();
$this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
$msg = $this->Mail->getSentMIMEMessage();
Expand Down Expand Up @@ -1650,7 +1700,19 @@ public function testAddressEscaping()
$this->buildBody();
$this->Mail->preSend();
$b = $this->Mail->getSentMIMEMessage();
$this->assertTrue((strpos($b, 'To: "Tim \"The Book\" O\'Reilly" <[email protected]>') !== false));
$this->assertTrue((false !== strpos($b, 'To: "Tim \"The Book\" O\'Reilly" <[email protected]>')));

$this->Mail->Subject .= ': Address escaping invalid';
$this->Mail->clearAddresses();
$this->Mail->addAddress('[email protected]', '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());
}

/**
Expand All @@ -1676,10 +1738,15 @@ public function testMIMEStructure()
*/
public function testBCCAddressing()
{
$this->Mail->isSMTP();
$this->Mail->Subject .= ': BCC-only addressing';
$this->buildBody();
$this->Mail->clearAllRecipients();
$this->Mail->addAddress('[email protected]', 'Foo');
$this->Mail->preSend();
$b = $this->Mail->getSentMIMEMessage();
$this->assertTrue($this->Mail->addBCC('[email protected]'), 'BCC addressing failed');
$this->assertTrue((false !== strpos($b, 'To: Foo <[email protected]>')));
$this->assertTrue($this->Mail->send(), 'send failed');
}

Expand Down Expand Up @@ -1721,6 +1788,7 @@ public function testEncodings()
$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'));
}

/**
Expand Down Expand Up @@ -2381,12 +2449,12 @@ public function testDuplicateIDNRemoved()
// 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'
);
}
Expand Down Expand Up @@ -2456,28 +2524,61 @@ public function testSmtpConnect()
$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
Expand Down

0 comments on commit cf58579

Please sign in to comment.