From 872785770360e97245e1fda088456e6156fcb5db Mon Sep 17 00:00:00 2001 From: Alexander Skrivanos Date: Mon, 11 Sep 2023 13:02:51 -0600 Subject: [PATCH] Fix get_upgrade_campaign causing undefined array key warnings --- .../wincher/wincher-account-action.php | 12 +++++------ .../wincher/wincher-account-action-test.php | 21 +++++++++++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/actions/wincher/wincher-account-action.php b/src/actions/wincher/wincher-account-action.php index ea9bb54cfd8..49351da6c83 100644 --- a/src/actions/wincher/wincher-account-action.php +++ b/src/actions/wincher/wincher-account-action.php @@ -72,13 +72,13 @@ public function check_limit() { */ public function get_upgrade_campaign() { try { - $result = $this->client->get( self::UPGRADE_CAMPAIGN_URL ); - $type = $result['type']; - $months = $result['months']; + $result = $this->client->get( self::UPGRADE_CAMPAIGN_URL ); + $type = isset( $result['type'] ) ? $result['type'] : null; + $months = isset( $result['months'] ) ? $result['months'] : null; + $discount = isset( $result['value'] ) ? $result['value'] : null; - // We display upgrade discount only if it's a rate discount and positive months. - if ( $type === 'RATE' && $months && $months > 0 ) { - $discount = $result['value']; + // We display upgrade discount only if it's a rate discount and positive months/discount. + if ( $type === 'RATE' && $months && $discount ) { return (object) [ 'discount' => $discount, diff --git a/tests/unit/actions/wincher/wincher-account-action-test.php b/tests/unit/actions/wincher/wincher-account-action-test.php index 7c6855f404f..649464e961c 100644 --- a/tests/unit/actions/wincher/wincher-account-action-test.php +++ b/tests/unit/actions/wincher/wincher-account-action-test.php @@ -248,4 +248,25 @@ public function test_valid_get_upgrade_campaign() { $this->instance->get_upgrade_campaign() ); } + + /** + * Tests empty get upgrade campaign. + * + * @covers ::get_upgrade_campaign + */ + public function test_empty_get_upgrade_campaign() { + $this->client_instance + ->expects( 'get' ) + ->with( 'https://api.wincher.com/v1/yoast/upgrade-campaign' ) + ->andReturn( [] ); + + $this->assertEquals( + (object) [ + 'discount' => null, + 'months' => null, + 'status' => 200, + ], + $this->instance->get_upgrade_campaign() + ); + } }