add a test that ensures the email and log message get generated
This commit is contained in:
parent
5a3ba9a0cb
commit
3850a07a2c
|
@ -9,6 +9,7 @@
|
|||
*/
|
||||
|
||||
use Automattic\WooCommerce\Internal\Traits\AccessiblePrivateMethods;
|
||||
use Automattic\WooCommerce\Proxies\LegacyProxy;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
|
@ -209,7 +210,7 @@ class WC_Payment_Gateways {
|
|||
);
|
||||
$email_addresses[] = $admin_email;
|
||||
|
||||
$logger = wc_get_logger();
|
||||
$logger = wc_get_container()->get( LegacyProxy::class )->call_function( 'wc_get_logger' );
|
||||
$logger->info( sprintf( 'Payment gateway enabled: "%s"', $gateway_title ) );
|
||||
|
||||
$email_text = sprintf(
|
||||
|
|
|
@ -13,6 +13,8 @@ class WC_Tests_Payment_Gateway extends WC_Unit_Test_Case {
|
|||
*/
|
||||
public function setUp(): void {
|
||||
parent::setUp();
|
||||
$this->reset_legacy_proxy_mocks();
|
||||
|
||||
WC()->session = null;
|
||||
$wc_payment_gateways = WC_Payment_Gateways::instance();
|
||||
$wc_payment_gateways->init();
|
||||
|
@ -56,4 +58,63 @@ class WC_Tests_Payment_Gateway extends WC_Unit_Test_Case {
|
|||
$this->assertTrue( $current_gateway->chosen );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that enabling a gateway sends an email to the site admin and logs the event.
|
||||
*/
|
||||
public function test_wc_payment_gateway_enabled_notification() {
|
||||
// Create a fake logger to capture log entries.
|
||||
// phpcs:disable Squiz.Commenting
|
||||
$fake_logger = new class() {
|
||||
public $infos = array();
|
||||
|
||||
public function info( $message, $data = array() ) {
|
||||
$this->infos[] = array(
|
||||
'message' => $message,
|
||||
'data' => $data,
|
||||
);
|
||||
}
|
||||
};
|
||||
// phpcs:enable Squiz.Commenting
|
||||
$this->register_legacy_proxy_function_mocks(
|
||||
array(
|
||||
'wc_get_logger' => function() use ( $fake_logger ) {
|
||||
return $fake_logger;
|
||||
},
|
||||
)
|
||||
);
|
||||
|
||||
// Register a watcher for wp_mail to capture email details.
|
||||
$email_details = array();
|
||||
$watcher = function( $args ) use ( &$email_details ) {
|
||||
$email_details = $args;
|
||||
};
|
||||
add_filter( 'wp_mail', $watcher );
|
||||
|
||||
// Enable each gateway and check that the email and log entry are created.
|
||||
foreach ( WC()->payment_gateways()->payment_gateways() as $gateway ) {
|
||||
// Disable the gateway and save the settings.
|
||||
$gateway->settings['enabled'] = 'no';
|
||||
update_option( $gateway->get_option_key(), $gateway->settings );
|
||||
|
||||
// Enable the gateway and save its settings; this should send the email and add a log entry.
|
||||
$gateway->settings['enabled'] = 'yes';
|
||||
update_option( $gateway->get_option_key(), $gateway->settings );
|
||||
|
||||
// Check that the log entry was created.
|
||||
$this->assertEquals( 'Payment gateway enabled: "' . $gateway->get_title() . '"', end( $fake_logger->infos )['message'] );
|
||||
|
||||
// Check that the email was sent correctly.
|
||||
$this->assertStringContainsString( '@', $email_details['to'][0] );
|
||||
$this->assertEquals( get_option( 'admin_email' ), $email_details['to'][0] );
|
||||
$this->assertEquals( '[Test Blog] Payment gateway "' . $gateway->get_title() . '" enabled', $email_details['subject'] );
|
||||
$this->assertStringContainsString( 'The payment gateway "' . $gateway->get_title() . '" was just enabled on this site', $email_details['message'] );
|
||||
$this->assertStringContainsString( 'If you did not enable this payment gateway, please log in to your site and consider disabling it here:', $email_details['message'] );
|
||||
$this->assertStringContainsString( '/wp-admin/admin.php?page=wc-settings&tab=checkout§ion=' . $gateway->id, $email_details['message'] );
|
||||
|
||||
// Reset the email details.
|
||||
$email_details = array();
|
||||
}
|
||||
remove_filter( 'wp_mail', $watcher );
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue