Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #6850 Prewarmup home in certain scenarios where it shouldn't #6881

Merged
merged 10 commits into from
Aug 23, 2024
25 changes: 15 additions & 10 deletions inc/Engine/Common/PerformanceHints/WarmUp/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,26 @@ public function __construct( array $factories, Options_Data $options, APIClient
$this->queue = $queue;
}

/**
* Should terminate early if true.
*
* @return bool
*/
private function is_allowed(): bool {
return ! (
'local' === wp_get_environment_type() ||
$this->user->is_license_expired_grace_period() ||
(bool) $this->options->get( 'remove_unused_css', 0 )
);
}

/**
* Send home URL for warm up.
*
* @return void
*/
public function warm_up_home(): void {
if ( (bool) $this->options->get( 'remove_unused_css', 0 ) ) {
if ( ! $this->is_allowed() ) {
return;
}

Expand All @@ -84,11 +97,7 @@ public function warm_up_home(): void {
* @return void
*/
public function warm_up(): void {
if ( 'local' === wp_get_environment_type() ) {
return;
}

if ( (bool) $this->options->get( 'remove_unused_css', 0 ) ) {
if ( ! $this->is_allowed() ) {
return;
}

Expand All @@ -109,10 +118,6 @@ public function warm_up(): void {
* @return array
*/
public function fetch_links(): array {
if ( $this->user->is_license_expired_grace_period() ) {
return [];
}

$user_agent = 'WP Rocket/Pre-fetch Home Links Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1';

$home_url = home_url();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,6 @@
$html_with_rss_feed_rest_api = '<!DOCTYPE html><html class="no-js" lang="en-US"><head><title></title><link href="https://fonts.gstatic.com" crossorigin="preconnect"><link rel="stylesheet" id="wp-block-library-css" href="https://example.org/wp-includes/css/dist/block-library/style.min.css?ver=6.4.3" media="all"></head><body><button data-link="https://example.org/hello-world">Click Here</button><a href="https://example.org/feed">RSS Feed</a><a href="https://example.org/wp-json/wp/v2/users">Rest API</a><a href="https://example.org/hello-world-4">Hello World 4</a><a href="https://example.org/hello-world-5">Hello World 5</a><a href="https://example.org/hello-world-6">Hello World 6</a><a href="https://example.org/hello-world-7">Hello World 7</a><a href="https://example.org/hello-world-8">Hello World 8</a><a href="https://example.org/hello-world-9">Hello World 9</a><a href="https://example.org/rich-dad-poor-dad">Rich Dad Poor Dad</a><a href="https://example.org/rebecca-brown-he-came-to-set-the-captives-free">Buy (He came to set the captives free) - Rebecca Brown</a><a href="https://example.org">Home</a></body></html>';

return [
'shouldReturnEmptyWhenLicenseExpired' => [
'config' => [
'license_expired' => true,
'headers' => [
'user-agent' => 'WP Rocket/Pre-fetch Home Links Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1',
'timeout' => 60,
],
'response' => [
'response' => [
'code' => 500,
],
],
],
'expected' => [],
],
'shouldReturnEmptyWhenNot200' => [
'config' => [
'license_expired' => false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,33 @@
'wp_env' => 'production',
'remove_unused_css' => 0,
'is_allowed' => [1],
'license_expired' => false,
'links' => [
'http://example.com/link1',
'http://example.com/link2',
],
],
'expected' => 2,
],
'testShouldNotCallSendToSaasWhenLicenseExpired' => [
'config' => [
'wp_env' => 'production',
'remove_unused_css' => 0,
'is_allowed' => [1],
'license_expired' => true,
'links' => [
'http://example.com/link1',
'http://example.com/link2',
],
],
'expected' => 0,
],
'testShouldNotCallSendToSaasWhenLocalEnv' => [
'config' => [
'wp_env' => 'local',
'remove_unused_css' => 0,
'is_allowed' => [1],
'license_expired' => false,
'links' => [
'http://example.com/link1',
'http://example.com/link2',
Expand All @@ -28,6 +43,7 @@
'config' => [
'wp_env' => 'production',
'remove_unused_css' => 1,
'license_expired' => false,
'is_allowed' => [1],
'links' => [
'http://example.com/link1',
Expand All @@ -40,6 +56,7 @@
'config' => [
'wp_env' => 'production',
'remove_unused_css' => 0,
'license_expired' => false,
'is_allowed' => [],
'links' => [
'http://example.com/link1',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
return [
'testShouldCallSendToSaas' => [
'config' => [
'wp_env' => 'production',
'remove_unused_css' => 0,
'factories' => [1],
'license_expired' => false,
'home_url' => 'http://example.com/',
],
'expected' => 1,
],
'testShouldNotCallSendToSaasWhenLicenseExpired' => [
'config' => [
'wp_env' => 'production',
'remove_unused_css' => 0,
'factories' => [1],
'license_expired' => true,
'home_url' => 'http://example.com/',
],
'expected' => 0,
],
'testShouldNotCallSendToSaasWhenLocalEnv' => [
'config' => [
'wp_env' => 'local',
'remove_unused_css' => 0,
'factories' => [1],
'license_expired' => false,
'home_url' => 'http://example.com/',
],
'expected' => 0,
],
'testShouldNotCallSendToSaasWhenRemoveUnusedCssEnabled' => [
'config' => [
'wp_env' => 'production',
'remove_unused_css' => 1,
'license_expired' => false,
'factories' => [1],
'home_url' => 'http://example.com/',
],
'expected' => 0,
],
'testShouldNotCallSendToSaasWhenFactoriesAreEmpty' => [
'config' => [
'wp_env' => 'production',
'remove_unused_css' => 0,
'license_expired' => false,
'factories' => [],
'home_url' => 'http://example.com/',
],
'expected' => 0,
],
];
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,19 @@ public function testShouldDoExpected( $config, $expected ) {
$options->shouldReceive('get')
->with('remove_unused_css', 0)
->andReturn($config['remove_unused_css']);

$user->shouldReceive( 'is_license_expired_grace_period' )
->once()
->andReturn( $config['license_expired'] );
}

$queue->shouldReceive('add_job_warmup_url')
->times($expected);


add_action('rocket_job_warmup', [$controller, 'warm_up']);

do_action('rocket_job_warmup');

remove_action('rocket_job_warmup', [$controller, 'warm_up']);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace WP_Rocket\tests\Integration\inc\Engine\Common\PerformanceHints\WarmUp\Subscriber;

use Mockery;
use Brain\Monkey\Functions;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Engine\Common\PerformanceHints\WarmUp\APIClient;
use WP_Rocket\Engine\Common\PerformanceHints\WarmUp\Controller;
use WP_Rocket\Engine\Common\PerformanceHints\WarmUp\Queue;
use WP_Rocket\Engine\License\API\User;
use WP_Rocket\Tests\Integration\TestCase;

/**
* Test class covering \WP_Rocket\Engine\Common\PerformanceHints\WarmUp\Subscriber::warm_up_home
*
* @group PerformanceHints
* @group WarmUp
*/
class Test_WarmUpHome extends TestCase {
/**
* Test should do expected.
*
* @dataProvider configTestData
*/
public function testShouldDoExpected( $config, $expected ) {
$options = Mockery::mock(Options_Data::class);
$api_client = Mockery::mock(APIClient::class);
$user = Mockery::mock(User::class);
$queue = Mockery::mock(Queue::class);
$controller = Mockery::mock(Controller::class, [$config['factories'], $options, $api_client, $user, $queue])->makePartial();

$controller->shouldReceive('send_to_saas')
->andReturn($config['home_url']);

Functions\expect( 'wp_get_environment_type' )->andReturn($config['wp_env']);

$queue->shouldReceive('add_job_warmup')
->times($expected);

if ( 'local' !== $config['wp_env'] ) {
$options->shouldReceive('get')
->with('remove_unused_css', 0)
->andReturn($config['remove_unused_css']);

$user->shouldReceive( 'is_license_expired_grace_period' )
->once()
->andReturn( $config['license_expired'] );
}

add_action('rocket_after_clear_performance_hints_data', [$controller, 'warm_up_home']);

do_action('rocket_after_clear_performance_hints_data');

remove_action('rocket_after_clear_performance_hints_data', [$controller, 'warm_up_home']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ protected function setUp(): void {
* @dataProvider configTestData
*/
public function testShouldReturnExpected( $config, $expected ) {
$this->user->shouldReceive( 'is_license_expired_grace_period' )
->once()
->andReturn( $config['license_expired'] );

Functions\when( 'home_url' )->alias(
function ( $link = '' ) {
Expand Down
Loading