Skip to content

Commit

Permalink
3.16 bug the warm up of lcpatf is not triggered when clicking clear c…
Browse files Browse the repository at this point in the history
…ritical images in the admin (#6517)
  • Loading branch information
Miraeld authored Apr 15, 2024
2 parents e31b05b + 9a13da7 commit 1c4062a
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 9 deletions.
12 changes: 6 additions & 6 deletions inc/Engine/Media/AboveTheFold/Admin/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,6 @@ public function truncate_atf() {
}

$this->delete_rows();

/**
* Fires after clearing lcp & atf data.
*/
do_action( 'rocket_after_clear_atf' );
}

/**
Expand All @@ -72,6 +67,11 @@ private function delete_rows() {
}

$this->table->truncate_atf_table();

/**
* Fires after clearing lcp & atf data.
*/
do_action( 'rocket_after_clear_atf' );
}

/**
Expand Down Expand Up @@ -123,7 +123,7 @@ public function delete_term_atf( $term_id ) {
*
* @return array
*/
public function truncate( $clean ) {
public function truncate_atf_admin( $clean ) {
if ( ! $this->context->is_allowed() ) {
return $clean;
}
Expand Down
6 changes: 3 additions & 3 deletions inc/Engine/Media/AboveTheFold/Admin/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static function get_subscribed_events(): array {
'wp_update_comment_count' => 'delete_post_atf',
'edit_term' => 'delete_term_atf',
'pre_delete_term' => 'delete_term_atf',
'rocket_saas_clean_all' => 'truncate',
'rocket_saas_clean_all' => 'truncate_atf_admin',
'rocket_saas_clean_url' => 'clean_url',
];
}
Expand Down Expand Up @@ -81,8 +81,8 @@ public function delete_term_atf( $term_id ) {
*
* @return array
*/
public function truncate( $clean ) {
return $this->controller->truncate( $clean );
public function truncate_atf_admin( $clean ) {
return $this->controller->truncate_atf_admin( $clean );
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

return [
'testShouldTruncateDb' => [
'config' => [
'rows' => [
[
'status' => 'completed',
'url' => 'http://example.org',
'lcp' => json_encode((object)[
'type' => 'img',
'src' => 'http://example.org/wp-content/uploads/image.jpg',
]),
'viewport' => json_encode([
0 => (object)[
'type' => 'img',
'src' => 'http://example.org/wp-content/uploads/image.jpg',
],
]),
],
[
'status' => 'completed',
'url' => 'http://example.org/page-1/',
'lcp' => json_encode((object)[
'type' => 'img',
'src' => 'http://example.org/wp-content/uploads/image.jpg',
]),
'viewport' => json_encode([
0 => (object)[
'type' => 'img',
'src' => 'http://example.org/wp-content/uploads/image.jpg',
],
])
]
],
'rocket_manage_options' => true,
],
'expected' => 0
],
'testShouldNotTruncateDb' => [
'config' => [
'rows' => [
[
'status' => 'completed',
'url' => 'http://example.org',
'lcp' => json_encode((object)[
'type' => 'img',
'src' => 'http://example.org/wp-content/uploads/image.jpg',
]),
'viewport' => json_encode([
0 => (object)[
'type' => 'img',
'src' => 'http://example.org/wp-content/uploads/image.jpg',
],
]),
],
[
'status' => 'completed',
'url' => 'http://example.org/page-1/',
'lcp' => json_encode((object)[
'type' => 'img',
'src' => 'http://example.org/wp-content/uploads/image.jpg',
]),
'viewport' => json_encode([
0 => (object)[
'type' => 'img',
'src' => 'http://example.org/wp-content/uploads/image.jpg',
],
])
]
],
'rocket_manage_options' => false,
],
'expected' => 2
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace WP_Rocket\Tests\Integration\inc\Engine\Media\AboveTheFold\Admin\Controller;

use WP_Rocket\Tests\Integration\DBTrait;
use WP_Rocket\Tests\Integration\TestCase;
use Brain\Monkey\Functions;
use Mockery;
use WP_Rocket\Engine\Media\AboveTheFold\WarmUp\Controller;

/**
* @covers \WP_Rocket\Engine\Media\AboveTheFold\Admin\Controller::truncate_atf_admin
*
* @group AboveTheFold
*/
class Test_TruncateAtfAdmin extends TestCase {
use DBTrait;

protected $path_to_test_data = '/inc/Engine/Media/AboveTheFold/Admin/Controller/truncateAtfAdmin.php';

protected $config;
public function set_up() {
parent::set_up();
}

/**
* @dataProvider configTestData
*/
public function testShouldDoAsExpected( $config, $expected ) {
$this->config = $config;
$container = apply_filters( 'rocket_container', null );
$warm_up_controller = Mockery::mock( Controller::class );
foreach ( $this->config['rows'] as $row ) {
self::addLcp( $row );
}
Functions\expect( 'current_user_can' )->once()->with('rocket_manage_options')->andReturn($config['rocket_manage_options']);
do_action( 'rocket_saas_clean_all' );

$atf_query = $container->get( 'atf_query' );
$result_atf_after_clean = $atf_query->query();

$this->assertCount( $expected, $result_atf_after_clean );
if ( ! $expected ) {
$this->assertSame( 1, did_action( 'rocket_after_clear_atf' ) );
}

}
}

0 comments on commit 1c4062a

Please sign in to comment.