]> BookStack Code Mirror - bookstack/blob - tests/Uploads/AttachmentTest.php
Update settings.php
[bookstack] / tests / Uploads / AttachmentTest.php
1 <?php namespace Tests\Uploads;
2
3 use BookStack\Uploads\Attachment;
4 use BookStack\Entities\Page;
5 use BookStack\Auth\Permissions\PermissionService;
6 use Tests\TestCase;
7
8 class AttachmentTest extends TestCase
9 {
10     /**
11      * Get a test file that can be uploaded
12      * @param $fileName
13      * @return \Illuminate\Http\UploadedFile
14      */
15     protected function getTestFile($fileName)
16     {
17         return new \Illuminate\Http\UploadedFile(base_path('tests/test-data/test-file.txt'), $fileName, 'text/plain', 55, null, true);
18     }
19
20     /**
21      * Uploads a file with the given name.
22      * @param $name
23      * @param int $uploadedTo
24      * @return \Illuminate\Foundation\Testing\TestResponse
25      */
26     protected function uploadFile($name, $uploadedTo = 0)
27     {
28         $file = $this->getTestFile($name);
29         return $this->call('POST', '/attachments/upload', ['uploaded_to' => $uploadedTo], [], ['file' => $file], []);
30     }
31
32     /**
33      * Delete all uploaded files.
34      * To assist with cleanup.
35      */
36     protected function deleteUploads()
37     {
38         $fileService = $this->app->make(\BookStack\Uploads\AttachmentService::class);
39         foreach (\BookStack\Uploads\Attachment::all() as $file) {
40             $fileService->deleteFile($file);
41         }
42     }
43
44     public function test_file_upload()
45     {
46         $page = Page::first();
47         $this->asAdmin();
48         $admin = $this->getAdmin();
49         $fileName = 'upload_test_file.txt';
50
51         $expectedResp = [
52             'name' => $fileName,
53             'uploaded_to'=> $page->id,
54             'extension' => 'txt',
55             'order' => 1,
56             'created_by' => $admin->id,
57             'updated_by' => $admin->id,
58         ];
59
60         $upload = $this->uploadFile($fileName, $page->id);
61         $upload->assertStatus(200);
62
63         $attachment = Attachment::query()->orderBy('id', 'desc')->first();
64         $expectedResp['path'] = $attachment->path;
65
66         $upload->assertJson($expectedResp);
67         $this->assertDatabaseHas('attachments', $expectedResp);
68
69         $this->deleteUploads();
70     }
71
72     public function test_file_upload_does_not_use_filename()
73     {
74         $page = Page::first();
75         $fileName = 'upload_test_file.txt';
76
77
78         $upload = $this->asAdmin()->uploadFile($fileName, $page->id);
79         $upload->assertStatus(200);
80
81         $attachment = Attachment::query()->orderBy('id', 'desc')->first();
82         $this->assertStringNotContainsString($fileName, $attachment->path);
83         $this->assertStringEndsWith('.txt', $attachment->path);
84     }
85
86     public function test_file_display_and_access()
87     {
88         $page = Page::first();
89         $this->asAdmin();
90         $fileName = 'upload_test_file.txt';
91
92         $upload = $this->uploadFile($fileName, $page->id);
93         $upload->assertStatus(200);
94         $attachment = Attachment::orderBy('id', 'desc')->take(1)->first();
95
96         $pageGet = $this->get($page->getUrl());
97         $pageGet->assertSeeText($fileName);
98         $pageGet->assertSee($attachment->getUrl());
99
100         $attachmentGet = $this->get($attachment->getUrl());
101         $attachmentGet->assertSee('Hi, This is a test file for testing the upload process.');
102
103         $this->deleteUploads();
104     }
105
106     public function test_attaching_link_to_page()
107     {
108         $page = Page::first();
109         $admin = $this->getAdmin();
110         $this->asAdmin();
111
112         $linkReq = $this->call('POST', 'attachments/link', [
113             'link' => 'https://example.com',
114             'name' => 'Example Attachment Link',
115             'uploaded_to' => $page->id,
116         ]);
117
118         $expectedResp = [
119             'path' => 'https://example.com',
120             'name' => 'Example Attachment Link',
121             'uploaded_to' => $page->id,
122             'created_by' => $admin->id,
123             'updated_by' => $admin->id,
124             'external' => true,
125             'order' => 1,
126             'extension' => ''
127         ];
128
129         $linkReq->assertStatus(200);
130         $linkReq->assertJson($expectedResp);
131         $this->assertDatabaseHas('attachments', $expectedResp);
132         $attachment = Attachment::orderBy('id', 'desc')->take(1)->first();
133
134         $pageGet = $this->get($page->getUrl());
135         $pageGet->assertSeeText('Example Attachment Link');
136         $pageGet->assertSee($attachment->getUrl());
137
138         $attachmentGet = $this->get($attachment->getUrl());
139         $attachmentGet->assertRedirect('https://example.com');
140
141         $this->deleteUploads();
142     }
143
144     public function test_attachment_updating()
145     {
146         $page = Page::first();
147         $this->asAdmin();
148
149         $this->call('POST', 'attachments/link', [
150             'link' => 'https://example.com',
151             'name' => 'Example Attachment Link',
152             'uploaded_to' => $page->id,
153         ]);
154
155         $attachmentId = \BookStack\Uploads\Attachment::first()->id;
156
157         $update = $this->call('PUT', 'attachments/' . $attachmentId, [
158             'uploaded_to' => $page->id,
159             'name' => 'My new attachment name',
160             'link' => 'https://test.example.com'
161         ]);
162
163         $expectedResp = [
164             'path' => 'https://test.example.com',
165             'name' => 'My new attachment name',
166             'uploaded_to' => $page->id
167         ];
168
169         $update->assertStatus(200);
170         $update->assertJson($expectedResp);
171         $this->assertDatabaseHas('attachments', $expectedResp);
172
173         $this->deleteUploads();
174     }
175
176     public function test_file_deletion()
177     {
178         $page = Page::first();
179         $this->asAdmin();
180         $fileName = 'deletion_test.txt';
181         $this->uploadFile($fileName, $page->id);
182
183         $attachment = Attachment::query()->orderBy('id', 'desc')->first();
184         $filePath = storage_path($attachment->path);
185         $this->assertTrue(file_exists($filePath), 'File at path ' . $filePath . ' does not exist');
186
187         $attachment = \BookStack\Uploads\Attachment::first();
188         $this->delete($attachment->getUrl());
189
190         $this->assertDatabaseMissing('attachments', [
191             'name' => $fileName
192         ]);
193         $this->assertFalse(file_exists($filePath), 'File at path ' . $filePath . ' was not deleted as expected');
194
195         $this->deleteUploads();
196     }
197
198     public function test_attachment_deletion_on_page_deletion()
199     {
200         $page = Page::first();
201         $this->asAdmin();
202         $fileName = 'deletion_test.txt';
203         $this->uploadFile($fileName, $page->id);
204
205         $attachment = Attachment::query()->orderBy('id', 'desc')->first();
206         $filePath = storage_path($attachment->path);
207
208         $this->assertTrue(file_exists($filePath), 'File at path ' . $filePath . ' does not exist');
209         $this->assertDatabaseHas('attachments', [
210             'name' => $fileName
211         ]);
212
213         $this->call('DELETE', $page->getUrl());
214
215         $this->assertDatabaseMissing('attachments', [
216             'name' => $fileName
217         ]);
218         $this->assertFalse(file_exists($filePath), 'File at path ' . $filePath . ' was not deleted as expected');
219
220         $this->deleteUploads();
221     }
222
223     public function test_attachment_access_without_permission_shows_404()
224     {
225         $admin = $this->getAdmin();
226         $viewer = $this->getViewer();
227         $page = Page::first(); /** @var Page $page */
228
229         $this->actingAs($admin);
230         $fileName = 'permission_test.txt';
231         $this->uploadFile($fileName, $page->id);
232         $attachment = Attachment::orderBy('id', 'desc')->take(1)->first();
233
234         $page->restricted = true;
235         $page->permissions()->delete();
236         $page->save();
237         $page->rebuildPermissions();
238         $page->load('jointPermissions');
239
240         $this->actingAs($viewer);
241         $attachmentGet = $this->get($attachment->getUrl());
242         $attachmentGet->assertStatus(404);
243         $attachmentGet->assertSee("Attachment not found");
244
245         $this->deleteUploads();
246     }
247 }