]> BookStack Code Mirror - bookstack/blob - tests/Uploads/UsesImages.php
Replace dots with something else on user create and edit screens
[bookstack] / tests / Uploads / UsesImages.php
1 <?php namespace Tests\Uploads;
2
3
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()
13     {
14         return base_path('tests/test-data/test-image.png');
15     }
16
17     /**
18      * Get a test image that can be uploaded
19      * @param $fileName
20      * @return UploadedFile
21      */
22     protected function getTestImage($fileName)
23     {
24         return new UploadedFile($this->getTestImageFilePath(), $fileName, 'image/png', 5238, null, true);
25     }
26
27     /**
28      * Get the raw file data for the test image.
29      * @return false|string
30      */
31     protected function getTestImageContent()
32     {
33         return file_get_contents($this->getTestImageFilePath());
34     }
35
36     /**
37      * Get the path for a test image.
38      * @param $type
39      * @param $fileName
40      * @return string
41      */
42     protected function getTestImagePath($type, $fileName)
43     {
44         return '/uploads/images/' . $type . '/' . Date('Y-m-M') . '/' . $fileName;
45     }
46
47     /**
48      * Uploads an image with the given name.
49      * @param $name
50      * @param int $uploadedTo
51      * @param string $contentType
52      * @return \Illuminate\Foundation\Testing\TestResponse
53      */
54     protected function uploadImage($name, $uploadedTo = 0, $contentType = 'image/png')
55     {
56         $file = $this->getTestImage($name);
57         return $this->withHeader('Content-Type', $contentType)
58             ->call('POST', '/images/gallery/upload', ['uploaded_to' => $uploadedTo], [], ['file' => $file], []);
59     }
60
61     /**
62      * Delete an uploaded image.
63      * @param $relPath
64      */
65     protected function deleteImage($relPath)
66     {
67         $path = public_path($relPath);
68         if (file_exists($path)) {
69             unlink($path);
70         }
71     }
72
73 }