Skip to content

Commit 6db1e85

Browse files
author
Felix Arntz
committed
Add tests for server timing admin UI.
1 parent 585911d commit 6db1e85

3 files changed

Lines changed: 113 additions & 0 deletions

File tree

‎tests/admin/load-tests.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
* @package performance-lab
66
*/
77

8+
/**
9+
* @group admin
10+
*/
811
class Admin_Load_Tests extends WP_UnitTestCase {
912

1013
private static $demo_modules = array(
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
/**
3+
* Tests for admin/server-timing.php
4+
*
5+
* @package performance-lab
6+
*/
7+
8+
/**
9+
* @group admin
10+
* @group server-timing
11+
*/
12+
class Admin_Server_Timing_Tests extends WP_UnitTestCase {
13+
14+
public function test_perflab_add_server_timing_page() {
15+
global $_wp_submenu_nopriv;
16+
17+
// Reset relevant globals and filters.
18+
$_wp_submenu_nopriv = array();
19+
remove_all_filters( 'plugin_action_links_' . plugin_basename( PERFLAB_MAIN_FILE ) );
20+
21+
// Rely on current user to be an administrator (with 'manage_options' capability).
22+
$user_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
23+
wp_set_current_user( $user_id );
24+
$hook_suffix = perflab_add_server_timing_page();
25+
$this->assertSame( get_plugin_page_hookname( PERFLAB_SERVER_TIMING_SCREEN, 'tools.php' ), $hook_suffix );
26+
$this->assertFalse( isset( $_wp_submenu_nopriv['tools.php'][ PERFLAB_SERVER_TIMING_SCREEN ] ) );
27+
}
28+
29+
public function test_perflab_add_server_timing_page_missing_caps() {
30+
global $_wp_submenu_nopriv;
31+
32+
// Reset relevant globals and filters.
33+
$_wp_submenu_nopriv = array();
34+
remove_all_filters( 'plugin_action_links_' . plugin_basename( PERFLAB_MAIN_FILE ) );
35+
36+
// The default user does not have the 'manage_options' capability.
37+
$hook_suffix = perflab_add_server_timing_page();
38+
$this->assertFalse( $hook_suffix );
39+
$this->assertTrue( isset( $_wp_submenu_nopriv['tools.php'][ PERFLAB_SERVER_TIMING_SCREEN ] ) );
40+
}
41+
42+
public function test_perflab_load_server_timing_page() {
43+
global $wp_settings_sections, $wp_settings_fields;
44+
45+
// Reset relevant globals.
46+
$wp_settings_sections = array();
47+
$wp_settings_fields = array();
48+
49+
perflab_load_server_timing_page();
50+
$this->assertArrayHasKey( PERFLAB_SERVER_TIMING_SCREEN, $wp_settings_sections );
51+
$this->assertEqualSets(
52+
array( 'benchmarking' ),
53+
array_keys( $wp_settings_sections[ PERFLAB_SERVER_TIMING_SCREEN ] )
54+
);
55+
$this->assertEqualSets(
56+
array( 'benchmarking' ),
57+
array_keys( $wp_settings_fields[ PERFLAB_SERVER_TIMING_SCREEN ] )
58+
);
59+
$this->assertEqualSets(
60+
array( 'benchmarking_actions', 'benchmarking_filters' ),
61+
array_keys( $wp_settings_fields[ PERFLAB_SERVER_TIMING_SCREEN ]['benchmarking'] )
62+
);
63+
}
64+
65+
public function test_perflab_render_server_timing_page() {
66+
ob_start();
67+
perflab_render_server_timing_page();
68+
$output = ob_get_clean();
69+
70+
$this->assertStringContainsString( '<div class="wrap">', $output );
71+
$this->assertStringContainsString( "<input type='hidden' name='option_page' value='" . PERFLAB_SERVER_TIMING_SCREEN . "' />", $output );
72+
}
73+
74+
public function test_perflab_render_server_timing_page_field() {
75+
$slug = 'benchmarking_actions';
76+
77+
ob_start();
78+
perflab_render_server_timing_page_field( $slug );
79+
$output = ob_get_clean();
80+
81+
$this->assertStringContainsString( '<textarea', $output );
82+
$this->assertStringContainsString( 'id="server_timing_' . $slug . '"', $output );
83+
$this->assertStringContainsString( 'name="' . PERFLAB_SERVER_TIMING_SETTING . '[' . $slug . ']"', $output );
84+
}
85+
86+
public function test_perflab_render_server_timing_page_field_empty_option() {
87+
delete_option( PERFLAB_SERVER_TIMING_SETTING );
88+
89+
ob_start();
90+
perflab_render_server_timing_page_field( 'benchmarking_actions' );
91+
$output = ob_get_clean();
92+
93+
$this->assertStringContainsString( '></textarea>', $output );
94+
}
95+
96+
public function test_perflab_render_server_timing_page_field_populated_option() {
97+
update_option(
98+
PERFLAB_SERVER_TIMING_SETTING,
99+
array( 'benchmarking_actions' => array( 'init', 'wp_loaded' ) )
100+
);
101+
102+
ob_start();
103+
perflab_render_server_timing_page_field( 'benchmarking_actions' );
104+
$output = ob_get_clean();
105+
106+
// Array is formatted/imploded as strings, one per line.
107+
$this->assertStringContainsString( ">init\nwp_loaded</textarea>", $output );
108+
}
109+
}

‎tests/bootstrap.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
'plugins_loaded',
4444
static function() {
4545
require_once TESTS_PLUGIN_DIR . '/admin/load.php';
46+
require_once TESTS_PLUGIN_DIR . '/admin/server-timing.php';
4647
$module_files = glob( TESTS_PLUGIN_DIR . '/modules/*/*/load.php' );
4748
if ( $module_files ) {
4849
foreach ( $module_files as $module_file ) {

0 commit comments

Comments
 (0)