From 11a902fcd4a76d303c7b13f38007cf316b9220cf Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Wed, 24 Jul 2024 09:59:17 -0700 Subject: [PATCH 1/3] Allow null to be passed into perflab_admin_pointer() --- plugins/performance-lab/includes/admin/load.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/plugins/performance-lab/includes/admin/load.php b/plugins/performance-lab/includes/admin/load.php index 0fd21a0f3d..819c116e28 100644 --- a/plugins/performance-lab/includes/admin/load.php +++ b/plugins/performance-lab/includes/admin/load.php @@ -76,9 +76,10 @@ function perflab_render_settings_page(): void { * * @since 1.0.0 * - * @param string $hook_suffix The current admin page. + * @param string|null $hook_suffix The current admin page. Note this can be null because `iframe_header()` does not + * ensure that `$hook_suffix` is a string when it calls `do_action( 'admin_enqueue_scripts', $hook_suffix )`. */ -function perflab_admin_pointer( string $hook_suffix ): void { +function perflab_admin_pointer( ?string $hook_suffix = '' ): void { // Do not show admin pointer in multisite Network admin or User admin UI. if ( is_network_admin() || is_user_admin() ) { return; From 2742bbd86aa3f7f4c519820db8f15a1538acff51 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Wed, 24 Jul 2024 12:59:16 -0700 Subject: [PATCH 2/3] Add tests for perflab_admin_pointer --- .../tests/includes/admin/test-load.php | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) diff --git a/plugins/performance-lab/tests/includes/admin/test-load.php b/plugins/performance-lab/tests/includes/admin/test-load.php index 6b119b1414..080620cf46 100644 --- a/plugins/performance-lab/tests/includes/admin/test-load.php +++ b/plugins/performance-lab/tests/includes/admin/test-load.php @@ -10,6 +10,30 @@ */ class Test_Admin_Load extends WP_UnitTestCase { + /** + * Runs the routine before each test is executed. + */ + public function set_up(): void { + parent::set_up(); + $this->reset_wp_dependencies(); + } + + /** + * After a test method runs, resets any state in WordPress the test method might have changed. + */ + public function tear_down(): void { + parent::tear_down(); + $this->reset_wp_dependencies(); + } + + /** + * Reset WP_Scripts and WP_Styles. + */ + private function reset_wp_dependencies(): void { + $GLOBALS['wp_scripts'] = null; + $GLOBALS['wp_styles'] = null; + } + /** * @covers ::perflab_add_features_page */ @@ -62,6 +86,94 @@ public function test_perflab_render_settings_page(): void { $this->assertStringNotContainsString( "", $output ); } + /** + * @return array + */ + public function data_provider_test_perflab_admin_pointer(): array { + return array( + 'null' => array( + 'set_up' => null, + 'hook_suffix' => null, + 'expected' => false, + 'assert' => null, + 'dismissed_wp_pointers' => '', + ), + 'edit.php' => array( + 'set_up' => null, + 'hook_suffix' => 'edit.php', + 'expected' => false, + 'assert' => null, + 'dismissed_wp_pointers' => '', + ), + 'dashboard_not_dismissed' => array( + 'set_up' => null, + 'hook_suffix' => 'index.php', + 'expected' => true, + 'assert' => null, + 'dismissed_wp_pointers' => '', + ), + 'plugins_not_dismissed' => array( + 'set_up' => null, + 'hook_suffix' => 'plugins.php', + 'expected' => true, + 'assert' => null, + 'dismissed_wp_pointers' => '', + ), + 'dashboard_yes_dismissed' => array( + 'set_up' => static function (): void { + update_user_meta( wp_get_current_user()->ID, 'dismissed_wp_pointers', 'perflab-admin-pointer' ); + }, + 'hook_suffix' => 'index.php', + 'expected' => false, + 'assert' => null, + 'dismissed_wp_pointers' => 'perflab-admin-pointer', + ), + 'perflab_screen_1st_time' => array( + 'set_up' => static function (): void { + $_GET['page'] = PERFLAB_SCREEN; + }, + 'hook_suffix' => 'options-general.php', + 'expected' => false, + 'assert' => null, + 'dismissed_wp_pointers' => 'perflab-admin-pointer', + ), + 'perflab_screen_2nd_time' => array( + 'set_up' => static function (): void { + $_GET['page'] = PERFLAB_SCREEN; + update_user_meta( wp_get_current_user()->ID, 'dismissed_wp_pointers', 'perflab-admin-pointer' ); + }, + 'hook_suffix' => 'options-general.php', + 'expected' => false, + 'assert' => null, + 'dismissed_wp_pointers' => 'perflab-admin-pointer', + ), + ); + } + + /** + * @covers ::perflab_admin_pointer + * @dataProvider data_provider_test_perflab_admin_pointer + * + * @param Closure|null $set_up Set up. + * @param string|null $hook_suffix Hook suffix. + * @param bool $expected Expected. + * @param Closure|null $assert Assert. + * @param string $dismissed_wp_pointers Dismissed admin pointers. + */ + public function test_perflab_admin_pointer( ?Closure $set_up, ?string $hook_suffix, bool $expected, ?Closure $assert, string $dismissed_wp_pointers ): void { + $user_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); + wp_set_current_user( $user_id ); + if ( $set_up instanceof Closure ) { + $set_up(); + } + $this->assertFalse( is_network_admin() || is_user_admin() ); + perflab_admin_pointer( $hook_suffix ); + $this->assertSame( $expected ? 10 : false, has_action( 'admin_print_footer_scripts', 'perflab_render_pointer' ) ); + $this->assertSame( $expected, wp_script_is( 'wp-pointer', 'enqueued' ) ); + $this->assertSame( $expected, wp_style_is( 'wp-pointer', 'enqueued' ) ); + $this->assertSame( $dismissed_wp_pointers, get_user_meta( $user_id, 'dismissed_wp_pointers', true ) ); + } + /** * @covers ::perflab_plugin_action_links_add_settings */ From 0a8b21f8674ddea1f1064e867706980a284f2878 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Wed, 24 Jul 2024 13:11:45 -0700 Subject: [PATCH 3/3] Appease spellcheck --- .../tests/includes/admin/test-load.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/plugins/performance-lab/tests/includes/admin/test-load.php b/plugins/performance-lab/tests/includes/admin/test-load.php index 080620cf46..b299351164 100644 --- a/plugins/performance-lab/tests/includes/admin/test-load.php +++ b/plugins/performance-lab/tests/includes/admin/test-load.php @@ -91,35 +91,35 @@ public function test_perflab_render_settings_page(): void { */ public function data_provider_test_perflab_admin_pointer(): array { return array( - 'null' => array( + 'null' => array( 'set_up' => null, 'hook_suffix' => null, 'expected' => false, 'assert' => null, 'dismissed_wp_pointers' => '', ), - 'edit.php' => array( + 'edit.php' => array( 'set_up' => null, 'hook_suffix' => 'edit.php', 'expected' => false, 'assert' => null, 'dismissed_wp_pointers' => '', ), - 'dashboard_not_dismissed' => array( + 'dashboard_not_dismissed' => array( 'set_up' => null, 'hook_suffix' => 'index.php', 'expected' => true, 'assert' => null, 'dismissed_wp_pointers' => '', ), - 'plugins_not_dismissed' => array( + 'plugins_not_dismissed' => array( 'set_up' => null, 'hook_suffix' => 'plugins.php', 'expected' => true, 'assert' => null, 'dismissed_wp_pointers' => '', ), - 'dashboard_yes_dismissed' => array( + 'dashboard_yes_dismissed' => array( 'set_up' => static function (): void { update_user_meta( wp_get_current_user()->ID, 'dismissed_wp_pointers', 'perflab-admin-pointer' ); }, @@ -128,7 +128,7 @@ public function data_provider_test_perflab_admin_pointer(): array { 'assert' => null, 'dismissed_wp_pointers' => 'perflab-admin-pointer', ), - 'perflab_screen_1st_time' => array( + 'perflab_screen_first_time' => array( 'set_up' => static function (): void { $_GET['page'] = PERFLAB_SCREEN; }, @@ -137,7 +137,7 @@ public function data_provider_test_perflab_admin_pointer(): array { 'assert' => null, 'dismissed_wp_pointers' => 'perflab-admin-pointer', ), - 'perflab_screen_2nd_time' => array( + 'perflab_screen_second_time' => array( 'set_up' => static function (): void { $_GET['page'] = PERFLAB_SCREEN; update_user_meta( wp_get_current_user()->ID, 'dismissed_wp_pointers', 'perflab-admin-pointer' );