1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\Tests\Feature; |
4
|
|
|
|
5
|
|
|
use Backpack\CRUD\Tests\config\CrudPanel\BaseDBCrudPanel; |
6
|
|
|
use Backpack\CRUD\Tests\config\Uploads\HasUploadedFiles; |
7
|
|
|
use Backpack\CRUD\Tests\config\Http\Controllers\UploaderConfigurationCrudController; |
8
|
|
|
use Backpack\CRUD\Tests\config\Models\Uploader; |
9
|
|
|
use Backpack\CRUD\Tests\config\Models\User; |
10
|
|
|
use Illuminate\Support\Facades\Storage; |
11
|
|
|
use PHPUnit\Framework\Attributes\Group; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @covers Backpack\CRUD\app\Library\Uploaders\Uploader |
15
|
|
|
* @covers Backpack\CRUD\app\Library\Uploaders\SingleFile |
16
|
|
|
* @covers Backpack\CRUD\app\Library\Uploaders\MultipleFiles |
17
|
|
|
* @covers Backpack\CRUD\app\Library\Uploaders\Support\RegisterUploadEvents |
18
|
|
|
* @covers Backpack\CRUD\app\Library\Uploaders\Support\UploadersRepository |
19
|
|
|
* @covers Backpack\CRUD\app\Library\Uploaders\Support\FileNameGenerator |
20
|
|
|
*/ |
21
|
|
|
class UploadersConfigurationTest extends BaseDBCrudPanel |
22
|
|
|
{ |
23
|
|
|
use HasUploadedFiles; |
24
|
|
|
|
25
|
|
|
protected string $testBaseUrl; |
26
|
|
|
|
27
|
|
|
protected function defineRoutes($router) |
28
|
|
|
{ |
29
|
|
|
$router->crud(config('backpack.base.route_prefix').'/uploader-configuration', UploaderConfigurationCrudController::class); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
protected function setUp(): void |
33
|
|
|
{ |
34
|
|
|
parent::setUp(); |
35
|
|
|
$this->testBaseUrl = config('backpack.base.route_prefix').'/uploader-configuration'; |
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_using_our_file_name_generator() |
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
|
|
|
foreach ($files as $file) { |
64
|
|
|
$this->assertMatchesRegularExpression('/avatar\d{1}-[a-zA-Z0-9]{4}\.jpg/', $file); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// get the entry from database and also make sure the file names are stored correctly |
68
|
|
|
$entry = Uploader::first(); |
69
|
|
|
$this->assertNotNull($entry); |
70
|
|
|
$this->assertMatchesRegularExpression('/avatar\d{1}-[a-zA-Z0-9]{4}\.jpg/', $entry->upload); |
|
|
|
|
71
|
|
|
$this->assertMatchesRegularExpression('/avatar\d{1}-[a-zA-Z0-9]{4}\.jpg/', $entry->upload_multiple[0]); |
|
|
|
|
72
|
|
|
$this->assertMatchesRegularExpression('/avatar\d{1}-[a-zA-Z0-9]{4}\.jpg/', $entry->upload_multiple[1]); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function test_it_validates_the_file_namer_invalid_string() |
76
|
|
|
{ |
77
|
|
|
$this->expectException(\Exception::class); |
78
|
|
|
|
79
|
|
|
$response = $this->get($this->testBaseUrl.'/invalid-file-namer'); |
80
|
|
|
|
81
|
|
|
$response->assertStatus(500); |
82
|
|
|
|
83
|
|
|
throw $response->exception; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function test_it_validates_the_file_namer_invalid_class() |
87
|
|
|
{ |
88
|
|
|
$this->expectException(\Exception::class); |
89
|
|
|
|
90
|
|
|
$response = $this->get($this->testBaseUrl.'/invalid-file-namer-class'); |
91
|
|
|
|
92
|
|
|
$response->assertStatus(500); |
93
|
|
|
|
94
|
|
|
throw $response->exception; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
#[Group('fail')] |
98
|
|
|
public function test_it_can_use_a_custom_uploader() |
99
|
|
|
{ |
100
|
|
|
$response = $this->post($this->testBaseUrl.'/custom-uploader', [ |
101
|
|
|
'upload' => $this->getUploadedFile('avatar1.jpg'), |
102
|
|
|
]); |
103
|
|
|
|
104
|
|
|
$response->assertStatus(302); |
105
|
|
|
|
106
|
|
|
$response->assertRedirect($this->testBaseUrl); |
107
|
|
|
|
108
|
|
|
$this->assertDatabaseCount('uploaders', 1); |
109
|
|
|
|
110
|
|
|
$files = Storage::disk('uploaders')->allFiles(); |
111
|
|
|
|
112
|
|
|
$this->assertEquals(1, count($files)); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
#[Group('fail')] |
116
|
|
|
public function test_it_validates_the_custom_uploader_class() |
117
|
|
|
{ |
118
|
|
|
$this->expectException(\Exception::class); |
119
|
|
|
|
120
|
|
|
$response = $this->post($this->testBaseUrl.'/custom-invalid-uploader', [ |
121
|
|
|
'upload' => $this->getUploadedFile('avatar1.jpg'), |
122
|
|
|
]); |
123
|
|
|
|
124
|
|
|
$response->assertStatus(500); |
125
|
|
|
|
126
|
|
|
throw $response->exception; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|