1 | <?php |
---|
2 | require_once ABSPATH . 'wp-includes/PHPMailer/PHPMailer.php'; |
---|
3 | require_once ABSPATH . 'wp-includes/PHPMailer/Exception.php'; |
---|
4 | |
---|
5 | class MockPHPMailer extends PHPMailer\PHPMailer\PHPMailer { |
---|
6 | public $mock_sent = array(); |
---|
7 | |
---|
8 | public function preSend() { |
---|
9 | $this->Encoding = '8bit'; |
---|
10 | return parent::preSend(); |
---|
11 | } |
---|
12 | |
---|
13 | /** |
---|
14 | * Override postSend() so mail isn't actually sent. |
---|
15 | */ |
---|
16 | public function postSend() { |
---|
17 | $this->mock_sent[] = array( |
---|
18 | 'to' => $this->to, |
---|
19 | 'cc' => $this->cc, |
---|
20 | 'bcc' => $this->bcc, |
---|
21 | 'header' => $this->MIMEHeader . $this->mailHeader, |
---|
22 | 'subject' => $this->Subject, |
---|
23 | 'body' => $this->MIMEBody, |
---|
24 | ); |
---|
25 | |
---|
26 | return true; |
---|
27 | } |
---|
28 | |
---|
29 | /** |
---|
30 | * Decorator to return the information for a sent mock. |
---|
31 | * |
---|
32 | * @since 4.5.0 |
---|
33 | * |
---|
34 | * @param int $index Optional. Array index of mock_sent value. |
---|
35 | * @return object |
---|
36 | */ |
---|
37 | public function get_sent( $index = 0 ) { |
---|
38 | $retval = false; |
---|
39 | if ( isset( $this->mock_sent[ $index ] ) ) { |
---|
40 | $retval = (object) $this->mock_sent[ $index ]; |
---|
41 | } |
---|
42 | return $retval; |
---|
43 | } |
---|
44 | |
---|
45 | /** |
---|
46 | * Get a recipient for a sent mock. |
---|
47 | * |
---|
48 | * @since 4.5.0 |
---|
49 | * |
---|
50 | * @param string $address_type The type of address for the email such as to, cc or bcc. |
---|
51 | * @param int $mock_sent_index Optional. The sent_mock index we want to get the recipient for. |
---|
52 | * @param int $recipient_index Optional. The recipient index in the array. |
---|
53 | * @return bool|object Returns object on success, or false if any of the indices don't exist. |
---|
54 | */ |
---|
55 | public function get_recipient( $address_type, $mock_sent_index = 0, $recipient_index = 0 ) { |
---|
56 | $retval = false; |
---|
57 | $mock = $this->get_sent( $mock_sent_index ); |
---|
58 | if ( $mock ) { |
---|
59 | if ( isset( $mock->{$address_type}[ $recipient_index ] ) ) { |
---|
60 | $address_index = $mock->{$address_type}[ $recipient_index ]; |
---|
61 | $recipient_data = array( |
---|
62 | 'address' => ( isset( $address_index[0] ) && ! empty( $address_index[0] ) ) ? $address_index[0] : 'No address set', |
---|
63 | 'name' => ( isset( $address_index[1] ) && ! empty( $address_index[1] ) ) ? $address_index[1] : 'No name set', |
---|
64 | ); |
---|
65 | |
---|
66 | $retval = (object) $recipient_data; |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|
70 | return $retval; |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | /** |
---|
75 | * Helper method to return the global phpmailer instance defined in the bootstrap |
---|
76 | * |
---|
77 | * @since 4.4.0 |
---|
78 | * |
---|
79 | * @return MockPHPMailer|false |
---|
80 | */ |
---|
81 | function tests_retrieve_phpmailer_instance() { |
---|
82 | $mailer = false; |
---|
83 | if ( isset( $GLOBALS['phpmailer'] ) ) { |
---|
84 | $mailer = $GLOBALS['phpmailer']; |
---|
85 | } |
---|
86 | return $mailer; |
---|
87 | } |
---|
88 | |
---|
89 | /** |
---|
90 | * Helper method to reset the phpmailer instance. |
---|
91 | * |
---|
92 | * @since 4.6.0 |
---|
93 | * |
---|
94 | * @return bool |
---|
95 | */ |
---|
96 | function reset_phpmailer_instance() { |
---|
97 | $mailer = tests_retrieve_phpmailer_instance(); |
---|
98 | if ( $mailer ) { |
---|
99 | $mailer = new MockPHPMailer( true ); |
---|
100 | $mailer::$validator = static function ( $email ) { |
---|
101 | return (bool) is_email( $email ); |
---|
102 | }; |
---|
103 | |
---|
104 | $GLOBALS['phpmailer'] = $mailer; |
---|
105 | return true; |
---|
106 | } |
---|
107 | |
---|
108 | return false; |
---|
109 | } |
---|