diff --git a/plugins/optimization-detective/tests/test-detection.php b/plugins/optimization-detective/tests/test-detection.php index 7242b456c..6756cd80b 100644 --- a/plugins/optimization-detective/tests/test-detection.php +++ b/plugins/optimization-detective/tests/test-detection.php @@ -7,6 +7,76 @@ class Test_OD_Detection extends WP_UnitTestCase { + /** + * Data provider. + * + * @return array + */ + public function data_provider_od_get_cache_purge_post_id(): array { + return array( + 'singular' => array( + 'set_up' => function () { + $post_id = self::factory()->post->create(); + $this->go_to( get_permalink( $post_id ) ); + return $post_id; + }, + 'expected_is_query_object' => true, + 'expected_query_object_class' => WP_Post::class, + ), + 'home' => array( + 'set_up' => function () { + $post_id = self::factory()->post->create(); + $this->go_to( home_url() ); + return $post_id; + }, + 'expected_is_query_object' => false, + 'expected_query_object_class' => null, + ), + 'category' => array( + 'set_up' => function () { + $cat_id = self::factory()->category->create(); + $post_id = self::factory()->post->create(); + wp_set_post_categories( $post_id, array( $cat_id ) ); + $this->go_to( get_category_link( $cat_id ) ); + return $post_id; + }, + 'expected_is_query_object' => false, + 'expected_query_object_class' => WP_Term::class, + ), + 'not_found' => array( + 'set_up' => function () { + $this->go_to( '/this-page-does-not-exist' ); + return null; + }, + 'expected_is_query_object' => false, + 'expected_query_object_class' => null, + ), + ); + } + + /** + * Tests od_get_cache_purge_post_id(). + * + * @covers ::od_get_cache_purge_post_id + * + * @dataProvider data_provider_od_get_cache_purge_post_id + */ + public function test_od_get_cache_purge_post_id( Closure $set_up, bool $expected_is_query_object, ?string $expected_query_object_class ): void { + $expected = $set_up(); + $this->assertSame( $expected, od_get_cache_purge_post_id() ); + if ( $expected_is_query_object ) { + $this->assertSame( $expected, get_queried_object_id() ); + } else { + $this->assertNotSame( $expected, get_queried_object_id() ); + } + + if ( null === $expected_query_object_class ) { + $this->assertNull( get_queried_object() ); + } else { + $this->assertSame( $expected_query_object_class, get_class( get_queried_object() ) ); + } + } + /** * Data provider. *