Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
a1fd89c
Implement logic to measure specific hook execution time with Server-T…
Jul 18, 2023
1c21d08
Merge branch 'trunk' into add/server-timing-hook-ui
Jul 21, 2023
ec9708b
Fix newly exposed lint violations.
Jul 21, 2023
6a1205f
Use more appropriate workaround when PHP_INT_MIN is not defined.
Jul 21, 2023
08d79c3
Merge branch 'trunk' into add/server-timing-hook-ui
Aug 3, 2023
694a6d1
Reuse logic to measure actions and filters, and provide inline docume…
Aug 3, 2023
6b5d12a
Clarify how the hooks are measured in WP Admin UI.
Aug 3, 2023
61f5ccc
Ensure that no duplicate hooks can be provided.
Aug 3, 2023
585911d
Add tests for server timing setting registration.
Aug 3, 2023
6db1e85
Add tests for server timing admin UI.
Aug 3, 2023
a8d141f
Remove XHTML syntax from WP Admin forms.
Aug 7, 2023
400c0a1
Modify hook measuring logic to use all hook.
Aug 7, 2023
c5fe384
Replace sanitize_key for hook name sanitization with custom regex to …
Aug 7, 2023
447f078
Have hook callbacks self-remove to only measure first occurrence.
Aug 7, 2023
c48d206
Remove novalidate attribute.
Aug 7, 2023
b85959e
Loosen hook name sanitization to rely on sanitize_text_field(), while…
Aug 7, 2023
124a7a6
Update test to cover whitespace in hookname.
Aug 7, 2023
7f99021
Use allowlist for Server-Timing option keys.
Aug 7, 2023
05d849e
Fix tests.
Aug 7, 2023
fd41db1
Add note on hook limitation unless output buffering is enabled.
Aug 7, 2023
f484a7d
Use placeholders for non-translatable strings.
Aug 8, 2023
7c94b9e
Add missing early return on top of procedural file.
Aug 8, 2023
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
Prev Previous commit
Next Next commit
Add tests for server timing admin UI.
  • Loading branch information
Felix Arntz
Felix Arntz committed Aug 3, 2023
commit 6db1e85c4e7fc2af233ab4871b9126af8e2fb31f
3 changes: 3 additions & 0 deletions tests/admin/load-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
* @package performance-lab
*/

/**
* @group admin
*/
class Admin_Load_Tests extends WP_UnitTestCase {

private static $demo_modules = array(
Expand Down
109 changes: 109 additions & 0 deletions tests/admin/server-timing-tests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php
/**
* Tests for admin/server-timing.php
*
* @package performance-lab
*/

/**
* @group admin
* @group server-timing
*/
class Admin_Server_Timing_Tests extends WP_UnitTestCase {

public function test_perflab_add_server_timing_page() {
global $_wp_submenu_nopriv;

// Reset relevant globals and filters.
$_wp_submenu_nopriv = array();
remove_all_filters( 'plugin_action_links_' . plugin_basename( PERFLAB_MAIN_FILE ) );

// Rely on current user to be an administrator (with 'manage_options' capability).
$user_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
wp_set_current_user( $user_id );
$hook_suffix = perflab_add_server_timing_page();
$this->assertSame( get_plugin_page_hookname( PERFLAB_SERVER_TIMING_SCREEN, 'tools.php' ), $hook_suffix );
$this->assertFalse( isset( $_wp_submenu_nopriv['tools.php'][ PERFLAB_SERVER_TIMING_SCREEN ] ) );
}

public function test_perflab_add_server_timing_page_missing_caps() {
global $_wp_submenu_nopriv;

// Reset relevant globals and filters.
$_wp_submenu_nopriv = array();
remove_all_filters( 'plugin_action_links_' . plugin_basename( PERFLAB_MAIN_FILE ) );

// The default user does not have the 'manage_options' capability.
$hook_suffix = perflab_add_server_timing_page();
$this->assertFalse( $hook_suffix );
$this->assertTrue( isset( $_wp_submenu_nopriv['tools.php'][ PERFLAB_SERVER_TIMING_SCREEN ] ) );
}

public function test_perflab_load_server_timing_page() {
global $wp_settings_sections, $wp_settings_fields;

// Reset relevant globals.
$wp_settings_sections = array();
$wp_settings_fields = array();

perflab_load_server_timing_page();
$this->assertArrayHasKey( PERFLAB_SERVER_TIMING_SCREEN, $wp_settings_sections );
$this->assertEqualSets(
array( 'benchmarking' ),
array_keys( $wp_settings_sections[ PERFLAB_SERVER_TIMING_SCREEN ] )
);
$this->assertEqualSets(
array( 'benchmarking' ),
array_keys( $wp_settings_fields[ PERFLAB_SERVER_TIMING_SCREEN ] )
);
$this->assertEqualSets(
array( 'benchmarking_actions', 'benchmarking_filters' ),
array_keys( $wp_settings_fields[ PERFLAB_SERVER_TIMING_SCREEN ]['benchmarking'] )
);
}

public function test_perflab_render_server_timing_page() {
ob_start();
perflab_render_server_timing_page();
$output = ob_get_clean();

$this->assertStringContainsString( '<div class="wrap">', $output );
$this->assertStringContainsString( "<input type='hidden' name='option_page' value='" . PERFLAB_SERVER_TIMING_SCREEN . "' />", $output );
}

public function test_perflab_render_server_timing_page_field() {
$slug = 'benchmarking_actions';

ob_start();
perflab_render_server_timing_page_field( $slug );
$output = ob_get_clean();

$this->assertStringContainsString( '<textarea', $output );
$this->assertStringContainsString( 'id="server_timing_' . $slug . '"', $output );
$this->assertStringContainsString( 'name="' . PERFLAB_SERVER_TIMING_SETTING . '[' . $slug . ']"', $output );
}

public function test_perflab_render_server_timing_page_field_empty_option() {
delete_option( PERFLAB_SERVER_TIMING_SETTING );

ob_start();
perflab_render_server_timing_page_field( 'benchmarking_actions' );
$output = ob_get_clean();

$this->assertStringContainsString( '></textarea>', $output );
}

public function test_perflab_render_server_timing_page_field_populated_option() {
update_option(
PERFLAB_SERVER_TIMING_SETTING,
array( 'benchmarking_actions' => array( 'init', 'wp_loaded' ) )
);

ob_start();
perflab_render_server_timing_page_field( 'benchmarking_actions' );
$output = ob_get_clean();

// Array is formatted/imploded as strings, one per line.
$this->assertStringContainsString( ">init\nwp_loaded</textarea>", $output );
}
}
1 change: 1 addition & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
'plugins_loaded',
static function() {
require_once TESTS_PLUGIN_DIR . '/admin/load.php';
require_once TESTS_PLUGIN_DIR . '/admin/server-timing.php';
$module_files = glob( TESTS_PLUGIN_DIR . '/modules/*/*/load.php' );
if ( $module_files ) {
foreach ( $module_files as $module_file ) {
Expand Down