Make WordPress Core

Ticket #28407: 28407.2.diff

File 28407.2.diff, 2.2 KB (added by swissspidy, 6 years ago)

Refreshed patch with unit tests

  • src/wp-includes/pluggable.php

    diff --git src/wp-includes/pluggable.php src/wp-includes/pluggable.php
    index 9af88a6ea2..16941939a6 100644
    if ( ! function_exists( 'wp_mail' ) ) : 
    465465                }
    466466
    467467                if ( ! empty( $attachments ) ) {
    468                         foreach ( $attachments as $attachment ) {
     468                        foreach ( $attachments as $filename => $attachment ) {
     469                                $filename = is_string( $filename ) ? $filename : '';
     470
    469471                                try {
    470                                         $phpmailer->addAttachment( $attachment );
     472                                        $phpmailer->addAttachment( $attachment, $filename );
    471473                                } catch ( phpmailerException $e ) {
    472474                                        continue;
    473475                                }
  • tests/phpunit/tests/mail.php

    diff --git tests/phpunit/tests/mail.php tests/phpunit/tests/mail.php
    index 951615136b..c985ae8181 100644
    class Tests_Mail extends WP_UnitTestCase { 
    391391                $this->assertEquals( 'wp_mail_failed', $call_args[0]->get_error_code() );
    392392                $this->assertEquals( $expected_error_data, $call_args[0]->get_error_data() );
    393393        }
     394
     395        /**
     396         * @ticket 28407
     397         */
     398        public function test_wp_mail_sends_attachments_with_original_name() {
     399                wp_mail( 'user@example.org', 'Subject', 'Hello World', '', array(
     400                        DIR_TESTDATA . '/images/canola.jpg',
     401                        DIR_TESTDATA . '/images/waffles.jpg'
     402                ) );
     403
     404                /** @var PHPMailer $mailer */
     405                $mailer = tests_retrieve_phpmailer_instance();
     406
     407                $attachments = $mailer->getAttachments();
     408
     409                $this->assertTrue( $mailer->attachmentExists() );
     410                $this->assertSame( $attachments[0][1], $attachments[0][2] );
     411                $this->assertSame( $attachments[1][1], $attachments[1][2] );
     412        }
     413
     414        /**
     415         * @ticket 28407
     416         */
     417        public function test_wp_mail_sends_attachments_with_custom_name() {
     418                wp_mail( 'user@example.org', 'Subject', 'Hello World', '', array(
     419                        'myawesomeimage.jpg' => DIR_TESTDATA . '/images/canola.jpg',
     420                        'foobar.jpg'         => DIR_TESTDATA . '/images/waffles.jpg'
     421                ) );
     422
     423                /** @var PHPMailer $mailer */
     424                $mailer = tests_retrieve_phpmailer_instance();
     425
     426                $attachments = $mailer->getAttachments();
     427
     428                $this->assertTrue( $mailer->attachmentExists() );
     429                $this->assertSame( ' myawesomeimage.jpg', $attachments[0][2] );
     430                $this->assertSame( 'foobar.jpg', $attachments[1][2] );
     431        }
    394432}