Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Pull Request — fix-uploaders (#5518)
by Cristian
25:53 queued 10:55
created

test_it_can_update_uploaded_files()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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