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