]> BookStack Code Mirror - bookstack/blob - tests/Uploads/UsesImages.php
Fixes padding issues of the sidebar's items
[bookstack] / tests / Uploads / UsesImages.php
1 <?php
2
3 namespace Tests\Uploads;
4
5 use BookStack\Entities\Models\Page;
6 use Illuminate\Http\UploadedFile;
7
8 trait UsesImages
9 {
10     /**
11      * Get the path to a file in the test-data-directory.
12      */
13     protected function getTestImageFilePath(?string $fileName = null): string
14     {
15         if (is_null($fileName)) {
16             $fileName = 'test-image.png';
17         }
18
19         return base_path('tests/test-data/' . $fileName);
20     }
21
22     /**
23      * Creates a new temporary image file using the given name,
24      * with the content decoded from the given bas64 file name.
25      * Is generally used for testing sketchy files that could trip AV.
26      */
27     protected function newTestImageFromBase64(string $base64FileName, $imageFileName): UploadedFile
28     {
29         $imagePath = implode(DIRECTORY_SEPARATOR, [sys_get_temp_dir(), $imageFileName]);
30         $base64FilePath = $this->getTestImageFilePath($base64FileName);
31         $data = file_get_contents($base64FilePath);
32         $decoded = base64_decode($data);
33         file_put_contents($imagePath, $decoded);
34
35         return new UploadedFile($imagePath, $imageFileName, 'image/png', null, true);
36     }
37
38     /**
39      * Get a test image that can be uploaded.
40      */
41     protected function getTestImage(string $fileName, ?string $testDataFileName = null): UploadedFile
42     {
43         return new UploadedFile($this->getTestImageFilePath($testDataFileName), $fileName, 'image/png', null, true);
44     }
45
46     /**
47      * Get the raw file data for the test image.
48      *
49      * @return false|string
50      */
51     protected function getTestImageContent()
52     {
53         return file_get_contents($this->getTestImageFilePath());
54     }
55
56     /**
57      * Get the path for a test image.
58      */
59     protected function getTestImagePath(string $type, string $fileName): string
60     {
61         return '/uploads/images/' . $type . '/' . date('Y-m') . '/' . $fileName;
62     }
63
64     /**
65      * Uploads an image with the given name.
66      *
67      * @param $name
68      * @param int    $uploadedTo
69      * @param string $contentType
70      *
71      * @return \Illuminate\Foundation\Testing\TestResponse
72      */
73     protected function uploadImage($name, $uploadedTo = 0, $contentType = 'image/png', ?string $testDataFileName = null)
74     {
75         $file = $this->getTestImage($name, $testDataFileName);
76
77         return $this->withHeader('Content-Type', $contentType)
78             ->call('POST', '/images/gallery', ['uploaded_to' => $uploadedTo], [], ['file' => $file], []);
79     }
80
81     /**
82      * Upload a new gallery image.
83      * Returns the image name.
84      * Can provide a page to relate the image to.
85      *
86      * @param Page|null $page
87      *
88      * @return array
89      */
90     protected function uploadGalleryImage(Page $page = null, ?string $testDataFileName = null)
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, 'image/png', $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 }