3 class AttachmentTest extends TestCase
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', '/files/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\FileService::class);
44 foreach (\BookStack\File::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('files', $expectedResp);
71 $this->deleteUploads();
74 public function test_file_display_and_access()
76 $page = \BookStack\Page::first();
78 $admin = $this->getAdmin();
79 $fileName = 'upload_test_file.txt';
81 $this->uploadFile($fileName, $page->id);
82 $this->assertResponseOk();
83 $this->visit($page->getUrl())
86 ->see('Hi, This is a test file for testing the upload process.');
88 $this->deleteUploads();
91 public function test_attaching_link_to_page()
93 $page = \BookStack\Page::first();
94 $admin = $this->getAdmin();
97 $this->call('POST', 'files/link', [
98 'link' => 'https://example.com',
99 'name' => 'Example Attachment Link',
100 'uploaded_to' => $page->id,
104 'path' => 'https://example.com',
105 'name' => 'Example Attachment Link',
106 'uploaded_to' => $page->id,
107 'created_by' => $admin->id,
108 'updated_by' => $admin->id,
114 $this->assertResponseOk();
115 $this->seeJsonContains($expectedResp);
116 $this->seeInDatabase('files', $expectedResp);
118 $this->visit($page->getUrl())->seeLink('Example Attachment Link')
119 ->click('Example Attachment Link')->seePageIs('https://example.com');
121 $this->deleteUploads();
124 public function test_attachment_updating()
126 $page = \BookStack\Page::first();
129 $this->call('POST', 'files/link', [
130 'link' => 'https://example.com',
131 'name' => 'Example Attachment Link',
132 'uploaded_to' => $page->id,
135 $attachmentId = \BookStack\File::first()->id;
137 $this->call('PUT', 'files/' . $attachmentId, [
138 'uploaded_to' => $page->id,
139 'name' => 'My new attachment name',
140 'link' => 'https://test.example.com'
144 'path' => 'https://test.example.com',
145 'name' => 'My new attachment name',
146 'uploaded_to' => $page->id
149 $this->assertResponseOk();
150 $this->seeJsonContains($expectedResp);
151 $this->seeInDatabase('files', $expectedResp);
153 $this->deleteUploads();
156 public function test_file_deletion()
158 $page = \BookStack\Page::first();
160 $fileName = 'deletion_test.txt';
161 $this->uploadFile($fileName, $page->id);
163 $filePath = base_path('storage/' . $this->getUploadPath($fileName));
165 $this->assertTrue(file_exists($filePath), 'File at path ' . $filePath . ' does not exist');
167 $attachmentId = \BookStack\File::first()->id;
168 $this->call('DELETE', 'files/' . $attachmentId);
170 $this->dontSeeInDatabase('files', [
173 $this->assertFalse(file_exists($filePath), 'File at path ' . $filePath . ' was not deleted as expected');
175 $this->deleteUploads();
178 public function test_attachment_deletion_on_page_deletion()
180 $page = \BookStack\Page::first();
182 $fileName = 'deletion_test.txt';
183 $this->uploadFile($fileName, $page->id);
185 $filePath = base_path('storage/' . $this->getUploadPath($fileName));
187 $this->assertTrue(file_exists($filePath), 'File at path ' . $filePath . ' does not exist');
188 $this->seeInDatabase('files', [
192 $this->call('DELETE', $page->getUrl());
194 $this->dontSeeInDatabase('files', [
197 $this->assertFalse(file_exists($filePath), 'File at path ' . $filePath . ' was not deleted as expected');
199 $this->deleteUploads();