3 namespace Tests\Uploads;
5 use BookStack\Entities\Models\Page;
6 use BookStack\Uploads\Image;
7 use Illuminate\Http\UploadedFile;
13 * Get the path to a file in the test-data-directory.
15 protected function getTestImageFilePath(?string $fileName = null): string
17 if (is_null($fileName)) {
18 $fileName = 'test-image.png';
21 return base_path('tests/test-data/' . $fileName);
25 * Creates a new temporary image file using the given name,
26 * with the content decoded from the given bas64 file name.
27 * Is generally used for testing sketchy files that could trip AV.
29 protected function newTestImageFromBase64(string $base64FileName, $imageFileName): UploadedFile
31 $imagePath = implode(DIRECTORY_SEPARATOR, [sys_get_temp_dir(), $imageFileName]);
32 $base64FilePath = $this->getTestImageFilePath($base64FileName);
33 $data = file_get_contents($base64FilePath);
34 $decoded = base64_decode($data);
35 file_put_contents($imagePath, $decoded);
37 return new UploadedFile($imagePath, $imageFileName, 'image/png', null, true);
41 * Get a test image that can be uploaded.
43 protected function getTestImage(string $fileName, ?string $testDataFileName = null, $mimeType = 'image/png'): UploadedFile
45 return new UploadedFile($this->getTestImageFilePath($testDataFileName), $fileName, $mimeType, null, true);
49 * Get the raw file data for the test image.
51 * @return false|string
53 protected function getTestImageContent()
55 return file_get_contents($this->getTestImageFilePath());
59 * Get the path for a test image.
61 protected function getTestImagePath(string $type, string $fileName): string
63 return '/uploads/images/' . $type . '/' . date('Y-m') . '/' . $fileName;
67 * Uploads an image with the given name.
70 * @param int $uploadedTo
71 * @param string $contentType
73 * @return \Illuminate\Foundation\Testing\TestResponse
75 protected function uploadImage($name, $uploadedTo = 0, $contentType = 'image/png', ?string $testDataFileName = null)
77 $file = $this->getTestImage($name, $testDataFileName, $contentType);
79 return $this->withHeader('Content-Type', $contentType)
80 ->call('POST', '/images/gallery', ['uploaded_to' => $uploadedTo], [], ['file' => $file], []);
84 * Upload a new gallery image.
85 * Returns the image name.
86 * Can provide a page to relate the image to.
88 * @return array{name: string, path: string, page: Page, response: stdClass}
90 protected function uploadGalleryImage(Page $page = null, string $testDataFileName = null, string $contentType = 'image/png')
93 $page = Page::query()->first();
96 $imageName = $testDataFileName ?? 'first-image.png';
97 $relPath = $this->getTestImagePath('gallery', $imageName);
98 $this->deleteImage($relPath);
100 $upload = $this->uploadImage($imageName, $page->id, $contentType, $testDataFileName);
101 $upload->assertStatus(200);
104 'name' => $imageName,
107 'response' => json_decode($upload->getContent()),
112 * Delete an uploaded image.
114 protected function deleteImage(string $relPath)
116 $path = public_path($relPath);
117 if (file_exists($path)) {