Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class DataManager implements LoggerAwareInterface {

'plugin_rocketcdn' => 'page_cdn',
'plugin_imagify' => 'imagify',
'plugin_rocketcdn_free' => 'page_cdn',
];

/**
Expand Down
16 changes: 15 additions & 1 deletion inc/Engine/CDN/RocketCDN/AdminPageSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,21 @@ public function maybe_add_rocketcdn_to_recommendations_api_params( array $params
return $params;
}

$params['enabled_options'][] = 'plugin_rocketcdn';
$subscription_data = $this->api_client->get_subscription_data();

if ( 'running' === $subscription_data['subscription_status'] && 'paid' === $subscription_data['plan_type'] ) {
$params['enabled_options'][] = 'plugin_rocketcdn';

return $params;
}
Comment thread
Khadreal marked this conversation as resolved.

if ( 'running' === $subscription_data['subscription_status'] && 'free' === $subscription_data['plan_type'] ) {
if ( ! $this->user->is_reseller_account() ) {
$params['enabled_options'][] = 'plugin_rocketcdn_free';
}

return $params;
}

return $params;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

return [
'test_data' => [
'shouldReturnDefaultParamsWhenNoCustomerDataORSubscriptionNotRunningORWhiteLabelNotActive' => [
'config' => [
'has_customer_data' => false,
'subscription_status' => 'cancelled',
'white_label' => false,
'params' => [
'shouldReturnParamsUnchangedWhenNoCDNOptionAndNoWhiteLabelAndNoRocketcdnStandalone' => [
'config' => [
'white_label' => false,
'is_reseller' => false,
'has_customer_data' => false,
'subscription_status' => 'cancelled',
'params' => [
'enabled_options' => [ 'plugin_example' ],
],
],
Expand All @@ -18,23 +19,92 @@
],
],

'shouldAddRocketCdnToRecommendationsApiParamsWhenCDNOptionIsEnabled' => [
'config' => [
'has_customer_data' => true,
'subscription_status' => 'cancelled',
'white_label' => false,
'params' => [
'enabled_options' => [
'cdn',
],
'shouldAddPluginRocketcdnWhenSubscriptionIsRunningAndPaidAndNotReseller' => [
'config' => [
'white_label' => false,
'is_reseller' => false,
'has_customer_data' => true,
'subscription_status' => 'running',
'plan_type' => 'paid',
'params' => [
'enabled_options' => [ 'cdn' ],
],
],
'expected' => [
'params' => [
'enabled_options' => [
'cdn',
'plugin_rocketcdn',
],
'enabled_options' => [ 'cdn', 'plugin_rocketcdn' ],
],
],
],

'shouldAddPluginRocketcdnWhenSubscriptionIsRunningAndPaidAndIsReseller' => [
'config' => [
'white_label' => false,
'is_reseller' => true,
'has_customer_data' => true,
'subscription_status' => 'running',
'plan_type' => 'paid',
'params' => [
'enabled_options' => [ 'cdn' ],
],
],
'expected' => [
'params' => [
'enabled_options' => [ 'cdn', 'plugin_rocketcdn' ],
],
],
],

'shouldAddFreeOptionWhenSubscriptionIsRunningAndFreeAndNotReseller' => [
'config' => [
'white_label' => false,
'is_reseller' => false,
'has_customer_data' => true,
'subscription_status' => 'running',
'plan_type' => 'free',
'params' => [
'enabled_options' => [ 'cdn' ],
],
],
'expected' => [
'params' => [
'enabled_options' => [ 'cdn', 'plugin_rocketcdn_free' ],
],
],
],

'shouldNotAddFreeOptionWhenSubscriptionIsRunningAndFreeAndIsReseller' => [
'config' => [
'white_label' => false,
'is_reseller' => true,
'has_customer_data' => true,
'subscription_status' => 'running',
'plan_type' => 'free',
'params' => [
'enabled_options' => [ 'cdn' ],
],
],
'expected' => [
'params' => [
'enabled_options' => [ 'cdn' ],
],
],
],

'shouldReturnParamsUnchangedWhenSubscriptionIsNotRunning' => [
'config' => [
'white_label' => false,
'is_reseller' => false,
'has_customer_data' => true,
'subscription_status' => 'cancelled',
'plan_type' => 'free',
'params' => [
'enabled_options' => [ 'cdn' ],
],
],
'expected' => [
'params' => [
'enabled_options' => [ 'cdn' ],
],
],
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,25 @@ public function tear_down() {
public function testShouldDoAsExpected( $config, $expected ) {
$this->white_label = $config['white_label'];

if ( $config['is_reseller'] ?? false ) {
$user_data = new \stdClass();
$user_data->is_reseller = true;
$container = apply_filters( 'rocket_container', null );
$container->get( 'user' )->set_user( $user_data );
}

if ( $config['has_customer_data'] ) {
set_transient( 'rocketcdn_customer_data', [ 'customer_id' => '123' ], HOUR_IN_SECONDS );
}

if ( isset( $config['subscription_status'] ) ) {
set_transient(
'rocketcdn_status',
[
'subscription_status' => $config['subscription_status'],
],
HOUR_IN_SECONDS
);
$status = [ 'subscription_status' => $config['subscription_status'] ];

if ( isset( $config['plan_type'] ) ) {
$status['plan_type'] = $config['plan_type'];
}

set_transient( 'rocketcdn_status', $status, HOUR_IN_SECONDS );
}

$result = wpm_apply_filters_typed( 'array', 'rocket_insights_api_recommendations_params', $config['params'] );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Tests\Unit\inc\Engine\CDN\RocketCDN\AdminPageSubscriber;

use Mockery;
use WP_Rocket\Engine\CDN\RocketCDN\AdminPageSubscriber;
use WP_Rocket\Engine\CDN\RocketCDN\APIClient;
use WP_Rocket\Engine\License\API\User;
use WP_Rocket\Engine\License\API\UserClient;
use WP_Rocket\Tests\Unit\TestCase;

/**
* Test class covering \WP_Rocket\Engine\CDN\RocketCDN\AdminPageSubscriber::maybe_add_rocketcdn_to_recommendations_api_params
*
* @covers \WP_Rocket\Engine\CDN\RocketCDN\AdminPageSubscriber::maybe_add_rocketcdn_to_recommendations_api_params
* @group RocketCDN
* @group RocketCDNAdminPage
*/
class Test_MaybeAddRocketcdnToRecommendationsApiParams extends TestCase {

/**
* @var Mockery\MockInterface|APIClient
*/
private $api_client;

/**
* @var Mockery\MockInterface|UserClient
*/
private $user_client;

/**
* @var Mockery\MockInterface|User
*/
private $user;

/**
* @var AdminPageSubscriber
*/
private $subscriber;

public function set_up(): void {
parent::set_up();

$this->constants['ROCKETCDN_VERSION'] = '';

$this->api_client = Mockery::mock( APIClient::class );
$this->user_client = Mockery::mock( UserClient::class );
$this->user = Mockery::mock( User::class );
$this->user->shouldReceive( 'is_reseller_account' )->andReturn( false )->byDefault();

$this->subscriber = new AdminPageSubscriber(
$this->api_client,
$this->user_client,
$this->user,
''
);
}

/**
* @dataProvider configTestData
*/
public function testShouldModifyParamsCorrectly( $config, $expected ): void {
$this->white_label = $config['white_label'] ?? false;

if ( $config['is_reseller'] ?? false ) {
$this->user->shouldReceive( 'is_reseller_account' )->andReturn( true );
}

$cdn_in_options = in_array( 'cdn', $config['params']['enabled_options'] ?? [], true );

if ( $this->white_label || $cdn_in_options ) {
$subscription_data = [
'subscription_status' => $config['subscription_status'] ?? '',
'plan_type' => $config['plan_type'] ?? '',
];

$this->api_client->shouldReceive( 'get_subscription_data' )
->once()
->andReturn( $subscription_data );
} else {
$this->api_client->shouldNotReceive( 'get_subscription_data' );
}

$result = $this->subscriber->maybe_add_rocketcdn_to_recommendations_api_params( $config['params'] );

$this->assertSame( $expected['params'], $result );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
$rocket_learn_more_url = $data['learn_more_url'];
$rocket_icon_slug = $data['icon_slug'];
$rocket_impact_tags = $data['impact_tags'];
$rocket_link_title = __( 'Activate', 'rocket' );

if ( 'plugin_rocketcdn' === $rocket_option_slug ) {
$rocket_link_title = __( 'Upgrade', 'rocket' );
}
?>
<div class="wpr-recommendation-item">
<div class="wpr-recommendation-item__inner">
Expand All @@ -39,7 +44,7 @@
'link',
'',
[
'label' => __( 'Activate', 'rocket' ),
'label' => $rocket_link_title,
'url' => esc_url( $data['section'] ),
'attributes' => [
'class' => 'wpr-recommendation-item__activate',
Expand Down
Loading