Skip to content

Commit

Permalink
Feat/esp report error handling (#1713)
Browse files Browse the repository at this point in the history
* feat: do not return errors when not configured

* feat: return error when misconfigured

* test: fix tests
  • Loading branch information
leogermani authored Nov 26, 2024
1 parent ad092ca commit 656befb
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -940,10 +940,10 @@ public function get_contact_esp_local_lists_ids( $email ) {
/**
* Get usage data for yesterday.
*
* @return Newspack_Newsletters_Service_Provider_Usage_Report|WP_Error
* @return Newspack_Newsletters_Service_Provider_Usage_Report|WP_Error|null Usage report, error or Null in case there's no need to check for a report.
*/
public function get_usage_report() {
return new WP_Error( 'newspack_newsletters_not_implemented', __( 'Not implemented', 'newspack-newsletters' ), [ 'status' => 400 ] );
return null; // Not implemented for the provider.
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,15 @@ class Newspack_Newsletters_Mailchimp_Usage_Reports {
* @return DrewM\MailChimp\MailChimp|WP_Error
*/
private static function get_mc_api() {
$api_key = Newspack_Newsletters_Mailchimp::instance()->api_key();
if ( empty( $api_key ) ) {
return new WP_Error(
'newspack_newsletters_mailchimp_empty_api_key',
__( 'Mailchimp API key is not set.', 'newspack-newsletters' )
);
}
try {
return new Mailchimp( Newspack_Newsletters_Mailchimp::instance()->api_key() );
return new Mailchimp( $api_key );
} catch ( Exception $e ) {
return new WP_Error(
'newspack_newsletters_mailchimp_error',
Expand All @@ -45,7 +52,9 @@ private static function get_list_activity_reports( $days_in_past_count = 1 ) {
}

$reports = [];
$lists = $mc_api->get( 'lists', [ 'count' => 1000 ] );
$lists = Newspack_Newsletters_Mailchimp::instance()->validate(
$mc_api->get( 'lists', [ 'count' => 1000 ] )
);
if ( ! isset( $lists['lists'] ) ) {
return $reports;
}
Expand Down Expand Up @@ -105,7 +114,16 @@ public static function get_usage_reports( $days_in_past ) {
// sent/opens/clicks data, the campaign reports have to be used.
// It appears that the sent/opens/clicks data in the lists activity are only added after a
// delay of 2-3 days.
$reports = self::get_list_activity_reports( $days_in_past );

try {
// Get the list activity reports.
$reports = self::get_list_activity_reports( $days_in_past );
} catch ( Exception $e ) {
return new WP_Error(
'newspack_newsletters_mailchimp_error',
$e->getMessage()
);
}

$campaign_reports = [];
// Look at reports for campaigns sent at most two weeks ago, unless $days_in_past is larger.
Expand Down Expand Up @@ -170,11 +188,17 @@ public static function get_usage_reports( $days_in_past ) {
/**
* Creates a usage report.
*
* @return Newspack_Newsletters_Service_Provider_Usage_Report|WP_Error Usage report or error.
* @return Newspack_Newsletters_Service_Provider_Usage_Report|WP_Error|null Usage report, error or Null in case there's no need to check for a report.
*/
public static function get_usage_report() {
$reports = self::get_usage_reports( 1 );
if ( \is_wp_error( $reports ) ) {

// if the api key is not set, Mailchimp is not in use, we shouldn't even try to get the usage report.
if ( $reports->get_error_code() === 'newspack_newsletters_mailchimp_empty_api_key' ) {
return null;
}

return $reports;
}
return reset( $reports );
Expand Down
49 changes: 46 additions & 3 deletions tests/test-usage-reports.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,42 @@ class Usage_Reports_Test extends WP_UnitTestCase {
/**
* Test usage report.
*/
public function test_usage_report_basic() {
// Ensure the API key is not set (might be set by a different test).
public function test_usage_report_no_api_key() {

delete_option( 'newspack_mailchimp_api_key' );

self::assertSame(
Newspack_Newsletters_Mailchimp_Usage_Reports::get_usage_report(),
null
);
}

/**
* Test usage report with invalid API key.
*/
public function test_usage_report_invalid_api_key() {

update_option( 'newspack_mailchimp_api_key', 'asd123' );

self::assertSame(
Newspack_Newsletters_Mailchimp_Usage_Reports::get_usage_report()->get_error_code(),
'newspack_newsletters_mailchimp_error'
);
}

/**
* Test usage report with basic data.
*/
public function test_usage_report_basic() {

update_option( 'newspack_mailchimp_api_key', 'asd123' );

add_filter( 'mailchimp_mock_get', [ __CLASS__, 'mock_get_response' ], 10, 3 );
$report = Newspack_Newsletters_Mailchimp_Usage_Reports::get_usage_report();
remove_filter( 'mailchimp_mock_get', [ __CLASS__, 'mock_get_response' ] );

self::assertEquals(
Newspack_Newsletters_Mailchimp_Usage_Reports::get_usage_report()->to_array(),
$report->to_array(),
[
'date' => gmdate( 'Y-m-d', strtotime( '-1 day' ) ),
'emails_sent' => 0,
Expand All @@ -30,4 +60,17 @@ public function test_usage_report_basic() {
]
);
}

/**
* Mock get response.
*
* @param string $method Request method.
* @param string $path Request path.
* @param array $options Request options.
*
* @return array Mock response.
*/
public static function mock_get_response( $method, $path, $options ) {
return [ 'lists' => [] ];
}
}

0 comments on commit 656befb

Please sign in to comment.