3 class ImageTest extends BrowserKitTest
7 * Get a test image that can be uploaded
9 * @return \Illuminate\Http\UploadedFile
11 protected function getTestImage($fileName)
13 return new \Illuminate\Http\UploadedFile(base_path('tests/test-data/test-image.jpg'), $fileName, 'image/jpeg', 5238);
17 * Get the path for a test image.
22 protected function getTestImagePath($type, $fileName)
24 return '/uploads/images/' . $type . '/' . Date('Y-m-M') . '/' . $fileName;
28 * Uploads an image with the given name.
30 * @param int $uploadedTo
33 protected function uploadImage($name, $uploadedTo = 0)
35 $file = $this->getTestImage($name);
36 $this->call('POST', '/images/gallery/upload', ['uploaded_to' => $uploadedTo], [], ['file' => $file], []);
37 return $this->getTestImagePath('gallery', $name);
41 * Delete an uploaded image.
44 protected function deleteImage($relPath)
46 unlink(public_path($relPath));
50 public function test_image_upload()
52 $page = \BookStack\Page::first();
54 $admin = $this->getAdmin();
55 $imageName = 'first-image.jpg';
57 $relPath = $this->uploadImage($imageName, $page->id);
58 $this->assertResponseOk();
60 $this->assertTrue(file_exists(public_path($relPath)), 'Uploaded image not found at path: '. public_path($relPath));
62 $this->deleteImage($relPath);
64 $this->seeInDatabase('images', [
65 'url' => $this->baseUrl . $relPath,
67 'uploaded_to' => $page->id,
69 'created_by' => $admin->id,
70 'updated_by' => $admin->id,
76 public function test_image_delete()
78 $page = \BookStack\Page::first();
80 $imageName = 'first-image.jpg';
82 $relPath = $this->uploadImage($imageName, $page->id);
83 $image = \BookStack\Image::first();
85 $this->call('DELETE', '/images/' . $image->id);
86 $this->assertResponseOk();
88 $this->dontSeeInDatabase('images', [
89 'url' => $this->baseUrl . $relPath,
93 $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded image has not been deleted as expected');