Skip to content

Commit

Permalink
Merge pull request #1393 from WordPress/fix/admin-pointer-typing
Browse files Browse the repository at this point in the history
Allow null to be passed into perflab_admin_pointer()
  • Loading branch information
westonruter authored Jul 24, 2024
2 parents 1a4eb75 + 0a8b21f commit 6e6be6b
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 2 deletions.
5 changes: 3 additions & 2 deletions plugins/performance-lab/includes/admin/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
112 changes: 112 additions & 0 deletions plugins/performance-lab/tests/includes/admin/test-load.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -62,6 +86,94 @@ public function test_perflab_render_settings_page(): void {
$this->assertStringNotContainsString( "<input type='hidden' name='option_page' value='" . PERFLAB_SCREEN . "' />", $output );
}

/**
* @return array<string, array{ hook_suffix: string|null, expected: bool }>
*/
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_first_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_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' );
},
'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
*/
Expand Down

0 comments on commit 6e6be6b

Please sign in to comment.