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.
46 protected function getTestImagePath($type, $fileName)
48 return '/uploads/images/' . $type . '/' . Date('Y-m') . '/' . $fileName;
52 * Uploads an image with the given name.
54 * @param int $uploadedTo
55 * @param string $contentType
56 * @return \Illuminate\Foundation\Testing\TestResponse
58 protected function uploadImage($name, $uploadedTo = 0, $contentType = 'image/png', ?string $testDataFileName = null)
60 $file = $this->getTestImage($name, $testDataFileName);
61 return $this->withHeader('Content-Type', $contentType)
62 ->call('POST', '/images/gallery', ['uploaded_to' => $uploadedTo], [], ['file' => $file], []);
66 * Upload a new gallery image.
67 * Returns the image name.
68 * Can provide a page to relate the image to.
69 * @param Page|null $page
72 protected function uploadGalleryImage(Page $page = null, ?string $testDataFileName = null)
75 $page = Page::query()->first();
78 $imageName = $testDataFileName ?? 'first-image.png';
79 $relPath = $this->getTestImagePath('gallery', $imageName);
80 $this->deleteImage($relPath);
82 $upload = $this->uploadImage($imageName, $page->id, 'image/png', $testDataFileName);
83 $upload->assertStatus(200);
88 'response' => json_decode($upload->getContent()),
93 * Delete an uploaded image.
96 protected function deleteImage($relPath)
98 $path = public_path($relPath);
99 if (file_exists($path)) {