]> BookStack Code Mirror - bookstack/blob - tests/Uploads/UsesImages.php
Fixed issue caused by changing test method defaults
[bookstack] / tests / Uploads / UsesImages.php
1 <?php
2
3 namespace Tests\Uploads;
4
5 use BookStack\Entities\Models\Page;
6 use BookStack\Uploads\Image;
7 use Illuminate\Http\UploadedFile;
8 use stdClass;
9
10 trait UsesImages
11 {
12     /**
13      * Get the path to a file in the test-data-directory.
14      */
15     protected function getTestImageFilePath(?string $fileName = null): string
16     {
17         if (is_null($fileName)) {
18             $fileName = 'test-image.png';
19         }
20
21         return base_path('tests/test-data/' . $fileName);
22     }
23
24     /**
25      * Creates a new temporary image file using the given name,
26      * with the content decoded from the given bas64 file name.
27      * Is generally used for testing sketchy files that could trip AV.
28      */
29     protected function newTestImageFromBase64(string $base64FileName, $imageFileName): UploadedFile
30     {
31         $imagePath = implode(DIRECTORY_SEPARATOR, [sys_get_temp_dir(), $imageFileName]);
32         $base64FilePath = $this->getTestImageFilePath($base64FileName);
33         $data = file_get_contents($base64FilePath);
34         $decoded = base64_decode($data);
35         file_put_contents($imagePath, $decoded);
36
37         return new UploadedFile($imagePath, $imageFileName, 'image/png', null, true);
38     }
39
40     /**
41      * Get a test image that can be uploaded.
42      */
43     protected function getTestImage(string $fileName, ?string $testDataFileName = null, $mimeType = 'image/png'): UploadedFile
44     {
45         return new UploadedFile($this->getTestImageFilePath($testDataFileName), $fileName, $mimeType, null, true);
46     }
47
48     /**
49      * Get the raw file data for the test image.
50      *
51      * @return false|string
52      */
53     protected function getTestImageContent()
54     {
55         return file_get_contents($this->getTestImageFilePath());
56     }
57
58     /**
59      * Get the path for a test image.
60      */
61     protected function getTestImagePath(string $type, string $fileName): string
62     {
63         return '/uploads/images/' . $type . '/' . date('Y-m') . '/' . $fileName;
64     }
65
66     /**
67      * Uploads an image with the given name.
68      *
69      * @param $name
70      * @param int    $uploadedTo
71      * @param string $contentType
72      *
73      * @return \Illuminate\Foundation\Testing\TestResponse
74      */
75     protected function uploadImage($name, $uploadedTo = 0, $contentType = 'image/png', ?string $testDataFileName = null)
76     {
77         $file = $this->getTestImage($name, $testDataFileName, $contentType);
78
79         return $this->withHeader('Content-Type', $contentType)
80             ->call('POST', '/images/gallery', ['uploaded_to' => $uploadedTo], [], ['file' => $file], []);
81     }
82
83     /**
84      * Upload a new gallery image.
85      * Returns the image name.
86      * Can provide a page to relate the image to.
87      *
88      * @return array{name: string, path: string, page: Page, response: stdClass}
89      */
90     protected function uploadGalleryImage(Page $page = null, string $testDataFileName = null, string $contentType = 'image/png')
91     {
92         if ($page === null) {
93             $page = Page::query()->first();
94         }
95
96         $imageName = $testDataFileName ?? 'first-image.png';
97         $relPath = $this->getTestImagePath('gallery', $imageName);
98         $this->deleteImage($relPath);
99
100         $upload = $this->uploadImage($imageName, $page->id, $contentType, $testDataFileName);
101         $upload->assertStatus(200);
102
103         return [
104             'name'     => $imageName,
105             'path'     => $relPath,
106             'page'     => $page,
107             'response' => json_decode($upload->getContent()),
108         ];
109     }
110
111     /**
112      * Delete an uploaded image.
113      */
114     protected function deleteImage(string $relPath)
115     {
116         $path = public_path($relPath);
117         if (file_exists($path)) {
118             unlink($path);
119         }
120     }
121 }