1 <?php namespace Tests\Uploads;
3 use BookStack\Uploads\Attachment;
4 use BookStack\Entities\Page;
5 use BookStack\Auth\Permissions\PermissionService;
8 class AttachmentTest extends TestCase
11 * Get a test file that can be uploaded
13 * @return \Illuminate\Http\UploadedFile
15 protected function getTestFile($fileName)
17 return new \Illuminate\Http\UploadedFile(base_path('tests/test-data/test-file.txt'), $fileName, 'text/plain', 55, null, true);
21 * Uploads a file with the given name.
23 * @param int $uploadedTo
24 * @return \Illuminate\Foundation\Testing\TestResponse
26 protected function uploadFile($name, $uploadedTo = 0)
28 $file = $this->getTestFile($name);
29 return $this->call('POST', '/attachments/upload', ['uploaded_to' => $uploadedTo], [], ['file' => $file], []);
33 * Delete all uploaded files.
34 * To assist with cleanup.
36 protected function deleteUploads()
38 $fileService = $this->app->make(\BookStack\Uploads\AttachmentService::class);
39 foreach (\BookStack\Uploads\Attachment::all() as $file) {
40 $fileService->deleteFile($file);
44 public function test_file_upload()
46 $page = Page::first();
48 $admin = $this->getAdmin();
49 $fileName = 'upload_test_file.txt';
53 'uploaded_to'=> $page->id,
56 'created_by' => $admin->id,
57 'updated_by' => $admin->id,
60 $upload = $this->uploadFile($fileName, $page->id);
61 $upload->assertStatus(200);
63 $attachment = Attachment::query()->orderBy('id', 'desc')->first();
64 $expectedResp['path'] = $attachment->path;
66 $upload->assertJson($expectedResp);
67 $this->assertDatabaseHas('attachments', $expectedResp);
69 $this->deleteUploads();
72 public function test_file_upload_does_not_use_filename()
74 $page = Page::first();
75 $fileName = 'upload_test_file.txt';
78 $upload = $this->asAdmin()->uploadFile($fileName, $page->id);
79 $upload->assertStatus(200);
81 $attachment = Attachment::query()->orderBy('id', 'desc')->first();
82 $this->assertStringNotContainsString($fileName, $attachment->path);
83 $this->assertStringEndsWith('.txt', $attachment->path);
86 public function test_file_display_and_access()
88 $page = Page::first();
90 $fileName = 'upload_test_file.txt';
92 $upload = $this->uploadFile($fileName, $page->id);
93 $upload->assertStatus(200);
94 $attachment = Attachment::orderBy('id', 'desc')->take(1)->first();
96 $pageGet = $this->get($page->getUrl());
97 $pageGet->assertSeeText($fileName);
98 $pageGet->assertSee($attachment->getUrl());
100 $attachmentGet = $this->get($attachment->getUrl());
101 $attachmentGet->assertSee('Hi, This is a test file for testing the upload process.');
103 $this->deleteUploads();
106 public function test_attaching_link_to_page()
108 $page = Page::first();
109 $admin = $this->getAdmin();
112 $linkReq = $this->call('POST', 'attachments/link', [
113 'link' => 'https://example.com',
114 'name' => 'Example Attachment Link',
115 'uploaded_to' => $page->id,
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,
129 $linkReq->assertStatus(200);
130 $linkReq->assertJson($expectedResp);
131 $this->assertDatabaseHas('attachments', $expectedResp);
132 $attachment = Attachment::orderBy('id', 'desc')->take(1)->first();
134 $pageGet = $this->get($page->getUrl());
135 $pageGet->assertSeeText('Example Attachment Link');
136 $pageGet->assertSee($attachment->getUrl());
138 $attachmentGet = $this->get($attachment->getUrl());
139 $attachmentGet->assertRedirect('https://example.com');
141 $this->deleteUploads();
144 public function test_attachment_updating()
146 $page = Page::first();
149 $this->call('POST', 'attachments/link', [
150 'link' => 'https://example.com',
151 'name' => 'Example Attachment Link',
152 'uploaded_to' => $page->id,
155 $attachmentId = \BookStack\Uploads\Attachment::first()->id;
157 $update = $this->call('PUT', 'attachments/' . $attachmentId, [
158 'uploaded_to' => $page->id,
159 'name' => 'My new attachment name',
160 'link' => 'https://test.example.com'
164 'path' => 'https://test.example.com',
165 'name' => 'My new attachment name',
166 'uploaded_to' => $page->id
169 $update->assertStatus(200);
170 $update->assertJson($expectedResp);
171 $this->assertDatabaseHas('attachments', $expectedResp);
173 $this->deleteUploads();
176 public function test_file_deletion()
178 $page = Page::first();
180 $fileName = 'deletion_test.txt';
181 $this->uploadFile($fileName, $page->id);
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');
187 $attachment = \BookStack\Uploads\Attachment::first();
188 $this->delete($attachment->getUrl());
190 $this->assertDatabaseMissing('attachments', [
193 $this->assertFalse(file_exists($filePath), 'File at path ' . $filePath . ' was not deleted as expected');
195 $this->deleteUploads();
198 public function test_attachment_deletion_on_page_deletion()
200 $page = Page::first();
202 $fileName = 'deletion_test.txt';
203 $this->uploadFile($fileName, $page->id);
205 $attachment = Attachment::query()->orderBy('id', 'desc')->first();
206 $filePath = storage_path($attachment->path);
208 $this->assertTrue(file_exists($filePath), 'File at path ' . $filePath . ' does not exist');
209 $this->assertDatabaseHas('attachments', [
213 $this->call('DELETE', $page->getUrl());
215 $this->assertDatabaseMissing('attachments', [
218 $this->assertFalse(file_exists($filePath), 'File at path ' . $filePath . ' was not deleted as expected');
220 $this->deleteUploads();
223 public function test_attachment_access_without_permission_shows_404()
225 $admin = $this->getAdmin();
226 $viewer = $this->getViewer();
227 $page = Page::first(); /** @var Page $page */
229 $this->actingAs($admin);
230 $fileName = 'permission_test.txt';
231 $this->uploadFile($fileName, $page->id);
232 $attachment = Attachment::orderBy('id', 'desc')->take(1)->first();
234 $page->restricted = true;
235 $page->permissions()->delete();
237 $page->rebuildPermissions();
238 $page->load('jointPermissions');
240 $this->actingAs($viewer);
241 $attachmentGet = $this->get($attachment->getUrl());
242 $attachmentGet->assertStatus(404);
243 $attachmentGet->assertSee("Attachment not found");
245 $this->deleteUploads();