2021-08-31 04:39:04 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* MailchimpScheduler tests
|
|
|
|
*
|
2022-03-17 20:10:43 +00:00
|
|
|
* @package Automattic\WooCommerce\Internal\Admin\Schedulers
|
2021-08-31 04:39:04 +00:00
|
|
|
*/
|
|
|
|
|
2022-03-17 20:10:43 +00:00
|
|
|
use Automattic\WooCommerce\Internal\Admin\Onboarding;
|
2022-02-17 18:48:31 +00:00
|
|
|
use Automattic\WooCommerce\Internal\Admin\Schedulers\MailchimpScheduler;
|
2022-03-17 20:10:43 +00:00
|
|
|
use Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingProfile;
|
2021-08-31 04:39:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class WC_Tests_Mailchimp_Scheduler
|
|
|
|
*/
|
|
|
|
class WC_Tests_Mailchimp_Scheduler extends WC_Unit_Test_Case {
|
|
|
|
/**
|
2022-02-17 18:48:31 +00:00
|
|
|
* @var \Automattic\WooCommerce\Internal\Admin\Schedulers\MailchimpScheduler MailchimpScheduler instance to test
|
2021-08-31 04:39:04 +00:00
|
|
|
*/
|
|
|
|
private $instance;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \PHPUnit\Framework\MockObject\MockObject WC_Logger_Interface mock
|
|
|
|
*/
|
|
|
|
private $logger_mock;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Overridden setUp() method.
|
|
|
|
*/
|
|
|
|
public function setUp() {
|
|
|
|
$this->logger_mock = $this->getMockBuilder( \WC_Logger_Interface::class )->getMock();
|
|
|
|
|
|
|
|
$this->instance = $this->getMockBuilder( MailchimpScheduler::class )
|
|
|
|
->setConstructorArgs( array( $this->logger_mock ) )
|
|
|
|
->setMethods( array( 'make_request' ) )
|
|
|
|
->getMock();
|
|
|
|
|
2022-02-23 01:37:00 +00:00
|
|
|
MailchimpScheduler::reset();
|
2021-08-31 04:39:04 +00:00
|
|
|
|
|
|
|
parent::setUp();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When woocommerce_onboarding_subscribed_to_mailchimp value us 'yes'.
|
|
|
|
* Then it should abort.
|
|
|
|
*/
|
|
|
|
public function test_it_aborts_if_already_subscribed() {
|
|
|
|
// When.
|
|
|
|
update_option( MailchimpScheduler::SUBSCRIBED_OPTION_NAME, 'yes' );
|
|
|
|
|
|
|
|
// Then.
|
|
|
|
$this->assertFalse( $this->instance->run() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When is_agree_marketing is missing or value is false.
|
|
|
|
* Then it should abort.
|
|
|
|
*/
|
|
|
|
public function test_it_aborts_if_is_agree_marketing_is_false_or_missing() {
|
|
|
|
// When.
|
|
|
|
$profile_data = array( 'store_email' => 'test@test.com' );
|
2022-03-17 20:10:43 +00:00
|
|
|
update_option( OnboardingProfile::DATA_OPTION, $profile_data );
|
2021-08-31 04:39:04 +00:00
|
|
|
$this->assertFalse( $this->instance->run() );
|
|
|
|
|
|
|
|
$profile_data = array(
|
|
|
|
'is_agree_marketing' => false,
|
|
|
|
'store_email' => 'test@test.com',
|
|
|
|
);
|
2022-03-17 20:10:43 +00:00
|
|
|
update_option( OnboardingProfile::DATA_OPTION, $profile_data );
|
2021-08-31 04:39:04 +00:00
|
|
|
|
|
|
|
// Then.
|
|
|
|
$this->assertFalse( $this->instance->run() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When store_email is missing.
|
|
|
|
* Then it should abort.
|
|
|
|
*/
|
|
|
|
public function test_it_aborts_if_store_email_is_missing() {
|
|
|
|
// When.
|
|
|
|
$profile_data = array( 'is_agree_marketing' => true );
|
2022-03-17 20:10:43 +00:00
|
|
|
update_option( OnboardingProfile::DATA_OPTION, $profile_data );
|
2021-08-31 04:39:04 +00:00
|
|
|
|
|
|
|
// Then.
|
|
|
|
$this->assertFalse( $this->instance->run() );
|
|
|
|
}
|
|
|
|
|
2022-02-23 01:37:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* When failed requests reaches the threshold.
|
|
|
|
* Then it should abort.
|
|
|
|
*/
|
|
|
|
public function test_it_aborts_if_failed_requests_reaches_the_threshold() {
|
|
|
|
// Given.
|
|
|
|
$profile_data = array(
|
|
|
|
'is_agree_marketing' => true,
|
|
|
|
'store_email' => 'test@test.com',
|
|
|
|
);
|
2022-03-17 20:10:43 +00:00
|
|
|
update_option( OnboardingProfile::DATA_OPTION, $profile_data );
|
2022-02-23 01:37:00 +00:00
|
|
|
|
|
|
|
// When.
|
|
|
|
update_option(
|
|
|
|
MailchimpScheduler::SUBSCRIBED_ERROR_COUNT_OPTION_NAME,
|
|
|
|
MailchimpScheduler::MAX_ERROR_THRESHOLD
|
|
|
|
);
|
|
|
|
|
|
|
|
// Then.
|
|
|
|
$this->assertFalse( $this->instance->run() );
|
|
|
|
}
|
|
|
|
|
2021-08-31 04:39:04 +00:00
|
|
|
/**
|
|
|
|
* Given is_agree_marketing and store_email values are set.
|
|
|
|
* When the request to the API returns WP_Error.
|
2022-02-23 01:37:00 +00:00
|
|
|
* Then it should log error message and error count.
|
2021-08-31 04:39:04 +00:00
|
|
|
*/
|
|
|
|
public function test_it_logs_error_when_wp_error_is_returned() {
|
|
|
|
// Given.
|
|
|
|
$profile_data = array(
|
|
|
|
'is_agree_marketing' => true,
|
|
|
|
'store_email' => 'test@test.com',
|
|
|
|
);
|
2022-03-17 20:10:43 +00:00
|
|
|
update_option( OnboardingProfile::DATA_OPTION, $profile_data );
|
2021-08-31 04:39:04 +00:00
|
|
|
|
|
|
|
// When.
|
|
|
|
$wp_error = new WP_Error();
|
|
|
|
$this->instance->method( 'make_request' )->willReturn( $wp_error );
|
|
|
|
|
|
|
|
// Then.
|
|
|
|
$this->logger_mock->expects( $this->exactly( 2 ) )
|
|
|
|
->method( 'error' )
|
|
|
|
->with( 'Error getting a response from Mailchimp API.', array( 'source' => MailchimpScheduler::LOGGER_CONTEXT ) );
|
|
|
|
|
|
|
|
$this->instance->run();
|
2022-02-23 01:37:00 +00:00
|
|
|
$this->assertEquals( 1, get_option( MailchimpScheduler::SUBSCRIBED_ERROR_COUNT_OPTION_NAME ) );
|
2021-08-31 04:39:04 +00:00
|
|
|
|
|
|
|
// Check for the missing 'body'.
|
|
|
|
$this->instance->method( 'make_request' )->willReturn( array() );
|
|
|
|
$this->instance->run();
|
2022-02-23 01:37:00 +00:00
|
|
|
$this->assertEquals( 2, get_option( MailchimpScheduler::SUBSCRIBED_ERROR_COUNT_OPTION_NAME ) );
|
2021-08-31 04:39:04 +00:00
|
|
|
}
|
|
|
|
|
2022-02-23 01:37:00 +00:00
|
|
|
|
|
|
|
|
2021-08-31 04:39:04 +00:00
|
|
|
/**
|
|
|
|
* Given is_agree_marketing and store_email values are set.
|
|
|
|
* When the request to the API returns body without 'success'.
|
2022-02-23 01:37:00 +00:00
|
|
|
* Then it should log error message and error count.
|
2021-08-31 04:39:04 +00:00
|
|
|
*/
|
|
|
|
public function test_it_logs_error_when_response_data_is_incorrect() {
|
|
|
|
// Given.
|
|
|
|
$profile_data = array(
|
|
|
|
'is_agree_marketing' => true,
|
|
|
|
'store_email' => 'test@test.com',
|
|
|
|
);
|
2022-03-17 20:10:43 +00:00
|
|
|
update_option( OnboardingProfile::DATA_OPTION, $profile_data );
|
2021-08-31 04:39:04 +00:00
|
|
|
|
|
|
|
// When.
|
|
|
|
$body = wp_json_encode( array() );
|
|
|
|
$this->instance->method( 'make_request' )->willReturn( array( 'body' => $body ) );
|
|
|
|
|
|
|
|
// Then.
|
|
|
|
$this->logger_mock->expects( $this->once() )
|
|
|
|
->method( 'error' )
|
|
|
|
->with(
|
|
|
|
// phpcs:ignore
|
|
|
|
'Incorrect response from Mailchimp API with: ' . print_r( array(), true ),
|
|
|
|
array( 'source' => MailchimpScheduler::LOGGER_CONTEXT )
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->instance->run();
|
2022-02-23 01:37:00 +00:00
|
|
|
$this->assertEquals( 1, get_option( MailchimpScheduler::SUBSCRIBED_ERROR_COUNT_OPTION_NAME ) );
|
2021-08-31 04:39:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given is_agree_marketing and store_email values are set
|
|
|
|
* When the request to the API returns success: true
|
|
|
|
* Then woocommerce_onboarding_subscribed_to_mailchimp should be updated to 'yes'
|
|
|
|
*/
|
|
|
|
public function test_it_updates_option_value_when_everything_went_well() {
|
|
|
|
// Given.
|
|
|
|
$profile_data = array(
|
|
|
|
'is_agree_marketing' => true,
|
|
|
|
'store_email' => 'test@test.com',
|
|
|
|
);
|
2022-03-17 20:10:43 +00:00
|
|
|
update_option( OnboardingProfile::DATA_OPTION, $profile_data );
|
2021-08-31 04:39:04 +00:00
|
|
|
|
|
|
|
// When.
|
|
|
|
$body = wp_json_encode( array( 'success' => true ) );
|
|
|
|
$this->instance->method( 'make_request' )->willReturn( array( 'body' => $body ) );
|
|
|
|
|
|
|
|
$this->instance->run();
|
|
|
|
|
|
|
|
// Then.
|
|
|
|
$this->assertEquals( 'yes', get_option( 'woocommerce_onboarding_subscribed_to_mailchimp' ) );
|
|
|
|
}
|
|
|
|
}
|