]> BookStack Code Mirror - bookstack/blob - tests/ImageTest.php
Fixed German translations for notifications
[bookstack] / tests / ImageTest.php
1 <?php namespace Tests;
2
3 use BookStack\Image;
4 use BookStack\Page;
5 use BookStack\Repos\EntityRepo;
6 use BookStack\Services\ImageService;
7
8 class ImageTest extends TestCase
9 {
10     /**
11      * Get the path to our basic test image.
12      * @return string
13      */
14     protected function getTestImageFilePath()
15     {
16         return base_path('tests/test-data/test-image.png');
17     }
18
19     /**
20      * Get a test image that can be uploaded
21      * @param $fileName
22      * @return \Illuminate\Http\UploadedFile
23      */
24     protected function getTestImage($fileName)
25     {
26         return new \Illuminate\Http\UploadedFile($this->getTestImageFilePath(), $fileName, 'image/png', 5238);
27     }
28
29     /**
30      * Get the path for a test image.
31      * @param $type
32      * @param $fileName
33      * @return string
34      */
35     protected function getTestImagePath($type, $fileName)
36     {
37         return '/uploads/images/' . $type . '/' . Date('Y-m-M') . '/' . $fileName;
38     }
39
40     /**
41      * Uploads an image with the given name.
42      * @param $name
43      * @param int $uploadedTo
44      * @return \Illuminate\Foundation\Testing\TestResponse
45      */
46     protected function uploadImage($name, $uploadedTo = 0)
47     {
48         $file = $this->getTestImage($name);
49         return $this->call('POST', '/images/gallery/upload', ['uploaded_to' => $uploadedTo], [], ['file' => $file], []);
50     }
51
52     /**
53      * Delete an uploaded image.
54      * @param $relPath
55      */
56     protected function deleteImage($relPath)
57     {
58         $path = public_path($relPath);
59         if (file_exists($path)) {
60             unlink($path);
61         }
62     }
63
64
65     public function test_image_upload()
66     {
67         $page = Page::first();
68         $admin = $this->getAdmin();
69         $this->actingAs($admin);
70
71         $imageName = 'first-image.png';
72         $relPath = $this->getTestImagePath('gallery', $imageName);
73         $this->deleteImage($relPath);
74
75         $upload = $this->uploadImage($imageName, $page->id);
76         $upload->assertStatus(200);
77
78         $this->assertTrue(file_exists(public_path($relPath)), 'Uploaded image not found at path: '. public_path($relPath));
79
80         $this->deleteImage($relPath);
81
82         $this->assertDatabaseHas('images', [
83             'url' => $this->baseUrl . $relPath,
84             'type' => 'gallery',
85             'uploaded_to' => $page->id,
86             'path' => $relPath,
87             'created_by' => $admin->id,
88             'updated_by' => $admin->id,
89             'name' => $imageName
90         ]);
91     }
92
93     public function test_secure_images_uploads_to_correct_place()
94     {
95         config()->set('filesystems.default', 'local_secure');
96         $this->asEditor();
97         $galleryFile = $this->getTestImage('my-secure-test-upload');
98         $page = Page::first();
99         $expectedPath = storage_path('uploads/images/gallery/' . Date('Y-m-M') . '/my-secure-test-upload');
100
101         $upload = $this->call('POST', '/images/gallery/upload', ['uploaded_to' => $page->id], [], ['file' => $galleryFile], []);
102         $upload->assertStatus(200);
103
104         $this->assertTrue(file_exists($expectedPath), 'Uploaded image not found at path: '. $expectedPath);
105
106         if (file_exists($expectedPath)) {
107             unlink($expectedPath);
108         }
109     }
110
111     public function test_secure_images_included_in_exports()
112     {
113         config()->set('filesystems.default', 'local_secure');
114         $this->asEditor();
115         $galleryFile = $this->getTestImage('my-secure-test-upload');
116         $page = Page::first();
117         $expectedPath = storage_path('uploads/images/gallery/' . Date('Y-m-M') . '/my-secure-test-upload');
118
119         $upload = $this->call('POST', '/images/gallery/upload', ['uploaded_to' => $page->id], [], ['file' => $galleryFile], []);
120         $imageUrl = json_decode($upload->getContent(), true)['url'];
121         $page->html .= "<img src=\"{$imageUrl}\">";
122         $page->save();
123         $upload->assertStatus(200);
124
125         $encodedImageContent = base64_encode(file_get_contents($expectedPath));
126         $export = $this->get($page->getUrl('/export/html'));
127         $this->assertTrue(str_contains($export->getContent(), $encodedImageContent), 'Uploaded image in export content');
128
129         if (file_exists($expectedPath)) {
130             unlink($expectedPath);
131         }
132     }
133
134     public function test_system_images_remain_public()
135     {
136         config()->set('filesystems.default', 'local_secure');
137         $this->asEditor();
138         $galleryFile = $this->getTestImage('my-system-test-upload');
139         $page = Page::first();
140         $expectedPath = public_path('uploads/images/system/' . Date('Y-m-M') . '/my-system-test-upload');
141
142         $upload = $this->call('POST', '/images/system/upload', ['uploaded_to' => $page->id], [], ['file' => $galleryFile], []);
143         $upload->assertStatus(200);
144
145         $this->assertTrue(file_exists($expectedPath), 'Uploaded image not found at path: '. $expectedPath);
146
147         if (file_exists($expectedPath)) {
148             unlink($expectedPath);
149         }
150     }
151
152     public function test_image_delete()
153     {
154         $page = Page::first();
155         $this->asAdmin();
156         $imageName = 'first-image.png';
157
158         $this->uploadImage($imageName, $page->id);
159         $image = Image::first();
160         $relPath = $this->getTestImagePath('gallery', $imageName);
161
162         $delete = $this->delete( '/images/' . $image->id);
163         $delete->assertStatus(200);
164
165         $this->assertDatabaseMissing('images', [
166             'url' => $this->baseUrl . $relPath,
167             'type' => 'gallery'
168         ]);
169
170         $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded image has not been deleted as expected');
171     }
172
173     public function testBase64Get()
174     {
175         $page = Page::first();
176         $this->asAdmin();
177         $imageName = 'first-image.png';
178
179         $this->uploadImage($imageName, $page->id);
180         $image = Image::first();
181
182         $imageGet = $this->getJson("/images/base64/{$image->id}");
183         $imageGet->assertJson([
184             'content' => 'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAIAAAACDbGyAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4gEcDCo5iYNs+gAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAAAFElEQVQI12O0jN/KgASYGFABqXwAZtoBV6Sl3hIAAAAASUVORK5CYII='
185         ]);
186     }
187
188     public function test_drawing_base64_upload()
189     {
190         $page = Page::first();
191         $editor = $this->getEditor();
192         $this->actingAs($editor);
193
194         $upload = $this->postJson('images/drawing/upload', [
195             'uploaded_to' => $page->id,
196             'image' => 'image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAIAAAACDbGyAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4gEcDCo5iYNs+gAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAAAFElEQVQI12O0jN/KgASYGFABqXwAZtoBV6Sl3hIAAAAASUVORK5CYII='
197         ]);
198
199         $upload->assertStatus(200);
200         $upload->assertJson([
201             'type' => 'drawio',
202             'uploaded_to' => $page->id,
203             'created_by' => $editor->id,
204             'updated_by' => $editor->id,
205         ]);
206
207         $image = Image::where('type', '=', 'drawio')->first();
208         $this->assertTrue(file_exists(public_path($image->path)), 'Uploaded image not found at path: '. public_path($image->path));
209
210         $testImageData = file_get_contents($this->getTestImageFilePath());
211         $uploadedImageData = file_get_contents(public_path($image->path));
212         $this->assertTrue($testImageData === $uploadedImageData, "Uploaded image file data does not match our test image as expected");
213     }
214
215     public function test_user_images_deleted_on_user_deletion()
216     {
217         $editor = $this->getEditor();
218         $this->actingAs($editor);
219
220         $imageName = 'profile.png';
221         $relPath = $this->getTestImagePath('gallery', $imageName);
222         $this->deleteImage($relPath);
223
224         $file = $this->getTestImage($imageName);
225         $this->call('POST', '/images/user/upload', [], [], ['file' => $file], []);
226         $this->call('POST', '/images/user/upload', [], [], ['file' => $file], []);
227
228         $profileImages = Image::where('type', '=', 'user')->where('created_by', '=', $editor->id)->get();
229         $this->assertTrue($profileImages->count() === 2, "Found profile images does not match upload count");
230
231         $userDelete = $this->asAdmin()->delete("/settings/users/{$editor->id}");
232         $userDelete->assertStatus(302);
233         $this->assertDatabaseMissing('images', [
234             'type' => 'user',
235             'created_by' => $editor->id
236         ]);
237     }
238
239     public function test_deleted_unused_images()
240     {
241         $page = Page::first();
242         $admin = $this->getAdmin();
243         $this->actingAs($admin);
244
245         $imageName = 'unused-image.png';
246         $relPath = $this->getTestImagePath('gallery', $imageName);
247         $this->deleteImage($relPath);
248
249         $upload = $this->uploadImage($imageName, $page->id);
250         $upload->assertStatus(200);
251         $image = Image::where('type', '=', 'gallery')->first();
252
253         $entityRepo = app(EntityRepo::class);
254         $entityRepo->updatePage($page, $page->book_id, [
255             'name' => $page->name,
256             'html' => $page->html . "<img src=\"{$image->url}\">",
257             'summary' => ''
258         ]);
259
260         // Ensure no images are reported as deletable
261         $imageService = app(ImageService::class);
262         $toDelete = $imageService->deleteUnusedImages(true, true);
263         $this->assertCount(0, $toDelete);
264
265         // Save a revision of our page without the image;
266         $entityRepo->updatePage($page, $page->book_id, [
267             'name' => $page->name,
268             'html' => "<p>Hello</p>",
269             'summary' => ''
270         ]);
271
272         // Ensure revision images are picked up okay
273         $imageService = app(ImageService::class);
274         $toDelete = $imageService->deleteUnusedImages(true, true);
275         $this->assertCount(0, $toDelete);
276         $toDelete = $imageService->deleteUnusedImages(false, true);
277         $this->assertCount(1, $toDelete);
278
279         // Check image is found when revisions are destroyed
280         $page->revisions()->delete();
281         $toDelete = $imageService->deleteUnusedImages(true, true);
282         $this->assertCount(1, $toDelete);
283
284         // Check the image is deleted
285         $absPath = public_path($relPath);
286         $this->assertTrue(file_exists($absPath), "Existing uploaded file at path {$absPath} exists");
287         $toDelete = $imageService->deleteUnusedImages(true, false);
288         $this->assertCount(1, $toDelete);
289         $this->assertFalse(file_exists($absPath));
290
291         $this->deleteImage($relPath);
292     }
293
294 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.