3 class AttachmentTest extends BrowserKitTest
6 * Get a test file that can be uploaded
8 * @return \Illuminate\Http\UploadedFile
10 protected function getTestFile($fileName)
12 return new \Illuminate\Http\UploadedFile(base_path('tests/test-data/test-file.txt'), $fileName, 'text/plain', 55, null, true);
16 * Uploads a file with the given name.
18 * @param int $uploadedTo
21 protected function uploadFile($name, $uploadedTo = 0)
23 $file = $this->getTestFile($name);
24 return $this->call('POST', '/attachments/upload', ['uploaded_to' => $uploadedTo], [], ['file' => $file], []);
28 * Get the expected upload path for a file.
32 protected function getUploadPath($fileName)
34 return 'uploads/files/' . Date('Y-m-M') . '/' . $fileName;
38 * Delete all uploaded files.
39 * To assist with cleanup.
41 protected function deleteUploads()
43 $fileService = $this->app->make(\BookStack\Services\AttachmentService::class);
44 foreach (\BookStack\Attachment::all() as $file) {
45 $fileService->deleteFile($file);
49 public function test_file_upload()
51 $page = \BookStack\Page::first();
53 $admin = $this->getAdmin();
54 $fileName = 'upload_test_file.txt';
58 'uploaded_to'=> $page->id,
61 'created_by' => $admin->id,
62 'updated_by' => $admin->id,
63 'path' => $this->getUploadPath($fileName)
66 $this->uploadFile($fileName, $page->id);
67 $this->assertResponseOk();
68 $this->seeJsonContains($expectedResp);
69 $this->seeInDatabase('attachments', $expectedResp);
71 $this->deleteUploads();
74 public function test_file_display_and_access()
76 $page = \BookStack\Page::first();
78 $fileName = 'upload_test_file.txt';
80 $this->uploadFile($fileName, $page->id);
81 $this->assertResponseOk();
82 $this->visit($page->getUrl())
85 ->see('Hi, This is a test file for testing the upload process.');
87 $this->deleteUploads();
90 public function test_attaching_link_to_page()
92 $page = \BookStack\Page::first();
93 $admin = $this->getAdmin();
96 $this->call('POST', 'attachments/link', [
97 'link' => 'https://example.com',
98 'name' => 'Example Attachment Link',
99 'uploaded_to' => $page->id,
103 'path' => 'https://example.com',
104 'name' => 'Example Attachment Link',
105 'uploaded_to' => $page->id,
106 'created_by' => $admin->id,
107 'updated_by' => $admin->id,
113 $this->assertResponseOk();
114 $this->seeJsonContains($expectedResp);
115 $this->seeInDatabase('attachments', $expectedResp);
117 $this->visit($page->getUrl())->seeLink('Example Attachment Link')
118 ->click('Example Attachment Link')->seePageIs('https://example.com');
120 $this->deleteUploads();
123 public function test_attachment_updating()
125 $page = \BookStack\Page::first();
128 $this->call('POST', 'attachments/link', [
129 'link' => 'https://example.com',
130 'name' => 'Example Attachment Link',
131 'uploaded_to' => $page->id,
134 $attachmentId = \BookStack\Attachment::first()->id;
136 $this->call('PUT', 'attachments/' . $attachmentId, [
137 'uploaded_to' => $page->id,
138 'name' => 'My new attachment name',
139 'link' => 'https://test.example.com'
143 'path' => 'https://test.example.com',
144 'name' => 'My new attachment name',
145 'uploaded_to' => $page->id
148 $this->assertResponseOk();
149 $this->seeJsonContains($expectedResp);
150 $this->seeInDatabase('attachments', $expectedResp);
152 $this->deleteUploads();
155 public function test_file_deletion()
157 $page = \BookStack\Page::first();
159 $fileName = 'deletion_test.txt';
160 $this->uploadFile($fileName, $page->id);
162 $filePath = base_path('storage/' . $this->getUploadPath($fileName));
164 $this->assertTrue(file_exists($filePath), 'File at path ' . $filePath . ' does not exist');
166 $attachmentId = \BookStack\Attachment::first()->id;
167 $this->call('DELETE', 'attachments/' . $attachmentId);
169 $this->dontSeeInDatabase('attachments', [
172 $this->assertFalse(file_exists($filePath), 'File at path ' . $filePath . ' was not deleted as expected');
174 $this->deleteUploads();
177 public function test_attachment_deletion_on_page_deletion()
179 $page = \BookStack\Page::first();
181 $fileName = 'deletion_test.txt';
182 $this->uploadFile($fileName, $page->id);
184 $filePath = base_path('storage/' . $this->getUploadPath($fileName));
186 $this->assertTrue(file_exists($filePath), 'File at path ' . $filePath . ' does not exist');
187 $this->seeInDatabase('attachments', [
191 $this->call('DELETE', $page->getUrl());
193 $this->dontSeeInDatabase('attachments', [
196 $this->assertFalse(file_exists($filePath), 'File at path ' . $filePath . ' was not deleted as expected');
198 $this->deleteUploads();