Skip to content

Commit

Permalink
Merge pull request #2588 from Automattic/change/plugin-information-en…
Browse files Browse the repository at this point in the history
…dpoint
  • Loading branch information
fjorgemota authored Oct 3, 2023
2 parents e15265f + 0ef43c6 commit c7ea142
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 17 deletions.
41 changes: 36 additions & 5 deletions includes/helper/class-wp-job-manager-helper-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,44 @@ public function bulk_update_check( array $plugins ) {
* Sends and receives data related to plugin information from the WPJobManager.com API.
*
* @param array $args The arguments to pass to the endpoint.
* @return array|false The response, or false if the request failed.
* @return \stdClass|false The response, or false if the request failed.
*/
public function plugin_information( $args ) {
$args = wp_parse_args( $args );
$args['wc-api'] = 'wp_plugin_licencing_update_api';
$args['request'] = 'plugininformation';
return $this->request( $args );
$args = wp_parse_args( $args );
$data = $this->request_endpoint(
'wp-json/wpjmcom-licensing/v1/plugin-information',
[
'method' => 'GET',
'body' => [
'site_url' => $this->get_site_url(),
'license_key' => $args['license_key'],
'product_slug' => $args['api_product_id'],
],
]
);
if ( ! is_array( $data ) ) {
return false;
}
$response = new \stdClass();
$response->name = $data['name'];
$slug = $data['slug'];
$response->plugin = $slug . '/' . $slug . '.php';
$response->slug = $slug;
$response->version = $data['version'];
$response->last_updated = $data['last_updated'];
$response->author = $data['author'];
$response->requires = $data['requires'];
$response->tested = $data['tested'];
$response->homepage = $data['homepage'];

// set sections.
$response->sections = [
'description' => $data['sections']['description'],
'changelog' => $data['sections']['changelog'],
];

$response->download_link = $data['download_link'];
return $response;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,44 @@ public function test_wp_job_manager_api_instance() {
*/
public function test_plugin_information_valid() {
$base_args = $this->get_base_args();
$this->set_expected_response(
[
'args' => wp_parse_args(
[
'wc-api' => 'wp_plugin_licencing_update_api',
'request' => 'plugininformation',
],
$base_args
),
]
);
$data = [
'name' => 'Sample Plugin',
'slug' => 'sample-plugin',
'version' => '1.0.0',
'last_updated' => '2023-09-22',
'author' => 'John Doe',
'requires' => 'WordPress 5.0+',
'tested' => 'WordPress 5.8',
'homepage' => 'https://www.example.com/sample-plugin',
'sections' => array(
'description' => 'This is a sample plugin for demonstration purposes.',
'changelog' => 'Version 1.0.0 - Initial release',
),
'download_link' => 'https://www.example.com/sample-plugin/sample-plugin.zip',
];
$this->mock_http_request( '/wp-json/wpjmcom-licensing/v1/plugin-information', $data);
$instance = new WP_Job_Manager_Helper_API();
$response = $instance->plugin_information( $base_args );

// If a request was made that we don't expect, `$response` would be false.
$this->assertEquals( $this->default_valid_response(), $response );

$this->assertInstanceOf(stdClass::class, $response);
$this->assertEquals($data['name'], $response->name);
$this->assertEquals($data['slug'], $response->slug);
$this->assertEquals($data['version'], $response->version);
$this->assertEquals($data['last_updated'], $response->last_updated);
$this->assertEquals($data['author'], $response->author);
$this->assertEquals($data['requires'], $response->requires);
$this->assertEquals($data['tested'], $response->tested);
$this->assertEquals($data['homepage'], $response->homepage);

$this->assertEquals($data['sections']['description'], $response->sections['description']);
$this->assertEquals($data['sections']['changelog'], $response->sections['changelog']);

$this->assertEquals($data['download_link'], $response->download_link);

$expectedPlugin = $data['slug'] . '/' . $data['slug'] . '.php';
$this->assertEquals($expectedPlugin, $response->plugin);
}

/**
Expand Down

0 comments on commit c7ea142

Please sign in to comment.