1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Tests\Unit\Http\Requests; |
6
|
|
|
|
7
|
|
|
use Illuminate\Contracts\Translation\Translator; |
8
|
|
|
use Illuminate\Support\Arr; |
9
|
|
|
use Illuminate\Support\Facades\Validator; |
10
|
|
|
use Illuminate\Validation\ValidationException; |
11
|
|
|
use Mockery; |
12
|
|
|
use Tests\Unit\TestCase; |
13
|
|
|
|
14
|
|
|
use function strpos; |
15
|
|
|
|
16
|
|
|
class RestrictsExtraAttributesTest extends TestCase |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @test |
20
|
|
|
* |
21
|
|
|
* @dataProvider provideData |
22
|
|
|
*/ |
23
|
|
|
public function it_should_throw_validation_error_on_extra_arguments( |
24
|
|
|
array $rules, |
25
|
|
|
array $attributes, |
26
|
|
|
array $errorAttributes |
27
|
|
|
): void { |
28
|
|
|
$formRequest = $this->createValidator($rules, $attributes); |
29
|
|
|
|
30
|
|
|
try { |
31
|
|
|
$formRequest->validateResolved(); |
32
|
|
|
} catch (ValidationException $e) { |
33
|
|
|
if (! empty($errorAttributes)) { |
34
|
|
|
$errors = Arr::dot($e->errors()); |
|
|
|
|
35
|
|
|
|
36
|
|
|
foreach ($errorAttributes as $errorAttr) { |
37
|
|
|
$found = false; |
38
|
|
|
foreach ($errors as $error) { |
39
|
|
|
if (strpos($error, $errorAttr) !== false) { |
40
|
|
|
$found = true; |
41
|
|
|
break; |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
$this->assertTrue($found, 'Failed asserting that validation errors contains "' . $errorAttr . '" string'); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$this->assertInstanceOf(CustomRequest::class, $formRequest); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function provideData(): array |
53
|
|
|
{ |
54
|
|
|
return [ |
55
|
|
|
[ |
56
|
|
|
[ |
57
|
|
|
'field1' => 'required', |
58
|
|
|
'field2' => 'required', |
59
|
|
|
], |
60
|
|
|
[ |
61
|
|
|
'field1' => 'Some Data 1', |
62
|
|
|
'field2' => 'Some Data 2', |
63
|
|
|
'field3' => 'Some Data 3', |
64
|
|
|
], |
65
|
|
|
[ |
66
|
|
|
'field3', |
67
|
|
|
], |
68
|
|
|
], |
69
|
|
|
[ |
70
|
|
|
[ |
71
|
|
|
'field1' => 'required', |
72
|
|
|
'field2.subfield1' => 'required', |
73
|
|
|
'field2.subfield2' => 'required', |
74
|
|
|
'field2.subfield3' => 'required', |
75
|
|
|
], |
76
|
|
|
[ |
77
|
|
|
'field1' => 'Some Data 1', |
78
|
|
|
'field2' => [ |
79
|
|
|
'subfield1' => 'Some Data 2-1', |
80
|
|
|
'subfield2' => 'Some Data 2-2', |
81
|
|
|
'subfield3' => 'Some Data 2-3', |
82
|
|
|
'subfield4' => 'Some Data 2-3', |
83
|
|
|
], |
84
|
|
|
'field3' => 'Some Data 3', |
85
|
|
|
], |
86
|
|
|
[ |
87
|
|
|
'field2.subfield4', |
88
|
|
|
'field3', |
89
|
|
|
], |
90
|
|
|
], |
91
|
|
|
[ |
92
|
|
|
[ |
93
|
|
|
'field1' => 'required', |
94
|
|
|
'field2.*.subfield1' => 'required', |
95
|
|
|
'field2.*.subfield2' => 'required', |
96
|
|
|
'field2.*.subfield3' => 'required', |
97
|
|
|
'field3' => 'required', |
98
|
|
|
'field4' => 'required', |
99
|
|
|
], |
100
|
|
|
[ |
101
|
|
|
'field1' => 'Some Data 1', |
102
|
|
|
'field2' => [ |
103
|
|
|
[ |
104
|
|
|
'subfield1' => 'Some Data 2-1', |
105
|
|
|
'subfield2' => 'Some Data 2-2', |
106
|
|
|
], |
107
|
|
|
[ |
108
|
|
|
'subfield3' => 'Some Data 2-3', |
109
|
|
|
'subfield4' => 'Some Data 2-3', |
110
|
|
|
], |
111
|
|
|
], |
112
|
|
|
'field3' => 'Some Data 3', |
113
|
|
|
'field4' => 'Some Data 4', |
114
|
|
|
'field5' => 'Some Data 3', |
115
|
|
|
], |
116
|
|
|
[ |
117
|
|
|
'field2.*.subfield4', |
118
|
|
|
'field5', |
119
|
|
|
], |
120
|
|
|
], |
121
|
|
|
[ |
122
|
|
|
[ |
123
|
|
|
'field1' => 'required', |
124
|
|
|
'field2' => 'required', |
125
|
|
|
'field2.*' => 'required', |
126
|
|
|
], |
127
|
|
|
[ |
128
|
|
|
'field1' => 'Some Data 1', |
129
|
|
|
'field2' => [ |
130
|
|
|
[ |
131
|
|
|
'subfield1' => 'Some Data 2-1', |
132
|
|
|
'subfield2' => 'Some Data 2-2', |
133
|
|
|
], |
134
|
|
|
[ |
135
|
|
|
'subfield3' => 'Some Data 2-3', |
136
|
|
|
'subfield4' => 'Some Data 2-3', |
137
|
|
|
], |
138
|
|
|
], |
139
|
|
|
'field3' => 'Some Data 3', |
140
|
|
|
], |
141
|
|
|
[ |
142
|
|
|
'field3', |
143
|
|
|
], |
144
|
|
|
], |
145
|
|
|
]; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
private function createValidator(array $rules, array $attributes): CustomRequest |
149
|
|
|
{ |
150
|
|
|
/** @var \Mockery\MockInterface|\Tests\Unit\Http\Requests\CustomRequest $formRequest */ |
151
|
|
|
$formRequest = Mockery::mock(CustomRequest::class)->makePartial(); |
152
|
|
|
$formRequest->shouldReceive('rules')->andReturn($rules); |
|
|
|
|
153
|
|
|
$formRequest->setContainer($this->app); |
154
|
|
|
$formRequest->initialize($attributes); |
155
|
|
|
$formRequest->setValidator(Validator::make($formRequest->all(), $rules)); |
156
|
|
|
|
157
|
|
|
app(Translator::class)->addLines([ |
158
|
|
|
'validation.restrict_extra_attributes' => 'The :attribute key is not allowed in the request body.', |
159
|
|
|
], 'en'); |
160
|
|
|
|
161
|
|
|
return $formRequest; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|