3 namespace Tests\Uploads;
5 use BookStack\Entities\Repos\PageRepo;
6 use BookStack\Uploads\Image;
7 use BookStack\Uploads\ImageService;
8 use BookStack\Uploads\UserAvatars;
9 use BookStack\Users\Models\Role;
10 use Illuminate\Support\Str;
13 class ImageTest extends TestCase
15 public function test_image_upload()
17 $page = $this->entities->page();
18 $admin = $this->users->admin();
19 $this->actingAs($admin);
21 $imgDetails = $this->files->uploadGalleryImageToPage($this, $page);
22 $relPath = $imgDetails['path'];
24 $this->assertTrue(file_exists(public_path($relPath)), 'Uploaded image found at path: ' . public_path($relPath));
26 $this->files->deleteAtRelativePath($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 = $this->entities->page();
42 $admin = $this->users->admin();
43 $this->actingAs($admin);
45 $originalFile = $this->files->testFilePath('compressed.png');
46 $originalFileSize = filesize($originalFile);
47 $imgDetails = $this->files->uploadGalleryImageToPage($this, $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->files->deleteAtRelativePath($relPath);
58 $this->files->deleteAtRelativePath($displayImageRelPath);
60 $this->assertEquals($originalFileSize, $displayFileSize, 'Display thumbnail generation should not increase image size');
63 public function test_image_display_thumbnail_generation_for_apng_images_uses_original_file()
65 $page = $this->entities->page();
66 $admin = $this->users->admin();
67 $this->actingAs($admin);
69 $imgDetails = $this->files->uploadGalleryImageToPage($this, $page, 'animated.png');
70 $this->files->deleteAtRelativePath($imgDetails['path']);
72 $this->assertStringContainsString('thumbs-', $imgDetails['response']->thumbs->gallery);
73 $this->assertStringNotContainsString('scaled-', $imgDetails['response']->thumbs->display);
76 public function test_image_display_thumbnail_generation_for_animated_avif_images_uses_original_file()
78 if (! function_exists('imageavif')) {
79 $this->markTestSkipped('imageavif() is not available');
82 $page = $this->entities->page();
83 $admin = $this->users->admin();
84 $this->actingAs($admin);
86 $imgDetails = $this->files->uploadGalleryImageToPage($this, $page, 'animated.avif');
87 $this->files->deleteAtRelativePath($imgDetails['path']);
89 $this->assertStringContainsString('thumbs-', $imgDetails['response']->thumbs->gallery);
90 $this->assertStringNotContainsString('scaled-', $imgDetails['response']->thumbs->display);
93 public function test_image_edit()
95 $editor = $this->users->editor();
96 $this->actingAs($editor);
98 $imgDetails = $this->files->uploadGalleryImageToPage($this, $this->entities->page());
99 $image = Image::query()->first();
101 $newName = Str::random();
102 $update = $this->put('/images/' . $image->id, ['name' => $newName]);
103 $update->assertSuccessful();
104 $update->assertSee($newName);
106 $this->files->deleteAtRelativePath($imgDetails['path']);
108 $this->assertDatabaseHas('images', [
114 public function test_image_file_update()
116 $page = $this->entities->page();
119 $imgDetails = $this->files->uploadGalleryImageToPage($this, $page);
120 $relPath = $imgDetails['path'];
122 $newUpload = $this->files->uploadedImage('updated-image.png', 'compressed.png');
123 $this->assertFileEquals($this->files->testFilePath('test-image.png'), public_path($relPath));
125 $imageId = $imgDetails['response']->id;
126 $image = Image::findOrFail($imageId);
127 $image->updated_at = now()->subMonth();
130 $this->call('PUT', "/images/{$imageId}/file", [], [], ['file' => $newUpload])
133 $this->assertFileEquals($this->files->testFilePath('compressed.png'), public_path($relPath));
136 $this->assertTrue($image->updated_at->gt(now()->subMinute()));
138 $this->files->deleteAtRelativePath($relPath);
141 public function test_image_file_update_allows_case_differences()
143 $page = $this->entities->page();
146 $imgDetails = $this->files->uploadGalleryImageToPage($this, $page);
147 $relPath = $imgDetails['path'];
149 $newUpload = $this->files->uploadedImage('updated-image.PNG', 'compressed.png');
150 $this->assertFileEquals($this->files->testFilePath('test-image.png'), public_path($relPath));
152 $imageId = $imgDetails['response']->id;
153 $image = Image::findOrFail($imageId);
154 $image->updated_at = now()->subMonth();
157 $this->call('PUT', "/images/{$imageId}/file", [], [], ['file' => $newUpload])
160 $this->assertFileEquals($this->files->testFilePath('compressed.png'), public_path($relPath));
163 $this->assertTrue($image->updated_at->gt(now()->subMinute()));
165 $this->files->deleteAtRelativePath($relPath);
168 public function test_image_file_update_does_not_allow_change_in_image_extension()
170 $page = $this->entities->page();
173 $imgDetails = $this->files->uploadGalleryImageToPage($this, $page);
174 $relPath = $imgDetails['path'];
175 $newUpload = $this->files->uploadedImage('updated-image.jpg', 'compressed.png');
177 $imageId = $imgDetails['response']->id;
178 $this->call('PUT', "/images/{$imageId}/file", [], [], ['file' => $newUpload])
180 "message" => "Image file replacements must be of the same type",
184 $this->files->deleteAtRelativePath($relPath);
187 public function test_gallery_get_list_format()
191 $imgDetails = $this->files->uploadGalleryImageToPage($this, $this->entities->page());
192 $image = Image::query()->first();
194 $pageId = $imgDetails['page']->id;
195 $firstPageRequest = $this->get("/images/gallery?page=1&uploaded_to={$pageId}");
196 $firstPageRequest->assertSuccessful();
197 $this->withHtml($firstPageRequest)->assertElementExists('div');
198 $firstPageRequest->assertSuccessful()->assertSeeText($image->name);
200 $secondPageRequest = $this->get("/images/gallery?page=2&uploaded_to={$pageId}");
201 $secondPageRequest->assertSuccessful();
202 $this->withHtml($secondPageRequest)->assertElementNotExists('div');
204 $namePartial = substr($imgDetails['name'], 0, 3);
205 $searchHitRequest = $this->get("/images/gallery?page=1&uploaded_to={$pageId}&search={$namePartial}");
206 $searchHitRequest->assertSuccessful()->assertSee($imgDetails['name']);
208 $namePartial = Str::random(16);
209 $searchFailRequest = $this->get("/images/gallery?page=1&uploaded_to={$pageId}&search={$namePartial}");
210 $searchFailRequest->assertSuccessful()->assertDontSee($imgDetails['name']);
211 $searchFailRequest->assertSuccessful();
212 $this->withHtml($searchFailRequest)->assertElementNotExists('div');
215 public function test_image_gallery_lists_for_draft_page()
217 $this->actingAs($this->users->editor());
218 $draft = $this->entities->newDraftPage();
219 $this->files->uploadGalleryImageToPage($this, $draft);
220 $image = Image::query()->where('uploaded_to', '=', $draft->id)->firstOrFail();
222 $resp = $this->get("/images/gallery?page=1&uploaded_to={$draft->id}");
223 $resp->assertSee($image->getThumb(150, 150));
226 public function test_image_usage()
228 $page = $this->entities->page();
229 $editor = $this->users->editor();
230 $this->actingAs($editor);
232 $imgDetails = $this->files->uploadGalleryImageToPage($this, $page);
234 $image = Image::query()->first();
235 $page->html = '<img src="' . $image->url . '">';
238 $usage = $this->get('/images/edit/' . $image->id . '?delete=true');
239 $usage->assertSuccessful();
240 $usage->assertSeeText($page->name);
241 $usage->assertSee($page->getUrl());
243 $this->files->deleteAtRelativePath($imgDetails['path']);
246 public function test_php_files_cannot_be_uploaded()
248 $page = $this->entities->page();
249 $admin = $this->users->admin();
250 $this->actingAs($admin);
252 $fileName = 'bad.php';
253 $relPath = $this->files->expectedImagePath('gallery', $fileName);
254 $this->files->deleteAtRelativePath($relPath);
256 $file = $this->files->imageFromBase64File('bad-php.base64', $fileName);
257 $upload = $this->withHeader('Content-Type', 'image/jpeg')->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $file], []);
258 $upload->assertStatus(500);
259 $this->assertStringContainsString('The file must have a valid & supported image extension', $upload->json('message'));
261 $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded php file was uploaded but should have been stopped');
263 $this->assertDatabaseMissing('images', [
269 public function test_php_like_files_cannot_be_uploaded()
271 $page = $this->entities->page();
272 $admin = $this->users->admin();
273 $this->actingAs($admin);
275 $fileName = 'bad.phtml';
276 $relPath = $this->files->expectedImagePath('gallery', $fileName);
277 $this->files->deleteAtRelativePath($relPath);
279 $file = $this->files->imageFromBase64File('bad-phtml.base64', $fileName);
280 $upload = $this->withHeader('Content-Type', 'image/jpeg')->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $file], []);
281 $upload->assertStatus(500);
282 $this->assertStringContainsString('The file must have a valid & supported image extension', $upload->json('message'));
284 $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded php file was uploaded but should have been stopped');
287 public function test_files_with_double_extensions_will_get_sanitized()
289 $page = $this->entities->page();
290 $admin = $this->users->admin();
291 $this->actingAs($admin);
293 $fileName = 'bad.phtml.png';
294 $relPath = $this->files->expectedImagePath('gallery', $fileName);
295 $expectedRelPath = dirname($relPath) . '/bad-phtml.png';
296 $this->files->deleteAtRelativePath($expectedRelPath);
298 $file = $this->files->imageFromBase64File('bad-phtml-png.base64', $fileName);
299 $upload = $this->withHeader('Content-Type', 'image/png')->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $file], []);
300 $upload->assertStatus(200);
302 $lastImage = Image::query()->latest('id')->first();
304 $this->assertEquals('bad.phtml.png', $lastImage->name);
305 $this->assertEquals('bad-phtml.png', basename($lastImage->path));
306 $this->assertFileDoesNotExist(public_path($relPath), 'Uploaded image file name was not stripped of dots');
307 $this->assertFileExists(public_path($expectedRelPath));
309 $this->files->deleteAtRelativePath($lastImage->path);
312 public function test_url_entities_removed_from_filenames()
316 'bad-char-#-image.png',
317 'bad-char-?-image.png',
322 foreach ($badNames as $name) {
323 $galleryFile = $this->files->uploadedImage($name);
324 $page = $this->entities->page();
325 $badPath = $this->files->expectedImagePath('gallery', $name);
326 $this->files->deleteAtRelativePath($badPath);
328 $upload = $this->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $galleryFile], []);
329 $upload->assertStatus(200);
331 $lastImage = Image::query()->latest('id')->first();
332 $newFileName = explode('.', basename($lastImage->path))[0];
334 $this->assertEquals($lastImage->name, $name);
335 $this->assertFalse(strpos($lastImage->path, $name), 'Path contains original image name');
336 $this->assertFalse(file_exists(public_path($badPath)), 'Uploaded image file name was not stripped of url entities');
338 $this->assertTrue(strlen($newFileName) > 0, 'File name was reduced to nothing');
340 $this->files->deleteAtRelativePath($lastImage->path);
344 public function test_secure_images_uploads_to_correct_place()
346 config()->set('filesystems.images', 'local_secure');
348 $galleryFile = $this->files->uploadedImage('my-secure-test-upload.png');
349 $page = $this->entities->page();
350 $expectedPath = storage_path('uploads/images/gallery/' . date('Y-m') . '/my-secure-test-upload.png');
352 $upload = $this->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $galleryFile], []);
353 $upload->assertStatus(200);
355 $this->assertTrue(file_exists($expectedPath), 'Uploaded image not found at path: ' . $expectedPath);
357 if (file_exists($expectedPath)) {
358 unlink($expectedPath);
362 public function test_secure_image_paths_traversal_causes_500()
364 config()->set('filesystems.images', 'local_secure');
367 $resp = $this->get('/uploads/images/../../logs/laravel.log');
368 $resp->assertStatus(500);
371 public function test_secure_image_paths_traversal_on_non_secure_images_causes_404()
373 config()->set('filesystems.images', 'local');
376 $resp = $this->get('/uploads/images/../../logs/laravel.log');
377 $resp->assertStatus(404);
380 public function test_secure_image_paths_dont_serve_non_images()
382 config()->set('filesystems.images', 'local_secure');
385 $testFilePath = storage_path('/uploads/images/testing.txt');
386 file_put_contents($testFilePath, 'hello from test_secure_image_paths_dont_serve_non_images');
388 $resp = $this->get('/uploads/images/testing.txt');
389 $resp->assertStatus(404);
392 public function test_secure_images_included_in_exports()
394 config()->set('filesystems.images', 'local_secure');
396 $galleryFile = $this->files->uploadedImage('my-secure-test-upload.png');
397 $page = $this->entities->page();
398 $expectedPath = storage_path('uploads/images/gallery/' . date('Y-m') . '/my-secure-test-upload.png');
400 $upload = $this->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $galleryFile], []);
401 $imageUrl = json_decode($upload->getContent(), true)['url'];
402 $page->html .= "<img src=\"{$imageUrl}\">";
404 $upload->assertStatus(200);
406 $encodedImageContent = base64_encode(file_get_contents($expectedPath));
407 $export = $this->get($page->getUrl('/export/html'));
408 $this->assertTrue(strpos($export->getContent(), $encodedImageContent) !== false, 'Uploaded image in export content');
410 if (file_exists($expectedPath)) {
411 unlink($expectedPath);
415 public function test_system_images_remain_public_with_local_secure()
417 config()->set('filesystems.images', 'local_secure');
419 $galleryFile = $this->files->uploadedImage('my-system-test-upload.png');
420 $expectedPath = public_path('uploads/images/system/' . date('Y-m') . '/my-system-test-upload.png');
422 $upload = $this->call('POST', '/settings/customization', [], [], ['app_logo' => $galleryFile], []);
423 $upload->assertRedirect('/settings/customization');
425 $this->assertTrue(file_exists($expectedPath), 'Uploaded image not found at path: ' . $expectedPath);
427 if (file_exists($expectedPath)) {
428 unlink($expectedPath);
432 public function test_secure_images_not_tracked_in_session_history()
434 config()->set('filesystems.images', 'local_secure');
436 $page = $this->entities->page();
437 $result = $this->files->uploadGalleryImageToPage($this, $page);
438 $expectedPath = storage_path($result['path']);
439 $this->assertFileExists($expectedPath);
441 $this->get('/books');
442 $this->assertEquals(url('/books'), session()->previousUrl());
444 $resp = $this->get($result['path']);
446 $resp->assertHeader('Content-Type', 'image/png');
448 $this->assertEquals(url('/books'), session()->previousUrl());
450 if (file_exists($expectedPath)) {
451 unlink($expectedPath);
455 public function test_system_images_remain_public_with_local_secure_restricted()
457 config()->set('filesystems.images', 'local_secure_restricted');
459 $galleryFile = $this->files->uploadedImage('my-system-test-restricted-upload.png');
460 $expectedPath = public_path('uploads/images/system/' . date('Y-m') . '/my-system-test-restricted-upload.png');
462 $upload = $this->call('POST', '/settings/customization', [], [], ['app_logo' => $galleryFile], []);
463 $upload->assertRedirect('/settings/customization');
465 $this->assertTrue(file_exists($expectedPath), 'Uploaded image not found at path: ' . $expectedPath);
467 if (file_exists($expectedPath)) {
468 unlink($expectedPath);
472 public function test_avatar_images_visible_only_when_public_access_enabled_with_local_secure_restricted()
474 config()->set('filesystems.images', 'local_secure_restricted');
475 $user = $this->users->admin();
476 $avatars = $this->app->make(UserAvatars::class);
477 $avatars->assignToUserFromExistingData($user, $this->files->pngImageData(), 'png');
479 $avatarUrl = $user->getAvatar();
481 $resp = $this->get($avatarUrl);
482 $resp->assertRedirect('/login');
484 $this->permissions->makeAppPublic();
486 $resp = $this->get($avatarUrl);
489 $this->files->deleteAtRelativePath($user->avatar->path);
492 public function test_secure_restricted_images_inaccessible_without_relation_permission()
494 config()->set('filesystems.images', 'local_secure_restricted');
496 $galleryFile = $this->files->uploadedImage('my-secure-restricted-test-upload.png');
497 $page = $this->entities->page();
499 $upload = $this->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $galleryFile], []);
500 $upload->assertStatus(200);
501 $expectedUrl = url('uploads/images/gallery/' . date('Y-m') . '/my-secure-restricted-test-upload.png');
502 $expectedPath = storage_path('uploads/images/gallery/' . date('Y-m') . '/my-secure-restricted-test-upload.png');
504 $this->get($expectedUrl)->assertOk();
506 $this->permissions->setEntityPermissions($page, [], []);
508 $resp = $this->get($expectedUrl);
509 $resp->assertNotFound();
511 if (file_exists($expectedPath)) {
512 unlink($expectedPath);
516 public function test_secure_restricted_images_accessible_with_public_guest_access()
518 config()->set('filesystems.images', 'local_secure_restricted');
519 $this->permissions->makeAppPublic();
522 $page = $this->entities->page();
523 $this->files->uploadGalleryImageToPage($this, $page);
524 $image = Image::query()->where('type', '=', 'gallery')
525 ->where('uploaded_to', '=', $page->id)
528 $expectedUrl = url($image->path);
529 $expectedPath = storage_path($image->path);
532 $this->get($expectedUrl)->assertOk();
534 $this->permissions->setEntityPermissions($page, [], []);
536 $resp = $this->get($expectedUrl);
537 $resp->assertNotFound();
539 $this->permissions->setEntityPermissions($page, ['view'], [Role::getSystemRole('public')]);
541 $this->get($expectedUrl)->assertOk();
543 if (file_exists($expectedPath)) {
544 unlink($expectedPath);
548 public function test_thumbnail_path_handled_by_secure_restricted_images()
550 config()->set('filesystems.images', 'local_secure_restricted');
552 $galleryFile = $this->files->uploadedImage('my-secure-restricted-thumb-test-test.png');
553 $page = $this->entities->page();
555 $upload = $this->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $galleryFile], []);
556 $upload->assertStatus(200);
557 $expectedUrl = url('uploads/images/gallery/' . date('Y-m') . '/thumbs-150-150/my-secure-restricted-thumb-test-test.png');
558 $expectedPath = storage_path('uploads/images/gallery/' . date('Y-m') . '/my-secure-restricted-thumb-test-test.png');
560 $this->get($expectedUrl)->assertOk();
562 $this->permissions->setEntityPermissions($page, [], []);
564 $resp = $this->get($expectedUrl);
565 $resp->assertNotFound();
567 if (file_exists($expectedPath)) {
568 unlink($expectedPath);
572 public function test_secure_restricted_image_access_controlled_in_exports()
574 config()->set('filesystems.images', 'local_secure_restricted');
576 $galleryFile = $this->files->uploadedImage('my-secure-restricted-export-test.png');
578 $pageA = $this->entities->page();
579 $pageB = $this->entities->page();
580 $expectedPath = storage_path('uploads/images/gallery/' . date('Y-m') . '/my-secure-restricted-export-test.png');
582 $upload = $this->asEditor()->call('POST', '/images/gallery', ['uploaded_to' => $pageA->id], [], ['file' => $galleryFile], []);
585 $imageUrl = json_decode($upload->getContent(), true)['url'];
586 $pageB->html .= "<img src=\"{$imageUrl}\">";
589 $encodedImageContent = base64_encode(file_get_contents($expectedPath));
590 $export = $this->get($pageB->getUrl('/export/html'));
591 $this->assertStringContainsString($encodedImageContent, $export->getContent());
593 $this->permissions->setEntityPermissions($pageA, [], []);
595 $export = $this->get($pageB->getUrl('/export/html'));
596 $this->assertStringNotContainsString($encodedImageContent, $export->getContent());
598 if (file_exists($expectedPath)) {
599 unlink($expectedPath);
603 public function test_image_delete()
605 $page = $this->entities->page();
607 $imageName = 'first-image.png';
608 $relPath = $this->files->expectedImagePath('gallery', $imageName);
609 $this->files->deleteAtRelativePath($relPath);
611 $this->files->uploadGalleryImage($this, $imageName, $page->id);
612 $image = Image::first();
614 $delete = $this->delete('/images/' . $image->id);
615 $delete->assertStatus(200);
617 $this->assertDatabaseMissing('images', [
618 'url' => $this->baseUrl . $relPath,
622 $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded image has not been deleted as expected');
625 public function test_image_delete_does_not_delete_similar_images()
627 $page = $this->entities->page();
629 $imageName = 'first-image.png';
631 $relPath = $this->files->expectedImagePath('gallery', $imageName);
632 $this->files->deleteAtRelativePath($relPath);
634 $this->files->uploadGalleryImage($this, $imageName, $page->id);
635 $this->files->uploadGalleryImage($this, $imageName, $page->id);
636 $this->files->uploadGalleryImage($this, $imageName, $page->id);
638 $image = Image::first();
639 $folder = public_path(dirname($relPath));
640 $imageCount = count(glob($folder . '/*'));
642 $delete = $this->delete('/images/' . $image->id);
643 $delete->assertStatus(200);
645 $newCount = count(glob($folder . '/*'));
646 $this->assertEquals($imageCount - 1, $newCount, 'More files than expected have been deleted');
647 $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded image has not been deleted as expected');
650 public function test_image_manager_delete_button_only_shows_with_permission()
652 $page = $this->entities->page();
654 $imageName = 'first-image.png';
655 $relPath = $this->files->expectedImagePath('gallery', $imageName);
656 $this->files->deleteAtRelativePath($relPath);
657 $viewer = $this->users->viewer();
659 $this->files->uploadGalleryImage($this, $imageName, $page->id);
660 $image = Image::first();
662 $resp = $this->get("/images/edit/{$image->id}");
663 $this->withHtml($resp)->assertElementExists('button#image-manager-delete');
665 $resp = $this->actingAs($viewer)->get("/images/edit/{$image->id}");
666 $this->withHtml($resp)->assertElementNotExists('button#image-manager-delete');
668 $this->permissions->grantUserRolePermissions($viewer, ['image-delete-all']);
670 $resp = $this->actingAs($viewer)->get("/images/edit/{$image->id}");
671 $this->withHtml($resp)->assertElementExists('button#image-manager-delete');
673 $this->files->deleteAtRelativePath($relPath);
676 public function test_image_manager_regen_thumbnails()
679 $imageName = 'first-image.png';
680 $relPath = $this->files->expectedImagePath('gallery', $imageName);
681 $this->files->deleteAtRelativePath($relPath);
683 $this->files->uploadGalleryImage($this, $imageName, $this->entities->page()->id);
684 $image = Image::first();
686 $resp = $this->get("/images/edit/{$image->id}");
687 $this->withHtml($resp)->assertElementExists('button#image-manager-rebuild-thumbs');
689 $expectedThumbPath = dirname($relPath) . '/scaled-1680-/' . basename($relPath);
690 $this->files->deleteAtRelativePath($expectedThumbPath);
691 $this->assertFileDoesNotExist($this->files->relativeToFullPath($expectedThumbPath));
693 $resp = $this->put("/images/{$image->id}/rebuild-thumbnails");
696 $this->assertFileExists($this->files->relativeToFullPath($expectedThumbPath));
697 $this->files->deleteAtRelativePath($relPath);
700 public function test_gif_thumbnail_generation()
703 $originalFile = $this->files->testFilePath('animated.gif');
704 $originalFileSize = filesize($originalFile);
706 $imgDetails = $this->files->uploadGalleryImageToPage($this, $this->entities->page(), 'animated.gif');
707 $relPath = $imgDetails['path'];
709 $this->assertTrue(file_exists(public_path($relPath)), 'Uploaded image found at path: ' . public_path($relPath));
710 $galleryThumb = $imgDetails['response']->thumbs->gallery;
711 $displayThumb = $imgDetails['response']->thumbs->display;
713 // Ensure display thumbnail is original image
714 $this->assertStringEndsWith($imgDetails['path'], $displayThumb);
715 $this->assertStringNotContainsString('thumbs', $displayThumb);
717 // Ensure gallery thumbnail is reduced image (single frame)
718 $galleryThumbRelPath = implode('/', array_slice(explode('/', $galleryThumb), 3));
719 $galleryThumbPath = public_path($galleryThumbRelPath);
720 $galleryFileSize = filesize($galleryThumbPath);
722 // Basic scan of GIF content to check frame count
723 $originalFrameCount = count(explode("\x00\x21\xF9", file_get_contents($originalFile))) - 1;
724 $galleryFrameCount = count(explode("\x00\x21\xF9", file_get_contents($galleryThumbPath))) - 1;
726 $this->files->deleteAtRelativePath($relPath);
727 $this->files->deleteAtRelativePath($galleryThumbRelPath);
729 $this->assertNotEquals($originalFileSize, $galleryFileSize);
730 $this->assertEquals(2, $originalFrameCount);
731 $this->assertLessThan(2, $galleryFrameCount);
734 protected function getTestProfileImage()
736 $imageName = 'profile.png';
737 $relPath = $this->files->expectedImagePath('user', $imageName);
738 $this->files->deleteAtRelativePath($relPath);
740 return $this->files->uploadedImage($imageName);
743 public function test_user_image_upload()
745 $editor = $this->users->editor();
746 $admin = $this->users->admin();
747 $this->actingAs($admin);
749 $file = $this->getTestProfileImage();
750 $this->call('PUT', '/settings/users/' . $editor->id, [], [], ['profile_image' => $file], []);
752 $this->assertDatabaseHas('images', [
754 'uploaded_to' => $editor->id,
755 'created_by' => $admin->id,
759 public function test_user_images_deleted_on_user_deletion()
761 $editor = $this->users->editor();
762 $this->actingAs($editor);
764 $file = $this->getTestProfileImage();
765 $this->call('PUT', '/my-account/profile', [], [], ['profile_image' => $file], []);
767 $profileImages = Image::where('type', '=', 'user')->where('created_by', '=', $editor->id)->get();
768 $this->assertTrue($profileImages->count() === 1, 'Found profile images does not match upload count');
770 $imagePath = public_path($profileImages->first()->path);
771 $this->assertTrue(file_exists($imagePath));
773 $userDelete = $this->asAdmin()->delete($editor->getEditUrl());
774 $userDelete->assertStatus(302);
776 $this->assertDatabaseMissing('images', [
778 'created_by' => $editor->id,
780 $this->assertDatabaseMissing('images', [
782 'uploaded_to' => $editor->id,
785 $this->assertFalse(file_exists($imagePath));
788 public function test_deleted_unused_images()
790 $page = $this->entities->page();
791 $admin = $this->users->admin();
792 $this->actingAs($admin);
794 $imageName = 'unused-image.png';
795 $relPath = $this->files->expectedImagePath('gallery', $imageName);
796 $this->files->deleteAtRelativePath($relPath);
798 $upload = $this->files->uploadGalleryImage($this, $imageName, $page->id);
799 $upload->assertStatus(200);
800 $image = Image::where('type', '=', 'gallery')->first();
802 $pageRepo = app(PageRepo::class);
803 $pageRepo->update($page, [
804 'name' => $page->name,
805 'html' => $page->html . "<img src=\"{$image->url}\">",
809 // Ensure no images are reported as deletable
810 $imageService = app(ImageService::class);
811 $toDelete = $imageService->deleteUnusedImages(true, true);
812 $this->assertCount(0, $toDelete);
814 // Save a revision of our page without the image;
815 $pageRepo->update($page, [
816 'name' => $page->name,
817 'html' => '<p>Hello</p>',
821 // Ensure revision images are picked up okay
822 $imageService = app(ImageService::class);
823 $toDelete = $imageService->deleteUnusedImages(true, true);
824 $this->assertCount(0, $toDelete);
825 $toDelete = $imageService->deleteUnusedImages(false, true);
826 $this->assertCount(1, $toDelete);
828 // Check image is found when revisions are destroyed
829 $page->revisions()->delete();
830 $toDelete = $imageService->deleteUnusedImages(true, true);
831 $this->assertCount(1, $toDelete);
833 // Check the image is deleted
834 $absPath = public_path($relPath);
835 $this->assertTrue(file_exists($absPath), "Existing uploaded file at path {$absPath} exists");
836 $toDelete = $imageService->deleteUnusedImages(true, false);
837 $this->assertCount(1, $toDelete);
838 $this->assertFalse(file_exists($absPath));
840 $this->files->deleteAtRelativePath($relPath);