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