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 Pedro
41:44 queued 27:03
created

UploadersTest::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 Tests\Feature;
4
5
use Backpack\CRUD\Tests\config\CrudPanel\BaseDBCrudPanel;
6
use Backpack\CRUD\Tests\config\Http\Controllers\UploaderCrudController;
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
12
/**
13
 * @covers Backpack\CRUD\app\Library\Uploaders\Uploader
14
 * @covers Backpack\CRUD\app\Library\Uploaders\SingleFile
15
 * @covers Backpack\CRUD\app\Library\Uploaders\MultipleFiles
16
 * @covers Backpack\CRUD\app\Library\Uploaders\Support\RegisterUploadEvents
17
 * @covers Backpack\CRUD\app\Library\Uploaders\Support\UploadersRepository
18
 */
19
class UploadersTest extends BaseDBCrudPanel
20
{
21
    private string $testBaseUrl;
22
23
    protected function defineRoutes($router)
24
    {
25
        $this->testBaseUrl = config('backpack.base.route_prefix').'/uploader';
26
        $router->crud($this->testBaseUrl, UploaderCrudController::class);
27
    }
28
29
    protected function setUp(): void
30
    {
31
        parent::setUp();
32
33
        Storage::fake('uploaders');
34
        $this->actingAs(User::find(1));
35
    }
36
37
    public function test_it_can_access_the_uploaders_create_page()
38
    {
39
        $response = $this->get($this->testBaseUrl.'/create');
40
        $response->assertStatus(200);
41
    }
42
43
    public function test_it_can_store_uploaded_files()
44
    {
45
        $response = $this->post(config('backpack.base.route_prefix').'/uploader', [
46
            'upload' => UploadedFile::fake()->image('avatar.jpg'),
47
            'upload_multiple' => [UploadedFile::fake()->image('avatar1.jpg'), UploadedFile::fake()->image('avatar2.jpg')],
48
        ]);
49
50
        $response->assertStatus(302);
51
52
        $response->assertRedirect(config('backpack.base.route_prefix').'/uploader');
53
54
        $this->assertDatabaseCount('uploaders', 1);
55
56
        $files = Storage::disk('uploaders')->allFiles();
57
58
        $this->assertEquals(3, count($files));
59
60
        $this->assertDatabaseHas('uploaders', [
61
            'upload' => 'avatar.jpg',
62
            'upload_multiple' => json_encode(['avatar1.jpg',  'avatar2.jpg']),
63
        ]);
64
65
        $this->assertTrue(Storage::disk('uploaders')->exists('avatar.jpg'));
66
        $this->assertTrue(Storage::disk('uploaders')->exists('avatar1.jpg'));
67
        $this->assertTrue(Storage::disk('uploaders')->exists('avatar2.jpg'));
68
    }
69
70
    public function test_it_display_the_edit_page_without_files()
71
    {
72
        self::initUploader();
73
74
        $response = $this->get($this->testBaseUrl.'/1/edit');
75
        $response->assertStatus(200);
76
    }
77
78
    public function test_it_display_the_upload_page_with_files()
79
    {
80
        self::initUploaderWithFiles();
81
        $response = $this->get($this->testBaseUrl.'/1/edit');
82
83
        $response->assertStatus(200);
84
85
        $response->assertSee('avatar1.jpg');
86
        $response->assertSee('avatar2.jpg');
87
        $response->assertSee('avatar3.jpg');
88
    }
89
90
    public function test_it_can_update_uploaded_files()
91
    {
92
        self::initUploaderWithFiles();
93
94
        $response = $this->put($this->testBaseUrl.'/1', [
95
            'upload' => UploadedFile::fake()->image('avatar4.jpg'),
96
            'upload_multiple' => [UploadedFile::fake()->image('avatar5.jpg'), UploadedFile::fake()->image('avatar6.jpg')],
97
            'clear_upload_multiple' => ['avatar2.jpg',  'avatar3.jpg'],
98
            'id' => 1,
99
        ]);
100
101
        $response->assertStatus(302);
102
103
        $response->assertRedirect($this->testBaseUrl);
104
105
        $this->assertDatabaseCount('uploaders', 1);
106
107
        $this->assertDatabaseHas('uploaders', [
108
            'upload' => 'avatar4.jpg',
109
            'upload_multiple' => json_encode(['avatar5.jpg',  'avatar6.jpg']),
110
        ]);
111
112
        $files = Storage::disk('uploaders')->allFiles();
113
114
        $this->assertEquals(3, count($files));
115
116
        $this->assertTrue(Storage::disk('uploaders')->exists('avatar4.jpg'));
117
        $this->assertTrue(Storage::disk('uploaders')->exists('avatar5.jpg'));
118
        $this->assertTrue(Storage::disk('uploaders')->exists('avatar6.jpg'));
119
    }
120
121
    public function test_single_upload_deletes_files_when_updated_without_values()
122
    {
123
        self::initUploaderWithFiles();
124
125
        $response = $this->put($this->testBaseUrl.'/1', [
126
            'upload' => null,
127
            'id' => 1,
128
        ]);
129
130
        $response->assertStatus(302);
131
132
        $response->assertRedirect(config('backpack.base.route_prefix').'/uploader');
133
134
        $this->assertDatabaseCount('uploaders', 1);
135
136
        $this->assertDatabaseHas('uploaders', [
137
            'upload' => null,
138
            'upload_multiple' => json_encode(['avatar2.jpg',  'avatar3.jpg']),
139
        ]);
140
141
        $files = Storage::disk('uploaders')->allFiles();
142
143
        $this->assertEquals(2, count($files));
144
145
        $this->assertFalse(Storage::disk('uploaders')->exists('avatar1.jpg'));
146
    }
147
148
    public function test_it_can_delete_uploaded_files()
149
    {
150
        self::initUploaderWithFiles();
151
152
        $response = $this->delete($this->testBaseUrl.'/1');
153
154
        $response->assertStatus(200);
155
156
        $this->assertDatabaseCount('uploaders', 0);
157
158
        $files = Storage::disk('uploaders')->allFiles();
159
160
        $this->assertEquals(0, count($files));
161
    }
162
163
    public function test_it_keeps_previous_values_unchaged_when_not_deleted()
164
    {
165
        self::initUploaderWithFiles();
166
167
        $response = $this->put($this->testBaseUrl.'/1', [
168
            'upload_multiple' => ['avatar2.jpg',  'avatar3.jpg'],
169
            'id' => 1,
170
        ]);
171
172
        $response->assertStatus(302);
173
174
        $response->assertRedirect(config('backpack.base.route_prefix').'/uploader');
175
176
        $this->assertDatabaseCount('uploaders', 1);
177
178
        $this->assertDatabaseHas('uploaders', [
179
            'upload' => 'avatar1.jpg',
180
            'upload_multiple' => json_encode(['avatar2.jpg',  'avatar3.jpg']),
181
        ]);
182
183
        $files = Storage::disk('uploaders')->allFiles();
184
185
        $this->assertEquals(3, count($files));
186
187
        $this->assertTrue(Storage::disk('uploaders')->exists('avatar1.jpg'));
188
        $this->assertTrue(Storage::disk('uploaders')->exists('avatar2.jpg'));
189
        $this->assertTrue(Storage::disk('uploaders')->exists('avatar3.jpg'));
190
    }
191
192
    public function test_upload_multiple_can_delete_uploaded_files_and_add_at_the_same_time()
193
    {
194
        self::initUploaderWithFiles();
195
196
        $response = $this->put($this->testBaseUrl.'/1', [
197
            'upload_multiple' => [UploadedFile::fake()->image('avatar4.jpg'), UploadedFile::fake()->image('avatar5.jpg')],
198
            'clear_upload_multiple' => ['avatar2.jpg'],
199
            'id' => 1,
200
        ]);
201
202
        $response->assertStatus(302);
203
204
        $response->assertRedirect(config('backpack.base.route_prefix').'/uploader');
205
206
        $this->assertDatabaseCount('uploaders', 1);
207
208
        $this->assertDatabaseHas('uploaders', [
209
            'upload' => 'avatar1.jpg',
210
            'upload_multiple' => json_encode(['avatar3.jpg', 'avatar4.jpg',  'avatar5.jpg']),
211
        ]);
212
213
        $files = Storage::disk('uploaders')->allFiles();
214
215
        $this->assertEquals(4, count($files));
216
217
        $this->assertTrue(Storage::disk('uploaders')->exists('avatar1.jpg'));
218
        $this->assertTrue(Storage::disk('uploaders')->exists('avatar3.jpg'));
219
        $this->assertTrue(Storage::disk('uploaders')->exists('avatar4.jpg'));
220
        $this->assertTrue(Storage::disk('uploaders')->exists('avatar5.jpg'));
221
    }
222
223
    protected static function initUploaderWithFiles()
224
    {
225
        UploadedFile::fake()->image('avatar1.jpg')->storeAs('', 'avatar1.jpg', ['disk' => 'uploaders']);
226
        UploadedFile::fake()->image('avatar2.jpg')->storeAs('', 'avatar2.jpg', ['disk' => 'uploaders']);
227
        UploadedFile::fake()->image('avatar3.jpg')->storeAs('', 'avatar3.jpg', ['disk' => 'uploaders']);
228
229
        Uploader::create([
230
            'upload' => 'avatar1.jpg',
231
            'upload_multiple' => json_encode(['avatar2.jpg',  'avatar3.jpg']),
232
        ]);
233
    }
234
235
    protected static function initUploader()
236
    {
237
        Uploader::create([
238
            'upload' => null,
239
            'upload_multiple' => null,
240
        ]);
241
    }
242
}
243