mock-mailer.php 2.8 KB

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