1 <?php namespace Tests\Uploads;
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;
10 class ImageTest extends TestCase
15 public function test_image_upload()
17 $page = Page::first();
18 $admin = $this->getAdmin();
19 $this->actingAs($admin);
21 $imgDetails = $this->uploadGalleryImage($page);
22 $relPath = $imgDetails['path'];
24 $this->assertTrue(file_exists(public_path($relPath)), 'Uploaded image found at path: '. public_path($relPath));
26 $this->deleteImage($relPath);
28 $this->assertDatabaseHas('images', [
29 'url' => $this->baseUrl . $relPath,
31 'uploaded_to' => $page->id,
33 'created_by' => $admin->id,
34 'updated_by' => $admin->id,
35 'name' => $imgDetails['name'],
39 public function test_image_display_thumbnail_generation_does_not_increase_image_size()
41 $page = Page::first();
42 $admin = $this->getAdmin();
43 $this->actingAs($admin);
45 $originalFile = $this->getTestImageFilePath('compressed.png');
46 $originalFileSize = filesize($originalFile);
47 $imgDetails = $this->uploadGalleryImage($page, 'compressed.png');
48 $relPath = $imgDetails['path'];
50 $this->assertTrue(file_exists(public_path($relPath)), 'Uploaded image found at path: '. public_path($relPath));
51 $displayImage = $imgDetails['response']->thumbs->display;
53 $displayImageRelPath = implode('/', array_slice(explode('/', $displayImage), 3));
54 $displayImagePath = public_path($displayImageRelPath);
55 $displayFileSize = filesize($displayImagePath);
57 $this->deleteImage($relPath);
58 $this->deleteImage($displayImageRelPath);
60 $this->assertEquals($originalFileSize, $displayFileSize, 'Display thumbnail generation should not increase image size');
63 public function test_image_edit()
65 $editor = $this->getEditor();
66 $this->actingAs($editor);
68 $imgDetails = $this->uploadGalleryImage();
69 $image = Image::query()->first();
71 $newName = Str::random();
72 $update = $this->put('/images/' . $image->id, ['name' => $newName]);
73 $update->assertSuccessful();
80 $this->deleteImage($imgDetails['path']);
82 $this->assertDatabaseHas('images', [
88 public function test_gallery_get_list_format()
92 $imgDetails = $this->uploadGalleryImage();
93 $image = Image::query()->first();
95 $emptyJson = ['images' => [], 'has_more' => false];
100 'name' => $imgDetails['name'],
106 $pageId = $imgDetails['page']->id;
107 $firstPageRequest = $this->get("/images/gallery?page=1&uploaded_to={$pageId}");
108 $firstPageRequest->assertSuccessful()->assertJson($resultJson);
110 $secondPageRequest = $this->get("/images/gallery?page=2&uploaded_to={$pageId}");
111 $secondPageRequest->assertSuccessful()->assertExactJson($emptyJson);
113 $namePartial = substr($imgDetails['name'], 0, 3);
114 $searchHitRequest = $this->get("/images/gallery?page=1&uploaded_to={$pageId}&search={$namePartial}");
115 $searchHitRequest->assertSuccessful()->assertJson($resultJson);
117 $namePartial = Str::random(16);
118 $searchHitRequest = $this->get("/images/gallery?page=1&uploaded_to={$pageId}&search={$namePartial}");
119 $searchHitRequest->assertSuccessful()->assertExactJson($emptyJson);
122 public function test_image_usage()
124 $page = Page::first();
125 $editor = $this->getEditor();
126 $this->actingAs($editor);
128 $imgDetails = $this->uploadGalleryImage($page);
130 $image = Image::query()->first();
131 $page->html = '<img src="'.$image->url.'">';
134 $usage = $this->get('/images/usage/' . $image->id);
135 $usage->assertSuccessful();
139 'name' => $page->name
143 $this->deleteImage($imgDetails['path']);
146 public function test_php_files_cannot_be_uploaded()
148 $page = Page::first();
149 $admin = $this->getAdmin();
150 $this->actingAs($admin);
152 $fileName = 'bad.php';
153 $relPath = $this->getTestImagePath('gallery', $fileName);
154 $this->deleteImage($relPath);
156 $file = $this->getTestImage($fileName);
157 $upload = $this->withHeader('Content-Type', 'image/jpeg')->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $file], []);
158 $upload->assertStatus(302);
160 $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded php file was uploaded but should have been stopped');
162 $this->assertDatabaseMissing('images', [
168 public function test_php_like_files_cannot_be_uploaded()
170 $page = Page::first();
171 $admin = $this->getAdmin();
172 $this->actingAs($admin);
174 $fileName = 'bad.phtml';
175 $relPath = $this->getTestImagePath('gallery', $fileName);
176 $this->deleteImage($relPath);
178 $file = $this->getTestImage($fileName);
179 $upload = $this->withHeader('Content-Type', 'image/jpeg')->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $file], []);
180 $upload->assertStatus(302);
182 $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded php file was uploaded but should have been stopped');
185 public function test_files_with_double_extensions_cannot_be_uploaded()
187 $page = Page::first();
188 $admin = $this->getAdmin();
189 $this->actingAs($admin);
191 $fileName = 'bad.phtml.png';
192 $relPath = $this->getTestImagePath('gallery', $fileName);
193 $this->deleteImage($relPath);
195 $file = $this->getTestImage($fileName);
196 $upload = $this->withHeader('Content-Type', 'image/png')->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $file], []);
197 $upload->assertStatus(302);
199 $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded double extension file was uploaded but should have been stopped');
202 public function test_secure_images_uploads_to_correct_place()
204 config()->set('filesystems.images', 'local_secure');
206 $galleryFile = $this->getTestImage('my-secure-test-upload.png');
207 $page = Page::first();
208 $expectedPath = storage_path('uploads/images/gallery/' . Date('Y-m') . '/my-secure-test-upload.png');
210 $upload = $this->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $galleryFile], []);
211 $upload->assertStatus(200);
213 $this->assertTrue(file_exists($expectedPath), 'Uploaded image not found at path: '. $expectedPath);
215 if (file_exists($expectedPath)) {
216 unlink($expectedPath);
220 public function test_secure_images_included_in_exports()
222 config()->set('filesystems.images', 'local_secure');
224 $galleryFile = $this->getTestImage('my-secure-test-upload.png');
225 $page = Page::first();
226 $expectedPath = storage_path('uploads/images/gallery/' . Date('Y-m') . '/my-secure-test-upload.png');
228 $upload = $this->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $galleryFile], []);
229 $imageUrl = json_decode($upload->getContent(), true)['url'];
230 $page->html .= "<img src=\"{$imageUrl}\">";
232 $upload->assertStatus(200);
234 $encodedImageContent = base64_encode(file_get_contents($expectedPath));
235 $export = $this->get($page->getUrl('/export/html'));
236 $this->assertTrue(strpos($export->getContent(), $encodedImageContent) !== false, 'Uploaded image in export content');
238 if (file_exists($expectedPath)) {
239 unlink($expectedPath);
243 public function test_system_images_remain_public()
245 config()->set('filesystems.images', 'local_secure');
247 $galleryFile = $this->getTestImage('my-system-test-upload.png');
248 $expectedPath = public_path('uploads/images/system/' . Date('Y-m') . '/my-system-test-upload.png');
250 $upload = $this->call('POST', '/settings', [], [], ['app_logo' => $galleryFile], []);
251 $upload->assertRedirect('/settings');
253 $this->assertTrue(file_exists($expectedPath), 'Uploaded image not found at path: '. $expectedPath);
255 if (file_exists($expectedPath)) {
256 unlink($expectedPath);
260 public function test_image_delete()
262 $page = Page::first();
264 $imageName = 'first-image.png';
265 $relPath = $this->getTestImagePath('gallery', $imageName);
266 $this->deleteImage($relPath);
268 $this->uploadImage($imageName, $page->id);
269 $image = Image::first();
271 $delete = $this->delete( '/images/' . $image->id);
272 $delete->assertStatus(200);
274 $this->assertDatabaseMissing('images', [
275 'url' => $this->baseUrl . $relPath,
279 $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded image has not been deleted as expected');
282 public function test_image_delete_does_not_delete_similar_images()
284 $page = Page::first();
286 $imageName = 'first-image.png';
288 $relPath = $this->getTestImagePath('gallery', $imageName);
289 $this->deleteImage($relPath);
291 $this->uploadImage($imageName, $page->id);
292 $this->uploadImage($imageName, $page->id);
293 $this->uploadImage($imageName, $page->id);
295 $image = Image::first();
296 $folder = public_path(dirname($relPath));
297 $imageCount = count(glob($folder . '/*'));
299 $delete = $this->delete( '/images/' . $image->id);
300 $delete->assertStatus(200);
302 $newCount = count(glob($folder . '/*'));
303 $this->assertEquals($imageCount - 1, $newCount, 'More files than expected have been deleted');
304 $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded image has not been deleted as expected');
307 protected function getTestProfileImage()
309 $imageName = 'profile.png';
310 $relPath = $this->getTestImagePath('user', $imageName);
311 $this->deleteImage($relPath);
313 return $this->getTestImage($imageName);
316 public function test_user_image_upload()
318 $editor = $this->getEditor();
319 $admin = $this->getAdmin();
320 $this->actingAs($admin);
322 $file = $this->getTestProfileImage();
323 $this->call('PUT', '/settings/users/' . $editor->id, [], [], ['profile_image' => $file], []);
325 $this->assertDatabaseHas('images', [
327 'uploaded_to' => $editor->id,
328 'created_by' => $admin->id,
332 public function test_user_images_deleted_on_user_deletion()
334 $editor = $this->getEditor();
335 $this->actingAs($editor);
337 $file = $this->getTestProfileImage();
338 $this->call('PUT', '/settings/users/' . $editor->id, [], [], ['profile_image' => $file], []);
340 $profileImages = Image::where('type', '=', 'user')->where('created_by', '=', $editor->id)->get();
341 $this->assertTrue($profileImages->count() === 1, "Found profile images does not match upload count");
343 $imagePath = public_path($profileImages->first()->path);
344 $this->assertTrue(file_exists($imagePath));
346 $userDelete = $this->asAdmin()->delete("/settings/users/{$editor->id}");
347 $userDelete->assertStatus(302);
349 $this->assertDatabaseMissing('images', [
351 'created_by' => $editor->id
353 $this->assertDatabaseMissing('images', [
355 'uploaded_to' => $editor->id
358 $this->assertFalse(file_exists($imagePath));
361 public function test_deleted_unused_images()
363 $page = Page::first();
364 $admin = $this->getAdmin();
365 $this->actingAs($admin);
367 $imageName = 'unused-image.png';
368 $relPath = $this->getTestImagePath('gallery', $imageName);
369 $this->deleteImage($relPath);
371 $upload = $this->uploadImage($imageName, $page->id);
372 $upload->assertStatus(200);
373 $image = Image::where('type', '=', 'gallery')->first();
375 $pageRepo = app(PageRepo::class);
376 $pageRepo->update($page, [
377 'name' => $page->name,
378 'html' => $page->html . "<img src=\"{$image->url}\">",
382 // Ensure no images are reported as deletable
383 $imageService = app(ImageService::class);
384 $toDelete = $imageService->deleteUnusedImages(true, true);
385 $this->assertCount(0, $toDelete);
387 // Save a revision of our page without the image;
388 $pageRepo->update($page, [
389 'name' => $page->name,
390 'html' => "<p>Hello</p>",
394 // Ensure revision images are picked up okay
395 $imageService = app(ImageService::class);
396 $toDelete = $imageService->deleteUnusedImages(true, true);
397 $this->assertCount(0, $toDelete);
398 $toDelete = $imageService->deleteUnusedImages(false, true);
399 $this->assertCount(1, $toDelete);
401 // Check image is found when revisions are destroyed
402 $page->revisions()->delete();
403 $toDelete = $imageService->deleteUnusedImages(true, true);
404 $this->assertCount(1, $toDelete);
406 // Check the image is deleted
407 $absPath = public_path($relPath);
408 $this->assertTrue(file_exists($absPath), "Existing uploaded file at path {$absPath} exists");
409 $toDelete = $imageService->deleteUnusedImages(true, false);
410 $this->assertCount(1, $toDelete);
411 $this->assertFalse(file_exists($absPath));
413 $this->deleteImage($relPath);