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 (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 'attachment_link_url' => 'https://example.com',
114 'attachment_link_name' => 'Example Attachment Link',
115 'attachment_link_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 $this->assertDatabaseHas('attachments', $expectedData);
131 $attachment = Attachment::orderBy('id', 'desc')->take(1)->first();
133 $pageGet = $this->get($page->getUrl());
134 $pageGet->assertSeeText('Example Attachment Link');
135 $pageGet->assertSee($attachment->getUrl());
137 $attachmentGet = $this->get($attachment->getUrl());
138 $attachmentGet->assertRedirect('https://example.com');
140 $this->deleteUploads();
143 public function test_attachment_updating()
145 $page = Page::first();
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,
154 $attachmentId = Attachment::first()->id;
156 $update = $this->call('PUT', 'attachments/' . $attachmentId, [
157 'attachment_edit_name' => 'My new attachment name',
158 'attachment_edit_url' => 'https://test.example.com'
162 'id' => $attachmentId,
163 'path' => 'https://test.example.com',
164 'name' => 'My new attachment name',
165 'uploaded_to' => $page->id
168 $update->assertStatus(200);
169 $this->assertDatabaseHas('attachments', $expectedData);
171 $this->deleteUploads();
174 public function test_file_deletion()
176 $page = Page::first();
178 $fileName = 'deletion_test.txt';
179 $this->uploadFile($fileName, $page->id);
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');
185 $attachment = Attachment::first();
186 $this->delete($attachment->getUrl());
188 $this->assertDatabaseMissing('attachments', [
191 $this->assertFalse(file_exists($filePath), 'File at path ' . $filePath . ' was not deleted as expected');
193 $this->deleteUploads();
196 public function test_attachment_deletion_on_page_deletion()
198 $page = Page::first();
200 $fileName = 'deletion_test.txt';
201 $this->uploadFile($fileName, $page->id);
203 $attachment = Attachment::query()->orderBy('id', 'desc')->first();
204 $filePath = storage_path($attachment->path);
206 $this->assertTrue(file_exists($filePath), 'File at path ' . $filePath . ' does not exist');
207 $this->assertDatabaseHas('attachments', [
211 $this->call('DELETE', $page->getUrl());
213 $this->assertDatabaseMissing('attachments', [
216 $this->assertFalse(file_exists($filePath), 'File at path ' . $filePath . ' was not deleted as expected');
218 $this->deleteUploads();
221 public function test_attachment_access_without_permission_shows_404()
223 $admin = $this->getAdmin();
224 $viewer = $this->getViewer();
225 $page = Page::first(); /** @var Page $page */
227 $this->actingAs($admin);
228 $fileName = 'permission_test.txt';
229 $this->uploadFile($fileName, $page->id);
230 $attachment = Attachment::orderBy('id', 'desc')->take(1)->first();
232 $page->restricted = true;
233 $page->permissions()->delete();
235 $page->rebuildPermissions();
236 $page->load('jointPermissions');
238 $this->actingAs($viewer);
239 $attachmentGet = $this->get($attachment->getUrl());
240 $attachmentGet->assertStatus(404);
241 $attachmentGet->assertSee("Attachment not found");
243 $this->deleteUploads();