2016-11-27 19:19:59 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class WC_Tests_Log_Handler_Email
|
|
|
|
* @package WooCommerce\Tests\Log
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-27 19:19:59 +00:00
|
|
|
*/
|
|
|
|
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
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-27 19:19:59 +00:00
|
|
|
*/
|
|
|
|
public function test_handle() {
|
2019-05-01 22:05:00 +00:00
|
|
|
$mailer = tests_retrieve_phpmailer_instance();
|
|
|
|
$time = time();
|
2016-12-21 19:56:53 +00:00
|
|
|
$site_name = get_bloginfo( 'name' );
|
2016-12-11 20:10:53 +00:00
|
|
|
|
2016-12-11 11:02:40 +00:00
|
|
|
$handler = new WC_Log_Handler_Email();
|
|
|
|
$handler->handle( $time, 'emergency', 'msg_emergency', array() );
|
2016-12-16 20:02:54 +00:00
|
|
|
$handler->handle( $time, 'emergency', 'msg_emergency 2', array() );
|
|
|
|
$handler->send_log_email();
|
2016-11-27 19:19:59 +00:00
|
|
|
|
|
|
|
$this->assertEquals(
|
2016-12-16 20:02:54 +00:00
|
|
|
(
|
|
|
|
'You have received the following WooCommerce log messages:'
|
|
|
|
. PHP_EOL
|
|
|
|
. PHP_EOL
|
|
|
|
. date( 'c', $time ) . ' EMERGENCY msg_emergency'
|
|
|
|
. PHP_EOL
|
|
|
|
. date( 'c', $time ) . ' EMERGENCY msg_emergency 2'
|
|
|
|
. PHP_EOL
|
|
|
|
. PHP_EOL
|
|
|
|
. "Visit {$site_name} admin area:"
|
|
|
|
. PHP_EOL
|
|
|
|
. admin_url()
|
|
|
|
. PHP_EOL
|
|
|
|
),
|
2016-12-21 19:56:53 +00:00
|
|
|
$mailer->get_sent( 0 )->body
|
2016-11-27 19:19:59 +00:00
|
|
|
);
|
2016-12-11 20:10:53 +00:00
|
|
|
$this->assertEquals(
|
2016-12-21 19:56:53 +00:00
|
|
|
"[{$site_name}] EMERGENCY: 2 WooCommerce log messages",
|
|
|
|
$mailer->get_sent( 0 )->subject
|
2016-12-11 20:10:53 +00:00
|
|
|
);
|
|
|
|
$this->assertEquals( get_option( 'admin_email' ), $mailer->get_recipient( 'to' )->address );
|
2016-12-22 22:34:24 +00:00
|
|
|
|
|
|
|
$handler->handle( $time, 'emergency', 'msg_emergency', array() );
|
|
|
|
$handler->send_log_email();
|
|
|
|
$this->assertEquals(
|
|
|
|
(
|
|
|
|
'You have received the following WooCommerce log message:'
|
|
|
|
. PHP_EOL
|
|
|
|
. PHP_EOL
|
|
|
|
. date( 'c', $time ) . ' EMERGENCY msg_emergency'
|
|
|
|
. PHP_EOL
|
|
|
|
. PHP_EOL
|
|
|
|
. "Visit {$site_name} admin area:"
|
|
|
|
. PHP_EOL
|
|
|
|
. admin_url()
|
|
|
|
. PHP_EOL
|
|
|
|
),
|
|
|
|
$mailer->get_sent( 1 )->body
|
|
|
|
);
|
|
|
|
|
2016-11-27 19:19:59 +00:00
|
|
|
}
|
|
|
|
|
2016-12-11 20:10:53 +00:00
|
|
|
|
2016-12-21 19:56:53 +00:00
|
|
|
/**
|
|
|
|
* Test email subject
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-12-21 19:56:53 +00:00
|
|
|
*/
|
|
|
|
public function test_email_subject() {
|
2019-05-01 22:05:00 +00:00
|
|
|
$mailer = tests_retrieve_phpmailer_instance();
|
|
|
|
$time = time();
|
2016-12-21 19:56:53 +00:00
|
|
|
$site_name = get_bloginfo( 'name' );
|
|
|
|
|
|
|
|
$handler = new WC_Log_Handler_Email( null, WC_Log_Levels::DEBUG );
|
|
|
|
$handler->handle( $time, 'debug', '', array() );
|
|
|
|
$handler->send_log_email();
|
|
|
|
|
|
|
|
$handler->handle( $time, 'alert', '', array() );
|
|
|
|
$handler->handle( $time, 'critical', '', array() );
|
|
|
|
$handler->handle( $time, 'debug', '', array() );
|
|
|
|
$handler->send_log_email();
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
"[{$site_name}] DEBUG: 1 WooCommerce log message",
|
|
|
|
$mailer->get_sent( 0 )->subject
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
"[{$site_name}] ALERT: 3 WooCommerce log messages",
|
|
|
|
$mailer->get_sent( 1 )->subject
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-12-11 20:10:53 +00:00
|
|
|
/**
|
|
|
|
* Test multiple recipients receive emails.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-12-11 20:10:53 +00:00
|
|
|
*/
|
|
|
|
public function test_multiple_recipients() {
|
|
|
|
$mailer = tests_retrieve_phpmailer_instance();
|
|
|
|
|
2019-05-01 22:05:00 +00:00
|
|
|
$handler = new WC_Log_Handler_Email(
|
|
|
|
array(
|
|
|
|
'first@test.com',
|
|
|
|
'Second Recipient <second@test.com>',
|
|
|
|
)
|
|
|
|
);
|
2016-12-11 20:10:53 +00:00
|
|
|
$handler->handle( time(), 'emergency', '', array() );
|
2016-12-16 20:02:54 +00:00
|
|
|
$handler->send_log_email();
|
2016-12-11 20:10:53 +00:00
|
|
|
|
|
|
|
$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.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-12-11 20:10:53 +00:00
|
|
|
*/
|
|
|
|
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() );
|
2016-12-16 20:02:54 +00:00
|
|
|
$handler->send_log_email();
|
2016-12-11 20:10:53 +00:00
|
|
|
|
2019-05-01 22:05:00 +00:00
|
|
|
$recipient = $mailer->get_recipient( 'to' );
|
2016-12-11 20:10:53 +00:00
|
|
|
$this->assertEquals( 'user@test.com', $recipient->address );
|
|
|
|
$this->assertEquals( 'User', $recipient->name );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test threshold.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-12-11 20:10:53 +00:00
|
|
|
*/
|
|
|
|
public function test_threshold() {
|
|
|
|
$mailer = tests_retrieve_phpmailer_instance();
|
|
|
|
|
|
|
|
$handler = new WC_Log_Handler_Email( null, 'notice' );
|
|
|
|
$handler->handle( time(), 'info', '', array() );
|
2016-12-16 20:02:54 +00:00
|
|
|
$handler->send_log_email();
|
2016-12-11 20:10:53 +00:00
|
|
|
|
|
|
|
// Info should not be handled, get_sent is false
|
|
|
|
$this->assertFalse( $mailer->get_sent( 0 ) );
|
|
|
|
|
|
|
|
$handler->handle( time(), 'notice', '', array() );
|
2016-12-16 20:02:54 +00:00
|
|
|
$handler->send_log_email();
|
2016-12-11 20:10:53 +00:00
|
|
|
$this->assertObjectHasAttribute( 'body', $mailer->get_sent( 0 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test set_threshold().
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-12-11 20:10:53 +00:00
|
|
|
*/
|
|
|
|
public function test_set_threshold() {
|
|
|
|
$mailer = tests_retrieve_phpmailer_instance();
|
|
|
|
|
|
|
|
$handler = new WC_Log_Handler_Email( null, 'notice' );
|
|
|
|
$handler->handle( time(), 'info', '', array() );
|
2016-12-16 20:02:54 +00:00
|
|
|
$handler->send_log_email();
|
2016-12-11 20:10:53 +00:00
|
|
|
|
|
|
|
// Info should not be handled, get_sent is false
|
|
|
|
$this->assertFalse( $mailer->get_sent( 0 ) );
|
|
|
|
|
|
|
|
$handler->set_threshold( 'info' );
|
|
|
|
$handler->handle( time(), 'info', '', array() );
|
2016-12-16 20:02:54 +00:00
|
|
|
$handler->send_log_email();
|
|
|
|
|
2016-12-11 20:10:53 +00:00
|
|
|
$this->assertObjectHasAttribute( 'body', $mailer->get_sent( 0 ) );
|
|
|
|
}
|
2016-12-16 20:02:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test send_log_email clears logs.
|
|
|
|
*
|
|
|
|
* Send log email could be called multiple times during a request. The same log should not be
|
|
|
|
* sent multiple times.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-12-16 20:02:54 +00:00
|
|
|
*/
|
|
|
|
public function test_multiple_send_log() {
|
|
|
|
$mailer = tests_retrieve_phpmailer_instance();
|
|
|
|
|
|
|
|
$handler = new WC_Log_Handler_Email();
|
2019-05-01 22:05:00 +00:00
|
|
|
$time = time();
|
2016-12-16 20:02:54 +00:00
|
|
|
$handler->handle( $time, 'emergency', 'message 1', array() );
|
|
|
|
$handler->send_log_email();
|
|
|
|
$handler->handle( $time, 'emergency', 'message 2', array() );
|
|
|
|
$handler->send_log_email();
|
|
|
|
|
|
|
|
$site_name = get_bloginfo( 'name' );
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
(
|
2016-12-22 22:34:24 +00:00
|
|
|
'You have received the following WooCommerce log message:'
|
2016-12-16 20:02:54 +00:00
|
|
|
. PHP_EOL
|
|
|
|
. PHP_EOL
|
|
|
|
. date( 'c', $time ) . ' EMERGENCY message 1'
|
|
|
|
. PHP_EOL
|
|
|
|
. PHP_EOL
|
|
|
|
. "Visit {$site_name} admin area:"
|
|
|
|
. PHP_EOL
|
|
|
|
. admin_url()
|
|
|
|
. PHP_EOL
|
|
|
|
),
|
|
|
|
$mailer->get_sent( 0 )->body
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
(
|
2016-12-22 22:34:24 +00:00
|
|
|
'You have received the following WooCommerce log message:'
|
2016-12-16 20:02:54 +00:00
|
|
|
. PHP_EOL
|
|
|
|
. PHP_EOL
|
|
|
|
. date( 'c', $time ) . ' EMERGENCY message 2'
|
|
|
|
. PHP_EOL
|
|
|
|
. PHP_EOL
|
|
|
|
. "Visit {$site_name} admin area:"
|
|
|
|
. PHP_EOL
|
|
|
|
. admin_url()
|
|
|
|
. PHP_EOL
|
|
|
|
),
|
|
|
|
$mailer->get_sent( 1 )->body
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-27 19:19:59 +00:00
|
|
|
}
|