]> BookStack Code Mirror - bookstack/blob - tests/ImageTest.php
Add socialite authentication for okta
[bookstack] / tests / ImageTest.php
1 <?php namespace Tests;
2
3 class ImageTest extends BrowserKitTest
4 {
5
6     /**
7      * Get a test image that can be uploaded
8      * @param $fileName
9      * @return \Illuminate\Http\UploadedFile
10      */
11     protected function getTestImage($fileName)
12     {
13         return new \Illuminate\Http\UploadedFile(base_path('tests/test-data/test-image.jpg'), $fileName, 'image/jpeg', 5238);
14     }
15
16     /**
17      * Get the path for a test image.
18      * @param $type
19      * @param $fileName
20      * @return string
21      */
22     protected function getTestImagePath($type, $fileName)
23     {
24         return '/uploads/images/' . $type . '/' . Date('Y-m-M') . '/' . $fileName;
25     }
26
27     /**
28      * Uploads an image with the given name.
29      * @param $name
30      * @param int $uploadedTo
31      * @return string
32      */
33     protected function uploadImage($name, $uploadedTo = 0)
34     {
35         $file = $this->getTestImage($name);
36         $this->call('POST', '/images/gallery/upload', ['uploaded_to' => $uploadedTo], [], ['file' => $file], []);
37         return $this->getTestImagePath('gallery', $name);
38     }
39
40     /**
41      * Delete an uploaded image.
42      * @param $relPath
43      */
44     protected function deleteImage($relPath)
45     {
46         unlink(public_path($relPath));
47     }
48
49
50     public function test_image_upload()
51     {
52         $page = \BookStack\Page::first();
53         $this->asAdmin();
54         $admin = $this->getAdmin();
55         $imageName = 'first-image.jpg';
56
57         $relPath = $this->uploadImage($imageName, $page->id);
58         $this->assertResponseOk();
59
60         $this->assertTrue(file_exists(public_path($relPath)), 'Uploaded image not found at path: '. public_path($relPath));
61
62         $this->deleteImage($relPath);
63
64         $this->seeInDatabase('images', [
65             'url' => $this->baseUrl . $relPath,
66             'type' => 'gallery',
67             'uploaded_to' => $page->id,
68             'path' => $relPath,
69             'created_by' => $admin->id,
70             'updated_by' => $admin->id,
71             'name' => $imageName
72         ]);
73
74     }
75
76     public function test_image_delete()
77     {
78         $page = \BookStack\Page::first();
79         $this->asAdmin();
80         $imageName = 'first-image.jpg';
81
82         $relPath = $this->uploadImage($imageName, $page->id);
83         $image = \BookStack\Image::first();
84
85         $this->call('DELETE', '/images/' . $image->id);
86         $this->assertResponseOk();
87
88         $this->dontSeeInDatabase('images', [
89             'url' => $this->baseUrl . $relPath,
90             'type' => 'gallery'
91         ]);
92
93         $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded image has not been deleted as expected');
94     }
95
96 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.