Skip to content

Commit 16a785e

Browse files
authored
ContextHelper::is_in_isset_or_empty(): add basic tests (#2725)
* ContextHelper::is_in_isset_or_empty(): add basic tests * Apply suggestions from code review * Apply more review suggestions
1 parent 1f6e183 commit 16a785e

3 files changed

Lines changed: 181 additions & 1 deletion

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* The below should NOT be recognized as being inside an isset/empty check.
5+
*/
6+
7+
/* testBareVariable */ $value;
8+
some_function( /* testOtherFunctionCall */ $value );
9+
array_key_exists( /* testMissingArrayParam */ $key );
10+
key_exists( /* testKeyParamNotArrayParam */ $value, $array );
11+
MyNamespace\key_exists( 'key', /* testPartiallyQualifiedFunction */ $array );
12+
\MyNamespace\array_key_exists( 'key', /* testFullyQualifiedNamespacedFunction */ $array );
13+
namespace\array_key_exists( 'key', /* testNamespaceRelativeFunction */ $array ); // The method should start recognizing this once it can resolve relative namespaces.
14+
namespace\Sub\key_exists( 'key', /* testNamespaceRelativeSubFunction */ $array );
15+
$obj->array_key_exists( 'key', /* testObjectMethod */ $array );
16+
$obj?->key_exists( 'key', /* testNullsafeObjectMethod */ $array );
17+
MyClass::array_key_exists( 'key', /* testStaticMethod */ $array );
18+
key_exists( 'key', my_function( /* testNestedNonTargetFunctionCall */ $array ) );
19+
$obj->isset( /* testIssetObjectMethod */ $value );
20+
Foo::empty( /* testEmptyStaticMethod */ $value );
21+
MyNamespace\isset( /* testIssetNamespacedFunction */ $value );
22+
23+
/*
24+
* The below should be recognized as being inside an isset/empty check.
25+
*/
26+
27+
isset( /* testIsset */ $value );
28+
empty( /* testEmpty */ $value );
29+
array_key_exists( 'key', /* testUnqualifiedFunction */ $array );
30+
Key_Exists( 'key', /* testMixedCaseFunction */ $array );
31+
\array_key_exists( 'key', /* testFullyQualifiedFunction */ $array );
32+
\KEY_EXISTS( 'key', /* testFullyQualifiedUpperCaseFunction */ $array );
33+
array_key_exists( array: /* testNamedParamReversedOrder */ $array, key: 'foo' );
34+
array_key_exists( 'key', \key_exists( 'key', /* testNestedValidFunctionCall */ $array ) );
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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_isset_or_empty()` utility method.
17+
*
18+
* @since 3.4.0
19+
*
20+
* @covers \WordPressCS\WordPress\Helpers\ContextHelper::is_in_isset_or_empty
21+
*/
22+
final class IsInIssetOrEmptyUnitTest extends UtilityMethodTestCase {
23+
24+
/**
25+
* Test is_in_isset_or_empty().
26+
*
27+
* @dataProvider dataIsInIssetOrEmpty
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 testIsInIssetOrEmpty( $testMarker, $expectedResult ) {
35+
$stackPtr = $this->getTargetToken( $testMarker, \T_VARIABLE );
36+
$result = ContextHelper::is_in_isset_or_empty( self::$phpcsFile, $stackPtr );
37+
38+
$this->assertSame( $expectedResult, $result );
39+
}
40+
41+
/**
42+
* Data provider.
43+
*
44+
* @see testIsInIssetOrEmpty()
45+
*
46+
* @return array<string, array<string, bool|string>>
47+
*/
48+
public static function dataIsInIssetOrEmpty() {
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+
'missing_array_param' => array(
60+
'testMarker' => '/* testMissingArrayParam */',
61+
'expectedResult' => false,
62+
),
63+
'key_param_not_array_param' => array(
64+
'testMarker' => '/* testKeyParamNotArrayParam */',
65+
'expectedResult' => false,
66+
),
67+
'partially_qualified_function' => array(
68+
'testMarker' => '/* testPartiallyQualifiedFunction */',
69+
'expectedResult' => false,
70+
),
71+
'fully_qualified_namespaced_function' => array(
72+
'testMarker' => '/* testFullyQualifiedNamespacedFunction */',
73+
'expectedResult' => false,
74+
),
75+
'namespace_relative_function' => array(
76+
'testMarker' => '/* testNamespaceRelativeFunction */',
77+
'expectedResult' => false,
78+
),
79+
'namespace_relative_sub_function' => array(
80+
'testMarker' => '/* testNamespaceRelativeSubFunction */',
81+
'expectedResult' => false,
82+
),
83+
'object_method' => array(
84+
'testMarker' => '/* testObjectMethod */',
85+
'expectedResult' => false,
86+
),
87+
'nullsafe_object_method' => array(
88+
'testMarker' => '/* testNullsafeObjectMethod */',
89+
'expectedResult' => false,
90+
),
91+
'static_method' => array(
92+
'testMarker' => '/* testStaticMethod */',
93+
'expectedResult' => false,
94+
),
95+
'nested_non_target_function_call' => array(
96+
'testMarker' => '/* testNestedNonTargetFunctionCall */',
97+
'expectedResult' => false,
98+
),
99+
'isset_object_method' => array(
100+
'testMarker' => '/* testIssetObjectMethod */',
101+
'expectedResult' => false,
102+
),
103+
'empty_static_method' => array(
104+
'testMarker' => '/* testEmptyStaticMethod */',
105+
'expectedResult' => false,
106+
),
107+
'isset_namespaced_function' => array(
108+
'testMarker' => '/* testIssetNamespacedFunction */',
109+
'expectedResult' => false,
110+
),
111+
112+
// Cases that should return true.
113+
'isset' => array(
114+
'testMarker' => '/* testIsset */',
115+
'expectedResult' => true,
116+
),
117+
'empty' => array(
118+
'testMarker' => '/* testEmpty */',
119+
'expectedResult' => true,
120+
),
121+
'unqualified_function' => array(
122+
'testMarker' => '/* testUnqualifiedFunction */',
123+
'expectedResult' => true,
124+
),
125+
'mixed_case_function' => array(
126+
'testMarker' => '/* testMixedCaseFunction */',
127+
'expectedResult' => true,
128+
),
129+
'fully_qualified_function' => array(
130+
'testMarker' => '/* testFullyQualifiedFunction */',
131+
'expectedResult' => true,
132+
),
133+
'fully_qualified_upper_case_function' => array(
134+
'testMarker' => '/* testFullyQualifiedUpperCaseFunction */',
135+
'expectedResult' => true,
136+
),
137+
'named_param_reversed_order' => array(
138+
'testMarker' => '/* testNamedParamReversedOrder */',
139+
'expectedResult' => true,
140+
),
141+
'nested_valid_function_call' => array(
142+
'testMarker' => '/* testNestedValidFunctionCall */',
143+
'expectedResult' => true,
144+
),
145+
);
146+
}
147+
}

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_isset_or_empty
2322
* @covers \WordPressCS\WordPress\Helpers\ContextHelper::is_in_array_comparison
2423
* @covers \WordPressCS\WordPress\Sniffs\Security\NonceVerificationSniff
2524
*/

0 commit comments

Comments
 (0)