|
| 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 | +} |
0 commit comments