Dan Brown

Added in attachment tests

...@@ -34,7 +34,6 @@ class FileController extends Controller ...@@ -34,7 +34,6 @@ class FileController extends Controller
34 */ 34 */
35 public function upload(Request $request) 35 public function upload(Request $request)
36 { 36 {
37 - // TODO - ensure uploads are deleted on page delete.
38 $this->validate($request, [ 37 $this->validate($request, [
39 'uploaded_to' => 'required|integer|exists:pages,id', 38 'uploaded_to' => 'required|integer|exists:pages,id',
40 'file' => 'required|file' 39 'file' => 'required|file'
......
...@@ -639,6 +639,7 @@ module.exports = function (ngApp, events) { ...@@ -639,6 +639,7 @@ module.exports = function (ngApp, events) {
639 * @param fileLink 639 * @param fileLink
640 */ 640 */
641 $scope.attachLinkSubmit = function(file) { 641 $scope.attachLinkSubmit = function(file) {
642 + file.uploaded_to = pageId;
642 $http.post('/files/link', file).then(resp => { 643 $http.post('/files/link', file).then(resp => {
643 $scope.files.unshift(resp.data); 644 $scope.files.unshift(resp.data);
644 events.emit('success', 'Link attached'); 645 events.emit('success', 'Link attached');
......
1 1
2 <div class="book-tree" ng-non-bindable> 2 <div class="book-tree" ng-non-bindable>
3 3
4 - @if ($page->files->count() > 0) 4 + @if (isset($page) && $page->files->count() > 0)
5 <h6 class="text-muted">Attachments</h6> 5 <h6 class="text-muted">Attachments</h6>
6 @foreach($page->files as $file) 6 @foreach($page->files as $file)
7 <div class="attachment"> 7 <div class="attachment">
......
1 +<?php
2 +
3 +class AttachmentTest extends TestCase
4 +{
5 + /**
6 + * Get a test file that can be uploaded
7 + * @param $fileName
8 + * @return \Illuminate\Http\UploadedFile
9 + */
10 + protected function getTestFile($fileName)
11 + {
12 + return new \Illuminate\Http\UploadedFile(base_path('tests/test-data/test-file.txt'), $fileName, 'text/plain', 55, null, true);
13 + }
14 +
15 + /**
16 + * Uploads a file with the given name.
17 + * @param $name
18 + * @param int $uploadedTo
19 + * @return string
20 + */
21 + protected function uploadFile($name, $uploadedTo = 0)
22 + {
23 + $file = $this->getTestFile($name);
24 + return $this->call('POST', '/files/upload', ['uploaded_to' => $uploadedTo], [], ['file' => $file], []);
25 + }
26 +
27 + /**
28 + * Get the expected upload path for a file.
29 + * @param $fileName
30 + * @return string
31 + */
32 + protected function getUploadPath($fileName)
33 + {
34 + return 'uploads/files/' . Date('Y-m-M') . '/' . $fileName;
35 + }
36 +
37 + /**
38 + * Delete all uploaded files.
39 + * To assist with cleanup.
40 + */
41 + protected function deleteUploads()
42 + {
43 + $fileService = $this->app->make(\BookStack\Services\FileService::class);
44 + foreach (\BookStack\File::all() as $file) {
45 + $fileService->deleteFile($file);
46 + }
47 + }
48 +
49 + public function test_file_upload()
50 + {
51 + $page = \BookStack\Page::first();
52 + $this->asAdmin();
53 + $admin = $this->getAdmin();
54 + $fileName = 'upload_test_file.txt';
55 +
56 + $expectedResp = [
57 + 'name' => $fileName,
58 + 'uploaded_to'=> $page->id,
59 + 'extension' => 'txt',
60 + 'order' => 1,
61 + 'created_by' => $admin->id,
62 + 'updated_by' => $admin->id,
63 + 'path' => $this->getUploadPath($fileName)
64 + ];
65 +
66 + $this->uploadFile($fileName, $page->id);
67 + $this->assertResponseOk();
68 + $this->seeJsonContains($expectedResp);
69 + $this->seeInDatabase('files', $expectedResp);
70 +
71 + $this->deleteUploads();
72 + }
73 +
74 + public function test_file_display_and_access()
75 + {
76 + $page = \BookStack\Page::first();
77 + $this->asAdmin();
78 + $admin = $this->getAdmin();
79 + $fileName = 'upload_test_file.txt';
80 +
81 + $this->uploadFile($fileName, $page->id);
82 + $this->assertResponseOk();
83 + $this->visit($page->getUrl())
84 + ->seeLink($fileName)
85 + ->click($fileName)
86 + ->see('Hi, This is a test file for testing the upload process.');
87 +
88 + $this->deleteUploads();
89 + }
90 +
91 + public function test_attaching_link_to_page()
92 + {
93 + $page = \BookStack\Page::first();
94 + $admin = $this->getAdmin();
95 + $this->asAdmin();
96 +
97 + $this->call('POST', 'files/link', [
98 + 'link' => 'https://example.com',
99 + 'name' => 'Example Attachment Link',
100 + 'uploaded_to' => $page->id,
101 + ]);
102 +
103 + $expectedResp = [
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,
109 + 'external' => true,
110 + 'order' => 1,
111 + 'extension' => ''
112 + ];
113 +
114 + $this->assertResponseOk();
115 + $this->seeJsonContains($expectedResp);
116 + $this->seeInDatabase('files', $expectedResp);
117 +
118 + $this->visit($page->getUrl())->seeLink('Example Attachment Link')
119 + ->click('Example Attachment Link')->seePageIs('https://example.com');
120 +
121 + $this->deleteUploads();
122 + }
123 +
124 + public function test_attachment_updating()
125 + {
126 + $page = \BookStack\Page::first();
127 + $this->asAdmin();
128 +
129 + $this->call('POST', 'files/link', [
130 + 'link' => 'https://example.com',
131 + 'name' => 'Example Attachment Link',
132 + 'uploaded_to' => $page->id,
133 + ]);
134 +
135 + $attachmentId = \BookStack\File::first()->id;
136 +
137 + $this->call('PUT', 'files/' . $attachmentId, [
138 + 'uploaded_to' => $page->id,
139 + 'name' => 'My new attachment name',
140 + 'link' => 'https://test.example.com'
141 + ]);
142 +
143 + $expectedResp = [
144 + 'path' => 'https://test.example.com',
145 + 'name' => 'My new attachment name',
146 + 'uploaded_to' => $page->id
147 + ];
148 +
149 + $this->assertResponseOk();
150 + $this->seeJsonContains($expectedResp);
151 + $this->seeInDatabase('files', $expectedResp);
152 +
153 + $this->deleteUploads();
154 + }
155 +
156 + public function test_file_deletion()
157 + {
158 + $page = \BookStack\Page::first();
159 + $this->asAdmin();
160 + $fileName = 'deletion_test.txt';
161 + $this->uploadFile($fileName, $page->id);
162 +
163 + $filePath = base_path('storage/' . $this->getUploadPath($fileName));
164 +
165 + $this->assertTrue(file_exists($filePath), 'File at path ' . $filePath . ' does not exist');
166 +
167 + $attachmentId = \BookStack\File::first()->id;
168 + $this->call('DELETE', 'files/' . $attachmentId);
169 +
170 + $this->dontSeeInDatabase('files', [
171 + 'name' => $fileName
172 + ]);
173 + $this->assertFalse(file_exists($filePath), 'File at path ' . $filePath . ' was not deleted as expected');
174 +
175 + $this->deleteUploads();
176 + }
177 +
178 + public function test_attachment_deletion_on_page_deletion()
179 + {
180 + $page = \BookStack\Page::first();
181 + $this->asAdmin();
182 + $fileName = 'deletion_test.txt';
183 + $this->uploadFile($fileName, $page->id);
184 +
185 + $filePath = base_path('storage/' . $this->getUploadPath($fileName));
186 +
187 + $this->assertTrue(file_exists($filePath), 'File at path ' . $filePath . ' does not exist');
188 + $this->seeInDatabase('files', [
189 + 'name' => $fileName
190 + ]);
191 +
192 + $this->call('DELETE', $page->getUrl());
193 +
194 + $this->dontSeeInDatabase('files', [
195 + 'name' => $fileName
196 + ]);
197 + $this->assertFalse(file_exists($filePath), 'File at path ' . $filePath . ' was not deleted as expected');
198 +
199 + $this->deleteUploads();
200 + }
201 +}
...@@ -10,7 +10,7 @@ class ImageTest extends TestCase ...@@ -10,7 +10,7 @@ class ImageTest extends TestCase
10 */ 10 */
11 protected function getTestImage($fileName) 11 protected function getTestImage($fileName)
12 { 12 {
13 - return new \Illuminate\Http\UploadedFile(base_path('tests/test-image.jpg'), $fileName, 'image/jpeg', 5238); 13 + return new \Illuminate\Http\UploadedFile(base_path('tests/test-data/test-image.jpg'), $fileName, 'image/jpeg', 5238);
14 } 14 }
15 15
16 /** 16 /**
......
1 +Hi, This is a test file for testing the upload process.
...\ No newline at end of file ...\ No newline at end of file