diff --git a/includes/Admin/class-admin.php b/includes/Admin/class-admin.php index ae4b85f..74373b2 100644 --- a/includes/Admin/class-admin.php +++ b/includes/Admin/class-admin.php @@ -52,12 +52,16 @@ class Admin { // @phpcs:disable WordPress.Security.NonceVerification.Recommended // @phpcs:disable WordPress.Security.NonceVerification.Missing - // For production, use filter_input + // For production, use filter_input. + $page = ''; if ( defined( 'PHPUNIT_RUNNING' ) && PHPUNIT_RUNNING ) { - // For testing, use $_GET directly - $page = isset( $_GET['page'] ) ? $_GET['page'] : ''; - } else { - // For production, use filter_input + // For testing, use $_GET directly. + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- We're sanitizing with wp_unslash and validating later + $page = isset( $_GET['page'] ) ? \wp_unslash( $_GET['page'] ) : ''; + } + + // Use filter_input for production environment. + if ( empty( $page ) ) { $page = filter_input( INPUT_GET, 'page', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); } diff --git a/tests/test-admin.php b/tests/test-admin.php index f0a0b85..a154d8f 100644 --- a/tests/test-admin.php +++ b/tests/test-admin.php @@ -78,6 +78,12 @@ class AdminTest extends \WP_Mock\Tools\TestCase { // Set up the superglobal for the test $_GET['page'] = 'wp_plugin_starter_template_settings'; + // Mock wp_unslash function + WP_Mock::userFunction('wp_unslash', [ + 'args' => ['wp_plugin_starter_template_settings'], + 'return' => 'wp_plugin_starter_template_settings', + ]); + // Mock WordPress functions used in the method WP_Mock::userFunction('plugin_dir_url', [ 'return' => 'http://example.com/wp-content/plugins/wp-plugin-starter-template/includes/Admin/',