Wordpress/tests/phpunit/includes/mock-mailer.php
Jonathan Desrosiers 0933882c6e External Libraries: Upgrade PHPMailer to version 6.1.6.
Now that WordPress Core supports PHP >= 5.6, the PHPMailer library can be updated to the latest version.

The PHPMailer files now reside in a new directory, `wp-includes/PHPMailer`. These files are copied verbatim from the library upstream and will make updating in the future easier. For backwards compatibility, the old files will remain and trigger deprecated file warnings.

The PHPMailer class is also now under the `PHPMailer\PHPMailer\PHPMailer` namespace. The `PHPMailer` class in the global namespace has been aliased for a seamless transition.

This upgrade also clears up a handful of PHP compatibility issues detailed in #49922.

For a full list of changes, see the PHPMailer GitHub: https://github.com/PHPMailer/PHPMailer/compare/v5.2.27...v6.1.6.

Props Synchro, SergeyBiryukov, desrosj, donmhico, ayeshrajans.
Fixes #41750.

git-svn-id: https://develop.svn.wordpress.org/trunk@48033 602fd350-edb4-49c9-b593-d223f7449a82
2020-06-12 15:45:30 +00:00

105 lines
2.6 KiB
PHP

<?php
require_once ABSPATH . '/wp-includes/PHPMailer/PHPMailer.php';
require_once ABSPATH . '/wp-includes/PHPMailer/Exception.php';
class MockPHPMailer extends PHPMailer\PHPMailer\PHPMailer {
var $mock_sent = array();
function preSend() {
$this->Encoding = '8bit';
return parent::preSend();
}
/**
* Override postSend() so mail isn't actually sent.
*/
function postSend() {
$this->mock_sent[] = array(
'to' => $this->to,
'cc' => $this->cc,
'bcc' => $this->bcc,
'header' => $this->MIMEHeader . $this->mailHeader,
'subject' => $this->Subject,
'body' => $this->MIMEBody,
);
return true;
}
/**
* Decorator to return the information for a sent mock.
*
* @since 4.5.0
*
* @param int $index Optional. Array index of mock_sent value.
* @return object
*/
public function get_sent( $index = 0 ) {
$retval = false;
if ( isset( $this->mock_sent[ $index ] ) ) {
$retval = (object) $this->mock_sent[ $index ];
}
return $retval;
}
/**
* Get a recipient for a sent mock.
*
* @since 4.5.0
*
* @param string $address_type The type of address for the email such as to, cc or bcc.
* @param int $mock_sent_index Optional. The sent_mock index we want to get the recipient for.
* @param int $recipient_index Optional. The recipient index in the array.
* @return bool|object Returns object on success, or false if any of the indices don't exist.
*/
public function get_recipient( $address_type, $mock_sent_index = 0, $recipient_index = 0 ) {
$retval = false;
$mock = $this->get_sent( $mock_sent_index );
if ( $mock ) {
if ( isset( $mock->{$address_type}[ $recipient_index ] ) ) {
$address_index = $mock->{$address_type}[ $recipient_index ];
$recipient_data = array(
'address' => ( isset( $address_index[0] ) && ! empty( $address_index[0] ) ) ? $address_index[0] : 'No address set',
'name' => ( isset( $address_index[1] ) && ! empty( $address_index[1] ) ) ? $address_index[1] : 'No name set',
);
$retval = (object) $recipient_data;
}
}
return $retval;
}
}
/**
* Helper method to return the global phpmailer instance defined in the bootstrap
*
* @since 4.4.0
*
* @return object|bool
*/
function tests_retrieve_phpmailer_instance() {
$mailer = false;
if ( isset( $GLOBALS['phpmailer'] ) ) {
$mailer = $GLOBALS['phpmailer'];
}
return $mailer;
}
/**
* Helper method to reset the phpmailer instance.
*
* @since 4.6.0
*
* @return bool
*/
function reset_phpmailer_instance() {
$mailer = tests_retrieve_phpmailer_instance();
if ( $mailer ) {
$GLOBALS['phpmailer'] = new MockPHPMailer( true );
return true;
}
return false;
}