]> BookStack Code Mirror - bookstack/blob - tests/Uploads/UsesImages.php
Update settings.php
[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      * @param $type
43      * @param $fileName
44      * @return string
45      */
46     protected function getTestImagePath($type, $fileName)
47     {
48         return '/uploads/images/' . $type . '/' . Date('Y-m') . '/' . $fileName;
49     }
50
51     /**
52      * Uploads an image with the given name.
53      * @param $name
54      * @param int $uploadedTo
55      * @param string $contentType
56      * @return \Illuminate\Foundation\Testing\TestResponse
57      */
58     protected function uploadImage($name, $uploadedTo = 0, $contentType = 'image/png', ?string $testDataFileName = null)
59     {
60         $file = $this->getTestImage($name, $testDataFileName);
61         return $this->withHeader('Content-Type', $contentType)
62             ->call('POST', '/images/gallery', ['uploaded_to' => $uploadedTo], [], ['file' => $file], []);
63     }
64
65     /**
66      * Upload a new gallery image.
67      * Returns the image name.
68      * Can provide a page to relate the image to.
69      * @param Page|null $page
70      * @return array
71      */
72     protected function uploadGalleryImage(Page $page = null, ?string $testDataFileName = null)
73     {
74         if ($page === null) {
75             $page = Page::query()->first();
76         }
77
78         $imageName = $testDataFileName ?? 'first-image.png';
79         $relPath = $this->getTestImagePath('gallery', $imageName);
80         $this->deleteImage($relPath);
81
82         $upload = $this->uploadImage($imageName, $page->id, 'image/png', $testDataFileName);
83         $upload->assertStatus(200);
84         return [
85             'name' => $imageName,
86             'path' => $relPath,
87             'page' => $page,
88             'response' => json_decode($upload->getContent()),
89         ];
90     }
91
92     /**
93      * Delete an uploaded image.
94      * @param $relPath
95      */
96     protected function deleteImage($relPath)
97     {
98         $path = public_path($relPath);
99         if (file_exists($path)) {
100             unlink($path);
101         }
102     }
103
104 }