diff --git a/WordPress/Tests/Helpers/ContextHelper/IsInArrayComparisonUnitTest.inc b/WordPress/Tests/Helpers/ContextHelper/IsInArrayComparisonUnitTest.inc new file mode 100644 index 0000000000..09385129a0 --- /dev/null +++ b/WordPress/Tests/Helpers/ContextHelper/IsInArrayComparisonUnitTest.inc @@ -0,0 +1,30 @@ +array_search( /* testObjectMethod */ $value, $haystack ); +$obj?->array_keys( $array, /* testNullsafeObjectMethod */ $value ); +ArrayHelper::in_array( /* testStaticMethod */ $value, $haystack ); +array_keys( /* testArrayKeysFirstParamOnly */ $array ); +array_keys( array: /* testArrayKeysWrongNamedParam */ $array, search_value: 'value', strict: true ); + +/* + * The below should be recognized as being inside an array comparison function call. + */ + +in_array( /* testInArray */ $value, $haystack ); +array_search( /* testArraySearch */ $value, $haystack ); +array_keys( $array, /* testArrayKeysWithFilterValue */ $value ); +array_keys( filter_value: /* testArrayKeysNamedParam */ $value, array: $array ); +IN_array( /* testMixedCaseFunction */ $value, $haystack ); +\array_search( /* testFullyQualifiedFunction */ $value, $haystack ); +\ARRAY_KEYS( $array, /* testFullyQualifiedUpperCaseFunction */ $value ); +in_array( my_function( /* testNestedFunctionCall */ $value ), $haystack ); diff --git a/WordPress/Tests/Helpers/ContextHelper/IsInArrayComparisonUnitTest.php b/WordPress/Tests/Helpers/ContextHelper/IsInArrayComparisonUnitTest.php new file mode 100644 index 0000000000..e92a450d14 --- /dev/null +++ b/WordPress/Tests/Helpers/ContextHelper/IsInArrayComparisonUnitTest.php @@ -0,0 +1,131 @@ +getTargetToken( $testMarker, \T_VARIABLE ); + $result = ContextHelper::is_in_array_comparison( self::$phpcsFile, $stackPtr ); + + $this->assertSame( $expectedResult, $result ); + } + + /** + * Data provider. + * + * @see testIsInArrayComparison() + * + * @return array> + */ + public static function dataIsInArrayComparison() { + return array( + // Cases that should return false. + 'bare_variable' => array( + 'testMarker' => '/* testBareVariable */', + 'expectedResult' => false, + ), + 'other_function_call' => array( + 'testMarker' => '/* testOtherFunctionCall */', + 'expectedResult' => false, + ), + 'partially_qualified_function' => array( + 'testMarker' => '/* testPartiallyQualifiedFunction */', + 'expectedResult' => false, + ), + 'fully_qualified_namespaced_function' => array( + 'testMarker' => '/* testFullyQualifiedNamespacedFunction */', + 'expectedResult' => false, + ), + 'namespace_relative_function' => array( + 'testMarker' => '/* testNamespaceRelativeFunction */', + 'expectedResult' => false, + ), + 'namespace_relative_sub_function' => array( + 'testMarker' => '/* testNamespaceRelativeSubFunction */', + 'expectedResult' => false, + ), + 'object_method' => array( + 'testMarker' => '/* testObjectMethod */', + 'expectedResult' => false, + ), + 'nullsafe_object_method' => array( + 'testMarker' => '/* testNullsafeObjectMethod */', + 'expectedResult' => false, + ), + 'static_method' => array( + 'testMarker' => '/* testStaticMethod */', + 'expectedResult' => false, + ), + 'array_keys_first_param_only' => array( + 'testMarker' => '/* testArrayKeysFirstParamOnly */', + 'expectedResult' => false, + ), + 'array_keys_wrong_named_param' => array( + 'testMarker' => '/* testArrayKeysWrongNamedParam */', + 'expectedResult' => false, + ), + + // Cases that should return true. + 'in_array' => array( + 'testMarker' => '/* testInArray */', + 'expectedResult' => true, + ), + 'array_search' => array( + 'testMarker' => '/* testArraySearch */', + 'expectedResult' => true, + ), + 'array_keys_with_filter_value' => array( + 'testMarker' => '/* testArrayKeysWithFilterValue */', + 'expectedResult' => true, + ), + 'array_keys_named_param' => array( + 'testMarker' => '/* testArrayKeysNamedParam */', + 'expectedResult' => true, + ), + 'mixed_case_function' => array( + 'testMarker' => '/* testMixedCaseFunction */', + 'expectedResult' => true, + ), + 'fully_qualified_function' => array( + 'testMarker' => '/* testFullyQualifiedFunction */', + 'expectedResult' => true, + ), + 'fully_qualified_upper_case_function' => array( + 'testMarker' => '/* testFullyQualifiedUpperCaseFunction */', + 'expectedResult' => true, + ), + 'nested_function_call' => array( + 'testMarker' => '/* testNestedFunctionCall */', + 'expectedResult' => true, + ), + ); + } +} diff --git a/WordPress/Tests/Security/NonceVerificationUnitTest.php b/WordPress/Tests/Security/NonceVerificationUnitTest.php index 5f009f92e0..081de9a00f 100644 --- a/WordPress/Tests/Security/NonceVerificationUnitTest.php +++ b/WordPress/Tests/Security/NonceVerificationUnitTest.php @@ -19,7 +19,6 @@ * @since 1.0.0 This sniff has been moved from the `CSRF` category to the `Security` category. * * @covers \WordPressCS\WordPress\Helpers\ContextHelper::is_in_type_test - * @covers \WordPressCS\WordPress\Helpers\ContextHelper::is_in_array_comparison * @covers \WordPressCS\WordPress\Sniffs\Security\NonceVerificationSniff */ final class NonceVerificationUnitTest extends AbstractSniffUnitTest {