diff --git a/WordPress/Tests/Helpers/ContextHelper/IsInIssetOrEmptyUnitTest.inc b/WordPress/Tests/Helpers/ContextHelper/IsInIssetOrEmptyUnitTest.inc new file mode 100644 index 0000000000..b3567121f8 --- /dev/null +++ b/WordPress/Tests/Helpers/ContextHelper/IsInIssetOrEmptyUnitTest.inc @@ -0,0 +1,34 @@ +array_key_exists( 'key', /* testObjectMethod */ $array ); +$obj?->key_exists( 'key', /* testNullsafeObjectMethod */ $array ); +MyClass::array_key_exists( 'key', /* testStaticMethod */ $array ); +key_exists( 'key', my_function( /* testNestedNonTargetFunctionCall */ $array ) ); +$obj->isset( /* testIssetObjectMethod */ $value ); +Foo::empty( /* testEmptyStaticMethod */ $value ); +MyNamespace\isset( /* testIssetNamespacedFunction */ $value ); + +/* + * The below should be recognized as being inside an isset/empty check. + */ + +isset( /* testIsset */ $value ); +empty( /* testEmpty */ $value ); +array_key_exists( 'key', /* testUnqualifiedFunction */ $array ); +Key_Exists( 'key', /* testMixedCaseFunction */ $array ); +\array_key_exists( 'key', /* testFullyQualifiedFunction */ $array ); +\KEY_EXISTS( 'key', /* testFullyQualifiedUpperCaseFunction */ $array ); +array_key_exists( array: /* testNamedParamReversedOrder */ $array, key: 'foo' ); +array_key_exists( 'key', \key_exists( 'key', /* testNestedValidFunctionCall */ $array ) ); diff --git a/WordPress/Tests/Helpers/ContextHelper/IsInIssetOrEmptyUnitTest.php b/WordPress/Tests/Helpers/ContextHelper/IsInIssetOrEmptyUnitTest.php new file mode 100644 index 0000000000..83bb739adb --- /dev/null +++ b/WordPress/Tests/Helpers/ContextHelper/IsInIssetOrEmptyUnitTest.php @@ -0,0 +1,147 @@ +getTargetToken( $testMarker, \T_VARIABLE ); + $result = ContextHelper::is_in_isset_or_empty( self::$phpcsFile, $stackPtr ); + + $this->assertSame( $expectedResult, $result ); + } + + /** + * Data provider. + * + * @see testIsInIssetOrEmpty() + * + * @return array> + */ + public static function dataIsInIssetOrEmpty() { + return array( + // Cases that should return false. + 'bare_variable' => array( + 'testMarker' => '/* testBareVariable */', + 'expectedResult' => false, + ), + 'other_function_call' => array( + 'testMarker' => '/* testOtherFunctionCall */', + 'expectedResult' => false, + ), + 'missing_array_param' => array( + 'testMarker' => '/* testMissingArrayParam */', + 'expectedResult' => false, + ), + 'key_param_not_array_param' => array( + 'testMarker' => '/* testKeyParamNotArrayParam */', + '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, + ), + 'nested_non_target_function_call' => array( + 'testMarker' => '/* testNestedNonTargetFunctionCall */', + 'expectedResult' => false, + ), + 'isset_object_method' => array( + 'testMarker' => '/* testIssetObjectMethod */', + 'expectedResult' => false, + ), + 'empty_static_method' => array( + 'testMarker' => '/* testEmptyStaticMethod */', + 'expectedResult' => false, + ), + 'isset_namespaced_function' => array( + 'testMarker' => '/* testIssetNamespacedFunction */', + 'expectedResult' => false, + ), + + // Cases that should return true. + 'isset' => array( + 'testMarker' => '/* testIsset */', + 'expectedResult' => true, + ), + 'empty' => array( + 'testMarker' => '/* testEmpty */', + 'expectedResult' => true, + ), + 'unqualified_function' => array( + 'testMarker' => '/* testUnqualifiedFunction */', + '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, + ), + 'named_param_reversed_order' => array( + 'testMarker' => '/* testNamedParamReversedOrder */', + 'expectedResult' => true, + ), + 'nested_valid_function_call' => array( + 'testMarker' => '/* testNestedValidFunctionCall */', + 'expectedResult' => true, + ), + ); + } +} diff --git a/WordPress/Tests/Security/NonceVerificationUnitTest.php b/WordPress/Tests/Security/NonceVerificationUnitTest.php index 7920859d6d..5f009f92e0 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_isset_or_empty * @covers \WordPressCS\WordPress\Helpers\ContextHelper::is_in_array_comparison * @covers \WordPressCS\WordPress\Sniffs\Security\NonceVerificationSniff */