]> BookStack Code Mirror - bookstack/blob - tests/Uploads/AttachmentTest.php
Updated callout link formatting
[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 (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             'attachment_link_url' => 'https://example.com',
114             'attachment_link_name' => 'Example Attachment Link',
115             'attachment_link_uploaded_to' => $page->id,
116         ]);
117
118         $expectedData = [
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         $this->assertDatabaseHas('attachments', $expectedData);
131         $attachment = Attachment::orderBy('id', 'desc')->take(1)->first();
132
133         $pageGet = $this->get($page->getUrl());
134         $pageGet->assertSeeText('Example Attachment Link');
135         $pageGet->assertSee($attachment->getUrl());
136
137         $attachmentGet = $this->get($attachment->getUrl());
138         $attachmentGet->assertRedirect('https://example.com');
139
140         $this->deleteUploads();
141     }
142
143     public function test_attachment_updating()
144     {
145         $page = Page::first();
146         $this->asAdmin();
147
148         $this->call('POST', 'attachments/link', [
149             'attachment_link_url' => 'https://example.com',
150             'attachment_link_name' => 'Example Attachment Link',
151             'attachment_link_uploaded_to' => $page->id,
152         ]);
153
154         $attachmentId = Attachment::first()->id;
155
156         $update = $this->call('PUT', 'attachments/' . $attachmentId, [
157             'attachment_edit_name' => 'My new attachment name',
158             'attachment_edit_url' => 'https://test.example.com'
159         ]);
160
161         $expectedData = [
162             'id' => $attachmentId,
163             'path' => 'https://test.example.com',
164             'name' => 'My new attachment name',
165             'uploaded_to' => $page->id
166         ];
167
168         $update->assertStatus(200);
169         $this->assertDatabaseHas('attachments', $expectedData);
170
171         $this->deleteUploads();
172     }
173
174     public function test_file_deletion()
175     {
176         $page = Page::first();
177         $this->asAdmin();
178         $fileName = 'deletion_test.txt';
179         $this->uploadFile($fileName, $page->id);
180
181         $attachment = Attachment::query()->orderBy('id', 'desc')->first();
182         $filePath = storage_path($attachment->path);
183         $this->assertTrue(file_exists($filePath), 'File at path ' . $filePath . ' does not exist');
184
185         $attachment = Attachment::first();
186         $this->delete($attachment->getUrl());
187
188         $this->assertDatabaseMissing('attachments', [
189             'name' => $fileName
190         ]);
191         $this->assertFalse(file_exists($filePath), 'File at path ' . $filePath . ' was not deleted as expected');
192
193         $this->deleteUploads();
194     }
195
196     public function test_attachment_deletion_on_page_deletion()
197     {
198         $page = Page::first();
199         $this->asAdmin();
200         $fileName = 'deletion_test.txt';
201         $this->uploadFile($fileName, $page->id);
202
203         $attachment = Attachment::query()->orderBy('id', 'desc')->first();
204         $filePath = storage_path($attachment->path);
205
206         $this->assertTrue(file_exists($filePath), 'File at path ' . $filePath . ' does not exist');
207         $this->assertDatabaseHas('attachments', [
208             'name' => $fileName
209         ]);
210
211         $this->call('DELETE', $page->getUrl());
212
213         $this->assertDatabaseMissing('attachments', [
214             'name' => $fileName
215         ]);
216         $this->assertFalse(file_exists($filePath), 'File at path ' . $filePath . ' was not deleted as expected');
217
218         $this->deleteUploads();
219     }
220
221     public function test_attachment_access_without_permission_shows_404()
222     {
223         $admin = $this->getAdmin();
224         $viewer = $this->getViewer();
225         $page = Page::first(); /** @var Page $page */
226
227         $this->actingAs($admin);
228         $fileName = 'permission_test.txt';
229         $this->uploadFile($fileName, $page->id);
230         $attachment = Attachment::orderBy('id', 'desc')->take(1)->first();
231
232         $page->restricted = true;
233         $page->permissions()->delete();
234         $page->save();
235         $page->rebuildPermissions();
236         $page->load('jointPermissions');
237
238         $this->actingAs($viewer);
239         $attachmentGet = $this->get($attachment->getUrl());
240         $attachmentGet->assertStatus(404);
241         $attachmentGet->assertSee("Attachment not found");
242
243         $this->deleteUploads();
244     }
245 }