|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use Encore\Admin\Auth\Database\Administrator; |
|
4
|
|
|
use Illuminate\Support\Facades\File; |
|
|
|
|
|
|
5
|
|
|
use Tests\Models\Image; |
|
6
|
|
|
use Tests\Models\MultipleImage; |
|
7
|
|
|
|
|
8
|
|
|
class ImageUploadTest extends TestCase |
|
9
|
|
|
{ |
|
10
|
|
|
public function setUp() |
|
11
|
|
|
{ |
|
12
|
|
|
parent::setUp(); |
|
13
|
|
|
|
|
14
|
|
|
$this->be(Administrator::first(), 'admin'); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public function testDisableFilter() |
|
18
|
|
|
{ |
|
19
|
|
|
$this->visit('admin/images') |
|
20
|
|
|
->dontSeeElement('input[name=id]'); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
View Code Duplication |
public function testImageUploadPage() |
|
|
|
|
|
|
24
|
|
|
{ |
|
25
|
|
|
$this->visit('admin/images/create') |
|
26
|
|
|
->see('Upload image') |
|
27
|
|
|
->seeInElement('h3[class=box-title]', 'Create') |
|
28
|
|
|
->seeElement('input[name=image1]') |
|
29
|
|
|
->seeElement('input[name=image2]') |
|
30
|
|
|
->seeElement('input[name=image3]') |
|
31
|
|
|
->seeElement('input[name=image4]') |
|
32
|
|
|
->seeElement('input[name=image5]') |
|
33
|
|
|
->seeElement('input[name=image6]') |
|
34
|
|
|
->seeInElement('button[type=reset]', 'Reset') |
|
35
|
|
|
->seeInElement('button[type=submit]', 'Submit'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
protected function uploadImages() |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->visit('admin/images/create') |
|
41
|
|
|
->attach(__DIR__.'/assets/test.jpg', 'image1') |
|
42
|
|
|
->attach(__DIR__.'/assets/test.jpg', 'image2') |
|
43
|
|
|
->attach(__DIR__.'/assets/test.jpg', 'image3') |
|
44
|
|
|
->attach(__DIR__.'/assets/test.jpg', 'image4') |
|
45
|
|
|
->attach(__DIR__.'/assets/test.jpg', 'image5') |
|
46
|
|
|
->attach(__DIR__.'/assets/test.jpg', 'image6') |
|
47
|
|
|
->press('Submit'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function testUploadImage() |
|
51
|
|
|
{ |
|
52
|
|
|
File::cleanDirectory(public_path('uploads/images')); |
|
53
|
|
|
|
|
54
|
|
|
$this->uploadImages() |
|
55
|
|
|
->seePageIs('admin/images'); |
|
56
|
|
|
|
|
57
|
|
|
$this->assertEquals(Image::count(), 1); |
|
58
|
|
|
|
|
59
|
|
|
$this->seeInDatabase('test_images', ['image4' => 'images/renamed.jpeg']); |
|
60
|
|
|
|
|
61
|
|
|
$images = Image::first()->toArray(); |
|
62
|
|
|
|
|
63
|
|
|
foreach (range(1, 6) as $index) { |
|
64
|
|
|
$this->assertFileExists(public_path('uploads/'.$images['image'.$index])); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$this->assertFileExists(public_path('uploads/images/asdasdasdasdasd.jpeg')); |
|
68
|
|
|
|
|
69
|
|
|
File::cleanDirectory(public_path('uploads/images')); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function testRemoveImage() |
|
73
|
|
|
{ |
|
74
|
|
|
File::cleanDirectory(public_path('uploads/images')); |
|
75
|
|
|
|
|
76
|
|
|
$this->uploadImages(); |
|
77
|
|
|
|
|
78
|
|
|
$this->assertEquals($this->fileCountInImageDir(), 6); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
View Code Duplication |
public function testUpdateImage() |
|
|
|
|
|
|
82
|
|
|
{ |
|
83
|
|
|
File::cleanDirectory(public_path('uploads/images')); |
|
84
|
|
|
|
|
85
|
|
|
$this->uploadImages(); |
|
86
|
|
|
|
|
87
|
|
|
$old = Image::first(); |
|
88
|
|
|
|
|
89
|
|
|
$this->visit('admin/images/1/edit') |
|
90
|
|
|
->see('ID') |
|
91
|
|
|
->see('Created At') |
|
92
|
|
|
->see('Updated At') |
|
93
|
|
|
->seeElement('input[name=image1]') |
|
94
|
|
|
->seeElement('input[name=image2]') |
|
95
|
|
|
->seeElement('input[name=image3]') |
|
96
|
|
|
->seeElement('input[name=image4]') |
|
97
|
|
|
->seeElement('input[name=image5]') |
|
98
|
|
|
->seeElement('input[name=image6]') |
|
99
|
|
|
->seeInElement('button[type=reset]', 'Reset') |
|
100
|
|
|
->seeInElement('button[type=submit]', 'Submit'); |
|
101
|
|
|
|
|
102
|
|
|
$this->attach(__DIR__.'/assets/test.jpg', 'image3') |
|
103
|
|
|
->attach(__DIR__.'/assets/test.jpg', 'image4') |
|
104
|
|
|
->attach(__DIR__.'/assets/test.jpg', 'image5') |
|
105
|
|
|
->press('Submit'); |
|
106
|
|
|
|
|
107
|
|
|
$new = Image::first(); |
|
108
|
|
|
|
|
109
|
|
|
$this->assertEquals($old->id, $new->id); |
|
110
|
|
|
$this->assertEquals($old->image1, $new->image1); |
|
111
|
|
|
$this->assertEquals($old->image2, $new->image2); |
|
112
|
|
|
$this->assertEquals($old->image6, $new->image6); |
|
113
|
|
|
|
|
114
|
|
|
$this->assertNotEquals($old->image3, $new->image3); |
|
115
|
|
|
$this->assertNotEquals($old->image4, $new->image4); |
|
116
|
|
|
$this->assertNotEquals($old->image5, $new->image5); |
|
117
|
|
|
|
|
118
|
|
|
File::cleanDirectory(public_path('uploads/images')); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
View Code Duplication |
public function testDeleteImages() |
|
|
|
|
|
|
122
|
|
|
{ |
|
123
|
|
|
File::cleanDirectory(public_path('uploads/images')); |
|
124
|
|
|
|
|
125
|
|
|
$this->uploadImages(); |
|
126
|
|
|
|
|
127
|
|
|
$this->visit('admin/images') |
|
128
|
|
|
->seeInElement('td', 1); |
|
129
|
|
|
|
|
130
|
|
|
$images = Image::first()->toArray(); |
|
131
|
|
|
|
|
132
|
|
|
$this->delete('admin/images/1') |
|
133
|
|
|
->dontSeeInDatabase('test_images', ['id' => 1]); |
|
134
|
|
|
|
|
135
|
|
|
foreach (range(1, 6) as $index) { |
|
136
|
|
|
$this->assertFileNotExists(public_path('uploads/'.$images['image'.$index])); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
$this->visit('admin/images') |
|
140
|
|
|
->seeInElement('td', 'svg'); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function testBatchDelete() |
|
144
|
|
|
{ |
|
145
|
|
|
File::cleanDirectory(public_path('uploads/images')); |
|
146
|
|
|
|
|
147
|
|
|
$this->uploadImages(); |
|
148
|
|
|
$this->uploadImages(); |
|
149
|
|
|
$this->uploadImages(); |
|
150
|
|
|
|
|
151
|
|
|
$this->visit('admin/images') |
|
152
|
|
|
->seeInElement('td', 1) |
|
153
|
|
|
->seeInElement('td', 2) |
|
154
|
|
|
->seeInElement('td', 3); |
|
155
|
|
|
|
|
156
|
|
|
$this->assertEquals($this->fileCountInImageDir(), 18); |
|
157
|
|
|
|
|
158
|
|
|
$this->assertEquals(Image::count(), 3); |
|
159
|
|
|
|
|
160
|
|
|
$this->delete('admin/images/1,2,3'); |
|
161
|
|
|
|
|
162
|
|
|
$this->assertEquals(Image::count(), 0); |
|
163
|
|
|
|
|
164
|
|
|
$this->visit('admin/images') |
|
165
|
|
|
->seeInElement('td', 'svg'); |
|
166
|
|
|
|
|
167
|
|
|
$this->assertEquals($this->fileCountInImageDir(), 0); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function testUploadMultipleImage() |
|
171
|
|
|
{ |
|
172
|
|
|
File::cleanDirectory(public_path('uploads/images')); |
|
173
|
|
|
|
|
174
|
|
|
$this->visit('admin/multiple-images/create') |
|
175
|
|
|
->seeElement('input[type=file][name="pictures[]"][multiple=1]'); |
|
176
|
|
|
|
|
177
|
|
|
$path = __DIR__.'/assets/test.jpg'; |
|
178
|
|
|
|
|
179
|
|
|
$file = new \Illuminate\Http\UploadedFile( |
|
180
|
|
|
$path, 'test.jpg', 'image/jpeg', filesize($path), null, true |
|
181
|
|
|
); |
|
182
|
|
|
|
|
183
|
|
|
$size = rand(10, 20); |
|
184
|
|
|
$files = ['pictures' => array_pad([], $size, $file)]; |
|
185
|
|
|
|
|
186
|
|
|
$this->call( |
|
187
|
|
|
'POST', // $method |
|
188
|
|
|
'/admin/multiple-images', // $action |
|
189
|
|
|
[], // $parameters |
|
190
|
|
|
[], |
|
191
|
|
|
$files |
|
192
|
|
|
); |
|
193
|
|
|
|
|
194
|
|
|
$this->assertResponseStatus(302); |
|
195
|
|
|
$this->assertRedirectedTo('/admin/multiple-images'); |
|
196
|
|
|
|
|
197
|
|
|
$this->assertEquals($this->fileCountInImageDir(), $size); |
|
198
|
|
|
|
|
199
|
|
|
$pictures = MultipleImage::first()->pictures; |
|
200
|
|
|
|
|
201
|
|
|
$this->assertCount($size, $pictures); |
|
202
|
|
|
|
|
203
|
|
|
foreach ($pictures as $picture) { |
|
204
|
|
|
$this->assertFileExists(public_path('uploads/'.$picture)); |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
public function testRemoveMultipleFiles() |
|
209
|
|
|
{ |
|
210
|
|
|
File::cleanDirectory(public_path('uploads/images')); |
|
211
|
|
|
|
|
212
|
|
|
// upload files |
|
213
|
|
|
$path = __DIR__.'/assets/test.jpg'; |
|
214
|
|
|
|
|
215
|
|
|
$file = new \Illuminate\Http\UploadedFile( |
|
216
|
|
|
$path, 'test.jpg', 'image/jpeg', filesize($path), null, true |
|
217
|
|
|
); |
|
218
|
|
|
|
|
219
|
|
|
$size = rand(10, 20); |
|
220
|
|
|
$files = ['pictures' => array_pad([], $size, $file)]; |
|
221
|
|
|
|
|
222
|
|
|
$this->call( |
|
223
|
|
|
'POST', // $method |
|
224
|
|
|
'/admin/multiple-images', // $action |
|
225
|
|
|
[], // $parameters |
|
226
|
|
|
[], |
|
227
|
|
|
$files |
|
228
|
|
|
); |
|
229
|
|
|
|
|
230
|
|
|
$this->assertEquals($this->fileCountInImageDir(), $size); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
protected function fileCountInImageDir($dir = 'uploads/images') |
|
234
|
|
|
{ |
|
235
|
|
|
$file = new FilesystemIterator(public_path($dir), FilesystemIterator::SKIP_DOTS); |
|
236
|
|
|
|
|
237
|
|
|
return iterator_count($file); |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: