3 namespace Tests\Uploads;
5 use BookStack\Entities\Models\Page;
6 use BookStack\Entities\Repos\PageRepo;
7 use BookStack\Uploads\Image;
8 use BookStack\Uploads\ImageService;
9 use Illuminate\Support\Str;
12 class ImageTest extends TestCase
16 public function test_image_upload()
18 $page = Page::query()->first();
19 $admin = $this->getAdmin();
20 $this->actingAs($admin);
22 $imgDetails = $this->uploadGalleryImage($page);
23 $relPath = $imgDetails['path'];
25 $this->assertTrue(file_exists(public_path($relPath)), 'Uploaded image found at path: ' . public_path($relPath));
27 $this->deleteImage($relPath);
29 $this->assertDatabaseHas('images', [
30 'url' => $this->baseUrl . $relPath,
32 'uploaded_to' => $page->id,
34 'created_by' => $admin->id,
35 'updated_by' => $admin->id,
36 'name' => $imgDetails['name'],
40 public function test_image_display_thumbnail_generation_does_not_increase_image_size()
42 $page = Page::query()->first();
43 $admin = $this->getAdmin();
44 $this->actingAs($admin);
46 $originalFile = $this->getTestImageFilePath('compressed.png');
47 $originalFileSize = filesize($originalFile);
48 $imgDetails = $this->uploadGalleryImage($page, 'compressed.png');
49 $relPath = $imgDetails['path'];
51 $this->assertTrue(file_exists(public_path($relPath)), 'Uploaded image found at path: ' . public_path($relPath));
52 $displayImage = $imgDetails['response']->thumbs->display;
54 $displayImageRelPath = implode('/', array_slice(explode('/', $displayImage), 3));
55 $displayImagePath = public_path($displayImageRelPath);
56 $displayFileSize = filesize($displayImagePath);
58 $this->deleteImage($relPath);
59 $this->deleteImage($displayImageRelPath);
61 $this->assertEquals($originalFileSize, $displayFileSize, 'Display thumbnail generation should not increase image size');
64 public function test_image_edit()
66 $editor = $this->getEditor();
67 $this->actingAs($editor);
69 $imgDetails = $this->uploadGalleryImage();
70 $image = Image::query()->first();
72 $newName = Str::random();
73 $update = $this->put('/images/' . $image->id, ['name' => $newName]);
74 $update->assertSuccessful();
75 $update->assertSee($newName);
77 $this->deleteImage($imgDetails['path']);
79 $this->assertDatabaseHas('images', [
85 public function test_gallery_get_list_format()
89 $imgDetails = $this->uploadGalleryImage();
90 $image = Image::query()->first();
92 $pageId = $imgDetails['page']->id;
93 $firstPageRequest = $this->get("/images/gallery?page=1&uploaded_to={$pageId}");
94 $firstPageRequest->assertSuccessful()->assertElementExists('div');
95 $firstPageRequest->assertSuccessful()->assertSeeText($image->name);
97 $secondPageRequest = $this->get("/images/gallery?page=2&uploaded_to={$pageId}");
98 $secondPageRequest->assertSuccessful()->assertElementNotExists('div');
100 $namePartial = substr($imgDetails['name'], 0, 3);
101 $searchHitRequest = $this->get("/images/gallery?page=1&uploaded_to={$pageId}&search={$namePartial}");
102 $searchHitRequest->assertSuccessful()->assertSee($imgDetails['name']);
104 $namePartial = Str::random(16);
105 $searchFailRequest = $this->get("/images/gallery?page=1&uploaded_to={$pageId}&search={$namePartial}");
106 $searchFailRequest->assertSuccessful()->assertDontSee($imgDetails['name']);
107 $searchFailRequest->assertSuccessful()->assertElementNotExists('div');
110 public function test_image_usage()
112 $page = Page::query()->first();
113 $editor = $this->getEditor();
114 $this->actingAs($editor);
116 $imgDetails = $this->uploadGalleryImage($page);
118 $image = Image::query()->first();
119 $page->html = '<img src="' . $image->url . '">';
122 $usage = $this->get('/images/edit/' . $image->id . '?delete=true');
123 $usage->assertSuccessful();
124 $usage->assertSeeText($page->name);
125 $usage->assertSee($page->getUrl());
127 $this->deleteImage($imgDetails['path']);
130 public function test_php_files_cannot_be_uploaded()
132 $page = Page::query()->first();
133 $admin = $this->getAdmin();
134 $this->actingAs($admin);
136 $fileName = 'bad.php';
137 $relPath = $this->getTestImagePath('gallery', $fileName);
138 $this->deleteImage($relPath);
140 $file = $this->newTestImageFromBase64('bad-php.base64', $fileName);
141 $upload = $this->withHeader('Content-Type', 'image/jpeg')->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $file], []);
142 $upload->assertStatus(302);
144 $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded php file was uploaded but should have been stopped');
146 $this->assertDatabaseMissing('images', [
152 public function test_php_like_files_cannot_be_uploaded()
154 $page = Page::query()->first();
155 $admin = $this->getAdmin();
156 $this->actingAs($admin);
158 $fileName = 'bad.phtml';
159 $relPath = $this->getTestImagePath('gallery', $fileName);
160 $this->deleteImage($relPath);
162 $file = $this->newTestImageFromBase64('bad-phtml.base64', $fileName);
163 $upload = $this->withHeader('Content-Type', 'image/jpeg')->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $file], []);
164 $upload->assertStatus(302);
166 $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded php file was uploaded but should have been stopped');
169 public function test_files_with_double_extensions_will_get_sanitized()
171 $page = Page::query()->first();
172 $admin = $this->getAdmin();
173 $this->actingAs($admin);
175 $fileName = 'bad.phtml.png';
176 $relPath = $this->getTestImagePath('gallery', $fileName);
177 $expectedRelPath = dirname($relPath) . '/bad-phtml.png';
178 $this->deleteImage($expectedRelPath);
180 $file = $this->newTestImageFromBase64('bad-phtml-png.base64', $fileName);
181 $upload = $this->withHeader('Content-Type', 'image/png')->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $file], []);
182 $upload->assertStatus(200);
184 $lastImage = Image::query()->latest('id')->first();
186 $this->assertEquals('bad.phtml.png', $lastImage->name);
187 $this->assertEquals('bad-phtml.png', basename($lastImage->path));
188 $this->assertFileDoesNotExist(public_path($relPath), 'Uploaded image file name was not stripped of dots');
189 $this->assertFileExists(public_path($expectedRelPath));
191 $this->deleteImage($lastImage->path);
194 public function test_url_entities_removed_from_filenames()
198 'bad-char-#-image.png',
199 'bad-char-?-image.png',
204 foreach ($badNames as $name) {
205 $galleryFile = $this->getTestImage($name);
206 $page = Page::query()->first();
207 $badPath = $this->getTestImagePath('gallery', $name);
208 $this->deleteImage($badPath);
210 $upload = $this->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $galleryFile], []);
211 $upload->assertStatus(200);
213 $lastImage = Image::query()->latest('id')->first();
214 $newFileName = explode('.', basename($lastImage->path))[0];
216 $this->assertEquals($lastImage->name, $name);
217 $this->assertFalse(strpos($lastImage->path, $name), 'Path contains original image name');
218 $this->assertFalse(file_exists(public_path($badPath)), 'Uploaded image file name was not stripped of url entities');
220 $this->assertTrue(strlen($newFileName) > 0, 'File name was reduced to nothing');
222 $this->deleteImage($lastImage->path);
226 public function test_secure_images_uploads_to_correct_place()
228 config()->set('filesystems.images', 'local_secure');
230 $galleryFile = $this->getTestImage('my-secure-test-upload.png');
231 $page = Page::query()->first();
232 $expectedPath = storage_path('uploads/images/gallery/' . date('Y-m') . '/my-secure-test-upload.png');
234 $upload = $this->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $galleryFile], []);
235 $upload->assertStatus(200);
237 $this->assertTrue(file_exists($expectedPath), 'Uploaded image not found at path: ' . $expectedPath);
239 if (file_exists($expectedPath)) {
240 unlink($expectedPath);
244 public function test_secure_images_included_in_exports()
246 config()->set('filesystems.images', 'local_secure');
248 $galleryFile = $this->getTestImage('my-secure-test-upload.png');
249 $page = Page::query()->first();
250 $expectedPath = storage_path('uploads/images/gallery/' . date('Y-m') . '/my-secure-test-upload.png');
252 $upload = $this->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $galleryFile], []);
253 $imageUrl = json_decode($upload->getContent(), true)['url'];
254 $page->html .= "<img src=\"{$imageUrl}\">";
256 $upload->assertStatus(200);
258 $encodedImageContent = base64_encode(file_get_contents($expectedPath));
259 $export = $this->get($page->getUrl('/export/html'));
260 $this->assertTrue(strpos($export->getContent(), $encodedImageContent) !== false, 'Uploaded image in export content');
262 if (file_exists($expectedPath)) {
263 unlink($expectedPath);
267 public function test_system_images_remain_public()
269 config()->set('filesystems.images', 'local_secure');
271 $galleryFile = $this->getTestImage('my-system-test-upload.png');
272 $expectedPath = public_path('uploads/images/system/' . date('Y-m') . '/my-system-test-upload.png');
274 $upload = $this->call('POST', '/settings', [], [], ['app_logo' => $galleryFile], []);
275 $upload->assertRedirect('/settings');
277 $this->assertTrue(file_exists($expectedPath), 'Uploaded image not found at path: ' . $expectedPath);
279 if (file_exists($expectedPath)) {
280 unlink($expectedPath);
284 public function test_image_delete()
286 $page = Page::query()->first();
288 $imageName = 'first-image.png';
289 $relPath = $this->getTestImagePath('gallery', $imageName);
290 $this->deleteImage($relPath);
292 $this->uploadImage($imageName, $page->id);
293 $image = Image::first();
295 $delete = $this->delete('/images/' . $image->id);
296 $delete->assertStatus(200);
298 $this->assertDatabaseMissing('images', [
299 'url' => $this->baseUrl . $relPath,
303 $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded image has not been deleted as expected');
306 public function test_image_delete_does_not_delete_similar_images()
308 $page = Page::query()->first();
310 $imageName = 'first-image.png';
312 $relPath = $this->getTestImagePath('gallery', $imageName);
313 $this->deleteImage($relPath);
315 $this->uploadImage($imageName, $page->id);
316 $this->uploadImage($imageName, $page->id);
317 $this->uploadImage($imageName, $page->id);
319 $image = Image::first();
320 $folder = public_path(dirname($relPath));
321 $imageCount = count(glob($folder . '/*'));
323 $delete = $this->delete('/images/' . $image->id);
324 $delete->assertStatus(200);
326 $newCount = count(glob($folder . '/*'));
327 $this->assertEquals($imageCount - 1, $newCount, 'More files than expected have been deleted');
328 $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded image has not been deleted as expected');
331 protected function getTestProfileImage()
333 $imageName = 'profile.png';
334 $relPath = $this->getTestImagePath('user', $imageName);
335 $this->deleteImage($relPath);
337 return $this->getTestImage($imageName);
340 public function test_user_image_upload()
342 $editor = $this->getEditor();
343 $admin = $this->getAdmin();
344 $this->actingAs($admin);
346 $file = $this->getTestProfileImage();
347 $this->call('PUT', '/settings/users/' . $editor->id, [], [], ['profile_image' => $file], []);
349 $this->assertDatabaseHas('images', [
351 'uploaded_to' => $editor->id,
352 'created_by' => $admin->id,
356 public function test_user_images_deleted_on_user_deletion()
358 $editor = $this->getEditor();
359 $this->actingAs($editor);
361 $file = $this->getTestProfileImage();
362 $this->call('PUT', '/settings/users/' . $editor->id, [], [], ['profile_image' => $file], []);
364 $profileImages = Image::where('type', '=', 'user')->where('created_by', '=', $editor->id)->get();
365 $this->assertTrue($profileImages->count() === 1, 'Found profile images does not match upload count');
367 $imagePath = public_path($profileImages->first()->path);
368 $this->assertTrue(file_exists($imagePath));
370 $userDelete = $this->asAdmin()->delete("/settings/users/{$editor->id}");
371 $userDelete->assertStatus(302);
373 $this->assertDatabaseMissing('images', [
375 'created_by' => $editor->id,
377 $this->assertDatabaseMissing('images', [
379 'uploaded_to' => $editor->id,
382 $this->assertFalse(file_exists($imagePath));
385 public function test_deleted_unused_images()
387 $page = Page::query()->first();
388 $admin = $this->getAdmin();
389 $this->actingAs($admin);
391 $imageName = 'unused-image.png';
392 $relPath = $this->getTestImagePath('gallery', $imageName);
393 $this->deleteImage($relPath);
395 $upload = $this->uploadImage($imageName, $page->id);
396 $upload->assertStatus(200);
397 $image = Image::where('type', '=', 'gallery')->first();
399 $pageRepo = app(PageRepo::class);
400 $pageRepo->update($page, [
401 'name' => $page->name,
402 'html' => $page->html . "<img src=\"{$image->url}\">",
406 // Ensure no images are reported as deletable
407 $imageService = app(ImageService::class);
408 $toDelete = $imageService->deleteUnusedImages(true, true);
409 $this->assertCount(0, $toDelete);
411 // Save a revision of our page without the image;
412 $pageRepo->update($page, [
413 'name' => $page->name,
414 'html' => '<p>Hello</p>',
418 // Ensure revision images are picked up okay
419 $imageService = app(ImageService::class);
420 $toDelete = $imageService->deleteUnusedImages(true, true);
421 $this->assertCount(0, $toDelete);
422 $toDelete = $imageService->deleteUnusedImages(false, true);
423 $this->assertCount(1, $toDelete);
425 // Check image is found when revisions are destroyed
426 $page->revisions()->delete();
427 $toDelete = $imageService->deleteUnusedImages(true, true);
428 $this->assertCount(1, $toDelete);
430 // Check the image is deleted
431 $absPath = public_path($relPath);
432 $this->assertTrue(file_exists($absPath), "Existing uploaded file at path {$absPath} exists");
433 $toDelete = $imageService->deleteUnusedImages(true, false);
434 $this->assertCount(1, $toDelete);
435 $this->assertFalse(file_exists($absPath));
437 $this->deleteImage($relPath);