From 3d25cc6618f1ac1b3d479adc0b8aa3807191ba0f Mon Sep 17 00:00:00 2001 From: Mathieu Lamiot Date: Tue, 26 Mar 2024 19:26:39 +0100 Subject: [PATCH 1/3] Move rocket_after_clear_atf firing in delete_rows to make sure it fires for all truncate Rename truncate method to something a bit more explicit --- inc/Engine/Media/AboveTheFold/Admin/Controller.php | 12 +++++++----- inc/Engine/Media/AboveTheFold/Admin/Subscriber.php | 6 +++--- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/inc/Engine/Media/AboveTheFold/Admin/Controller.php b/inc/Engine/Media/AboveTheFold/Admin/Controller.php index 705a56b1a1..93b8f4c8ca 100644 --- a/inc/Engine/Media/AboveTheFold/Admin/Controller.php +++ b/inc/Engine/Media/AboveTheFold/Admin/Controller.php @@ -54,10 +54,6 @@ public function truncate_atf() { $this->delete_rows(); - /** - * Fires after clearing lcp & atf data. - */ - do_action( 'rocket_after_clear_atf' ); } /** @@ -72,6 +68,12 @@ private function delete_rows() { } $this->table->truncate_atf_table(); + + /** + * Fires after clearing lcp & atf data. + */ + do_action( 'rocket_after_clear_atf' ); + } /** @@ -123,7 +125,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; } diff --git a/inc/Engine/Media/AboveTheFold/Admin/Subscriber.php b/inc/Engine/Media/AboveTheFold/Admin/Subscriber.php index 6f2906956b..bc1ca32a9e 100644 --- a/inc/Engine/Media/AboveTheFold/Admin/Subscriber.php +++ b/inc/Engine/Media/AboveTheFold/Admin/Subscriber.php @@ -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', ]; } @@ -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 ); } /** From 8820380e94a492d62a566d477c41d10c969b3177 Mon Sep 17 00:00:00 2001 From: Mathieu Lamiot Date: Tue, 9 Apr 2024 12:02:22 +0200 Subject: [PATCH 2/3] Fix linter --- inc/Engine/Media/AboveTheFold/Admin/Controller.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/inc/Engine/Media/AboveTheFold/Admin/Controller.php b/inc/Engine/Media/AboveTheFold/Admin/Controller.php index 93b8f4c8ca..a1ebd78e26 100644 --- a/inc/Engine/Media/AboveTheFold/Admin/Controller.php +++ b/inc/Engine/Media/AboveTheFold/Admin/Controller.php @@ -53,7 +53,6 @@ public function truncate_atf() { } $this->delete_rows(); - } /** @@ -73,7 +72,6 @@ private function delete_rows() { * Fires after clearing lcp & atf data. */ do_action( 'rocket_after_clear_atf' ); - } /** From 9a13da75e2020a11b768d9f82b984227ced904e5 Mon Sep 17 00:00:00 2001 From: Gael Robin Date: Fri, 12 Apr 2024 15:07:54 +0200 Subject: [PATCH 3/3] Add integration tests --- .../Admin/Controller/truncateAtfAdmin.php | 76 +++++++++++++++++++ .../Admin/Controller/truncateAtfAdmin.php | 48 ++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 tests/Fixtures/inc/Engine/Media/AboveTheFold/Admin/Controller/truncateAtfAdmin.php create mode 100644 tests/Integration/inc/Engine/Media/AboveTheFold/Admin/Controller/truncateAtfAdmin.php diff --git a/tests/Fixtures/inc/Engine/Media/AboveTheFold/Admin/Controller/truncateAtfAdmin.php b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Admin/Controller/truncateAtfAdmin.php new file mode 100644 index 0000000000..87fd271c9b --- /dev/null +++ b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Admin/Controller/truncateAtfAdmin.php @@ -0,0 +1,76 @@ + [ + '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 + ], +]; diff --git a/tests/Integration/inc/Engine/Media/AboveTheFold/Admin/Controller/truncateAtfAdmin.php b/tests/Integration/inc/Engine/Media/AboveTheFold/Admin/Controller/truncateAtfAdmin.php new file mode 100644 index 0000000000..7efd9f008b --- /dev/null +++ b/tests/Integration/inc/Engine/Media/AboveTheFold/Admin/Controller/truncateAtfAdmin.php @@ -0,0 +1,48 @@ +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' ) ); + } + + } +}