1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Feature; |
4
|
|
|
|
5
|
|
|
use Backpack\CRUD\Tests\config\CrudPanel\BaseDBCrudPanel; |
6
|
|
|
use Backpack\CRUD\Tests\config\Http\Controllers\UploaderValidationCrudController; |
7
|
|
|
use Backpack\CRUD\Tests\config\Models\Uploader; |
8
|
|
|
use Backpack\CRUD\Tests\config\Models\User; |
9
|
|
|
use Illuminate\Http\UploadedFile; |
10
|
|
|
use Illuminate\Support\Facades\Storage; |
11
|
|
|
use PHPUnit\Framework\Attributes\Group; |
12
|
|
|
|
13
|
|
|
class UploadersValidationTest extends BaseDBCrudPanel |
14
|
|
|
{ |
15
|
|
|
private string $testBaseUrl; |
16
|
|
|
|
17
|
|
|
protected function defineRoutes($router) |
18
|
|
|
{ |
19
|
|
|
$router->crud(config('backpack.base.route_prefix').'/uploader-validation', UploaderValidationCrudController::class); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
protected function setUp(): void |
23
|
|
|
{ |
24
|
|
|
parent::setUp(); |
25
|
|
|
|
26
|
|
|
$this->testBaseUrl = config('backpack.base.route_prefix').'/uploader-validation'; |
27
|
|
|
Storage::fake('uploaders'); |
28
|
|
|
$this->actingAs(User::find(1)); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function test_it_can_access_the_uploaders_create_page() |
32
|
|
|
{ |
33
|
|
|
$response = $this->get($this->testBaseUrl.'/create'); |
34
|
|
|
$response->assertStatus(200); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function test_it_can_store_uploaded_files() |
38
|
|
|
{ |
39
|
|
|
$response = $this->post($this->testBaseUrl, [ |
40
|
|
|
'upload' => UploadedFile::fake()->create('avatar.pdf'), |
41
|
|
|
'upload_multiple' => [UploadedFile::fake()->create('avatar1.pdf'), UploadedFile::fake()->create('avatar2.pdf')], |
42
|
|
|
]); |
43
|
|
|
|
44
|
|
|
$response->assertStatus(302); |
45
|
|
|
|
46
|
|
|
$response->assertRedirect($this->testBaseUrl); |
47
|
|
|
|
48
|
|
|
$this->assertDatabaseCount('uploaders', 1); |
49
|
|
|
|
50
|
|
|
$files = Storage::disk('uploaders')->allFiles(); |
51
|
|
|
|
52
|
|
|
$this->assertEquals(3, count($files)); |
53
|
|
|
|
54
|
|
|
$this->assertDatabaseHas('uploaders', [ |
55
|
|
|
'upload' => 'avatar.pdf', |
56
|
|
|
'upload_multiple' => json_encode(['avatar1.pdf', 'avatar2.pdf']), |
57
|
|
|
]); |
58
|
|
|
|
59
|
|
|
$this->assertTrue(Storage::disk('uploaders')->exists('avatar.pdf')); |
60
|
|
|
$this->assertTrue(Storage::disk('uploaders')->exists('avatar1.pdf')); |
61
|
|
|
$this->assertTrue(Storage::disk('uploaders')->exists('avatar2.pdf')); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function test_it_display_the_edit_page_without_files() |
65
|
|
|
{ |
66
|
|
|
self::initUploader(); |
67
|
|
|
|
68
|
|
|
$response = $this->get($this->testBaseUrl.'/1/edit'); |
69
|
|
|
$response->assertStatus(200); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function test_it_display_the_upload_page_with_files() |
73
|
|
|
{ |
74
|
|
|
self::initUploaderWithImages(); |
75
|
|
|
|
76
|
|
|
$response = $this->get($this->testBaseUrl.'/1/edit'); |
77
|
|
|
|
78
|
|
|
$response->assertStatus(200); |
79
|
|
|
|
80
|
|
|
$response->assertSee('avatar1.jpg'); |
81
|
|
|
$response->assertSee('avatar2.jpg'); |
82
|
|
|
$response->assertSee('avatar3.jpg'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function test_it_can_update_uploaded_files() |
86
|
|
|
{ |
87
|
|
|
self::initUploaderWithFiles(); |
88
|
|
|
|
89
|
|
|
$response = $this->put($this->testBaseUrl.'/1', [ |
90
|
|
|
'upload' => UploadedFile::fake()->create('avatar4.pdf'), |
91
|
|
|
'upload_multiple' => [UploadedFile::fake()->create('avatar5.pdf'), UploadedFile::fake()->create('avatar6.pdf')], |
92
|
|
|
'clear_upload_multiple' => ['avatar2.pdf', 'avatar3.pdf'], |
93
|
|
|
'id' => 1, |
94
|
|
|
]); |
95
|
|
|
|
96
|
|
|
$response->assertStatus(302); |
97
|
|
|
|
98
|
|
|
$response->assertRedirect($this->testBaseUrl); |
99
|
|
|
|
100
|
|
|
$this->assertDatabaseCount('uploaders', 1); |
101
|
|
|
|
102
|
|
|
$this->assertDatabaseHas('uploaders', [ |
103
|
|
|
'upload' => 'avatar4.pdf', |
104
|
|
|
'upload_multiple' => json_encode(['avatar5.pdf', 'avatar6.pdf']), |
105
|
|
|
]); |
106
|
|
|
|
107
|
|
|
$files = Storage::disk('uploaders')->allFiles(); |
108
|
|
|
|
109
|
|
|
$this->assertEquals(3, count($files)); |
110
|
|
|
|
111
|
|
|
$this->assertTrue(Storage::disk('uploaders')->exists('avatar4.pdf')); |
112
|
|
|
$this->assertTrue(Storage::disk('uploaders')->exists('avatar5.pdf')); |
113
|
|
|
$this->assertTrue(Storage::disk('uploaders')->exists('avatar6.pdf')); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function test_it_can_delete_uploaded_files() |
117
|
|
|
{ |
118
|
|
|
self::initUploaderWithImages(); |
119
|
|
|
|
120
|
|
|
$response = $this->delete($this->testBaseUrl.'/1'); |
121
|
|
|
|
122
|
|
|
$response->assertStatus(200); |
123
|
|
|
|
124
|
|
|
$this->assertDatabaseCount('uploaders', 0); |
125
|
|
|
|
126
|
|
|
$files = Storage::disk('uploaders')->allFiles(); |
127
|
|
|
|
128
|
|
|
$this->assertEquals(0, count($files)); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function test_it_keeps_previous_values_unchaged_when_not_deleted() |
132
|
|
|
{ |
133
|
|
|
self::initUploaderWithFiles(); |
134
|
|
|
|
135
|
|
|
$response = $this->put($this->testBaseUrl.'/1', [ |
136
|
|
|
'upload_multiple' => ['avatar2.pdf', 'avatar3.pdf'], |
137
|
|
|
'id' => 1, |
138
|
|
|
]); |
139
|
|
|
|
140
|
|
|
$response->assertStatus(302); |
141
|
|
|
|
142
|
|
|
$response->assertRedirect($this->testBaseUrl); |
143
|
|
|
|
144
|
|
|
$this->assertDatabaseCount('uploaders', 1); |
145
|
|
|
|
146
|
|
|
$this->assertDatabaseHas('uploaders', [ |
147
|
|
|
'upload' => 'avatar1.pdf', |
148
|
|
|
'upload_multiple' => json_encode(['avatar2.pdf', 'avatar3.pdf']), |
149
|
|
|
]); |
150
|
|
|
|
151
|
|
|
$files = Storage::disk('uploaders')->allFiles(); |
152
|
|
|
|
153
|
|
|
$this->assertEquals(3, count($files)); |
154
|
|
|
|
155
|
|
|
$this->assertTrue(Storage::disk('uploaders')->exists('avatar1.pdf')); |
156
|
|
|
$this->assertTrue(Storage::disk('uploaders')->exists('avatar2.pdf')); |
157
|
|
|
$this->assertTrue(Storage::disk('uploaders')->exists('avatar3.pdf')); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function test_upload_multiple_can_delete_uploaded_files_and_add_at_the_same_time() |
161
|
|
|
{ |
162
|
|
|
self::initUploaderWithFiles(); |
163
|
|
|
|
164
|
|
|
$response = $this->put($this->testBaseUrl.'/1', [ |
165
|
|
|
'upload_multiple' => [UploadedFile::fake()->create('avatar4.pdf'), UploadedFile::fake()->create('avatar5.pdf')], |
166
|
|
|
'clear_upload_multiple' => ['avatar2.pdf'], |
167
|
|
|
'id' => 1, |
168
|
|
|
]); |
169
|
|
|
|
170
|
|
|
$response->assertStatus(302); |
171
|
|
|
|
172
|
|
|
$response->assertRedirect($this->testBaseUrl); |
173
|
|
|
|
174
|
|
|
$this->assertDatabaseCount('uploaders', 1); |
175
|
|
|
|
176
|
|
|
$this->assertDatabaseHas('uploaders', [ |
177
|
|
|
'upload' => 'avatar1.pdf', |
178
|
|
|
'upload_multiple' => json_encode(['avatar3.pdf', 'avatar4.pdf', 'avatar5.pdf']), |
179
|
|
|
]); |
180
|
|
|
|
181
|
|
|
$files = Storage::disk('uploaders')->allFiles(); |
182
|
|
|
|
183
|
|
|
$this->assertEquals(4, count($files)); |
184
|
|
|
|
185
|
|
|
$this->assertTrue(Storage::disk('uploaders')->exists('avatar1.pdf')); |
186
|
|
|
$this->assertTrue(Storage::disk('uploaders')->exists('avatar3.pdf')); |
187
|
|
|
$this->assertTrue(Storage::disk('uploaders')->exists('avatar4.pdf')); |
188
|
|
|
$this->assertTrue(Storage::disk('uploaders')->exists('avatar5.pdf')); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function test_it_validates_files_on_a_single_upload() |
192
|
|
|
{ |
193
|
|
|
$response = $this->post($this->testBaseUrl, [ |
194
|
|
|
'upload' => 'not-a-file', |
195
|
|
|
'upload_multiple' => [UploadedFile::fake()->create('avatar1.pdf'), UploadedFile::fake()->create('avatar2.pdf')], |
196
|
|
|
]); |
197
|
|
|
|
198
|
|
|
$response->assertStatus(302); |
199
|
|
|
$response->assertSessionHasErrors('upload'); |
200
|
|
|
|
201
|
|
|
$this->assertDatabaseCount('uploaders', 0); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function test_it_validates_files_on_multiple_uploader() |
205
|
|
|
{ |
206
|
|
|
$response = $this->post($this->testBaseUrl, [ |
207
|
|
|
'upload' => UploadedFile::fake()->create('avatar.pdf'), |
208
|
|
|
'upload_multiple' => [UploadedFile::fake()->create('avatar1.pdf'), 'not-a-file'], |
209
|
|
|
]); |
210
|
|
|
|
211
|
|
|
$response->assertStatus(302); |
212
|
|
|
$response->assertSessionHasErrors('upload_multiple'); |
213
|
|
|
|
214
|
|
|
$this->assertDatabaseCount('uploaders', 0); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
public function test_it_validates_mime_types_on_single_and_multi_uploads() |
218
|
|
|
{ |
219
|
|
|
$response = $this->post($this->testBaseUrl, [ |
220
|
|
|
'upload' => UploadedFile::fake()->create('avatar2.jpg'), |
221
|
|
|
'upload_multiple' => [UploadedFile::fake()->create('avatar1.jpg'), UploadedFile::fake()->create('avatar3.jpg')], |
222
|
|
|
]); |
223
|
|
|
|
224
|
|
|
$response->assertStatus(302); |
225
|
|
|
|
226
|
|
|
$response->assertSessionHasErrors('upload_multiple'); |
227
|
|
|
$response->assertSessionHasErrors('upload'); |
228
|
|
|
|
229
|
|
|
// assert the error content |
230
|
|
|
$this->assertEquals('The upload multiple field must be a file of type: pdf.', session('errors')->get('upload_multiple')[0]); |
231
|
|
|
$this->assertEquals('The upload field must be a file of type: pdf.', session('errors')->get('upload')[0]); |
232
|
|
|
|
233
|
|
|
$this->assertDatabaseCount('uploaders', 0); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
public function test_it_validates_file_size_on_single_and_multi_uploads() |
237
|
|
|
{ |
238
|
|
|
$response = $this->post($this->testBaseUrl, [ |
239
|
|
|
'upload' => UploadedFile::fake()->create('avatar.pdf', 2000), |
240
|
|
|
'upload_multiple' => [UploadedFile::fake()->create('avatar1.pdf', 2000), UploadedFile::fake()->create('avatar2.pdf', 2000)], |
241
|
|
|
]); |
242
|
|
|
|
243
|
|
|
$response->assertStatus(302); |
244
|
|
|
|
245
|
|
|
$response->assertSessionHasErrors('upload_multiple'); |
246
|
|
|
$response->assertSessionHasErrors('upload'); |
247
|
|
|
|
248
|
|
|
// assert the error content |
249
|
|
|
$this->assertEquals('The upload multiple field must not be greater than 1024 kilobytes.', session('errors')->get('upload_multiple')[0]); |
250
|
|
|
$this->assertEquals('The upload field must not be greater than 1024 kilobytes.', session('errors')->get('upload')[0]); |
251
|
|
|
|
252
|
|
|
$this->assertDatabaseCount('uploaders', 0); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
public function test_it_validates_min_files_on_multi_uploads() |
256
|
|
|
{ |
257
|
|
|
$response = $this->post($this->testBaseUrl, [ |
258
|
|
|
'upload' => UploadedFile::fake()->create('avatar.pdf'), |
259
|
|
|
'upload_multiple' => [UploadedFile::fake()->create('avatar1.pdf')], |
260
|
|
|
]); |
261
|
|
|
|
262
|
|
|
$response->assertStatus(302); |
263
|
|
|
|
264
|
|
|
$response->assertSessionHasErrors('upload_multiple'); |
265
|
|
|
|
266
|
|
|
// assert the error content |
267
|
|
|
$this->assertEquals('The upload multiple field must have at least 2 items.', session('errors')->get('upload_multiple')[0]); |
268
|
|
|
|
269
|
|
|
$this->assertDatabaseCount('uploaders', 0); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
public function test_it_validates_required_files_on_single_and_multi_uploads() |
273
|
|
|
{ |
274
|
|
|
$response = $this->post($this->testBaseUrl, [ |
275
|
|
|
'upload' => null, |
276
|
|
|
'upload_multiple' => null, |
277
|
|
|
]); |
278
|
|
|
|
279
|
|
|
$response->assertStatus(302); |
280
|
|
|
|
281
|
|
|
$response->assertSessionHasErrors('upload'); |
282
|
|
|
$response->assertSessionHasErrors('upload_multiple'); |
283
|
|
|
|
284
|
|
|
// assert the error content |
285
|
|
|
$this->assertEquals('The upload field is required.', session('errors')->get('upload')[0]); |
286
|
|
|
$this->assertEquals('The upload multiple field is required.', session('errors')->get('upload_multiple')[0]); |
287
|
|
|
|
288
|
|
|
$this->assertDatabaseCount('uploaders', 0); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
public function test_it_validates_required_when_not_present_in_request() |
292
|
|
|
{ |
293
|
|
|
$response = $this->post($this->testBaseUrl, []); |
294
|
|
|
|
295
|
|
|
$response->assertStatus(302); |
296
|
|
|
|
297
|
|
|
$response->assertSessionHasErrors('upload'); |
298
|
|
|
$response->assertSessionHasErrors('upload_multiple'); |
299
|
|
|
|
300
|
|
|
$this->assertEquals('The upload field is required.', session('errors')->get('upload')[0]); |
301
|
|
|
$this->assertEquals('The upload multiple field is required.', session('errors')->get('upload_multiple')[0]); |
302
|
|
|
|
303
|
|
|
$this->assertDatabaseCount('uploaders', 0); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
#[Group('fail')] |
307
|
|
|
public function test_it_validates_required_files_on_single_and_multi_uploads_when_updating() |
308
|
|
|
{ |
309
|
|
|
self::initUploader(); |
310
|
|
|
|
311
|
|
|
$response = $this->put($this->testBaseUrl.'/1', [ |
312
|
|
|
'upload' => null, |
313
|
|
|
'upload_multiple' => null, |
314
|
|
|
'id' => 1, |
315
|
|
|
]); |
316
|
|
|
|
317
|
|
|
$response->assertStatus(302); |
318
|
|
|
|
319
|
|
|
$response->assertSessionHasErrors('upload'); |
320
|
|
|
$response->assertSessionHasErrors('upload_multiple'); |
321
|
|
|
|
322
|
|
|
// assert the error content |
323
|
|
|
$this->assertEquals('The upload field is required.', session('errors')->get('upload')[0]); |
324
|
|
|
$this->assertEquals('The upload multiple field is required.', session('errors')->get('upload_multiple')[0]); |
325
|
|
|
|
326
|
|
|
$this->assertDatabaseCount('uploaders', 1); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
public function test_it_validates_required_files_on_single_and_multi_uploads_when_updating_with_files() |
330
|
|
|
{ |
331
|
|
|
self::initUploaderWithFiles(); |
332
|
|
|
|
333
|
|
|
$response = $this->put($this->testBaseUrl.'/1', [ |
334
|
|
|
'upload' => null, |
335
|
|
|
'upload_multiple' => null, |
336
|
|
|
'clear_upload_multiple' => ['avatar2.pdf', 'avatar3.pdf'], |
337
|
|
|
'id' => 1, |
338
|
|
|
]); |
339
|
|
|
|
340
|
|
|
$response->assertStatus(302); |
341
|
|
|
|
342
|
|
|
$response->assertSessionHasErrors('upload'); |
343
|
|
|
$response->assertSessionHasErrors('upload_multiple'); |
344
|
|
|
|
345
|
|
|
// assert the error content |
346
|
|
|
$this->assertEquals('The upload field is required.', session('errors')->get('upload')[0]); |
347
|
|
|
$this->assertEquals('The upload multiple field is required.', session('errors')->get('upload_multiple')[0]); |
348
|
|
|
|
349
|
|
|
$this->assertDatabaseCount('uploaders', 1); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
#[Group('fail')] |
353
|
|
|
public function test_it_validates_min_files_on_multi_uploads_when_updating() |
354
|
|
|
{ |
355
|
|
|
self::initUploaderWithFiles(); |
356
|
|
|
|
357
|
|
|
$response = $this->put($this->testBaseUrl.'/1', [ |
358
|
|
|
'upload_multiple' => [UploadedFile::fake()->create('avatar1.pdf')], |
359
|
|
|
'clear_upload_multiple' => ['avatar2.pdf', 'avatar3.pdf'], |
360
|
|
|
'id' => 1, |
361
|
|
|
]); |
362
|
|
|
|
363
|
|
|
$response->assertStatus(302); |
364
|
|
|
|
365
|
|
|
$response->assertSessionHasErrors('upload_multiple'); |
366
|
|
|
|
367
|
|
|
// assert the error content |
368
|
|
|
$this->assertEquals('The upload multiple field must have at least 2 items.', session('errors')->get('upload_multiple')[0]); |
369
|
|
|
|
370
|
|
|
$this->assertDatabaseCount('uploaders', 1); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
protected static function initUploaderWithImages() |
374
|
|
|
{ |
375
|
|
|
UploadedFile::fake()->image('avatar1.jpg')->storeAs('', 'avatar1.jpg', ['disk' => 'uploaders']); |
376
|
|
|
UploadedFile::fake()->image('avatar2.jpg')->storeAs('', 'avatar2.jpg', ['disk' => 'uploaders']); |
377
|
|
|
UploadedFile::fake()->image('avatar3.jpg')->storeAs('', 'avatar3.jpg', ['disk' => 'uploaders']); |
378
|
|
|
|
379
|
|
|
Uploader::create([ |
380
|
|
|
'upload' => 'avatar1.jpg', |
381
|
|
|
'upload_multiple' => json_encode(['avatar2.jpg', 'avatar3.jpg']), |
382
|
|
|
]); |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
protected static function initUploaderWithFiles() |
386
|
|
|
{ |
387
|
|
|
UploadedFile::fake()->create('avatar1.pdf')->storeAs('', 'avatar1.pdf', ['disk' => 'uploaders']); |
388
|
|
|
UploadedFile::fake()->create('avatar2.pdf')->storeAs('', 'avatar2.pdf', ['disk' => 'uploaders']); |
389
|
|
|
UploadedFile::fake()->create('avatar3.pdf')->storeAs('', 'avatar3.pdf', ['disk' => 'uploaders']); |
390
|
|
|
|
391
|
|
|
Uploader::create([ |
392
|
|
|
'upload' => 'avatar1.pdf', |
393
|
|
|
'upload_multiple' => json_encode(['avatar2.pdf', 'avatar3.pdf']), |
394
|
|
|
]); |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
protected static function initUploader() |
398
|
|
|
{ |
399
|
|
|
Uploader::create([ |
400
|
|
|
'upload' => null, |
401
|
|
|
'upload_multiple' => null, |
402
|
|
|
]); |
403
|
|
|
} |
404
|
|
|
} |
405
|
|
|
|