From cf2a41f1f58d30ce786ca12ded8fe367c83ed202 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Thu, 4 Dec 2025 16:10:38 -0300 Subject: [PATCH 1/3] WPHookHelper::get_hook_name_param(): add basic tests --- .../WPHookHelper/GetHookNameParamUnitTest.php | 86 +++++++++++++++++++ .../ValidHookNameUnitTest.php | 2 +- 2 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 WordPress/Tests/Helpers/WPHookHelper/GetHookNameParamUnitTest.php diff --git a/WordPress/Tests/Helpers/WPHookHelper/GetHookNameParamUnitTest.php b/WordPress/Tests/Helpers/WPHookHelper/GetHookNameParamUnitTest.php new file mode 100644 index 0000000000..335fad88d4 --- /dev/null +++ b/WordPress/Tests/Helpers/WPHookHelper/GetHookNameParamUnitTest.php @@ -0,0 +1,86 @@ + $parameters The parameters array to pass to the method. + * @param array|false $expectedResult The expected return value. + * + * @return void + */ + public function testGetHookNameParam( $functionName, $parameters, $expectedResult ) { + $this->assertSame( + $expectedResult, + WPHookHelper::get_hook_name_param( $functionName, $parameters ) + ); + } + + /** + * Data provider. + * + * @see testGetHookNameParam() + * + * @return array|false|string>> + */ + public static function dataGetHookNameParam() { + // Note: The parameter arrays use a simplified format instead of the real PassedParameters::getParameters() + // output as most of the array entries are not relevant for the method under test. + return array( + 'not_a_hook_function' => array( + 'functionName' => 'printf', + 'parameters' => array( 1 => array( 'my_action' ) ), + 'expectedResult' => false, + ), + 'hook_name_param_missing' => array( + 'functionName' => 'apply_filters_ref_array', + 'parameters' => array( 2 => array( '$arg' ) ), + 'expectedResult' => false, + ), + 'lowercase_name' => array( + 'functionName' => 'do_action', + 'parameters' => array( 1 => array( 'my_action' ) ), + 'expectedResult' => array( 'my_action' ), + ), + 'mixedcase_name' => array( + 'functionName' => 'aPpLy_FiLtErS', + 'parameters' => array( + 1 => array( 'my_filter' ), + 2 => array( '$value' ), + ), + 'expectedResult' => array( 'my_filter' ), + ), + 'named_parameter' => array( + 'functionName' => 'do_action_deprecated', + 'parameters' => array( + 'args' => array( '$args' ), + 'hook_name' => array( 'my_action' ), + ), + 'expectedResult' => array( 'my_action' ), + ), + ); + } +} diff --git a/WordPress/Tests/NamingConventions/ValidHookNameUnitTest.php b/WordPress/Tests/NamingConventions/ValidHookNameUnitTest.php index 98649fb620..763760f1c3 100644 --- a/WordPress/Tests/NamingConventions/ValidHookNameUnitTest.php +++ b/WordPress/Tests/NamingConventions/ValidHookNameUnitTest.php @@ -17,7 +17,7 @@ * @since 0.10.0 * @since 0.13.0 Class name changed: this class is now namespaced. * - * @covers \WordPressCS\WordPress\Helpers\WPHookHelper + * @covers \WordPressCS\WordPress\Helpers\WPHookHelper::get_functions * @covers \WordPressCS\WordPress\Sniffs\NamingConventions\ValidHookNameSniff */ final class ValidHookNameUnitTest extends AbstractSniffUnitTest { From acd5b1036d3f2e7373be41e15749a987e4dbb243 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Mon, 29 Jun 2026 15:21:06 +0000 Subject: [PATCH 2/3] Rework tests to use a case file as suggested during PR review --- .../WPHookHelper/GetHookNameParamUnitTest.inc | 24 ++++++++ .../WPHookHelper/GetHookNameParamUnitTest.php | 61 +++++++++---------- 2 files changed, 53 insertions(+), 32 deletions(-) create mode 100644 WordPress/Tests/Helpers/WPHookHelper/GetHookNameParamUnitTest.inc diff --git a/WordPress/Tests/Helpers/WPHookHelper/GetHookNameParamUnitTest.inc b/WordPress/Tests/Helpers/WPHookHelper/GetHookNameParamUnitTest.inc new file mode 100644 index 0000000000..e1992e34b8 --- /dev/null +++ b/WordPress/Tests/Helpers/WPHookHelper/GetHookNameParamUnitTest.inc @@ -0,0 +1,24 @@ + $parameters The parameters array to pass to the method. - * @param array|false $expectedResult The expected return value. + * @param string $testMarker The comment which prefaces the target token in the test file. + * @param string|false $expectedResult The raw content of the expected hook name parameter, + * or `false` when no hook name parameter is expected. * * @return void */ - public function testGetHookNameParam( $functionName, $parameters, $expectedResult ) { - $this->assertSame( - $expectedResult, - WPHookHelper::get_hook_name_param( $functionName, $parameters ) - ); + public function testGetHookNameParam( $testMarker, $expectedResult ) { + $stackPtr = $this->getTargetToken( $testMarker, \T_STRING ); + $functionName = self::$phpcsFile->getTokens()[ $stackPtr ]['content']; + $parameters = PassedParameters::getParameters( self::$phpcsFile, $stackPtr ); + + $result = WPHookHelper::get_hook_name_param( $functionName, $parameters ); + + if ( is_array( $result ) ) { + // The details of the parameter are populated by PassedParameters::getParameters(). + // Here we only verify which parameter was selected. + $result = $result['raw']; + } + + $this->assertSame( $expectedResult, $result ); } /** @@ -44,42 +54,29 @@ public function testGetHookNameParam( $functionName, $parameters, $expectedResul * * @see testGetHookNameParam() * - * @return array|false|string>> + * @return array> */ public static function dataGetHookNameParam() { - // Note: The parameter arrays use a simplified format instead of the real PassedParameters::getParameters() - // output as most of the array entries are not relevant for the method under test. return array( 'not_a_hook_function' => array( - 'functionName' => 'printf', - 'parameters' => array( 1 => array( 'my_action' ) ), + 'testMarker' => '/* testNotAHookFunction */', 'expectedResult' => false, ), 'hook_name_param_missing' => array( - 'functionName' => 'apply_filters_ref_array', - 'parameters' => array( 2 => array( '$arg' ) ), + 'testMarker' => '/* testHookNameParamMissing */', 'expectedResult' => false, ), 'lowercase_name' => array( - 'functionName' => 'do_action', - 'parameters' => array( 1 => array( 'my_action' ) ), - 'expectedResult' => array( 'my_action' ), + 'testMarker' => '/* testLowercaseName */', + 'expectedResult' => "'my_action'", ), 'mixedcase_name' => array( - 'functionName' => 'aPpLy_FiLtErS', - 'parameters' => array( - 1 => array( 'my_filter' ), - 2 => array( '$value' ), - ), - 'expectedResult' => array( 'my_filter' ), + 'testMarker' => '/* testMixedCaseName */', + 'expectedResult' => "'my_filter'", ), 'named_parameter' => array( - 'functionName' => 'do_action_deprecated', - 'parameters' => array( - 'args' => array( '$args' ), - 'hook_name' => array( 'my_action' ), - ), - 'expectedResult' => array( 'my_action' ), + 'testMarker' => '/* testNamedParameter */', + 'expectedResult' => "'my_action'", ), ); } From 6df2dd097fd28688fefc01300c0a8dc452007570 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Fri, 3 Jul 2026 09:07:52 -0300 Subject: [PATCH 3/3] Update WordPress/Tests/Helpers/WPHookHelper/GetHookNameParamUnitTest.php Co-authored-by: Juliette <663378+jrfnl@users.noreply.github.com> --- .../Tests/Helpers/WPHookHelper/GetHookNameParamUnitTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WordPress/Tests/Helpers/WPHookHelper/GetHookNameParamUnitTest.php b/WordPress/Tests/Helpers/WPHookHelper/GetHookNameParamUnitTest.php index 64bb96dc85..e90cef82a3 100644 --- a/WordPress/Tests/Helpers/WPHookHelper/GetHookNameParamUnitTest.php +++ b/WordPress/Tests/Helpers/WPHookHelper/GetHookNameParamUnitTest.php @@ -43,7 +43,7 @@ public function testGetHookNameParam( $testMarker, $expectedResult ) { if ( is_array( $result ) ) { // The details of the parameter are populated by PassedParameters::getParameters(). // Here we only verify which parameter was selected. - $result = $result['raw']; + $result = $result['clean']; } $this->assertSame( $expectedResult, $result );