Skip to content

Commit 606fc2f

Browse files
committed
ContextHelper::is_in_isset_or_empty(): add basic tests
1 parent 1f6e183 commit 606fc2f

3 files changed

Lines changed: 156 additions & 1 deletion

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* The below should NOT be recognized as being inside an isset/empty check.
5+
*/
6+
7+
some_function( /* testOtherFunctionCall */ $value );
8+
array_key_exists( /* testMissingArrayParam */ $key );
9+
key_exists( /* testKeyParamNotArrayParam */ $value, $array );
10+
MyNamespace\key_exists( 'key', /* testPartiallyQualifiedFunction */ $array );
11+
\MyNamespace\array_key_exists( 'key', /* testFullyQualifiedNamespacedFunction */ $array );
12+
namespace\array_key_exists( 'key', /* testNamespaceRelativeFunction */ $array ); // The method should start recognizing this once it can resolve relative namespaces.
13+
namespace\Sub\key_exists( 'key', /* testNamespaceRelativeSubFunction */ $array );
14+
$obj->array_key_exists( 'key', /* testObjectMethod */ $array );
15+
$obj?->key_exists( 'key', /* testNullsafeObjectMethod */ $array );
16+
MyClass::array_key_exists( 'key', /* testStaticMethod */ $array );
17+
key_exists( 'key', my_function( /* testNestedFunctionCall */ $array ) );
18+
19+
/*
20+
* The below should be recognized as being inside an isset/empty check.
21+
*/
22+
23+
isset( /* testIsset */ $value );
24+
empty( /* testEmpty */ $value );
25+
array_key_exists( 'key', /* testUnqualifiedFunction */ $array );
26+
Key_Exists( 'key', /* testMixedCaseFunction */ $array );
27+
\array_key_exists( 'key', /* testFullyQualifiedFunction */ $array );
28+
\KEY_EXISTS( 'key', /* testFullyQualifiedUpperCaseFunction */ $array );
29+
array_key_exists( array: /* testNamedParamReversedOrder */ $array, key: 'foo' );
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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+
'other_function_call' => array(
52+
'testMarker' => '/* testOtherFunctionCall */',
53+
'expectedResult' => false,
54+
),
55+
'missing_array_param' => array(
56+
'testMarker' => '/* testMissingArrayParam */',
57+
'expectedResult' => false,
58+
),
59+
'key_param_not_array_param' => array(
60+
'testMarker' => '/* testKeyParamNotArrayParam */',
61+
'expectedResult' => false,
62+
),
63+
'partially_qualified_function' => array(
64+
'testMarker' => '/* testPartiallyQualifiedFunction */',
65+
'expectedResult' => false,
66+
),
67+
'fully_qualified_namespaced_function' => array(
68+
'testMarker' => '/* testFullyQualifiedNamespacedFunction */',
69+
'expectedResult' => false,
70+
),
71+
'namespace_relative_function' => array(
72+
'testMarker' => '/* testNamespaceRelativeFunction */',
73+
'expectedResult' => false,
74+
),
75+
'namespace_relative_sub_function' => array(
76+
'testMarker' => '/* testNamespaceRelativeSubFunction */',
77+
'expectedResult' => false,
78+
),
79+
'object_method' => array(
80+
'testMarker' => '/* testObjectMethod */',
81+
'expectedResult' => false,
82+
),
83+
'nullsafe_object_method' => array(
84+
'testMarker' => '/* testNullsafeObjectMethod */',
85+
'expectedResult' => false,
86+
),
87+
'static_method' => array(
88+
'testMarker' => '/* testStaticMethod */',
89+
'expectedResult' => false,
90+
),
91+
'nested_function_call' => array(
92+
'testMarker' => '/* testNestedFunctionCall */',
93+
'expectedResult' => false,
94+
),
95+
96+
// Cases that should return true.
97+
'isset' => array(
98+
'testMarker' => '/* testIsset */',
99+
'expectedResult' => true,
100+
),
101+
'empty' => array(
102+
'testMarker' => '/* testEmpty */',
103+
'expectedResult' => true,
104+
),
105+
'unqualified_function' => array(
106+
'testMarker' => '/* testUnqualifiedFunction */',
107+
'expectedResult' => true,
108+
),
109+
'mixed_case_function' => array(
110+
'testMarker' => '/* testMixedCaseFunction */',
111+
'expectedResult' => true,
112+
),
113+
'fully_qualified_function' => array(
114+
'testMarker' => '/* testFullyQualifiedFunction */',
115+
'expectedResult' => true,
116+
),
117+
'fully_qualified_upper_case_function' => array(
118+
'testMarker' => '/* testFullyQualifiedUpperCaseFunction */',
119+
'expectedResult' => true,
120+
),
121+
'named_param_reversed_order' => array(
122+
'testMarker' => '/* testNamedParamReversedOrder */',
123+
'expectedResult' => true,
124+
),
125+
);
126+
}
127+
}

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)