Skip to content
This repository has been archived by the owner on Nov 6, 2022. It is now read-only.

Commit

Permalink
Handle a case where there is no download_link
Browse files Browse the repository at this point in the history
It might be that the api response
has no ->download_link property
  • Loading branch information
kienstra committed Aug 29, 2020
1 parent 40ed157 commit 9fac8ca
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
7 changes: 7 additions & 0 deletions php/admin/migration/class-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,13 @@ public function get_download_link() {
return $api;
}

if ( empty( $api->download_link ) ) {
return new WP_Error(
'no_download_link',
__( 'There was no download_link in the API', 'block-lab' )
);
}

return $api->download_link;
}
}
Expand Down
24 changes: 24 additions & 0 deletions tests/php/unit/admin/migration/test-class-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,30 @@ static function() use ( $api ) {
$this->assertEquals( $download_link, $this->instance->get_download_link() );
}

/**
* Test get_download_link when there is no download_link.
*
* @covers Block_Lab\Admin\Migration\Api::get_download_link()
*/
public function test_get_download_link_no_download_link() {
add_filter(
'plugins_api_result',
static function() {
return new stdClass();
}
);

$actual = $this->instance->get_download_link();
$this->assertEquals(
'no_download_link',
$actual->get_error_code()
);
$this->assertEquals(
'There was no download_link in the API',
$actual->get_error_message()
);
}

/**
* Test get_download_link when it returns an error.
*
Expand Down

0 comments on commit 9fac8ca

Please sign in to comment.