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