woocommerce/tests/unit-tests/log/log-handler-email.php

121 lines
3.1 KiB
PHP
Raw Normal View History

2016-11-27 19:19:59 +00:00
<?php
/**
* Class WC_Tests_Log_Handler_Email
* @package WooCommerce\Tests\Log
* @since 2.8
*/
class WC_Tests_Log_Handler_Email extends WC_Unit_Test_Case {
function setUp() {
parent::setUp();
reset_phpmailer_instance();
}
function tearDown() {
reset_phpmailer_instance();
parent::tearDown();
}
/**
2016-12-11 20:10:53 +00:00
* Test handle sends email correctly.
2016-11-27 19:19:59 +00:00
*
* @since 2.8
*/
public function test_handle() {
2016-12-11 20:10:53 +00:00
$mailer = tests_retrieve_phpmailer_instance();
$handler = new WC_Log_Handler_Email();
2016-11-27 19:19:59 +00:00
$time = time();
$handler->handle( $time, 'emergency', 'msg_emergency', array() );
2016-11-27 19:19:59 +00:00
2016-12-11 20:10:53 +00:00
$site_name = get_bloginfo( 'name' );
2016-11-27 19:19:59 +00:00
$this->assertEquals(
2016-12-11 17:03:48 +00:00
'You have received the following WooCommerce log message:' . PHP_EOL . PHP_EOL . date( 'c', $time ) . ' EMERGENCY msg_emergency' . PHP_EOL,
2016-11-27 19:19:59 +00:00
$mailer->get_sent()->body
);
2016-12-11 20:10:53 +00:00
$this->assertEquals(
"[EMERGENCY] WooCommerce log message from {$site_name}",
$mailer->get_sent()->subject
);
$this->assertEquals( get_option( 'admin_email' ), $mailer->get_recipient( 'to' )->address );
2016-11-27 19:19:59 +00:00
}
2016-12-11 20:10:53 +00:00
/**
* Test multiple recipients receive emails.
*
* @since 2.8
*/
public function test_multiple_recipients() {
$mailer = tests_retrieve_phpmailer_instance();
$handler = new WC_Log_Handler_Email( array(
'first@test.com',
'Second Recipient <second@test.com>',
) );
$handler->handle( time(), 'emergency', '', array() );
$first_recipient = $mailer->get_recipient( 'to', 0, 0 );
$second_recipient = $mailer->get_recipient( 'to', 0, 1 );
$this->assertEquals( 'first@test.com', $first_recipient->address );
$this->assertEquals( 'second@test.com', $second_recipient->address );
$this->assertEquals( 'Second Recipient', $second_recipient->name );
}
/**
* Test single recipient receives emails.
*
* @since 2.8
*/
public function test_single_recipient() {
$mailer = tests_retrieve_phpmailer_instance();
$handler = new WC_Log_Handler_Email( 'User <user@test.com>' );
$handler->handle( time(), 'emergency', '', array() );
$recipient = $mailer->get_recipient( 'to' );
$this->assertEquals( 'user@test.com', $recipient->address );
$this->assertEquals( 'User', $recipient->name );
}
/**
* Test threshold.
*
* @since 2.8
*/
public function test_threshold() {
$mailer = tests_retrieve_phpmailer_instance();
$handler = new WC_Log_Handler_Email( null, 'notice' );
$handler->handle( time(), 'info', '', array() );
// Info should not be handled, get_sent is false
$this->assertFalse( $mailer->get_sent( 0 ) );
$handler->handle( time(), 'notice', '', array() );
$this->assertObjectHasAttribute( 'body', $mailer->get_sent( 0 ) );
}
/**
* Test set_threshold().
*
* @since 2.8
*/
public function test_set_threshold() {
$mailer = tests_retrieve_phpmailer_instance();
$handler = new WC_Log_Handler_Email( null, 'notice' );
$handler->handle( time(), 'info', '', array() );
// Info should not be handled, get_sent is false
$this->assertFalse( $mailer->get_sent( 0 ) );
$handler->set_threshold( 'info' );
$handler->handle( time(), 'info', '', array() );
$this->assertObjectHasAttribute( 'body', $mailer->get_sent( 0 ) );
}
2016-11-27 19:19:59 +00:00
}