3 namespace Tests\Uploads;
5 use BookStack\Entities\Repos\PageRepo;
6 use BookStack\Uploads\Image;
7 use BookStack\Uploads\ImageService;
8 use Illuminate\Support\Str;
11 class ImageTest extends TestCase
13 public function test_image_upload()
15 $page = $this->entities->page();
16 $admin = $this->users->admin();
17 $this->actingAs($admin);
19 $imgDetails = $this->files->uploadGalleryImageToPage($this, $page);
20 $relPath = $imgDetails['path'];
22 $this->assertTrue(file_exists(public_path($relPath)), 'Uploaded image found at path: ' . public_path($relPath));
24 $this->files->deleteAtRelativePath($relPath);
26 $this->assertDatabaseHas('images', [
27 'url' => $this->baseUrl . $relPath,
29 'uploaded_to' => $page->id,
31 'created_by' => $admin->id,
32 'updated_by' => $admin->id,
33 'name' => $imgDetails['name'],
37 public function test_image_display_thumbnail_generation_does_not_increase_image_size()
39 $page = $this->entities->page();
40 $admin = $this->users->admin();
41 $this->actingAs($admin);
43 $originalFile = $this->files->testFilePath('compressed.png');
44 $originalFileSize = filesize($originalFile);
45 $imgDetails = $this->files->uploadGalleryImageToPage($this, $page, 'compressed.png');
46 $relPath = $imgDetails['path'];
48 $this->assertTrue(file_exists(public_path($relPath)), 'Uploaded image found at path: ' . public_path($relPath));
49 $displayImage = $imgDetails['response']->thumbs->display;
51 $displayImageRelPath = implode('/', array_slice(explode('/', $displayImage), 3));
52 $displayImagePath = public_path($displayImageRelPath);
53 $displayFileSize = filesize($displayImagePath);
55 $this->files->deleteAtRelativePath($relPath);
56 $this->files->deleteAtRelativePath($displayImageRelPath);
58 $this->assertEquals($originalFileSize, $displayFileSize, 'Display thumbnail generation should not increase image size');
61 public function test_image_display_thumbnail_generation_for_apng_images_uses_original_file()
63 $page = $this->entities->page();
64 $admin = $this->users->admin();
65 $this->actingAs($admin);
67 $imgDetails = $this->files->uploadGalleryImageToPage($this, $page, 'animated.png');
68 $this->files->deleteAtRelativePath($imgDetails['path']);
70 $this->assertStringContainsString('thumbs-', $imgDetails['response']->thumbs->gallery);
71 $this->assertStringNotContainsString('thumbs-', $imgDetails['response']->thumbs->display);
74 public function test_image_edit()
76 $editor = $this->users->editor();
77 $this->actingAs($editor);
79 $imgDetails = $this->files->uploadGalleryImageToPage($this, $this->entities->page());
80 $image = Image::query()->first();
82 $newName = Str::random();
83 $update = $this->put('/images/' . $image->id, ['name' => $newName]);
84 $update->assertSuccessful();
85 $update->assertSee($newName);
87 $this->files->deleteAtRelativePath($imgDetails['path']);
89 $this->assertDatabaseHas('images', [
95 public function test_image_file_update()
97 $page = $this->entities->page();
100 $imgDetails = $this->files->uploadGalleryImageToPage($this, $page);
101 $relPath = $imgDetails['path'];
103 $newUpload = $this->files->uploadedImage('updated-image.png', 'compressed.png');
104 $this->assertFileEquals($this->files->testFilePath('test-image.png'), public_path($relPath));
106 $imageId = $imgDetails['response']->id;
107 $image = Image::findOrFail($imageId);
108 $image->updated_at = now()->subMonth();
111 $this->call('PUT', "/images/{$imageId}/file", [], [], ['file' => $newUpload])
114 $this->assertFileEquals($this->files->testFilePath('compressed.png'), public_path($relPath));
117 $this->assertTrue($image->updated_at->gt(now()->subMinute()));
119 $this->files->deleteAtRelativePath($relPath);
122 public function test_image_file_update_allows_case_differences()
124 $page = $this->entities->page();
127 $imgDetails = $this->files->uploadGalleryImageToPage($this, $page);
128 $relPath = $imgDetails['path'];
130 $newUpload = $this->files->uploadedImage('updated-image.PNG', 'compressed.png');
131 $this->assertFileEquals($this->files->testFilePath('test-image.png'), public_path($relPath));
133 $imageId = $imgDetails['response']->id;
134 $image = Image::findOrFail($imageId);
135 $image->updated_at = now()->subMonth();
138 $this->call('PUT', "/images/{$imageId}/file", [], [], ['file' => $newUpload])
141 $this->assertFileEquals($this->files->testFilePath('compressed.png'), public_path($relPath));
144 $this->assertTrue($image->updated_at->gt(now()->subMinute()));
146 $this->files->deleteAtRelativePath($relPath);
149 public function test_image_file_update_does_not_allow_change_in_image_extension()
151 $page = $this->entities->page();
154 $imgDetails = $this->files->uploadGalleryImageToPage($this, $page);
155 $relPath = $imgDetails['path'];
156 $newUpload = $this->files->uploadedImage('updated-image.jpg', 'compressed.png');
158 $imageId = $imgDetails['response']->id;
159 $this->call('PUT', "/images/{$imageId}/file", [], [], ['file' => $newUpload])
161 "message" => "Image file replacements must be of the same type",
165 $this->files->deleteAtRelativePath($relPath);
168 public function test_gallery_get_list_format()
172 $imgDetails = $this->files->uploadGalleryImageToPage($this, $this->entities->page());
173 $image = Image::query()->first();
175 $pageId = $imgDetails['page']->id;
176 $firstPageRequest = $this->get("/images/gallery?page=1&uploaded_to={$pageId}");
177 $firstPageRequest->assertSuccessful();
178 $this->withHtml($firstPageRequest)->assertElementExists('div');
179 $firstPageRequest->assertSuccessful()->assertSeeText($image->name);
181 $secondPageRequest = $this->get("/images/gallery?page=2&uploaded_to={$pageId}");
182 $secondPageRequest->assertSuccessful();
183 $this->withHtml($secondPageRequest)->assertElementNotExists('div');
185 $namePartial = substr($imgDetails['name'], 0, 3);
186 $searchHitRequest = $this->get("/images/gallery?page=1&uploaded_to={$pageId}&search={$namePartial}");
187 $searchHitRequest->assertSuccessful()->assertSee($imgDetails['name']);
189 $namePartial = Str::random(16);
190 $searchFailRequest = $this->get("/images/gallery?page=1&uploaded_to={$pageId}&search={$namePartial}");
191 $searchFailRequest->assertSuccessful()->assertDontSee($imgDetails['name']);
192 $searchFailRequest->assertSuccessful();
193 $this->withHtml($searchFailRequest)->assertElementNotExists('div');
196 public function test_image_gallery_lists_for_draft_page()
198 $this->actingAs($this->users->editor());
199 $draft = $this->entities->newDraftPage();
200 $this->files->uploadGalleryImageToPage($this, $draft);
201 $image = Image::query()->where('uploaded_to', '=', $draft->id)->firstOrFail();
203 $resp = $this->get("/images/gallery?page=1&uploaded_to={$draft->id}");
204 $resp->assertSee($image->getThumb(150, 150));
207 public function test_image_usage()
209 $page = $this->entities->page();
210 $editor = $this->users->editor();
211 $this->actingAs($editor);
213 $imgDetails = $this->files->uploadGalleryImageToPage($this, $page);
215 $image = Image::query()->first();
216 $page->html = '<img src="' . $image->url . '">';
219 $usage = $this->get('/images/edit/' . $image->id . '?delete=true');
220 $usage->assertSuccessful();
221 $usage->assertSeeText($page->name);
222 $usage->assertSee($page->getUrl());
224 $this->files->deleteAtRelativePath($imgDetails['path']);
227 public function test_php_files_cannot_be_uploaded()
229 $page = $this->entities->page();
230 $admin = $this->users->admin();
231 $this->actingAs($admin);
233 $fileName = 'bad.php';
234 $relPath = $this->files->expectedImagePath('gallery', $fileName);
235 $this->files->deleteAtRelativePath($relPath);
237 $file = $this->files->imageFromBase64File('bad-php.base64', $fileName);
238 $upload = $this->withHeader('Content-Type', 'image/jpeg')->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $file], []);
239 $upload->assertStatus(500);
240 $this->assertStringContainsString('The file must have a valid & supported image extension', $upload->json('message'));
242 $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded php file was uploaded but should have been stopped');
244 $this->assertDatabaseMissing('images', [
250 public function test_php_like_files_cannot_be_uploaded()
252 $page = $this->entities->page();
253 $admin = $this->users->admin();
254 $this->actingAs($admin);
256 $fileName = 'bad.phtml';
257 $relPath = $this->files->expectedImagePath('gallery', $fileName);
258 $this->files->deleteAtRelativePath($relPath);
260 $file = $this->files->imageFromBase64File('bad-phtml.base64', $fileName);
261 $upload = $this->withHeader('Content-Type', 'image/jpeg')->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $file], []);
262 $upload->assertStatus(500);
263 $this->assertStringContainsString('The file must have a valid & supported image extension', $upload->json('message'));
265 $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded php file was uploaded but should have been stopped');
268 public function test_files_with_double_extensions_will_get_sanitized()
270 $page = $this->entities->page();
271 $admin = $this->users->admin();
272 $this->actingAs($admin);
274 $fileName = 'bad.phtml.png';
275 $relPath = $this->files->expectedImagePath('gallery', $fileName);
276 $expectedRelPath = dirname($relPath) . '/bad-phtml.png';
277 $this->files->deleteAtRelativePath($expectedRelPath);
279 $file = $this->files->imageFromBase64File('bad-phtml-png.base64', $fileName);
280 $upload = $this->withHeader('Content-Type', 'image/png')->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $file], []);
281 $upload->assertStatus(200);
283 $lastImage = Image::query()->latest('id')->first();
285 $this->assertEquals('bad.phtml.png', $lastImage->name);
286 $this->assertEquals('bad-phtml.png', basename($lastImage->path));
287 $this->assertFileDoesNotExist(public_path($relPath), 'Uploaded image file name was not stripped of dots');
288 $this->assertFileExists(public_path($expectedRelPath));
290 $this->files->deleteAtRelativePath($lastImage->path);
293 public function test_url_entities_removed_from_filenames()
297 'bad-char-#-image.png',
298 'bad-char-?-image.png',
303 foreach ($badNames as $name) {
304 $galleryFile = $this->files->uploadedImage($name);
305 $page = $this->entities->page();
306 $badPath = $this->files->expectedImagePath('gallery', $name);
307 $this->files->deleteAtRelativePath($badPath);
309 $upload = $this->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $galleryFile], []);
310 $upload->assertStatus(200);
312 $lastImage = Image::query()->latest('id')->first();
313 $newFileName = explode('.', basename($lastImage->path))[0];
315 $this->assertEquals($lastImage->name, $name);
316 $this->assertFalse(strpos($lastImage->path, $name), 'Path contains original image name');
317 $this->assertFalse(file_exists(public_path($badPath)), 'Uploaded image file name was not stripped of url entities');
319 $this->assertTrue(strlen($newFileName) > 0, 'File name was reduced to nothing');
321 $this->files->deleteAtRelativePath($lastImage->path);
325 public function test_secure_images_uploads_to_correct_place()
327 config()->set('filesystems.images', 'local_secure');
329 $galleryFile = $this->files->uploadedImage('my-secure-test-upload.png');
330 $page = $this->entities->page();
331 $expectedPath = storage_path('uploads/images/gallery/' . date('Y-m') . '/my-secure-test-upload.png');
333 $upload = $this->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $galleryFile], []);
334 $upload->assertStatus(200);
336 $this->assertTrue(file_exists($expectedPath), 'Uploaded image not found at path: ' . $expectedPath);
338 if (file_exists($expectedPath)) {
339 unlink($expectedPath);
343 public function test_secure_image_paths_traversal_causes_500()
345 config()->set('filesystems.images', 'local_secure');
348 $resp = $this->get('/uploads/images/../../logs/laravel.log');
349 $resp->assertStatus(500);
352 public function test_secure_image_paths_traversal_on_non_secure_images_causes_404()
354 config()->set('filesystems.images', 'local');
357 $resp = $this->get('/uploads/images/../../logs/laravel.log');
358 $resp->assertStatus(404);
361 public function test_secure_image_paths_dont_serve_non_images()
363 config()->set('filesystems.images', 'local_secure');
366 $testFilePath = storage_path('/uploads/images/testing.txt');
367 file_put_contents($testFilePath, 'hello from test_secure_image_paths_dont_serve_non_images');
369 $resp = $this->get('/uploads/images/testing.txt');
370 $resp->assertStatus(404);
373 public function test_secure_images_included_in_exports()
375 config()->set('filesystems.images', 'local_secure');
377 $galleryFile = $this->files->uploadedImage('my-secure-test-upload.png');
378 $page = $this->entities->page();
379 $expectedPath = storage_path('uploads/images/gallery/' . date('Y-m') . '/my-secure-test-upload.png');
381 $upload = $this->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $galleryFile], []);
382 $imageUrl = json_decode($upload->getContent(), true)['url'];
383 $page->html .= "<img src=\"{$imageUrl}\">";
385 $upload->assertStatus(200);
387 $encodedImageContent = base64_encode(file_get_contents($expectedPath));
388 $export = $this->get($page->getUrl('/export/html'));
389 $this->assertTrue(strpos($export->getContent(), $encodedImageContent) !== false, 'Uploaded image in export content');
391 if (file_exists($expectedPath)) {
392 unlink($expectedPath);
396 public function test_system_images_remain_public_with_local_secure()
398 config()->set('filesystems.images', 'local_secure');
400 $galleryFile = $this->files->uploadedImage('my-system-test-upload.png');
401 $expectedPath = public_path('uploads/images/system/' . date('Y-m') . '/my-system-test-upload.png');
403 $upload = $this->call('POST', '/settings/customization', [], [], ['app_logo' => $galleryFile], []);
404 $upload->assertRedirect('/settings/customization');
406 $this->assertTrue(file_exists($expectedPath), 'Uploaded image not found at path: ' . $expectedPath);
408 if (file_exists($expectedPath)) {
409 unlink($expectedPath);
413 public function test_secure_images_not_tracked_in_session_history()
415 config()->set('filesystems.images', 'local_secure');
417 $page = $this->entities->page();
418 $result = $this->files->uploadGalleryImageToPage($this, $page);
419 $expectedPath = storage_path($result['path']);
420 $this->assertFileExists($expectedPath);
422 $this->get('/books');
423 $this->assertEquals(url('/books'), session()->previousUrl());
425 $resp = $this->get($result['path']);
427 $resp->assertHeader('Content-Type', 'image/png');
429 $this->assertEquals(url('/books'), session()->previousUrl());
431 if (file_exists($expectedPath)) {
432 unlink($expectedPath);
436 public function test_system_images_remain_public_with_local_secure_restricted()
438 config()->set('filesystems.images', 'local_secure_restricted');
440 $galleryFile = $this->files->uploadedImage('my-system-test-restricted-upload.png');
441 $expectedPath = public_path('uploads/images/system/' . date('Y-m') . '/my-system-test-restricted-upload.png');
443 $upload = $this->call('POST', '/settings/customization', [], [], ['app_logo' => $galleryFile], []);
444 $upload->assertRedirect('/settings/customization');
446 $this->assertTrue(file_exists($expectedPath), 'Uploaded image not found at path: ' . $expectedPath);
448 if (file_exists($expectedPath)) {
449 unlink($expectedPath);
453 public function test_secure_restricted_images_inaccessible_without_relation_permission()
455 config()->set('filesystems.images', 'local_secure_restricted');
457 $galleryFile = $this->files->uploadedImage('my-secure-restricted-test-upload.png');
458 $page = $this->entities->page();
460 $upload = $this->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $galleryFile], []);
461 $upload->assertStatus(200);
462 $expectedUrl = url('uploads/images/gallery/' . date('Y-m') . '/my-secure-restricted-test-upload.png');
463 $expectedPath = storage_path('uploads/images/gallery/' . date('Y-m') . '/my-secure-restricted-test-upload.png');
465 $this->get($expectedUrl)->assertOk();
467 $this->permissions->setEntityPermissions($page, [], []);
469 $resp = $this->get($expectedUrl);
470 $resp->assertNotFound();
472 if (file_exists($expectedPath)) {
473 unlink($expectedPath);
477 public function test_thumbnail_path_handled_by_secure_restricted_images()
479 config()->set('filesystems.images', 'local_secure_restricted');
481 $galleryFile = $this->files->uploadedImage('my-secure-restricted-thumb-test-test.png');
482 $page = $this->entities->page();
484 $upload = $this->call('POST', '/images/gallery', ['uploaded_to' => $page->id], [], ['file' => $galleryFile], []);
485 $upload->assertStatus(200);
486 $expectedUrl = url('uploads/images/gallery/' . date('Y-m') . '/thumbs-150-150/my-secure-restricted-thumb-test-test.png');
487 $expectedPath = storage_path('uploads/images/gallery/' . date('Y-m') . '/my-secure-restricted-thumb-test-test.png');
489 $this->get($expectedUrl)->assertOk();
491 $this->permissions->setEntityPermissions($page, [], []);
493 $resp = $this->get($expectedUrl);
494 $resp->assertNotFound();
496 if (file_exists($expectedPath)) {
497 unlink($expectedPath);
501 public function test_secure_restricted_image_access_controlled_in_exports()
503 config()->set('filesystems.images', 'local_secure_restricted');
505 $galleryFile = $this->files->uploadedImage('my-secure-restricted-export-test.png');
507 $pageA = $this->entities->page();
508 $pageB = $this->entities->page();
509 $expectedPath = storage_path('uploads/images/gallery/' . date('Y-m') . '/my-secure-restricted-export-test.png');
511 $upload = $this->asEditor()->call('POST', '/images/gallery', ['uploaded_to' => $pageA->id], [], ['file' => $galleryFile], []);
514 $imageUrl = json_decode($upload->getContent(), true)['url'];
515 $pageB->html .= "<img src=\"{$imageUrl}\">";
518 $encodedImageContent = base64_encode(file_get_contents($expectedPath));
519 $export = $this->get($pageB->getUrl('/export/html'));
520 $this->assertStringContainsString($encodedImageContent, $export->getContent());
522 $this->permissions->setEntityPermissions($pageA, [], []);
524 $export = $this->get($pageB->getUrl('/export/html'));
525 $this->assertStringNotContainsString($encodedImageContent, $export->getContent());
527 if (file_exists($expectedPath)) {
528 unlink($expectedPath);
532 public function test_image_delete()
534 $page = $this->entities->page();
536 $imageName = 'first-image.png';
537 $relPath = $this->files->expectedImagePath('gallery', $imageName);
538 $this->files->deleteAtRelativePath($relPath);
540 $this->files->uploadGalleryImage($this, $imageName, $page->id);
541 $image = Image::first();
543 $delete = $this->delete('/images/' . $image->id);
544 $delete->assertStatus(200);
546 $this->assertDatabaseMissing('images', [
547 'url' => $this->baseUrl . $relPath,
551 $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded image has not been deleted as expected');
554 public function test_image_delete_does_not_delete_similar_images()
556 $page = $this->entities->page();
558 $imageName = 'first-image.png';
560 $relPath = $this->files->expectedImagePath('gallery', $imageName);
561 $this->files->deleteAtRelativePath($relPath);
563 $this->files->uploadGalleryImage($this, $imageName, $page->id);
564 $this->files->uploadGalleryImage($this, $imageName, $page->id);
565 $this->files->uploadGalleryImage($this, $imageName, $page->id);
567 $image = Image::first();
568 $folder = public_path(dirname($relPath));
569 $imageCount = count(glob($folder . '/*'));
571 $delete = $this->delete('/images/' . $image->id);
572 $delete->assertStatus(200);
574 $newCount = count(glob($folder . '/*'));
575 $this->assertEquals($imageCount - 1, $newCount, 'More files than expected have been deleted');
576 $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded image has not been deleted as expected');
579 public function test_image_manager_delete_button_only_shows_with_permission()
581 $page = $this->entities->page();
583 $imageName = 'first-image.png';
584 $relPath = $this->files->expectedImagePath('gallery', $imageName);
585 $this->files->deleteAtRelativePath($relPath);
586 $viewer = $this->users->viewer();
588 $this->files->uploadGalleryImage($this, $imageName, $page->id);
589 $image = Image::first();
591 $resp = $this->get("/images/edit/{$image->id}");
592 $this->withHtml($resp)->assertElementExists('button#image-manager-delete');
594 $resp = $this->actingAs($viewer)->get("/images/edit/{$image->id}");
595 $this->withHtml($resp)->assertElementNotExists('button#image-manager-delete');
597 $this->permissions->grantUserRolePermissions($viewer, ['image-delete-all']);
599 $resp = $this->actingAs($viewer)->get("/images/edit/{$image->id}");
600 $this->withHtml($resp)->assertElementExists('button#image-manager-delete');
602 $this->files->deleteAtRelativePath($relPath);
605 public function test_image_manager_regen_thumbnails()
608 $imageName = 'first-image.png';
609 $relPath = $this->files->expectedImagePath('gallery', $imageName);
610 $this->files->deleteAtRelativePath($relPath);
612 $this->files->uploadGalleryImage($this, $imageName, $this->entities->page()->id);
613 $image = Image::first();
615 $resp = $this->get("/images/edit/{$image->id}");
616 $this->withHtml($resp)->assertElementExists('button#image-manager-rebuild-thumbs');
618 $expectedThumbPath = dirname($relPath) . '/scaled-1680-/' . basename($relPath);
619 $this->files->deleteAtRelativePath($expectedThumbPath);
620 $this->assertFileDoesNotExist($this->files->relativeToFullPath($expectedThumbPath));
622 $resp = $this->put("/images/{$image->id}/rebuild-thumbnails");
625 $this->assertFileExists($this->files->relativeToFullPath($expectedThumbPath));
626 $this->files->deleteAtRelativePath($relPath);
629 public function test_gif_thumbnail_generation()
632 $originalFile = $this->files->testFilePath('animated.gif');
633 $originalFileSize = filesize($originalFile);
635 $imgDetails = $this->files->uploadGalleryImageToPage($this, $this->entities->page(), 'animated.gif');
636 $relPath = $imgDetails['path'];
638 $this->assertTrue(file_exists(public_path($relPath)), 'Uploaded image found at path: ' . public_path($relPath));
639 $galleryThumb = $imgDetails['response']->thumbs->gallery;
640 $displayThumb = $imgDetails['response']->thumbs->display;
642 // Ensure display thumbnail is original image
643 $this->assertStringEndsWith($imgDetails['path'], $displayThumb);
644 $this->assertStringNotContainsString('thumbs', $displayThumb);
646 // Ensure gallery thumbnail is reduced image (single frame)
647 $galleryThumbRelPath = implode('/', array_slice(explode('/', $galleryThumb), 3));
648 $galleryThumbPath = public_path($galleryThumbRelPath);
649 $galleryFileSize = filesize($galleryThumbPath);
651 // Basic scan of GIF content to check frame count
652 $originalFrameCount = count(explode("\x00\x21\xF9", file_get_contents($originalFile)));
653 $galleryFrameCount = count(explode("\x00\x21\xF9", file_get_contents($galleryThumbPath)));
655 $this->files->deleteAtRelativePath($relPath);
656 $this->files->deleteAtRelativePath($galleryThumbRelPath);
658 $this->assertNotEquals($originalFileSize, $galleryFileSize);
659 $this->assertEquals(3, $originalFrameCount);
660 $this->assertEquals(1, $galleryFrameCount);
663 protected function getTestProfileImage()
665 $imageName = 'profile.png';
666 $relPath = $this->files->expectedImagePath('user', $imageName);
667 $this->files->deleteAtRelativePath($relPath);
669 return $this->files->uploadedImage($imageName);
672 public function test_user_image_upload()
674 $editor = $this->users->editor();
675 $admin = $this->users->admin();
676 $this->actingAs($admin);
678 $file = $this->getTestProfileImage();
679 $this->call('PUT', '/settings/users/' . $editor->id, [], [], ['profile_image' => $file], []);
681 $this->assertDatabaseHas('images', [
683 'uploaded_to' => $editor->id,
684 'created_by' => $admin->id,
688 public function test_user_images_deleted_on_user_deletion()
690 $editor = $this->users->editor();
691 $this->actingAs($editor);
693 $file = $this->getTestProfileImage();
694 $this->call('PUT', '/my-account/profile', [], [], ['profile_image' => $file], []);
696 $profileImages = Image::where('type', '=', 'user')->where('created_by', '=', $editor->id)->get();
697 $this->assertTrue($profileImages->count() === 1, 'Found profile images does not match upload count');
699 $imagePath = public_path($profileImages->first()->path);
700 $this->assertTrue(file_exists($imagePath));
702 $userDelete = $this->asAdmin()->delete($editor->getEditUrl());
703 $userDelete->assertStatus(302);
705 $this->assertDatabaseMissing('images', [
707 'created_by' => $editor->id,
709 $this->assertDatabaseMissing('images', [
711 'uploaded_to' => $editor->id,
714 $this->assertFalse(file_exists($imagePath));
717 public function test_deleted_unused_images()
719 $page = $this->entities->page();
720 $admin = $this->users->admin();
721 $this->actingAs($admin);
723 $imageName = 'unused-image.png';
724 $relPath = $this->files->expectedImagePath('gallery', $imageName);
725 $this->files->deleteAtRelativePath($relPath);
727 $upload = $this->files->uploadGalleryImage($this, $imageName, $page->id);
728 $upload->assertStatus(200);
729 $image = Image::where('type', '=', 'gallery')->first();
731 $pageRepo = app(PageRepo::class);
732 $pageRepo->update($page, [
733 'name' => $page->name,
734 'html' => $page->html . "<img src=\"{$image->url}\">",
738 // Ensure no images are reported as deletable
739 $imageService = app(ImageService::class);
740 $toDelete = $imageService->deleteUnusedImages(true, true);
741 $this->assertCount(0, $toDelete);
743 // Save a revision of our page without the image;
744 $pageRepo->update($page, [
745 'name' => $page->name,
746 'html' => '<p>Hello</p>',
750 // Ensure revision images are picked up okay
751 $imageService = app(ImageService::class);
752 $toDelete = $imageService->deleteUnusedImages(true, true);
753 $this->assertCount(0, $toDelete);
754 $toDelete = $imageService->deleteUnusedImages(false, true);
755 $this->assertCount(1, $toDelete);
757 // Check image is found when revisions are destroyed
758 $page->revisions()->delete();
759 $toDelete = $imageService->deleteUnusedImages(true, true);
760 $this->assertCount(1, $toDelete);
762 // Check the image is deleted
763 $absPath = public_path($relPath);
764 $this->assertTrue(file_exists($absPath), "Existing uploaded file at path {$absPath} exists");
765 $toDelete = $imageService->deleteUnusedImages(true, false);
766 $this->assertCount(1, $toDelete);
767 $this->assertFalse(file_exists($absPath));
769 $this->files->deleteAtRelativePath($relPath);