]> BookStack Code Mirror - bookstack/blob - tests/Uploads/ImageTest.php
Converted image-manager to be component/HTML based
[bookstack] / tests / Uploads / ImageTest.php
1 <?php namespace Tests\Uploads;
2
3 use BookStack\Entities\Repos\PageRepo;
4 use BookStack\Uploads\Image;
5 use BookStack\Entities\Page;
6 use BookStack\Uploads\ImageService;
7 use Illuminate\Support\Str;
8 use Tests\TestCase;
9
10 class ImageTest extends TestCase
11 {
12
13     use UsesImages;
14
15     public function test_image_upload()
16     {
17         $page = Page::first();
18         $admin = $this->getAdmin();
19         $this->actingAs($admin);
20
21         $imgDetails = $this->uploadGalleryImage($page);
22         $relPath = $imgDetails['path'];
23
24         $this->assertTrue(file_exists(public_path($relPath)), 'Uploaded image found at path: '. public_path($relPath));
25
26         $this->deleteImage($relPath);
27
28         $this->assertDatabaseHas('images', [
29             'url' => $this->baseUrl . $relPath,
30             'type' => 'gallery',
31             'uploaded_to' => $page->id,
32             'path' => $relPath,
33             'created_by' => $admin->id,
34             'updated_by' => $admin->id,
35             'name' => $imgDetails['name'],
36         ]);
37     }
38
39     public function test_image_display_thumbnail_generation_does_not_increase_image_size()
40     {
41         $page = Page::first();
42         $admin = $this->getAdmin();
43         $this->actingAs($admin);
44
45         $originalFile = $this->getTestImageFilePath('compressed.png');
46         $originalFileSize = filesize($originalFile);
47         $imgDetails = $this->uploadGalleryImage($page, 'compressed.png');
48         $relPath = $imgDetails['path'];
49
50         $this->assertTrue(file_exists(public_path($relPath)), 'Uploaded image found at path: '. public_path($relPath));
51         $displayImage = $imgDetails['response']->thumbs->display;
52
53         $displayImageRelPath = implode('/', array_slice(explode('/', $displayImage), 3));
54         $displayImagePath = public_path($displayImageRelPath);
55         $displayFileSize = filesize($displayImagePath);
56
57         $this->deleteImage($relPath);
58         $this->deleteImage($displayImageRelPath);
59
60         $this->assertEquals($originalFileSize, $displayFileSize, 'Display thumbnail generation should not increase image size');
61     }
62
63     public function test_image_edit()
64     {
65         $editor = $this->getEditor();
66         $this->actingAs($editor);
67
68         $imgDetails = $this->uploadGalleryImage();
69         $image = Image::query()->first();
70
71         $newName = Str::random();
72         $update = $this->put('/images/' . $image->id, ['name' => $newName]);
73         $update->assertSuccessful();
74         $update->assertSee($newName);
75
76         $this->deleteImage($imgDetails['path']);
77
78         $this->assertDatabaseHas('images', [
79             'type' => 'gallery',
80             'name' => $newName
81         ]);
82     }
83
84     public function test_gallery_get_list_format()
85     {
86         $this->asEditor();
87
88         $imgDetails = $this->uploadGalleryImage();
89         $image = Image::query()->first();
90
91         $pageId = $imgDetails['page']->id;
92         $firstPageRequest = $this->get("/images/gallery?page=1&uploaded_to={$pageId}");
93         $firstPageRequest->assertSuccessful()->assertElementExists('div');
94         $firstPageRequest->assertSuccessful()->assertSeeText($image->name);
95
96         $secondPageRequest = $this->get("/images/gallery?page=2&uploaded_to={$pageId}");
97         $secondPageRequest->assertSuccessful()->assertElementNotExists('div');
98
99         $namePartial = substr($imgDetails['name'], 0, 3);
100         $searchHitRequest = $this->get("/images/gallery?page=1&uploaded_to={$pageId}&search={$namePartial}");
101         $searchHitRequest->assertSuccessful()->assertSee($imgDetails['name']);
102
103         $namePartial = Str::random(16);
104         $searchFailRequest = $this->get("/images/gallery?page=1&uploaded_to={$pageId}&search={$namePartial}");
105         $searchFailRequest->assertSuccessful()->assertDontSee($imgDetails['name']);
106         $searchFailRequest->assertSuccessful()->assertElementNotExists('div');
107     }
108
109     public function test_image_usage()
110     {
111         $page = Page::first();
112         $editor = $this->getEditor();
113         $this->actingAs($editor);
114
115         $imgDetails = $this->uploadGalleryImage($page);
116
117         $image = Image::query()->first();
118         $page->html = '<img src="'.$image->url.'">';
119         $page->save();
120
121         $usage = $this->get('/images/edit/' . $image->id . '?delete=true');
122         $usage->assertSuccessful();
123         $usage->assertSeeText($page->name);
124         $usage->assertSee($page->getUrl());
125
126         $this->deleteImage($imgDetails['path']);
127     }
128
129     public function test_php_files_cannot_be_uploaded()
130     {
131         $page = Page::first();
132         $admin = $this->getAdmin();
133         $this->actingAs($admin);
134
135         $fileName = 'bad.php';
136         $relPath = $this->getTestImagePath('gallery', $fileName);
137         $this->deleteImage($relPath);
138
139         $file = $this->getTestImage($fileName);
140         $upload = $this->withHeader('Content-Type', 'image/jpeg')->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $file], []);
141         $upload->assertStatus(302);
142
143         $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded php file was uploaded but should have been stopped');
144
145         $this->assertDatabaseMissing('images', [
146             'type' => 'gallery',
147             'name' => $fileName
148         ]);
149     }
150
151     public function test_php_like_files_cannot_be_uploaded()
152     {
153         $page = Page::first();
154         $admin = $this->getAdmin();
155         $this->actingAs($admin);
156
157         $fileName = 'bad.phtml';
158         $relPath = $this->getTestImagePath('gallery', $fileName);
159         $this->deleteImage($relPath);
160
161         $file = $this->getTestImage($fileName);
162         $upload = $this->withHeader('Content-Type', 'image/jpeg')->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $file], []);
163         $upload->assertStatus(302);
164
165         $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded php file was uploaded but should have been stopped');
166     }
167
168     public function test_files_with_double_extensions_cannot_be_uploaded()
169     {
170         $page = Page::first();
171         $admin = $this->getAdmin();
172         $this->actingAs($admin);
173
174         $fileName = 'bad.phtml.png';
175         $relPath = $this->getTestImagePath('gallery', $fileName);
176         $this->deleteImage($relPath);
177
178         $file = $this->getTestImage($fileName);
179         $upload = $this->withHeader('Content-Type', 'image/png')->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $file], []);
180         $upload->assertStatus(302);
181
182         $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded double extension file was uploaded but should have been stopped');
183     }
184
185     public function test_secure_images_uploads_to_correct_place()
186     {
187         config()->set('filesystems.images', 'local_secure');
188         $this->asEditor();
189         $galleryFile = $this->getTestImage('my-secure-test-upload.png');
190         $page = Page::first();
191         $expectedPath = storage_path('uploads/images/gallery/' . Date('Y-m') . '/my-secure-test-upload.png');
192
193         $upload = $this->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $galleryFile], []);
194         $upload->assertStatus(200);
195
196         $this->assertTrue(file_exists($expectedPath), 'Uploaded image not found at path: '. $expectedPath);
197
198         if (file_exists($expectedPath)) {
199             unlink($expectedPath);
200         }
201     }
202
203     public function test_secure_images_included_in_exports()
204     {
205         config()->set('filesystems.images', 'local_secure');
206         $this->asEditor();
207         $galleryFile = $this->getTestImage('my-secure-test-upload.png');
208         $page = Page::first();
209         $expectedPath = storage_path('uploads/images/gallery/' . Date('Y-m') . '/my-secure-test-upload.png');
210
211         $upload = $this->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $galleryFile], []);
212         $imageUrl = json_decode($upload->getContent(), true)['url'];
213         $page->html .= "<img src=\"{$imageUrl}\">";
214         $page->save();
215         $upload->assertStatus(200);
216
217         $encodedImageContent = base64_encode(file_get_contents($expectedPath));
218         $export = $this->get($page->getUrl('/export/html'));
219         $this->assertTrue(strpos($export->getContent(), $encodedImageContent) !== false, 'Uploaded image in export content');
220
221         if (file_exists($expectedPath)) {
222             unlink($expectedPath);
223         }
224     }
225
226     public function test_system_images_remain_public()
227     {
228         config()->set('filesystems.images', 'local_secure');
229         $this->asAdmin();
230         $galleryFile = $this->getTestImage('my-system-test-upload.png');
231         $expectedPath = public_path('uploads/images/system/' . Date('Y-m') . '/my-system-test-upload.png');
232
233         $upload = $this->call('POST', '/settings', [], [], ['app_logo' => $galleryFile], []);
234         $upload->assertRedirect('/settings');
235
236         $this->assertTrue(file_exists($expectedPath), 'Uploaded image not found at path: '. $expectedPath);
237
238         if (file_exists($expectedPath)) {
239             unlink($expectedPath);
240         }
241     }
242
243     public function test_image_delete()
244     {
245         $page = Page::first();
246         $this->asAdmin();
247         $imageName = 'first-image.png';
248         $relPath = $this->getTestImagePath('gallery', $imageName);
249         $this->deleteImage($relPath);
250
251         $this->uploadImage($imageName, $page->id);
252         $image = Image::first();
253
254         $delete = $this->delete( '/images/' . $image->id);
255         $delete->assertStatus(200);
256
257         $this->assertDatabaseMissing('images', [
258             'url' => $this->baseUrl . $relPath,
259             'type' => 'gallery'
260         ]);
261
262         $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded image has not been deleted as expected');
263     }
264
265     public function test_image_delete_does_not_delete_similar_images()
266     {
267         $page = Page::first();
268         $this->asAdmin();
269         $imageName = 'first-image.png';
270
271         $relPath = $this->getTestImagePath('gallery', $imageName);
272         $this->deleteImage($relPath);
273
274         $this->uploadImage($imageName, $page->id);
275         $this->uploadImage($imageName, $page->id);
276         $this->uploadImage($imageName, $page->id);
277
278         $image = Image::first();
279         $folder = public_path(dirname($relPath));
280         $imageCount = count(glob($folder . '/*'));
281
282         $delete = $this->delete( '/images/' . $image->id);
283         $delete->assertStatus(200);
284
285         $newCount = count(glob($folder . '/*'));
286         $this->assertEquals($imageCount - 1, $newCount, 'More files than expected have been deleted');
287         $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded image has not been deleted as expected');
288     }
289
290     protected function getTestProfileImage()
291     {
292         $imageName = 'profile.png';
293         $relPath = $this->getTestImagePath('user', $imageName);
294         $this->deleteImage($relPath);
295
296         return $this->getTestImage($imageName);
297     }
298
299     public function test_user_image_upload()
300     {
301         $editor = $this->getEditor();
302         $admin = $this->getAdmin();
303         $this->actingAs($admin);
304
305         $file = $this->getTestProfileImage();
306         $this->call('PUT', '/settings/users/' . $editor->id, [], [], ['profile_image' => $file], []);
307
308         $this->assertDatabaseHas('images', [
309             'type' => 'user',
310             'uploaded_to' => $editor->id,
311             'created_by' => $admin->id,
312         ]);
313     }
314
315     public function test_user_images_deleted_on_user_deletion()
316     {
317         $editor = $this->getEditor();
318         $this->actingAs($editor);
319
320         $file = $this->getTestProfileImage();
321         $this->call('PUT', '/settings/users/' . $editor->id, [], [], ['profile_image' => $file], []);
322
323         $profileImages = Image::where('type', '=', 'user')->where('created_by', '=', $editor->id)->get();
324         $this->assertTrue($profileImages->count() === 1, "Found profile images does not match upload count");
325
326         $imagePath = public_path($profileImages->first()->path);
327         $this->assertTrue(file_exists($imagePath));
328
329         $userDelete = $this->asAdmin()->delete("/settings/users/{$editor->id}");
330         $userDelete->assertStatus(302);
331
332         $this->assertDatabaseMissing('images', [
333             'type' => 'user',
334             'created_by' => $editor->id
335         ]);
336         $this->assertDatabaseMissing('images', [
337             'type' => 'user',
338             'uploaded_to' => $editor->id
339         ]);
340
341         $this->assertFalse(file_exists($imagePath));
342     }
343
344     public function test_deleted_unused_images()
345     {
346         $page = Page::first();
347         $admin = $this->getAdmin();
348         $this->actingAs($admin);
349
350         $imageName = 'unused-image.png';
351         $relPath = $this->getTestImagePath('gallery', $imageName);
352         $this->deleteImage($relPath);
353
354         $upload = $this->uploadImage($imageName, $page->id);
355         $upload->assertStatus(200);
356         $image = Image::where('type', '=', 'gallery')->first();
357
358         $pageRepo = app(PageRepo::class);
359         $pageRepo->update($page, [
360             'name' => $page->name,
361             'html' => $page->html . "<img src=\"{$image->url}\">",
362             'summary' => ''
363         ]);
364
365         // Ensure no images are reported as deletable
366         $imageService = app(ImageService::class);
367         $toDelete = $imageService->deleteUnusedImages(true, true);
368         $this->assertCount(0, $toDelete);
369
370         // Save a revision of our page without the image;
371         $pageRepo->update($page, [
372             'name' => $page->name,
373             'html' => "<p>Hello</p>",
374             'summary' => ''
375         ]);
376
377         // Ensure revision images are picked up okay
378         $imageService = app(ImageService::class);
379         $toDelete = $imageService->deleteUnusedImages(true, true);
380         $this->assertCount(0, $toDelete);
381         $toDelete = $imageService->deleteUnusedImages(false, true);
382         $this->assertCount(1, $toDelete);
383
384         // Check image is found when revisions are destroyed
385         $page->revisions()->delete();
386         $toDelete = $imageService->deleteUnusedImages(true, true);
387         $this->assertCount(1, $toDelete);
388
389         // Check the image is deleted
390         $absPath = public_path($relPath);
391         $this->assertTrue(file_exists($absPath), "Existing uploaded file at path {$absPath} exists");
392         $toDelete = $imageService->deleteUnusedImages(true, false);
393         $this->assertCount(1, $toDelete);
394         $this->assertFalse(file_exists($absPath));
395
396         $this->deleteImage($relPath);
397     }
398
399 }