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