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

Fix logic inversion in od_can_optimize_response() #1659

Merged
merged 1 commit into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugins/optimization-detective/optimization.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ function od_can_optimize_response(): bool {
current_user_can( 'customize' ) ||
// Page caching plugins can only reliably be told to invalidate a cached page when a post is available to trigger
// the relevant actions on.
null !== od_get_cache_purge_post_id()
null === od_get_cache_purge_post_id()
);

/**
Expand Down
31 changes: 24 additions & 7 deletions plugins/optimization-detective/tests/test-optimization.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,27 +185,35 @@ public function test_od_maybe_add_template_output_buffer_filter_with_query_var_t
*/
public function data_provider_test_od_can_optimize_response(): array {
return array(
'homepage' => array(
'home_as_anonymous' => array(
'set_up' => function (): void {
$this->go_to( home_url( '/' ) );
},
'expected' => true,
),
'homepage_filtered' => array(
'home_filtered_as_anonymous' => array(
'set_up' => function (): void {
$this->go_to( home_url( '/' ) );
add_filter( 'od_can_optimize_response', '__return_false' );
},
'expected' => false,
),
'search' => array(
'singular_as_anonymous' => array(
'set_up' => function (): void {
$posts = get_posts();
$this->assertInstanceOf( WP_Post::class, $posts[0] );
$this->go_to( get_permalink( $posts[0] ) );
},
'expected' => true,
),
'search_as_anonymous' => array(
'set_up' => function (): void {
self::factory()->post->create( array( 'post_title' => 'Hello' ) );
$this->go_to( home_url( '?s=Hello' ) );
},
'expected' => false,
),
'customizer_preview' => array(
'home_customizer_preview_as_anonymous' => array(
'set_up' => function (): void {
$this->go_to( home_url( '/' ) );
global $wp_customize;
Expand All @@ -215,21 +223,28 @@ public function data_provider_test_od_can_optimize_response(): array {
},
'expected' => false,
),
'post_request' => array(
'home_post_request_as_anonymous' => array(
'set_up' => function (): void {
$this->go_to( home_url( '/' ) );
$_SERVER['REQUEST_METHOD'] = 'POST';
},
'expected' => false,
),
'subscriber_user' => array(
'home_as_subscriber' => array(
'set_up' => function (): void {
wp_set_current_user( self::factory()->user->create( array( 'role' => 'subscriber' ) ) );
$this->go_to( home_url( '/' ) );
},
'expected' => true,
),
'admin_user' => array(
'empty_author_page_as_anonymous' => array(
'set_up' => function (): void {
$user_id = self::factory()->user->create( array( 'role' => 'author' ) );
$this->go_to( get_author_posts_url( $user_id ) );
},
'expected' => false,
),
'home_as_admin' => array(
'set_up' => function (): void {
wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
$this->go_to( home_url( '/' ) );
Expand All @@ -243,10 +258,12 @@ public function data_provider_test_od_can_optimize_response(): array {
* Test od_can_optimize_response().
*
* @covers ::od_can_optimize_response
* @covers ::od_get_cache_purge_post_id
*
* @dataProvider data_provider_test_od_can_optimize_response
*/
public function test_od_can_optimize_response( Closure $set_up, bool $expected ): void {
self::factory()->post->create(); // Make sure there is at least one post in the DB.
$set_up();
$this->assertSame( $expected, od_can_optimize_response() );
}
Expand Down