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