1 <?php namespace Tests\Uploads;
3 use BookStack\Entities\Page;
4 use Illuminate\Http\UploadedFile;
9 * Get the path to our basic test image.
12 protected function getTestImageFilePath(?string $fileName = null)
14 if (is_null($fileName)) {
15 $fileName = 'test-image.png';
18 return base_path('tests/test-data/' . $fileName);
22 * Get a test image that can be uploaded
24 * @return UploadedFile
26 protected function getTestImage($fileName, ?string $testDataFileName = null)
28 return new UploadedFile($this->getTestImageFilePath($testDataFileName), $fileName, 'image/png', 5238, null, true);
32 * Get the raw file data for the test image.
33 * @return false|string
35 protected function getTestImageContent()
37 return file_get_contents($this->getTestImageFilePath());
41 * Get the path for a test image.
43 protected function getTestImagePath(string $type, string $fileName): string
45 return '/uploads/images/' . $type . '/' . Date('Y-m') . '/' . $fileName;
49 * Uploads an image with the given name.
51 * @param int $uploadedTo
52 * @param string $contentType
53 * @return \Illuminate\Foundation\Testing\TestResponse
55 protected function uploadImage($name, $uploadedTo = 0, $contentType = 'image/png', ?string $testDataFileName = null)
57 $file = $this->getTestImage($name, $testDataFileName);
58 return $this->withHeader('Content-Type', $contentType)
59 ->call('POST', '/images/gallery', ['uploaded_to' => $uploadedTo], [], ['file' => $file], []);
63 * Upload a new gallery image.
64 * Returns the image name.
65 * Can provide a page to relate the image to.
66 * @param Page|null $page
69 protected function uploadGalleryImage(Page $page = null, ?string $testDataFileName = null)
72 $page = Page::query()->first();
75 $imageName = $testDataFileName ?? 'first-image.png';
76 $relPath = $this->getTestImagePath('gallery', $imageName);
77 $this->deleteImage($relPath);
79 $upload = $this->uploadImage($imageName, $page->id, 'image/png', $testDataFileName);
80 $upload->assertStatus(200);
85 'response' => json_decode($upload->getContent()),
90 * Delete an uploaded image.
93 protected function deleteImage($relPath)
95 $path = public_path($relPath);
96 if (file_exists($path)) {