]> BookStack Code Mirror - bookstack/blob - tests/AttachmentTest.php
Added Swedish locale to config
[bookstack] / tests / AttachmentTest.php
1 <?php namespace Tests;
2
3 class AttachmentTest extends BrowserKitTest
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', '/attachments/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\AttachmentService::class);
44         foreach (\BookStack\Attachment::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('attachments', $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         $fileName = 'upload_test_file.txt';
79
80         $this->uploadFile($fileName, $page->id);
81         $this->assertResponseOk();
82         $this->visit($page->getUrl())
83             ->seeLink($fileName)
84             ->click($fileName)
85             ->see('Hi, This is a test file for testing the upload process.');
86
87         $this->deleteUploads();
88     }
89
90     public function test_attaching_link_to_page()
91     {
92         $page = \BookStack\Page::first();
93         $admin = $this->getAdmin();
94         $this->asAdmin();
95
96         $this->call('POST', 'attachments/link', [
97             'link' => 'https://example.com',
98             'name' => 'Example Attachment Link',
99             'uploaded_to' => $page->id,
100         ]);
101
102         $expectedResp = [
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,
108             'external' => true,
109             'order' => 1,
110             'extension' => ''
111         ];
112
113         $this->assertResponseOk();
114         $this->seeJsonContains($expectedResp);
115         $this->seeInDatabase('attachments', $expectedResp);
116
117         $this->visit($page->getUrl())->seeLink('Example Attachment Link')
118             ->click('Example Attachment Link')->seePageIs('https://example.com');
119
120         $this->deleteUploads();
121     }
122
123     public function test_attachment_updating()
124     {
125         $page = \BookStack\Page::first();
126         $this->asAdmin();
127
128         $this->call('POST', 'attachments/link', [
129             'link' => 'https://example.com',
130             'name' => 'Example Attachment Link',
131             'uploaded_to' => $page->id,
132         ]);
133
134         $attachmentId = \BookStack\Attachment::first()->id;
135
136         $this->call('PUT', 'attachments/' . $attachmentId, [
137             'uploaded_to' => $page->id,
138             'name' => 'My new attachment name',
139             'link' => 'https://test.example.com'
140         ]);
141
142         $expectedResp = [
143             'path' => 'https://test.example.com',
144             'name' => 'My new attachment name',
145             'uploaded_to' => $page->id
146         ];
147
148         $this->assertResponseOk();
149         $this->seeJsonContains($expectedResp);
150         $this->seeInDatabase('attachments', $expectedResp);
151
152         $this->deleteUploads();
153     }
154
155     public function test_file_deletion()
156     {
157         $page = \BookStack\Page::first();
158         $this->asAdmin();
159         $fileName = 'deletion_test.txt';
160         $this->uploadFile($fileName, $page->id);
161
162         $filePath = base_path('storage/' . $this->getUploadPath($fileName));
163
164         $this->assertTrue(file_exists($filePath), 'File at path ' . $filePath . ' does not exist');
165
166         $attachmentId = \BookStack\Attachment::first()->id;
167         $this->call('DELETE', 'attachments/' . $attachmentId);
168
169         $this->dontSeeInDatabase('attachments', [
170             'name' => $fileName
171         ]);
172         $this->assertFalse(file_exists($filePath), 'File at path ' . $filePath . ' was not deleted as expected');
173
174         $this->deleteUploads();
175     }
176
177     public function test_attachment_deletion_on_page_deletion()
178     {
179         $page = \BookStack\Page::first();
180         $this->asAdmin();
181         $fileName = 'deletion_test.txt';
182         $this->uploadFile($fileName, $page->id);
183
184         $filePath = base_path('storage/' . $this->getUploadPath($fileName));
185
186         $this->assertTrue(file_exists($filePath), 'File at path ' . $filePath . ' does not exist');
187         $this->seeInDatabase('attachments', [
188             'name' => $fileName
189         ]);
190
191         $this->call('DELETE', $page->getUrl());
192
193         $this->dontSeeInDatabase('attachments', [
194             'name' => $fileName
195         ]);
196         $this->assertFalse(file_exists($filePath), 'File at path ' . $filePath . ' was not deleted as expected');
197
198         $this->deleteUploads();
199     }
200 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.