|
| 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 | +} |
0 commit comments