diff --git a/.travis.yml b/.travis.yml index 1e4eed2a..58a2e428 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,8 +21,8 @@ env: - WP_VERSION=4.6 WP_MULTISITE=0 before_script: - - bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION - cd tests + - bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION - composer install --no-interaction script: composer test diff --git a/README.md b/README.md index afde21b2..e56bbf6d 100644 --- a/README.md +++ b/README.md @@ -39,9 +39,9 @@ git clone git@github.com:SparkPost/wordpress-sparkpost.git ~/src/wordpress/wp-co ## Running Tests * Make sure you're using PHP 5.6 or above. +* Go to `./tests` directory. * Install test files by running `bash bin/install-wp-tests.sh wordpress_test root '' localhost latest`. Details on [wp-cli.org](http://wp-cli.org/docs/plugin-unit-tests/). * [Install composer](https://getcomposer.org/doc/00-intro.md) -* Go to `./tests` directory. * Run `composer install` to install required packages. * To run tests, run `composer test`. * Add your tests in `tests/specs` directory. Upon pushing the branch, Travis will automatically run it and generate reports (tests and coverage). diff --git a/mailer.http.class.php b/mailer.http.class.php index 908746fc..3d580b00 100644 --- a/mailer.http.class.php +++ b/mailer.http.class.php @@ -232,20 +232,21 @@ protected function get_recipients() $recipients = array(); $recipients_header_to = array(); + //prepare header_to foreach ($this->to as $to) { - $recipients[] = $this->build_recipient($to[0], $to[1]); - - // prepare for header_to - if(!empty($to[1])) { - $recipients_header_to[] = sprintf('%s <%s>', $to[1], $to[0]); - } else { + if(empty($to[1])) { // if name is empty use only address $recipients_header_to[] = $to[0]; + } else { // otherwise, use name and email + $recipients_header_to[] = sprintf('%s <%s>', $to[1], $to[0]); } } - $recipients_header_to = implode(',', $recipients_header_to); + $recipients_header_to = implode(', ', $recipients_header_to); + + foreach ($this->to as $to) { + $recipients[] = $this->build_recipient($to[0], $to[1], $recipients_header_to); + } // include bcc to recipients - // sparkposts recipients list acts as bcc by default $recipients = array_merge($recipients, $this->get_bcc($recipients_header_to)); // include cc to recipients, they need to included in recipients and in headers (refer to get_headers method) diff --git a/bin/install-wp-tests.sh b/tests/bin/install-wp-tests.sh similarity index 100% rename from bin/install-wp-tests.sh rename to tests/bin/install-wp-tests.sh diff --git a/tests/specs/test-mailer.http.class.php b/tests/specs/test-mailer.http.class.php index 135145ec..e90041e9 100644 --- a/tests/specs/test-mailer.http.class.php +++ b/tests/specs/test-mailer.http.class.php @@ -13,39 +13,18 @@ function setUp() { $this->mailer = new SparkPostHTTPMailer(); } - function test_mailer_is_a_mailer_instance() { - $this->assertTrue( $this->mailer instanceof \PHPMailer ); + function test_mailSend_calls_sparkpost_send() { + $stub = Mockery::mock($this->mailer); + $stub->shouldReceive('sparkpost_send')->andReturn('woowoo'); + + $this->assertTrue(NSA::invokeMethod($stub, 'mailSend', null, null) == 'woowoo'); } - function test_recipients_list() { - - $this->mailer->addAddress('abc@xyz.com', 'abc'); - $this->mailer->addAddress('def@xyz.com', 'def'); - $this->mailer->addAddress('noname@xyz.com'); - $prepared_list = array( - array( - 'address' => array( - 'email' => 'abc@xyz.com', - 'name' => 'abc', - ) - ), - array( - 'address' => array( - 'name' => 'def', - 'email' => 'def@xyz.com' - ) - ), - array( - 'address' => array( - 'email' => 'noname@xyz.com', - 'name' => '' - ) - ) - ); - $this->assertTrue(NSA::invokeMethod($this->mailer, 'get_recipients') == $prepared_list); + function test_mailer_is_a_mailer_instance() { + $this->assertTrue( $this->mailer instanceof \PHPMailer ); } - function test_sender_with_name() { + function test_get_sender_with_name() { $this->mailer->setFrom( 'me@hello.com', 'me' ); $sender = array( 'name' => 'me', @@ -55,7 +34,7 @@ function test_sender_with_name() { $this->assertTrue(NSA::invokeMethod($this->mailer, 'get_sender') == $sender); } - function test_sender_without_name() { + function test_get_sender_without_name() { $this->mailer->setFrom( 'me@hello.com', '' ); $sender = array( 'email' => 'me@hello.com' @@ -131,4 +110,78 @@ function test_get_headers_should_include_cc_if_exists() { $this->assertTrue($formatted_headers == $expected); } + + function test_get_recipients() { + $this->mailer->addAddress('to@abc.com'); + $this->mailer->addAddress('to1@abc.com', 'to1'); + $this->mailer->addCc('cc@abc.com'); + $this->mailer->addCc('cc1@abc.com', 'cc1'); + $this->mailer->addBcc('bcc@abc.com'); + $this->mailer->addBcc('bcc1@abc.com', 'bcc1'); + + $header_to = implode(', ', [ + 'to@abc.com', + 'to1 ', + ]); + + $expected = [ + [ + 'address' => [ + 'email' => 'to@abc.com', + 'header_to' => $header_to + ] + ], + [ + 'address' => [ + 'email' => 'to1@abc.com', + 'header_to' => $header_to + ] + ], + [ + 'address' => [ + 'email' => 'bcc@abc.com', + 'header_to' => $header_to + ] + ], + [ + 'address' => [ + 'email' => 'bcc1@abc.com', + 'header_to' => $header_to + ] + ], + [ + 'address' => [ + 'email' => 'cc@abc.com', + 'header_to' => $header_to + ] + ], + [ + 'address' => [ + 'email' => 'cc1@abc.com', + 'header_to' => $header_to + ] + ] + ]; + + $recipients = NSA::invokeMethod($this->mailer, 'get_recipients'); + $this->assertTrue($recipients == $expected); + } + + function test_get_attachments() { + $temp = tempnam('/tmp', 'php-wordpress-sparkpost'); + file_put_contents($temp, 'TEST'); + $this->mailer->addAttachment($temp); + $attachments = NSA::invokeMethod($this->mailer, 'get_attachments'); + $this->assertTrue($attachments[0]['type'] === 'application/octet-stream'); + $this->assertTrue($attachments[0]['name'] === basename($temp)); + $this->assertTrue($attachments[0]['data'] === base64_encode('TEST')); + } + + function test_isMail() { + // test if isMail sets correct mailer + $this->mailer->Mailer = 'abc'; + $this->assertTrue($this->mailer->Mailer === 'abc'); + $this->mailer->isMail(); + $this->assertTrue($this->mailer->Mailer === 'sparkpost'); + } }