|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use Patchstack\Processor; |
| 7 | +use Patchstack\Extensions\Test\Extension; |
| 8 | + |
| 9 | +final class ProcessorTest extends TestCase |
| 10 | +{ |
| 11 | + /** |
| 12 | + * @var Processor |
| 13 | + */ |
| 14 | + protected $processor; |
| 15 | + |
| 16 | + /** |
| 17 | + * @var array |
| 18 | + */ |
| 19 | + protected $rules; |
| 20 | + |
| 21 | + /** |
| 22 | + * Setup the test for testing the header location redirect. |
| 23 | + * |
| 24 | + * @return void |
| 25 | + */ |
| 26 | + protected function setUp(): void |
| 27 | + { |
| 28 | + $this->rules = json_decode(file_get_contents(dirname(__FILE__) . '/data/Rules.json'), true); |
| 29 | + $this->processor = new Processor( |
| 30 | + new Extension(), |
| 31 | + $this->rules, |
| 32 | + [] |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + public function testMatchParameterValue() |
| 37 | + { |
| 38 | + // Test case for 'equals' type |
| 39 | + $match = ['type' => 'equals', 'value' => 10]; |
| 40 | + $value = 10; |
| 41 | + $this->assertTrue($this->processor->matchParameterValue($match, $value)); |
| 42 | + |
| 43 | + // Test case for 'equals_strict' type |
| 44 | + $match = ['type' => 'equals_strict', 'value' => 10]; |
| 45 | + $value = '10'; |
| 46 | + $this->assertFalse($this->processor->matchParameterValue($match, $value)); |
| 47 | + |
| 48 | + // Test case for 'more_than' type |
| 49 | + $match = ['type' => 'more_than', 'value' => 10]; |
| 50 | + $value = 15; |
| 51 | + $this->assertTrue($this->processor->matchParameterValue($match, $value)); |
| 52 | + |
| 53 | + // Test case for 'less_than' type |
| 54 | + $match = ['type' => 'less_than', 'value' => 10]; |
| 55 | + $value = 5; |
| 56 | + $this->assertTrue($this->processor->matchParameterValue($match, $value)); |
| 57 | + |
| 58 | + // Test case for 'isset' type |
| 59 | + $match = ['type' => 'isset']; |
| 60 | + $value = 'Hello'; |
| 61 | + $this->assertTrue($this->processor->matchParameterValue($match, $value)); |
| 62 | + |
| 63 | + // Test case for 'ctype_special' type |
| 64 | + $match = ['type' => 'ctype_special', 'value' => true]; |
| 65 | + $value = 'Hello-World123'; |
| 66 | + $this->assertTrue($this->processor->matchParameterValue($match, $value)); |
| 67 | + |
| 68 | + // Test case for 'ctype_digit' type |
| 69 | + $match = ['type' => 'ctype_digit', 'value' => false]; |
| 70 | + $value = '123'; |
| 71 | + $this->assertFalse($this->processor->matchParameterValue($match, $value)); |
| 72 | + |
| 73 | + // Test case for 'ctype_alnum' type |
| 74 | + $match = ['type' => 'ctype_alnum', 'value' => true]; |
| 75 | + $value = 'Hello123'; |
| 76 | + $this->assertTrue($this->processor->matchParameterValue($match, $value)); |
| 77 | + |
| 78 | + // Test case for 'is_numeric' type |
| 79 | + $match = ['type' => 'is_numeric', 'value' => true]; |
| 80 | + $value = '123'; |
| 81 | + $this->assertTrue($this->processor->matchParameterValue($match, $value)); |
| 82 | + |
| 83 | + // Test case for 'contains' type |
| 84 | + $match = ['type' => 'contains', 'value' => 'World']; |
| 85 | + $value = 'Hello World'; |
| 86 | + $this->assertTrue($this->processor->matchParameterValue($match, $value)); |
| 87 | + |
| 88 | + // Test case for 'not_contains' type |
| 89 | + $match = ['type' => 'not_contains', 'value' => 'World']; |
| 90 | + $value = 'Hello'; |
| 91 | + $this->assertTrue($this->processor->matchParameterValue($match, $value)); |
| 92 | + |
| 93 | + // Test case for 'quotes' type |
| 94 | + $match = ['type' => 'quotes', 'value' => true]; |
| 95 | + $value = 'Hello "World"'; |
| 96 | + $this->assertTrue($this->processor->matchParameterValue($match, $value)); |
| 97 | + |
| 98 | + // Test case for 'regex' type |
| 99 | + $match = ['type' => 'regex', 'value' => '/^\d{2}$/']; |
| 100 | + $value = '12'; |
| 101 | + $this->assertTrue($this->processor->matchParameterValue($match, $value)); |
| 102 | + |
| 103 | + // Test case for 'in_array' type |
| 104 | + $match = ['type' => 'in_array', 'value' => [1, 2, 3]]; |
| 105 | + $value = 2; |
| 106 | + $this->assertTrue($this->processor->matchParameterValue($match, $value)); |
| 107 | + |
| 108 | + // Test case for 'not_in_array' type |
| 109 | + $match = ['type' => 'not_in_array', 'value' => [1, 2, 3]]; |
| 110 | + $value = 4; |
| 111 | + $this->assertTrue($this->processor->matchParameterValue($match, $value)); |
| 112 | + |
| 113 | + // Test case for 'array_in_array' type |
| 114 | + $match = ['type' => 'array_in_array', 'value' => [1, 2, 3]]; |
| 115 | + $value = [3, 4, 5]; |
| 116 | + $this->assertTrue($this->processor->matchParameterValue($match, $value)); |
| 117 | + |
| 118 | + // Test case for 'inline_xss' type |
| 119 | + $match = ['type' => 'inline_xss']; |
| 120 | + $value = '"> <script src="https://evil.com/evil.js"></script>'; |
| 121 | + $this->assertTrue($this->processor->matchParameterValue($match, $value)); |
| 122 | + |
| 123 | + $value = 'This is a valid "string".'; |
| 124 | + $this->assertFalse($this->processor->matchParameterValue($match, $value)); |
| 125 | + } |
| 126 | +} |
0 commit comments