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