1 <?php namespace Tests\Uploads;
4 use BookStack\Entities\Page;
5 use Illuminate\Http\UploadedFile;
10 * Get the path to our basic test image.
13 protected function getTestImageFilePath(?string $fileName = null)
15 if (is_null($fileName)) {
16 $fileName = 'test-image.png';
19 return base_path('tests/test-data/' . $fileName);
23 * Get a test image that can be uploaded
25 * @return UploadedFile
27 protected function getTestImage($fileName, ?string $testDataFileName = null)
29 return new UploadedFile($this->getTestImageFilePath($testDataFileName), $fileName, 'image/png', 5238, null, true);
33 * Get the raw file data for the test image.
34 * @return false|string
36 protected function getTestImageContent()
38 return file_get_contents($this->getTestImageFilePath());
42 * Get the path for a test image.
47 protected function getTestImagePath($type, $fileName)
49 return '/uploads/images/' . $type . '/' . Date('Y-m') . '/' . $fileName;
53 * Uploads an image with the given name.
55 * @param int $uploadedTo
56 * @param string $contentType
57 * @return \Illuminate\Foundation\Testing\TestResponse
59 protected function uploadImage($name, $uploadedTo = 0, $contentType = 'image/png', ?string $testDataFileName = null)
61 $file = $this->getTestImage($name, $testDataFileName);
62 return $this->withHeader('Content-Type', $contentType)
63 ->call('POST', '/images/gallery', ['uploaded_to' => $uploadedTo], [], ['file' => $file], []);
67 * Upload a new gallery image.
68 * Returns the image name.
69 * Can provide a page to relate the image to.
70 * @param Page|null $page
73 protected function uploadGalleryImage(Page $page = null, ?string $testDataFileName = null)
76 $page = Page::query()->first();
79 $imageName = $testDataFileName ?? 'first-image.png';
80 $relPath = $this->getTestImagePath('gallery', $imageName);
81 $this->deleteImage($relPath);
83 $upload = $this->uploadImage($imageName, $page->id, 'image/png', $testDataFileName);
84 $upload->assertStatus(200);
89 'response' => json_decode($upload->getContent()),
94 * Delete an uploaded image.
97 protected function deleteImage($relPath)
99 $path = public_path($relPath);
100 if (file_exists($path)) {