Skip to content

Commit d7bb675

Browse files
committed
SanitizationHelperTrait::is_only_sanitized(): add tests
1 parent 58ac13b commit d7bb675

3 files changed

Lines changed: 176 additions & 1 deletion

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/*
4+
* Test cases that should not be considered only sanitized.
5+
*/
6+
7+
/* testNotSanitizedEcho */
8+
echo $_POST['foo'];
9+
10+
/* testInnerFunctionNotSanitizing */
11+
sanitize_text_field( strtolower( $_GET['name'] ) );
12+
13+
/* testOnlyUnslashed */
14+
wp_unslash( $_REQUEST['content'] );
15+
16+
/* testCastNestedInFunction */
17+
number_format( (float) $_POST['price'] );
18+
19+
/* testSanitizedNestedInFunction */
20+
trim( sanitize_text_field( $_POST['name'] ) );
21+
22+
/*
23+
* Test cases that should be considered only sanitized.
24+
*/
25+
26+
/* testSingleSanitizingFunction */
27+
sanitize_text_field( $_POST['name'] );
28+
29+
/* testUnslashingSanitizingFunction */
30+
absint( $_GET['id'] );
31+
32+
/* testArrayWalkingSanitizingCallback */
33+
array_map( 'sanitize_text_field', $_REQUEST['items'] );
34+
35+
/* testInUnset */
36+
unset( $_FILES['temp'] );
37+
38+
/* testSafeCast */
39+
(int) $_POST['count'];
40+
41+
sanitize_text_field( /* testStringTokenSanitized */ get_input() ); // T_STRING entry token: not reached via the sniffs that call the method (they only ever pass a T_VARIABLE), but the method supports any token type.
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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\SanitizationHelperTrait;
11+
12+
use PHPCSUtils\TestUtils\UtilityMethodTestCase;
13+
use WordPressCS\WordPress\Helpers\SanitizationHelperTrait;
14+
15+
/**
16+
* Tests for the `SanitizationHelperTrait::is_only_sanitized()` utility method.
17+
*
18+
* @since 3.4.0
19+
*
20+
* @covers \WordPressCS\WordPress\Helpers\SanitizationHelperTrait::is_only_sanitized
21+
*/
22+
final class IsOnlySanitizedUnitTest extends UtilityMethodTestCase {
23+
24+
/**
25+
* Test class using the SanitizationHelperTrait for testing purposes.
26+
*
27+
* @var object
28+
*/
29+
private static $testClass;
30+
31+
/**
32+
* Set up the test class.
33+
*
34+
* @beforeClass
35+
*
36+
* @return void
37+
*/
38+
public static function setUpBeforeClass(): void {
39+
parent::setUpBeforeClass();
40+
41+
self::$testClass = new class() {
42+
use SanitizationHelperTrait;
43+
};
44+
}
45+
46+
/**
47+
* Test is_only_sanitized() returns false if the token does not exist.
48+
*
49+
* @return void
50+
*/
51+
public function testIsOnlySanitizedReturnsFalseIfTokenDoesNotExist() {
52+
$this->assertFalse( self::$testClass->is_only_sanitized( self::$phpcsFile, -1 ) );
53+
}
54+
55+
/**
56+
* Test is_only_sanitized().
57+
*
58+
* @dataProvider dataIsOnlySanitized
59+
*
60+
* @param string $testMarker The comment which prefaces the target token in the test file.
61+
* @param bool $expectedResult The expected return value.
62+
* @param int|string $tokenType The token type to search for. Defaults to T_VARIABLE.
63+
*
64+
* @return void
65+
*/
66+
public function testIsOnlySanitized( $testMarker, $expectedResult, $tokenType = \T_VARIABLE ) {
67+
$stackPtr = $this->getTargetToken( $testMarker, $tokenType );
68+
$result = self::$testClass->is_only_sanitized(
69+
self::$phpcsFile,
70+
$stackPtr
71+
);
72+
73+
$this->assertSame( $expectedResult, $result );
74+
}
75+
76+
/**
77+
* Data provider.
78+
*
79+
* @see testIsOnlySanitized()
80+
*
81+
* @return array<string, array<string, bool|int|string>>
82+
*/
83+
public static function dataIsOnlySanitized() {
84+
return array(
85+
// Cases where false should be returned.
86+
'not_sanitized_echo' => array(
87+
'testMarker' => '/* testNotSanitizedEcho */',
88+
'expectedResult' => false,
89+
),
90+
'inner_function_not_sanitizing' => array(
91+
'testMarker' => '/* testInnerFunctionNotSanitizing */',
92+
'expectedResult' => false,
93+
),
94+
'only_unslashed' => array(
95+
'testMarker' => '/* testOnlyUnslashed */',
96+
'expectedResult' => false,
97+
),
98+
'cast_nested_in_function' => array(
99+
'testMarker' => '/* testCastNestedInFunction */',
100+
'expectedResult' => false,
101+
),
102+
'sanitized_nested_in_function' => array(
103+
'testMarker' => '/* testSanitizedNestedInFunction */',
104+
'expectedResult' => false,
105+
),
106+
107+
// Cases where true should be returned.
108+
'single_sanitizing_function' => array(
109+
'testMarker' => '/* testSingleSanitizingFunction */',
110+
'expectedResult' => true,
111+
),
112+
'unslashing_sanitizing_function' => array(
113+
'testMarker' => '/* testUnslashingSanitizingFunction */',
114+
'expectedResult' => true,
115+
),
116+
'array_walking_sanitizing_callback' => array(
117+
'testMarker' => '/* testArrayWalkingSanitizingCallback */',
118+
'expectedResult' => true,
119+
),
120+
'in_unset' => array(
121+
'testMarker' => '/* testInUnset */',
122+
'expectedResult' => true,
123+
),
124+
'safe_cast' => array(
125+
'testMarker' => '/* testSafeCast */',
126+
'expectedResult' => true,
127+
),
128+
'string_token_sanitized' => array(
129+
'testMarker' => '/* testStringTokenSanitized */',
130+
'expectedResult' => true,
131+
'tokenType' => \T_STRING,
132+
),
133+
);
134+
}
135+
}

WordPress/Tests/Security/ValidatedSanitizedInputUnitTest.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 `VIP` category to the `Security` category.
2020
*
2121
* @covers \WordPressCS\WordPress\Helpers\ArrayWalkingFunctionsHelper
22-
* @covers \WordPressCS\WordPress\Helpers\SanitizationHelperTrait
2322
* @covers \WordPressCS\WordPress\Helpers\UnslashingFunctionsHelper
2423
* @covers \WordPressCS\WordPress\Helpers\ValidationHelper
2524
* @covers \WordPressCS\WordPress\Helpers\VariableHelper

0 commit comments

Comments
 (0)