We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
1 | <?php |
||||
2 | |||||
3 | namespace Backpack\CRUD\Tests\Unit\CrudPanel; |
||||
4 | |||||
5 | use Backpack\CRUD\app\Library\Validation\Rules\ValidUpload; |
||||
6 | use Backpack\CRUD\app\Library\Validation\Rules\ValidUploadMultiple; |
||||
7 | use Backpack\CRUD\Tests\config\Http\Requests\UserRequest; |
||||
8 | use Backpack\CRUD\Tests\config\Models\User; |
||||
9 | use Illuminate\Http\UploadedFile; |
||||
10 | |||||
11 | /** |
||||
12 | * @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Validation |
||||
13 | * @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Read |
||||
14 | * @covers Backpack\CRUD\app\Library\Validation\Rules\BackpackCustomRule |
||||
15 | * @covers Backpack\CRUD\app\Library\Validation\Rules\ValidUpload |
||||
16 | * @covers Backpack\CRUD\app\Library\Validation\Rules\ValidUploadMultiple |
||||
17 | * @covers Backpack\CRUD\app\Library\Validation\Rules\ValidFileArray |
||||
18 | * @covers Backpack\CRUD\app\Library\Validation\Rules\Support\HasFiles |
||||
19 | */ |
||||
20 | class CrudPanelValidationTest extends \Backpack\CRUD\Tests\config\CrudPanel\BaseCrudPanel |
||||
21 | { |
||||
22 | public function testItThrowsValidationExceptions() |
||||
23 | { |
||||
24 | $this->crudPanel->setModel(User::class); |
||||
25 | $this->crudPanel->setValidation(UserRequest::class); |
||||
26 | |||||
27 | $request = request()->create('users/', 'POST', [ |
||||
28 | 'email' => '[email protected]', |
||||
29 | 'password' => 'test', |
||||
30 | ]); |
||||
31 | |||||
32 | $this->crudPanel->setRequest($request); |
||||
33 | $this->expectException(\Illuminate\Validation\ValidationException::class); |
||||
34 | $validatedRequest = $this->crudPanel->validateRequest(); |
||||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||||
35 | } |
||||
36 | |||||
37 | public function testItMergesFieldValidationWithRequestValidation() |
||||
38 | { |
||||
39 | $this->crudPanel->setModel(User::class); |
||||
40 | $this->crudPanel->setValidation(UserRequest::class); |
||||
41 | |||||
42 | $request = request()->create('users/', 'POST', [ |
||||
43 | 'name' => 'test name', |
||||
44 | 'email' => '[email protected]', |
||||
45 | 'password' => 'test', |
||||
46 | ]); |
||||
47 | |||||
48 | $request->setRouteResolver(function () use ($request) { |
||||
49 | return (new Route('POST', 'users', ['Backpack\CRUD\Tests\Config\Http\Controllers\UserCrudController', 'create']))->bind($request); |
||||
0 ignored issues
–
show
|
|||||
50 | }); |
||||
51 | |||||
52 | $this->crudPanel->addFields([ |
||||
53 | [ |
||||
54 | 'name' => 'email', |
||||
55 | 'validationRules' => 'required', |
||||
56 | ], |
||||
57 | [ |
||||
58 | 'name' => 'name', |
||||
59 | ], |
||||
60 | [ |
||||
61 | 'name' => 'password', |
||||
62 | ], |
||||
63 | ]); |
||||
64 | |||||
65 | $this->crudPanel->setRequest($request); |
||||
66 | |||||
67 | $this->crudPanel->validateRequest(); |
||||
68 | |||||
69 | $this->assertEquals(['email'], array_keys($this->crudPanel->getOperationSetting('validationRules'))); |
||||
0 ignored issues
–
show
It seems like
$this->crudPanel->getOpe...ting('validationRules') can also be of type null ; however, parameter $array of array_keys() does only seem to accept array , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
70 | } |
||||
71 | |||||
72 | public function testItCanGetTheValidationAttributesFromFields() |
||||
73 | { |
||||
74 | $this->crudPanel->addField([ |
||||
75 | 'name' => 'email', |
||||
76 | 'validationAttribute' => 'emailed', |
||||
77 | ]); |
||||
78 | |||||
79 | $this->crudPanel->setValidation(); |
||||
80 | |||||
81 | $this->assertEquals(['email' => 'emailed'], $this->crudPanel->getOperationSetting('validationAttributes')); |
||||
82 | } |
||||
83 | |||||
84 | public function testItCanGetTheValidationAttributesFromSubfields() |
||||
85 | { |
||||
86 | $this->crudPanel->addField([ |
||||
87 | 'name' => 'email', |
||||
88 | 'subfields' => [ |
||||
89 | [ |
||||
90 | 'name' => 'test', |
||||
91 | 'validationAttribute' => 'emailed', |
||||
92 | ], |
||||
93 | ], |
||||
94 | ]); |
||||
95 | |||||
96 | $this->crudPanel->setValidation(); |
||||
97 | |||||
98 | $this->assertEquals(['email.*.test' => 'emailed'], $this->crudPanel->getOperationSetting('validationAttributes')); |
||||
99 | } |
||||
100 | |||||
101 | public function testItMergesAllKindsOfValidation() |
||||
102 | { |
||||
103 | $this->crudPanel->setModel(User::class); |
||||
104 | |||||
105 | $this->crudPanel->setOperation('create'); |
||||
106 | $this->crudPanel->setValidation([ |
||||
107 | 'password' => 'required', |
||||
108 | ]); |
||||
109 | $this->crudPanel->setValidation(UserRequest::class); |
||||
110 | |||||
111 | $request = request()->create('users/', 'POST', [ |
||||
112 | 'name' => '', |
||||
113 | 'password' => '', |
||||
114 | 'email' => '', |
||||
115 | ]); |
||||
116 | |||||
117 | $request->setRouteResolver(function () use ($request) { |
||||
118 | return (new Route('POST', 'users', ['Backpack\CRUD\Tests\Config\Http\Controllers\UserCrudController', 'create']))->bind($request); |
||||
119 | }); |
||||
120 | |||||
121 | $this->crudPanel->addFields([ |
||||
122 | [ |
||||
123 | 'name' => 'email', |
||||
124 | 'validationRules' => 'required', |
||||
125 | ], |
||||
126 | [ |
||||
127 | 'name' => 'name', |
||||
128 | ], |
||||
129 | [ |
||||
130 | 'name' => 'password', |
||||
131 | ], |
||||
132 | ]); |
||||
133 | |||||
134 | $this->crudPanel->setRequest($request); |
||||
135 | |||||
136 | $this->expectException(\Illuminate\Validation\ValidationException::class); |
||||
137 | |||||
138 | try { |
||||
139 | $this->crudPanel->validateRequest(); |
||||
140 | } catch (\Illuminate\Validation\ValidationException $e) { |
||||
141 | $this->assertEquals(['password', 'email', 'name'], array_keys($e->errors())); |
||||
142 | throw $e; |
||||
143 | } |
||||
144 | } |
||||
145 | |||||
146 | public function testItCanGetTheValidationFromFields() |
||||
147 | { |
||||
148 | $this->crudPanel->setModel(User::class); |
||||
149 | $this->crudPanel->setOperation('create'); |
||||
150 | |||||
151 | $request = request()->create('users/', 'POST', [ |
||||
152 | 'name' => 'test name', |
||||
153 | 'email' => '[email protected]', |
||||
154 | 'password' => 'test', |
||||
155 | ]); |
||||
156 | |||||
157 | $request->setRouteResolver(function () use ($request) { |
||||
158 | return (new Route('POST', 'users', ['Backpack\CRUD\Tests\Config\Http\Controllers\UserCrudController', 'create']))->bind($request); |
||||
159 | }); |
||||
160 | |||||
161 | $this->crudPanel->addField([ |
||||
162 | 'name' => 'email', |
||||
163 | 'validationRules' => 'required', |
||||
164 | ]); |
||||
165 | |||||
166 | $this->crudPanel->addField([ |
||||
167 | 'name' => 'name', |
||||
168 | 'validationRules' => 'required', |
||||
169 | 'validationMessages' => [ |
||||
170 | 'required' => 'required ma friend', |
||||
171 | ], |
||||
172 | ]); |
||||
173 | |||||
174 | $this->crudPanel->addField([ |
||||
175 | 'name' => 'password', |
||||
176 | 'subfields' => [ |
||||
177 | [ |
||||
178 | 'name' => 'test', |
||||
179 | 'validationRules' => 'required', |
||||
180 | 'validationMessages' => [ |
||||
181 | 'required' => 'required ma friend', |
||||
182 | ], |
||||
183 | ], |
||||
184 | ], |
||||
185 | ]); |
||||
186 | |||||
187 | $this->crudPanel->setRequest($request); |
||||
188 | |||||
189 | $this->crudPanel->setValidation(); |
||||
190 | |||||
191 | $validatedRequest = $this->crudPanel->validateRequest(); |
||||
0 ignored issues
–
show
|
|||||
192 | |||||
193 | $this->assertEquals(['email', 'name', 'password.*.test'], array_keys($this->crudPanel->getOperationSetting('validationRules'))); |
||||
0 ignored issues
–
show
It seems like
$this->crudPanel->getOpe...ting('validationRules') can also be of type null ; however, parameter $array of array_keys() does only seem to accept array , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
194 | } |
||||
195 | |||||
196 | public function testItThrowsExceptionWithInvalidValidationClass() |
||||
197 | { |
||||
198 | $this->crudPanel->setModel(User::class); |
||||
199 | $this->crudPanel->setOperation('create'); |
||||
200 | |||||
201 | try { |
||||
202 | $this->crudPanel->setValidation('\Backpack\CRUD\Tests\config\Models\User'); |
||||
203 | } catch (\Throwable $e) { |
||||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
|
|||||
204 | } |
||||
205 | $this->assertEquals( |
||||
206 | new \Symfony\Component\HttpKernel\Exception\HttpException(500, 'Please pass setValidation() nothing, a rules array or a FormRequest class.', null, ['developer-error-exception']), |
||||
207 | $e |
||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||
208 | ); |
||||
209 | } |
||||
210 | |||||
211 | public function testItCanDisableTheValidation() |
||||
212 | { |
||||
213 | $this->crudPanel->setModel(User::class); |
||||
214 | $this->crudPanel->setOperation('create'); |
||||
215 | $this->crudPanel->setValidation([ |
||||
216 | 'name' => 'required', |
||||
217 | 'password' => 'required', |
||||
218 | ]); |
||||
219 | $this->crudPanel->setValidation(UserRequest::class); |
||||
220 | $this->assertEquals(['name', 'password'], array_keys($this->crudPanel->getOperationSetting('validationRules'))); |
||||
0 ignored issues
–
show
It seems like
$this->crudPanel->getOpe...ting('validationRules') can also be of type null ; however, parameter $array of array_keys() does only seem to accept array , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
221 | |||||
222 | $this->crudPanel->disableValidation(); |
||||
223 | $this->assertEquals([], $this->crudPanel->getOperationSetting('validationRules')); |
||||
224 | $this->assertEquals([], $this->crudPanel->getOperationSetting('validationMessages')); |
||||
225 | $this->assertEquals([], $this->crudPanel->getOperationSetting('requiredFields')); |
||||
226 | $this->assertEquals(false, $this->crudPanel->getFormRequest()); |
||||
227 | } |
||||
228 | |||||
229 | public function testItCanGetTheRequiredFields() |
||||
230 | { |
||||
231 | $this->crudPanel->setModel(User::class); |
||||
232 | $this->crudPanel->setOperation('create'); |
||||
233 | $this->assertFalse($this->crudPanel->isRequired('test')); |
||||
234 | $this->crudPanel->setValidation([ |
||||
235 | 'email' => 'required', |
||||
236 | 'password.*.test' => 'required', |
||||
237 | 'not_required' => 'present', |
||||
238 | ]); |
||||
239 | |||||
240 | $this->crudPanel->setValidation(UserRequest::class); |
||||
241 | $this->assertEquals(['email', 'password[test]', 'name'], array_values($this->crudPanel->getOperationSetting('requiredFields'))); |
||||
0 ignored issues
–
show
It seems like
$this->crudPanel->getOpe...tting('requiredFields') can also be of type null ; however, parameter $array of array_values() does only seem to accept array , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
242 | $this->assertTrue($this->crudPanel->isRequired('email')); |
||||
243 | $this->assertTrue($this->crudPanel->isRequired('password.test')); |
||||
244 | $this->assertTrue($this->crudPanel->isRequired('name')); |
||||
245 | } |
||||
246 | |||||
247 | public function testItCanGetTheRequiredFieldsFromCustomRules() |
||||
248 | { |
||||
249 | $this->crudPanel->setModel(User::class); |
||||
250 | |||||
251 | $this->crudPanel->setValidation([ |
||||
252 | 'email' => ValidUpload::field('required'), |
||||
253 | 'password' => ValidUploadMultiple::field('required'), |
||||
254 | 'not_required' => ValidUpload::field('present'), |
||||
255 | ]); |
||||
256 | |||||
257 | $this->assertEquals(['email', 'password'], array_values($this->crudPanel->getOperationSetting('requiredFields'))); |
||||
0 ignored issues
–
show
It seems like
$this->crudPanel->getOpe...tting('requiredFields') can also be of type null ; however, parameter $array of array_values() does only seem to accept array , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
258 | $this->assertTrue($this->crudPanel->isRequired('email')); |
||||
259 | $this->assertTrue($this->crudPanel->isRequired('password')); |
||||
260 | } |
||||
261 | |||||
262 | public function testItCanGetTheRequiredFieldsFromRulesArray() |
||||
263 | { |
||||
264 | $this->crudPanel->setModel(User::class); |
||||
265 | |||||
266 | $this->crudPanel->setValidation([ |
||||
267 | 'email' => ['required', 'string'], |
||||
268 | 'password' => ['string', 'required'], |
||||
269 | 'password_confirm' => ['same:password', ValidUploadMultiple::field('required')], |
||||
270 | 'not_required' => ['string', 'present', 'foobar'], |
||||
271 | ]); |
||||
272 | |||||
273 | $this->assertEquals(['email', 'password', 'password_confirm'], array_values($this->crudPanel->getOperationSetting('requiredFields'))); |
||||
0 ignored issues
–
show
It seems like
$this->crudPanel->getOpe...tting('requiredFields') can also be of type null ; however, parameter $array of array_values() does only seem to accept array , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
274 | $this->assertTrue($this->crudPanel->isRequired('email')); |
||||
275 | $this->assertTrue($this->crudPanel->isRequired('password')); |
||||
276 | $this->assertTrue($this->crudPanel->isRequired('password_confirm')); |
||||
277 | } |
||||
278 | |||||
279 | public function testItCanValidateCustomRules() |
||||
280 | { |
||||
281 | $this->crudPanel->setModel(User::class); |
||||
282 | |||||
283 | $pdf1 = UploadedFile::fake()->create('test1.pdf', 1000); |
||||
284 | $pdf2 = UploadedFile::fake()->create('test2.pdf', 1000); |
||||
285 | |||||
286 | $request = request()->create('users/', 'POST', [ |
||||
287 | 'email' => $pdf1, |
||||
288 | 'password' => [$pdf1, $pdf2], |
||||
289 | 'name' => 'test', |
||||
290 | ]); |
||||
291 | |||||
292 | $request->setRouteResolver(function () use ($request) { |
||||
293 | return (new Route('POST', 'users', ['Backpack\CRUD\Tests\Config\Http\Controllers\UserCrudController', 'create']))->bind($request); |
||||
294 | }); |
||||
295 | |||||
296 | $this->crudPanel->addFields([ |
||||
297 | [ |
||||
298 | 'name' => 'password', |
||||
299 | 'validationRules' => ValidUploadMultiple::field('required')->file('file|mimes:pdf|max:500'), |
||||
300 | ], |
||||
301 | [ |
||||
302 | 'name' => 'email', |
||||
303 | 'validationRules' => ValidUpload::field('required')->file('file|mimes:jpg'), |
||||
304 | ], |
||||
305 | ]); |
||||
306 | |||||
307 | $this->crudPanel->setRequest($request); |
||||
308 | |||||
309 | $this->expectException(\Illuminate\Validation\ValidationException::class); |
||||
310 | |||||
311 | try { |
||||
312 | $this->crudPanel->validateRequest(); |
||||
313 | } catch (\Illuminate\Validation\ValidationException $e) { |
||||
314 | $this->assertEquals([ |
||||
315 | 'password' => ['The password field must not be greater than 500 kilobytes.'], |
||||
316 | 'email' => ['The email field must be a file of type: jpg.'], |
||||
317 | ], $e->errors()); |
||||
318 | throw $e; |
||||
319 | } |
||||
320 | } |
||||
321 | } |
||||
322 |