diff --git a/inc/Engine/Common/PerformanceHints/WarmUp/Controller.php b/inc/Engine/Common/PerformanceHints/WarmUp/Controller.php index 5bffd9b05d..73019b2517 100644 --- a/inc/Engine/Common/PerformanceHints/WarmUp/Controller.php +++ b/inc/Engine/Common/PerformanceHints/WarmUp/Controller.php @@ -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; } @@ -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; } @@ -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(); diff --git a/tests/Fixtures/inc/Engine/Common/PerformanceHints/WarmUp/Controller/fetchLinks.php b/tests/Fixtures/inc/Engine/Common/PerformanceHints/WarmUp/Controller/fetchLinks.php index b87b5b1d41..0abf3937fb 100644 --- a/tests/Fixtures/inc/Engine/Common/PerformanceHints/WarmUp/Controller/fetchLinks.php +++ b/tests/Fixtures/inc/Engine/Common/PerformanceHints/WarmUp/Controller/fetchLinks.php @@ -11,21 +11,6 @@ $html_with_rss_feed_rest_api = 'RSS FeedRest APIHello World 4Hello World 5Hello World 6Hello World 7Hello World 8Hello World 9Rich Dad Poor DadBuy (He came to set the captives free) - Rebecca BrownHome'; 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, diff --git a/tests/Fixtures/inc/Engine/Common/PerformanceHints/WarmUp/Subscriber/warmUp.php b/tests/Fixtures/inc/Engine/Common/PerformanceHints/WarmUp/Subscriber/warmUp.php index 892aaad7b6..5c14bb2383 100644 --- a/tests/Fixtures/inc/Engine/Common/PerformanceHints/WarmUp/Subscriber/warmUp.php +++ b/tests/Fixtures/inc/Engine/Common/PerformanceHints/WarmUp/Subscriber/warmUp.php @@ -5,6 +5,7 @@ 'wp_env' => 'production', 'remove_unused_css' => 0, 'is_allowed' => [1], + 'license_expired' => false, 'links' => [ 'http://example.com/link1', 'http://example.com/link2', @@ -12,11 +13,25 @@ ], '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', @@ -28,6 +43,7 @@ 'config' => [ 'wp_env' => 'production', 'remove_unused_css' => 1, + 'license_expired' => false, 'is_allowed' => [1], 'links' => [ 'http://example.com/link1', @@ -40,6 +56,7 @@ 'config' => [ 'wp_env' => 'production', 'remove_unused_css' => 0, + 'license_expired' => false, 'is_allowed' => [], 'links' => [ 'http://example.com/link1', diff --git a/tests/Fixtures/inc/Engine/Common/PerformanceHints/WarmUp/Subscriber/warmUpHome.php b/tests/Fixtures/inc/Engine/Common/PerformanceHints/WarmUp/Subscriber/warmUpHome.php new file mode 100644 index 0000000000..1c18725374 --- /dev/null +++ b/tests/Fixtures/inc/Engine/Common/PerformanceHints/WarmUp/Subscriber/warmUpHome.php @@ -0,0 +1,53 @@ + [ + '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, + ], +]; diff --git a/tests/Integration/inc/Engine/Common/PerformanceHints/WarmUp/Subscriber/warmUp.php b/tests/Integration/inc/Engine/Common/PerformanceHints/WarmUp/Subscriber/warmUp.php index f8ba9d85ab..436dce0f71 100644 --- a/tests/Integration/inc/Engine/Common/PerformanceHints/WarmUp/Subscriber/warmUp.php +++ b/tests/Integration/inc/Engine/Common/PerformanceHints/WarmUp/Subscriber/warmUp.php @@ -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']); } } diff --git a/tests/Integration/inc/Engine/Common/PerformanceHints/WarmUp/Subscriber/warmUpHome.php b/tests/Integration/inc/Engine/Common/PerformanceHints/WarmUp/Subscriber/warmUpHome.php new file mode 100644 index 0000000000..df9703bc1c --- /dev/null +++ b/tests/Integration/inc/Engine/Common/PerformanceHints/WarmUp/Subscriber/warmUpHome.php @@ -0,0 +1,57 @@ +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']); + } +} diff --git a/tests/Unit/inc/Engine/Common/PerformanceHints/WarmUp/Controller/fetchLinks.php b/tests/Unit/inc/Engine/Common/PerformanceHints/WarmUp/Controller/fetchLinks.php index b8786206b6..b4817a5a01 100644 --- a/tests/Unit/inc/Engine/Common/PerformanceHints/WarmUp/Controller/fetchLinks.php +++ b/tests/Unit/inc/Engine/Common/PerformanceHints/WarmUp/Controller/fetchLinks.php @@ -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 = '' ) {