From 27701fdf8d98f3c69067b41d49d8db986aa6855a Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Thu, 21 Aug 2025 16:08:21 -0300 Subject: [PATCH 1/2] Security/EscapeOutput: add tests for namespaced names --- .../Tests/Security/EscapeOutputUnitTest.1.inc | 69 ++++++++++++++++++- .../Tests/Security/EscapeOutputUnitTest.php | 29 +++++++- 2 files changed, 95 insertions(+), 3 deletions(-) diff --git a/WordPress/Tests/Security/EscapeOutputUnitTest.1.inc b/WordPress/Tests/Security/EscapeOutputUnitTest.1.inc index 52c3a59976..0f20839189 100644 --- a/WordPress/Tests/Security/EscapeOutputUnitTest.1.inc +++ b/WordPress/Tests/Security/EscapeOutputUnitTest.1.inc @@ -258,7 +258,7 @@ echo esc_html_x( $some_nasty_var, 'context' ); // Ok. 1, 19 => 1, @@ -160,10 +163,34 @@ public function getErrorList( $testFile = '' ) { 655 => 1, 657 => 1, 663 => 1, - 664 => 1, + // PHPCS 3.13.3 changed the tokenization of FQN exit/die it impacts directly how this test case + // behaves (see https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/1201). + 664 => version_compare( $phpcs_version, '3.13.3', '>=' ) ? 1 : 0, 672 => 1, 673 => 1, 678 => 1, + 694 => 1, + 700 => 1, + 701 => 1, + 702 => 1, + 703 => 1, + 709 => 1, + 710 => 1, + 711 => 1, + 712 => 1, + 717 => 1, + 726 => 1, + 728 => 1, + 729 => 1, + 730 => 1, + 731 => 1, + 737 => 1, + 738 => 1, + 739 => 1, + 740 => 1, + 741 => 1, + 747 => 1, + 751 => 1, ); case 'EscapeOutputUnitTest.6.inc': From 322829bf6471d4113cd56eb4abe2a381847db224 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Fri, 14 Nov 2025 11:30:00 -0300 Subject: [PATCH 2/2] Security/EscapeOutput: add edge case tests for basename( __FILE__ ) pattern Add tests to ensure the `basename( __FILE__ )` pattern recognition in `_deprecated_file()` only applies to global `basename()` function calls, not to other constructs that might look similar. --- .../Tests/Security/EscapeOutputUnitTest.1.inc | 14 ++++++++++++++ WordPress/Tests/Security/EscapeOutputUnitTest.php | 9 +++++++++ 2 files changed, 23 insertions(+) diff --git a/WordPress/Tests/Security/EscapeOutputUnitTest.1.inc b/WordPress/Tests/Security/EscapeOutputUnitTest.1.inc index 0f20839189..afb3e51700 100644 --- a/WordPress/Tests/Security/EscapeOutputUnitTest.1.inc +++ b/WordPress/Tests/Security/EscapeOutputUnitTest.1.inc @@ -752,3 +752,17 @@ namespace\Sub\user_error( $message ); // Ok. namespace\_deprecated_file( basename( __FILE__ ), '1.3.0' ); // Ok. namespace\_DEPRECATED_FILE( $file, '1.3.0' ); // Ok. The sniff should start flagging this once it can resolve relative namespaces. namespace\Sub\_deprecated_file( $file, '1.3.0' ); // Ok. + +/* + * Safeguard that the basename( __FILE__ ) pattern recognition in _deprecated_file() only applies to + * the global basename() function and not to other constructs. + */ +_deprecated_file( $obj->basename( __FILE__ ), '1.3.0' ); // Bad. +_deprecated_file( $obj?->basename( __FILE__ ), '1.3.0' ); // Bad. +_deprecated_file( MyClass::basename( __FILE__ ), '1.3.0' ); // Bad. +_deprecated_file( BASENAME, __FILE__ ); // Bad. +_deprecated_file( MyNamespace\basename( __FILE__ ), '1.3.0' ); // Bad. +_deprecated_file( \MyNamespace\basename( __FILE__ ), '1.3.0' ); // Bad. +_deprecated_file( namespace\basename( __FILE__ ), '1.3.0' ); // Bad. We might want to update the regex so that the sniff stop flagging this once it can resolve relative namespaces. +_deprecated_file( namespace\Sub\basename( __FILE__ ), '1.3.0' ); // Bad. +_deprecated_file( basename(...), '1.3.0' ); // Bad. diff --git a/WordPress/Tests/Security/EscapeOutputUnitTest.php b/WordPress/Tests/Security/EscapeOutputUnitTest.php index fc5200ccd8..39d0c46a5a 100644 --- a/WordPress/Tests/Security/EscapeOutputUnitTest.php +++ b/WordPress/Tests/Security/EscapeOutputUnitTest.php @@ -191,6 +191,15 @@ public function getErrorList( $testFile = '' ) { 741 => 1, 747 => 1, 751 => 1, + 760 => 1, + 761 => 1, + 762 => 1, + 763 => 1, + 764 => 1, + 765 => 1, + 766 => 1, + 767 => 1, + 768 => 1, ); case 'EscapeOutputUnitTest.6.inc':