X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/68017e25534b13db5b784c634c0d6f2d3981affb..refs/pull/3406/head:/tests/Uploads/UsesImages.php diff --git a/tests/Uploads/UsesImages.php b/tests/Uploads/UsesImages.php index 16cb7c2b9..b55572248 100644 --- a/tests/Uploads/UsesImages.php +++ b/tests/Uploads/UsesImages.php @@ -1,29 +1,52 @@ -getTestImageFilePath($base64FileName); + $data = file_get_contents($base64FilePath); + $decoded = base64_decode($data); + file_put_contents($imagePath, $decoded); + + return new UploadedFile($imagePath, $imageFileName, 'image/png', null, true); } /** - * Get a test image that can be uploaded - * @param $fileName - * @return \Illuminate\Http\UploadedFile + * Get a test image that can be uploaded. */ - protected function getTestImage($fileName) + protected function getTestImage(string $fileName, ?string $testDataFileName = null): UploadedFile { - return new \Illuminate\Http\UploadedFile($this->getTestImageFilePath(), $fileName, 'image/png', 5238); + return new UploadedFile($this->getTestImageFilePath($testDataFileName), $fileName, 'image/png', null, true); } /** * Get the raw file data for the test image. + * * @return false|string */ protected function getTestImageContent() @@ -33,37 +56,67 @@ trait UsesImages /** * Get the path for a test image. - * @param $type - * @param $fileName - * @return string */ - protected function getTestImagePath($type, $fileName) + protected function getTestImagePath(string $type, string $fileName): string { - return '/uploads/images/' . $type . '/' . Date('Y-m-M') . '/' . $fileName; + return '/uploads/images/' . $type . '/' . date('Y-m') . '/' . $fileName; } /** * Uploads an image with the given name. + * * @param $name - * @param int $uploadedTo + * @param int $uploadedTo + * @param string $contentType + * * @return \Illuminate\Foundation\Testing\TestResponse */ - protected function uploadImage($name, $uploadedTo = 0) + protected function uploadImage($name, $uploadedTo = 0, $contentType = 'image/png', ?string $testDataFileName = null) + { + $file = $this->getTestImage($name, $testDataFileName); + + return $this->withHeader('Content-Type', $contentType) + ->call('POST', '/images/gallery', ['uploaded_to' => $uploadedTo], [], ['file' => $file], []); + } + + /** + * Upload a new gallery image. + * Returns the image name. + * Can provide a page to relate the image to. + * + * @param Page|null $page + * + * @return array{name: string, path: string, page: Page, response: stdClass} + */ + protected function uploadGalleryImage(Page $page = null, ?string $testDataFileName = null) { - $file = $this->getTestImage($name); - return $this->call('POST', '/images/gallery/upload', ['uploaded_to' => $uploadedTo], [], ['file' => $file], []); + if ($page === null) { + $page = Page::query()->first(); + } + + $imageName = $testDataFileName ?? 'first-image.png'; + $relPath = $this->getTestImagePath('gallery', $imageName); + $this->deleteImage($relPath); + + $upload = $this->uploadImage($imageName, $page->id, 'image/png', $testDataFileName); + $upload->assertStatus(200); + + return [ + 'name' => $imageName, + 'path' => $relPath, + 'page' => $page, + 'response' => json_decode($upload->getContent()), + ]; } /** * Delete an uploaded image. - * @param $relPath */ - protected function deleteImage($relPath) + protected function deleteImage(string $relPath) { $path = public_path($relPath); if (file_exists($path)) { unlink($path); } } - -} \ No newline at end of file +}