Skip to content

Commit 22d4758

Browse files
authored
ContextHelper::is_in_array_comparison(): add basic tests (#2726)
* ContextHelper::is_in_array_comparison(): add basic tests * Add a bare variable test for consistency with the other similar helper method tests
1 parent 16a785e commit 22d4758

3 files changed

Lines changed: 161 additions & 1 deletion

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* The below should NOT be recognized as being inside an array comparison function call.
5+
*/
6+
7+
/* testBareVariable */ $value;
8+
some_function( /* testOtherFunctionCall */ $value );
9+
MyNamespace\in_array( /* testPartiallyQualifiedFunction */ $value, $haystack );
10+
\MyNamespace\array_search( /* testFullyQualifiedNamespacedFunction */ $value, $haystack );
11+
namespace\array_keys( $array, /* testNamespaceRelativeFunction */ $value ); // The method should start recognizing this as an array comparison function once it can resolve relative namespaces.
12+
namespace\Sub\in_array( /* testNamespaceRelativeSubFunction */ $value, $haystack );
13+
$obj->array_search( /* testObjectMethod */ $value, $haystack );
14+
$obj?->array_keys( $array, /* testNullsafeObjectMethod */ $value );
15+
ArrayHelper::in_array( /* testStaticMethod */ $value, $haystack );
16+
array_keys( /* testArrayKeysFirstParamOnly */ $array );
17+
array_keys( array: /* testArrayKeysWrongNamedParam */ $array, search_value: 'value', strict: true );
18+
19+
/*
20+
* The below should be recognized as being inside an array comparison function call.
21+
*/
22+
23+
in_array( /* testInArray */ $value, $haystack );
24+
array_search( /* testArraySearch */ $value, $haystack );
25+
array_keys( $array, /* testArrayKeysWithFilterValue */ $value );
26+
array_keys( filter_value: /* testArrayKeysNamedParam */ $value, array: $array );
27+
IN_array( /* testMixedCaseFunction */ $value, $haystack );
28+
\array_search( /* testFullyQualifiedFunction */ $value, $haystack );
29+
\ARRAY_KEYS( $array, /* testFullyQualifiedUpperCaseFunction */ $value );
30+
in_array( my_function( /* testNestedFunctionCall */ $value ), $haystack );
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
/**
3+
* WordPress Coding Standard.
4+
*
5+
* @package WPCS\WordPressCodingStandards
6+
* @link https://github.com/WordPress/WordPress-Coding-Standards
7+
* @license https://opensource.org/licenses/MIT MIT
8+
*/
9+
10+
namespace WordPressCS\WordPress\Tests\Helpers\ContextHelper;
11+
12+
use PHPCSUtils\TestUtils\UtilityMethodTestCase;
13+
use WordPressCS\WordPress\Helpers\ContextHelper;
14+
15+
/**
16+
* Tests for the `ContextHelper::is_in_array_comparison()` utility method.
17+
*
18+
* @since 3.4.0
19+
*
20+
* @covers \WordPressCS\WordPress\Helpers\ContextHelper::is_in_array_comparison
21+
*/
22+
final class IsInArrayComparisonUnitTest extends UtilityMethodTestCase {
23+
24+
/**
25+
* Test is_in_array_comparison().
26+
*
27+
* @dataProvider dataIsInArrayComparison
28+
*
29+
* @param string $testMarker The comment which prefaces the target token in the test file.
30+
* @param bool $expectedResult The expected return value.
31+
*
32+
* @return void
33+
*/
34+
public function testIsInArrayComparison( $testMarker, $expectedResult ) {
35+
$stackPtr = $this->getTargetToken( $testMarker, \T_VARIABLE );
36+
$result = ContextHelper::is_in_array_comparison( self::$phpcsFile, $stackPtr );
37+
38+
$this->assertSame( $expectedResult, $result );
39+
}
40+
41+
/**
42+
* Data provider.
43+
*
44+
* @see testIsInArrayComparison()
45+
*
46+
* @return array<string, array<string, bool|string>>
47+
*/
48+
public static function dataIsInArrayComparison() {
49+
return array(
50+
// Cases that should return false.
51+
'bare_variable' => array(
52+
'testMarker' => '/* testBareVariable */',
53+
'expectedResult' => false,
54+
),
55+
'other_function_call' => array(
56+
'testMarker' => '/* testOtherFunctionCall */',
57+
'expectedResult' => false,
58+
),
59+
'partially_qualified_function' => array(
60+
'testMarker' => '/* testPartiallyQualifiedFunction */',
61+
'expectedResult' => false,
62+
),
63+
'fully_qualified_namespaced_function' => array(
64+
'testMarker' => '/* testFullyQualifiedNamespacedFunction */',
65+
'expectedResult' => false,
66+
),
67+
'namespace_relative_function' => array(
68+
'testMarker' => '/* testNamespaceRelativeFunction */',
69+
'expectedResult' => false,
70+
),
71+
'namespace_relative_sub_function' => array(
72+
'testMarker' => '/* testNamespaceRelativeSubFunction */',
73+
'expectedResult' => false,
74+
),
75+
'object_method' => array(
76+
'testMarker' => '/* testObjectMethod */',
77+
'expectedResult' => false,
78+
),
79+
'nullsafe_object_method' => array(
80+
'testMarker' => '/* testNullsafeObjectMethod */',
81+
'expectedResult' => false,
82+
),
83+
'static_method' => array(
84+
'testMarker' => '/* testStaticMethod */',
85+
'expectedResult' => false,
86+
),
87+
'array_keys_first_param_only' => array(
88+
'testMarker' => '/* testArrayKeysFirstParamOnly */',
89+
'expectedResult' => false,
90+
),
91+
'array_keys_wrong_named_param' => array(
92+
'testMarker' => '/* testArrayKeysWrongNamedParam */',
93+
'expectedResult' => false,
94+
),
95+
96+
// Cases that should return true.
97+
'in_array' => array(
98+
'testMarker' => '/* testInArray */',
99+
'expectedResult' => true,
100+
),
101+
'array_search' => array(
102+
'testMarker' => '/* testArraySearch */',
103+
'expectedResult' => true,
104+
),
105+
'array_keys_with_filter_value' => array(
106+
'testMarker' => '/* testArrayKeysWithFilterValue */',
107+
'expectedResult' => true,
108+
),
109+
'array_keys_named_param' => array(
110+
'testMarker' => '/* testArrayKeysNamedParam */',
111+
'expectedResult' => true,
112+
),
113+
'mixed_case_function' => array(
114+
'testMarker' => '/* testMixedCaseFunction */',
115+
'expectedResult' => true,
116+
),
117+
'fully_qualified_function' => array(
118+
'testMarker' => '/* testFullyQualifiedFunction */',
119+
'expectedResult' => true,
120+
),
121+
'fully_qualified_upper_case_function' => array(
122+
'testMarker' => '/* testFullyQualifiedUpperCaseFunction */',
123+
'expectedResult' => true,
124+
),
125+
'nested_function_call' => array(
126+
'testMarker' => '/* testNestedFunctionCall */',
127+
'expectedResult' => true,
128+
),
129+
);
130+
}
131+
}

WordPress/Tests/Security/NonceVerificationUnitTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
* @since 1.0.0 This sniff has been moved from the `CSRF` category to the `Security` category.
2020
*
2121
* @covers \WordPressCS\WordPress\Helpers\ContextHelper::is_in_type_test
22-
* @covers \WordPressCS\WordPress\Helpers\ContextHelper::is_in_array_comparison
2322
* @covers \WordPressCS\WordPress\Sniffs\Security\NonceVerificationSniff
2423
*/
2524
final class NonceVerificationUnitTest extends AbstractSniffUnitTest {

0 commit comments

Comments
 (0)