Skip to content

Commit

Permalink
fix all unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wordpressfan committed Jun 27, 2024
1 parent 649171e commit cff2006
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ private function send_request( $method, $params = [], $safe = false ) {

$response = $this->send_remote_request( $api_url, $method, $params, $safe );

$body = wp_remote_retrieve_body( $response );
if ( empty( $body ) ) {
if ( is_wp_error( $response ) ) {
$this->set_timeout_transients();
return new WP_Error( 500, __( 'Not valid response body.', 'rocket' ) );
return $response;
}

if ( is_wp_error( $response ) || ( is_array( $response ) && 200 !== $response['response']['code'] ) ) {
$body = wp_remote_retrieve_body( $response );
if ( empty( $body ) || ( is_array( $response ) && !empty( $response['response']['code'] ) && 200 !== $response['response']['code'] ) ) {

Check notice on line 75 in inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php#L75

Expected 1 space after "!"; 0 found
$this->set_timeout_transients();
return $response;
return new WP_Error( 500, __( 'Not valid response.', 'rocket' ) );
}

$this->delete_timeout_transients();
Expand All @@ -85,7 +85,7 @@ private function send_request( $method, $params = [], $safe = false ) {
/**
* Set the timeout transients.
*/
protected function set_timeout_transients() {
private function set_timeout_transients() {
$transient_key = $this->get_transient_key();

$timeout = (int) get_transient( $transient_key . '_timeout' );
Expand All @@ -110,7 +110,7 @@ protected function set_timeout_transients() {
*
* @return void
*/
protected function delete_timeout_transients() {
private function delete_timeout_transients() {
$transient_key = $this->get_transient_key();
delete_transient( $transient_key . '_timeout_active' );
delete_transient( $transient_key . '_timeout' );
Expand Down
2 changes: 1 addition & 1 deletion inc/Engine/License/API/LicenseValidationClient.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace WP_Rocket\Engine\Plugin;
namespace WP_Rocket\Engine\License\API;

use WP_Rocket\Engine\Common\JobManager\APIHandler\AbstractSafeAPIClient;

Expand Down
2 changes: 1 addition & 1 deletion inc/functions/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use WP_Rocket\Admin\Options;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Logger\Logger;
use WP_Rocket\Engine\Plugin\LicenseValidationClient;
use WP_Rocket\Engine\License\API\LicenseValidationClient;

defined( 'ABSPATH' ) || exit;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace WP_Rocket\Tests\Unit\Inc\Plugin\InformationSubscriber;

use Brain\Monkey\Functions;
use WP_Rocket\Engine\Plugin\InformationAPIClient;
use WP_Rocket\Engine\Plugin\InformationSubscriber;
use WP_Rocket\Tests\Unit\TestCase;
use Mockery;

/**
* Test class covering WP_Rocket\Engine\Plugin\InformationSubscriber::add_plugins_to_result
Expand All @@ -21,7 +23,8 @@ protected function setUp(): void {
[
'plugin_file' => 'wp-rocket/wp-rocket.php',
'api_url' => 'https://wp-rocket.me',
]
],
Mockery::mock( InformationAPIClient::class )
);
}

Expand Down
37 changes: 30 additions & 7 deletions tests/Unit/inc/functions/rocketCheckKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ public function testShouldReturnArrayWhenSuccessfulValidation() {
->andReturn( 'wp_rocket_settings' );
Functions\expect( 'rocket_valid_key' )->once()->andReturn( false );
Functions\expect( 'rocket_delete_licence_data_file' )->never();
Functions\expect( 'wp_remote_get' )
Functions\expect( 'wp_remote_request' )
->once()
->with( 'https://wp-rocket.me/valid_key.php', [ 'timeout' => 30 ] )
->with( 'https://wp-rocket.me/valid_key.php', [ 'method' => 'GET', 'timeout' => 30 ] )
->andReturn( [] );
Functions\expect( 'is_wp_error' )->once()->andReturn( false );
Functions\expect( 'is_wp_error' )->twice()->andReturn( false );
Functions\expect( 'wp_remote_retrieve_body' )
->once()
->twice()
->with( [] )
->andReturn( '{"success": true, "data":{"consumer_key":"ABCDEF","consumer_email":"[email protected]","secret_key":"secret"}}' );
Functions\expect( 'get_rocket_option' )->once()->with( 'license' )->andReturn( true );
Expand Down Expand Up @@ -80,6 +80,11 @@ public function testShouldReturnArrayWhenSuccessfulValidation() {
'secret_key' => 'secret',
];

Functions\expect( 'get_transient' )
->once()
->with( 'wp_rocket_license_validation_timeout_active' )
->andReturn( false );

$this->assertSame( $expected, rocket_check_key() );
}

Expand All @@ -89,7 +94,7 @@ public function testShouldReturnFalseWhenIsWPError() {
->with( 'WP_ROCKET_WEB_VALID' )
->andReturn( 'https://wp-rocket.me/valid_key.php' );
Functions\when( 'rocket_valid_key' )->justReturn( false );
Functions\when( 'wp_remote_get' )->alias( function() {
Functions\when( 'wp_remote_request' )->alias( function() {
$wp_error = \Mockery::mock( \WP_Error::class )->makePartial();
$wp_error->shouldReceive( 'get_error_messages' )
->withNoArgs()
Expand All @@ -104,6 +109,11 @@ public function testShouldReturnFalseWhenIsWPError() {
Functions\expect('update_option')
->never();

Functions\expect( 'get_transient' )
->once()
->with( 'wp_rocket_license_validation_timeout_active' )
->andReturn( false );

$this->assertFalse( rocket_check_key() );
}

Expand All @@ -113,14 +123,19 @@ public function testShouldReturnFalseWhenEmptyResponse() {
->with( 'WP_ROCKET_WEB_VALID' )
->andReturn( 'https://wp-rocket.me/valid_key.php' );
Functions\when( 'rocket_valid_key' )->justReturn( false );
Functions\when( 'wp_remote_get' )->justReturn( [] );
Functions\when( 'wp_remote_request' )->justReturn( [] );
Functions\when( 'is_wp_error' )->justReturn( false );
Functions\when( 'wp_remote_retrieve_body' )->justReturn( '' );
Functions\when( 'set_transient' )->justReturn( true );
Functions\expect( 'rocket_delete_licence_data_file' )->never();
Functions\expect('update_option')
->never();

Functions\expect( 'get_transient' )
->once()
->with( 'wp_rocket_license_validation_timeout_active' )
->andReturn( false );

$this->assertFalse( rocket_check_key() );
}

Expand All @@ -134,20 +149,28 @@ public function testShouldReturnArrayWhenSuccessFalse() {
->with( 'WP_ROCKET_SLUG' )
->andReturn( 'wp_rocket_settings' );
Functions\when( 'rocket_valid_key' )->justReturn( false );
Functions\when( 'wp_remote_get' )->justReturn( [] );
Functions\when( 'wp_remote_request' )->justReturn( [] );
Functions\when( 'is_wp_error' )->justReturn( false );
Functions\when( 'wp_remote_retrieve_body' )->justReturn( '{"success": false, "data":{"consumer_key":"ABCDEF","consumer_email":"[email protected]","reason":"BAD_KEY"}}' );
Functions\when( 'set_transient' )->justReturn( true );
Functions\expect( 'rocket_delete_licence_data_file' )->never();
Functions\expect('update_option')
->never();

Functions\expect( 'get_transient' )
->once()
->with( 'wp_rocket_license_validation_timeout_active' )
->andReturn( false );

$expected = [
'consumer_key' => 'ABCDEF',
'consumer_email' => '[email protected]',
'secret_key' => '',
];

Functions\expect( 'delete_transient' )->with( 'wp_rocket_license_validation_timeout' )->andReturn(null);
Functions\expect( 'delete_transient' )->with( 'wp_rocket_license_validation_timeout_active' )->andReturn(null);

$this->assertSame( $expected, rocket_check_key() );
}
}

0 comments on commit cff2006

Please sign in to comment.