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\Feature; |
||
4 | |||
5 | use Backpack\CRUD\Tests\config\CrudPanel\BaseDBCrudPanel; |
||
6 | use Backpack\CRUD\Tests\config\Http\Controllers\UploaderConfigurationCrudController; |
||
7 | use Backpack\CRUD\Tests\config\Models\Uploader; |
||
8 | use Backpack\CRUD\Tests\config\Models\User; |
||
9 | use Backpack\CRUD\Tests\config\Uploads\HasUploadedFiles; |
||
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 | * @covers Backpack\CRUD\app\Library\Uploaders\Support\FileNameGenerator |
||
19 | */ |
||
20 | class UploadersConfigurationTest extends BaseDBCrudPanel |
||
21 | { |
||
22 | use HasUploadedFiles; |
||
23 | |||
24 | protected string $testBaseUrl; |
||
25 | |||
26 | protected function defineRoutes($router) |
||
27 | { |
||
28 | $router->crud(config('backpack.base.route_prefix').'/uploader-configuration', UploaderConfigurationCrudController::class); |
||
29 | } |
||
30 | |||
31 | protected function setUp(): void |
||
32 | { |
||
33 | parent::setUp(); |
||
34 | $this->testBaseUrl = config('backpack.base.route_prefix').'/uploader-configuration'; |
||
35 | Storage::fake('uploaders'); |
||
36 | $this->actingAs(User::find(1)); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
37 | } |
||
38 | |||
39 | public function test_it_can_access_the_uploaders_create_page() |
||
40 | { |
||
41 | $response = $this->get($this->testBaseUrl.'/create'); |
||
42 | $response->assertStatus(200); |
||
43 | } |
||
44 | |||
45 | public function test_it_can_store_uploaded_files_using_our_file_name_generator() |
||
46 | { |
||
47 | $response = $this->post($this->testBaseUrl, [ |
||
48 | 'upload' => $this->getUploadedFile('avatar1.jpg'), |
||
49 | 'upload_multiple' => $this->getUploadedFiles(['avatar2.jpg', 'avatar3.jpg']), |
||
50 | ]); |
||
51 | |||
52 | $response->assertStatus(302); |
||
53 | |||
54 | $response->assertRedirect($this->testBaseUrl); |
||
55 | |||
56 | $this->assertDatabaseCount('uploaders', 1); |
||
57 | |||
58 | $files = Storage::disk('uploaders')->allFiles(); |
||
59 | |||
60 | $this->assertEquals(3, count($files)); |
||
61 | |||
62 | foreach ($files as $file) { |
||
63 | $this->assertMatchesRegularExpression('/avatar\d{1}-[a-zA-Z0-9]{4}\.jpg/', $file); |
||
64 | } |
||
65 | |||
66 | // get the entry from database and also make sure the file names are stored correctly |
||
67 | $entry = Uploader::first(); |
||
68 | $this->assertNotNull($entry); |
||
69 | $this->assertMatchesRegularExpression('/avatar\d{1}-[a-zA-Z0-9]{4}\.jpg/', $entry->upload); |
||
0 ignored issues
–
show
|
|||
70 | $this->assertMatchesRegularExpression('/avatar\d{1}-[a-zA-Z0-9]{4}\.jpg/', $entry->upload_multiple[0]); |
||
0 ignored issues
–
show
|
|||
71 | $this->assertMatchesRegularExpression('/avatar\d{1}-[a-zA-Z0-9]{4}\.jpg/', $entry->upload_multiple[1]); |
||
72 | } |
||
73 | |||
74 | public function test_it_validates_the_file_namer_invalid_string() |
||
75 | { |
||
76 | $this->expectException(\Exception::class); |
||
77 | |||
78 | $response = $this->get($this->testBaseUrl.'/invalid-file-namer'); |
||
79 | |||
80 | $response->assertStatus(500); |
||
81 | |||
82 | throw $response->exception; |
||
83 | } |
||
84 | |||
85 | public function test_it_validates_the_file_namer_invalid_class() |
||
86 | { |
||
87 | $this->expectException(\Exception::class); |
||
88 | |||
89 | $response = $this->get($this->testBaseUrl.'/invalid-file-namer-class'); |
||
90 | |||
91 | $response->assertStatus(500); |
||
92 | |||
93 | throw $response->exception; |
||
94 | } |
||
95 | |||
96 | public function test_it_can_use_a_custom_uploader() |
||
97 | { |
||
98 | $response = $this->post($this->testBaseUrl.'/custom-uploader', [ |
||
99 | 'upload' => $this->getUploadedFile('avatar1.jpg'), |
||
100 | ]); |
||
101 | |||
102 | $response->assertStatus(302); |
||
103 | |||
104 | $response->assertRedirect($this->testBaseUrl); |
||
105 | |||
106 | $this->assertDatabaseCount('uploaders', 1); |
||
107 | |||
108 | $files = Storage::disk('uploaders')->allFiles(); |
||
109 | |||
110 | $this->assertEquals(1, count($files)); |
||
111 | } |
||
112 | |||
113 | public function test_it_validates_the_custom_uploader_class() |
||
114 | { |
||
115 | $this->expectException(\Exception::class); |
||
116 | |||
117 | $response = $this->post($this->testBaseUrl.'/custom-invalid-uploader', [ |
||
118 | 'upload' => $this->getUploadedFile('avatar1.jpg'), |
||
119 | ]); |
||
120 | |||
121 | $response->assertStatus(500); |
||
122 | |||
123 | throw $response->exception; |
||
124 | } |
||
125 | } |
||
126 |