]> BookStack Code Mirror - bookstack/blob - tests/Uploads/UsesImages.php
Added crude example of captcha usage
[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()
14     {
15         return base_path('tests/test-data/test-image.png');
16     }
17
18     /**
19      * Get a test image that can be uploaded
20      * @param $fileName
21      * @return UploadedFile
22      */
23     protected function getTestImage($fileName)
24     {
25         return new UploadedFile($this->getTestImageFilePath(), $fileName, 'image/png', 5238, null, true);
26     }
27
28     /**
29      * Get the raw file data for the test image.
30      * @return false|string
31      */
32     protected function getTestImageContent()
33     {
34         return file_get_contents($this->getTestImageFilePath());
35     }
36
37     /**
38      * Get the path for a test image.
39      * @param $type
40      * @param $fileName
41      * @return string
42      */
43     protected function getTestImagePath($type, $fileName)
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')
56     {
57         $file = $this->getTestImage($name);
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)
70     {
71         if ($page === null) {
72             $page = Page::query()->first();
73         }
74
75         $imageName = 'first-image.png';
76         $relPath = $this->getTestImagePath('gallery', $imageName);
77         $this->deleteImage($relPath);
78
79         $upload = $this->uploadImage($imageName, $page->id);
80         $upload->assertStatus(200);
81         return [
82             'name' => $imageName,
83             'path' => $relPath,
84             'page' => $page
85         ];
86     }
87
88     /**
89      * Delete an uploaded image.
90      * @param $relPath
91      */
92     protected function deleteImage($relPath)
93     {
94         $path = public_path($relPath);
95         if (file_exists($path)) {
96             unlink($path);
97         }
98     }
99
100 }