3 namespace Tests\Helpers;
5 use BookStack\Entities\Models\Page;
6 use BookStack\Uploads\Attachment;
7 use BookStack\Uploads\AttachmentService;
8 use Illuminate\Http\UploadedFile;
9 use Illuminate\Testing\TestResponse;
16 * Get the path to a file in the test-data-directory.
18 public function testFilePath(string $fileName): string
20 return base_path('tests/test-data/' . $fileName);
24 * Creates a new temporary image file using the given name,
25 * with the content decoded from the given bas64 file name.
26 * Is generally used for testing sketchy files that could trip AV.
28 public function imageFromBase64File(string $base64FileName, string $imageFileName): UploadedFile
30 $imagePath = implode(DIRECTORY_SEPARATOR, [sys_get_temp_dir(), $imageFileName]);
31 $base64FilePath = $this->testFilePath($base64FileName);
32 $data = file_get_contents($base64FilePath);
33 $decoded = base64_decode($data);
34 file_put_contents($imagePath, $decoded);
36 return new UploadedFile($imagePath, $imageFileName, 'image/png', null, true);
40 * Get a test image UploadedFile instance, that can be uploaded via test requests.
42 public function uploadedImage(string $fileName, string $testDataFileName = ''): UploadedFile
44 return new UploadedFile($this->testFilePath($testDataFileName ?: 'test-image.png'), $fileName, 'image/png', null, true);
48 * Get a test txt UploadedFile instance, that can be uploaded via test requests.
50 public function uploadedTextFile(string $fileName): UploadedFile
52 return new UploadedFile($this->testFilePath('test-file.txt'), $fileName, 'text/plain', null, true);
56 * Get raw data for a PNG image test file.
58 public function pngImageData(): string
60 return file_get_contents($this->testFilePath('test-image.png'));
64 * Get the expected relative path for an uploaded image of the given type and filename.
66 public function expectedImagePath(string $imageType, string $fileName): string
68 return '/uploads/images/' . $imageType . '/' . date('Y-m') . '/' . $fileName;
72 * Performs an image gallery upload request with the given name.
74 public function uploadGalleryImage(TestCase $case, string $name, int $uploadedTo = 0, string $contentType = 'image/png', string $testDataFileName = ''): TestResponse
76 $file = $this->uploadedImage($name, $testDataFileName);
78 return $case->call('POST', '/images/gallery', ['uploaded_to' => $uploadedTo], [], ['file' => $file], ['CONTENT_TYPE' => $contentType]);
82 * Upload a new gallery image and return a set of details about the image,
83 * including the json decoded response of the upload.
84 * Ensures the upload succeeds.
86 * @return array{name: string, path: string, page: Page, response: stdClass}
88 public function uploadGalleryImageToPage(TestCase $case, Page $page, string $testDataFileName = ''): array
90 $imageName = $testDataFileName ?: 'first-image.png';
91 $relPath = $this->expectedImagePath('gallery', $imageName);
92 $this->deleteAtRelativePath($relPath);
94 $upload = $this->uploadGalleryImage($case, $imageName, $page->id, 'image/png', $testDataFileName);
95 $upload->assertStatus(200);
101 'response' => json_decode($upload->getContent()),
106 * Uploads an attachment file with the given name.
108 public function uploadAttachmentFile(TestCase $case, string $name, int $uploadedTo = 0): TestResponse
110 $file = $this->uploadedTextFile($name);
112 return $case->call('POST', '/attachments/upload', ['uploaded_to' => $uploadedTo], [], ['file' => $file], []);
116 * Upload a new attachment from the given raw data of the given type, to the given page.
117 * Returns the attachment
119 public function uploadAttachmentDataToPage(TestCase $case, Page $page, string $filename, string $content, string $mimeType): Attachment
122 $filePath = stream_get_meta_data($file)['uri'];
123 file_put_contents($filePath, $content);
124 $upload = new UploadedFile($filePath, $filename, $mimeType, null, true);
126 $case->call('POST', '/attachments/upload', ['uploaded_to' => $page->id], [], ['file' => $upload], []);
128 return $page->attachments()->where('uploaded_to', '=', $page->id)->latest()->firstOrFail();
132 * Delete an uploaded image.
134 public function deleteAtRelativePath(string $path): void
136 $fullPath = $this->relativeToFullPath($path);
137 if (file_exists($fullPath)) {
143 * Convert a relative path used by default in this provider to a full
144 * absolute local filesystem path.
146 public function relativeToFullPath(string $path): string
148 return public_path($path);
152 * Delete all uploaded files.
153 * To assist with cleanup.
155 public function deleteAllAttachmentFiles(): void
157 $fileService = app()->make(AttachmentService::class);
158 foreach (Attachment::all() as $file) {
159 $fileService->deleteFile($file);
164 * Reset the application favicon image in the public path.
166 public function resetAppFavicon(): void
168 file_put_contents(public_path('favicon.ico'), file_get_contents(public_path('icon.ico')));