Skip to content

Commit 5199140

Browse files
authored
[9.x] Add zero-width space to trimmed characters in TrimStrings middleware (#44906)
* Add zero-width space to list of characters trimmed in TrimStrings middleware * Add tests for zero-width space characters trimming in TrimStings middleware
1 parent 3640bb5 commit 5199140

File tree

2 files changed

+119
-1
lines changed

2 files changed

+119
-1
lines changed

src/Illuminate/Foundation/Http/Middleware/TrimStrings.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ protected function transform($key, $value)
5353
return $value;
5454
}
5555

56-
return preg_replace('~^[\s]+|[\s]+$~u', '', $value) ?? trim($value);
56+
return preg_replace('~^[\s]+|[\s]+$~u', '', $value) ?? trim($value);
5757
}
5858

5959
/**
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Http\Middleware;
4+
5+
use Illuminate\Foundation\Http\Middleware\TrimStrings;
6+
use Illuminate\Http\Request;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class TrimStringsTest extends TestCase
10+
{
11+
/**
12+
* Test no zero-width space character returns the same string
13+
*/
14+
public function test_no_zero_width_space_character_returns_the_same_string()
15+
{
16+
$request = new Request;
17+
18+
$request->merge([
19+
'title' => 'This title does not contains any zero-width space'
20+
]);
21+
22+
$middleware = new TrimStrings;
23+
24+
$middleware->handle($request, function ($req) {
25+
$this->assertEquals('This title does not contains any zero-width space', $req->title);
26+
});
27+
}
28+
29+
/**
30+
* Test leading zero-width space character is trimmed [ZWSP]
31+
*/
32+
public function test_leading_zero_width_space_character_is_trimmed()
33+
{
34+
$request = new Request;
35+
36+
$request->merge([
37+
'title' => '​This title contains a zero-width space at the begining'
38+
]);
39+
40+
$middleware = new TrimStrings;
41+
42+
$middleware->handle($request, function ($req) {
43+
$this->assertEquals('This title contains a zero-width space at the begining', $req->title);
44+
});
45+
}
46+
47+
/**
48+
* Test trailing zero-width space character is trimmed [ZWSP]
49+
*/
50+
public function test_trailing_zero_width_space_character_is_trimmed()
51+
{
52+
$request = new Request;
53+
54+
$request->merge([
55+
'title' => 'This title contains a zero-width space at the end​'
56+
]);
57+
58+
$middleware = new TrimStrings;
59+
60+
$middleware->handle($request, function ($req) {
61+
$this->assertEquals('This title contains a zero-width space at the end', $req->title);
62+
});
63+
}
64+
65+
/**
66+
* Test leading zero-width non-breakable space character is trimmed [ZWNBSP]
67+
*/
68+
public function test_leading_zero_width_non_breakable_space_character_is_trimmed()
69+
{
70+
$request = new Request;
71+
72+
$request->merge([
73+
'title' => 'This title contains a zero-width non-breakable space at the begining'
74+
]);
75+
76+
$middleware = new TrimStrings;
77+
78+
$middleware->handle($request, function ($req) {
79+
$this->assertEquals('This title contains a zero-width non-breakable space at the begining', $req->title);
80+
});
81+
}
82+
83+
/**
84+
* Test leading multiple zero-width non-breakable space characters are trimmed [ZWNBSP]
85+
*/
86+
public function test_leading_multiple_zero_width_non_breakable_space_characters_are_trimmed()
87+
{
88+
$request = new Request;
89+
90+
$request->merge([
91+
'title' => 'This title contains a zero-width non-breakable space at the begining'
92+
]);
93+
94+
$middleware = new TrimStrings;
95+
96+
$middleware->handle($request, function ($req) {
97+
$this->assertEquals('This title contains a zero-width non-breakable space at the begining', $req->title);
98+
});
99+
}
100+
101+
/**
102+
* Test a combination of leading and trailing zero-width non-breakable space and zero-width space characters are trimmed [ZWNBSP], [ZWSP]
103+
*/
104+
public function test_combination_of_leading_and_trailing_zero_width_non_breakable_space_and_zero_width_space_characters_are_trimmed()
105+
{
106+
$request = new Request;
107+
108+
$request->merge([
109+
'title' => '​This title contains a zero-width non-breakable space at the end​'
110+
]);
111+
112+
$middleware = new TrimStrings;
113+
114+
$middleware->handle($request, function ($req) {
115+
$this->assertEquals('This title contains a zero-width non-breakable space at the end', $req->title);
116+
});
117+
}
118+
}

0 commit comments

Comments
 (0)