3
3
namespace BristolSU \Module \Tests \UploadFile \Http \Controllers \AdminApi ;
4
4
5
5
use BristolSU \Module \Tests \UploadFile \TestCase ;
6
+ use BristolSU \Module \UploadFile \Events \DocumentUploaded ;
6
7
use BristolSU \Module \UploadFile \Models \File ;
8
+ use BristolSU \Support \ActivityInstance \ActivityInstance ;
9
+ use BristolSU \Support \ModuleInstance \Settings \ModuleInstanceSetting ;
10
+ use Illuminate \Http \UploadedFile ;
11
+ use Illuminate \Support \Facades \Event ;
12
+ use Illuminate \Support \Facades \Storage ;
7
13
8
14
class FileControllerTest extends TestCase
9
15
{
@@ -86,4 +92,276 @@ public function show_returns_the_file(){
86
92
'title ' => $ file ->title
87
93
]);
88
94
}
95
+
96
+
97
+ /** @test */
98
+ public function store_allows_a_single_file_to_be_uploaded ()
99
+ {
100
+ ModuleInstanceSetting::create ([
101
+ 'key ' => 'allowed_extensions ' , 'value ' => ['jpg ' , 'png ' ], 'module_instance_id ' => $ this ->getModuleInstance ()->id ()
102
+ ]);
103
+ $ activityInstance = factory (ActivityInstance::class)->create ();
104
+
105
+ $ this ->bypassAuthorization ();
106
+
107
+ Storage::fake ();
108
+ $ file = UploadedFile::fake ()->create ('filename.png ' , 58 , 'image/png ' );
109
+
110
+ $ response = $ this ->postJson ($ this ->adminApiUrl ('/file ' ), [
111
+ 'file ' => [$ file ],
112
+ 'title ' => 'ATitle ' ,
113
+ 'description ' => 'ADescription ' ,
114
+ 'activity_instance_id ' => $ activityInstance ->id
115
+ ]);
116
+
117
+ $ response ->assertStatus (200 );
118
+
119
+ $ this ->assertDatabaseHas ('uploadfile_files ' , [
120
+ 'title ' => 'ATitle ' ,
121
+ 'description ' => 'ADescription ' ,
122
+ 'filename ' => 'filename.png ' ,
123
+ 'mime ' => 'image/png ' ,
124
+ 'uploaded_by ' => $ this ->getControlUser ()->id (),
125
+ 'activity_instance_id ' => $ activityInstance ->id ,
126
+ 'module_instance_id ' => $ this ->getModuleInstance ()->id ()
127
+ ]);
128
+
129
+ $ file = File::where ('title ' , 'ATitle ' )->get ()->first ();
130
+ $ this ->assertInstanceOf (File::class, $ file );
131
+
132
+ Storage::assertExists ($ file ->path );
133
+
134
+ }
135
+
136
+ /** @test */
137
+ public function store_returns_the_file_meta_data_as_an_array ()
138
+ {
139
+ ModuleInstanceSetting::create ([
140
+ 'key ' => 'allowed_extensions ' , 'value ' => ['jpg ' , 'png ' ], 'module_instance_id ' => $ this ->getModuleInstance ()->id ()
141
+ ]);
142
+
143
+ $ activityInstance = factory (ActivityInstance::class)->create ();
144
+
145
+ $ this ->bypassAuthorization ();
146
+
147
+ Storage::fake ();
148
+ $ file = UploadedFile::fake ()->create ('filename.png ' , 58 , 'image/png ' );
149
+
150
+ $ response = $ this ->postJson ($ this ->adminApiUrl ('/file ' ), [
151
+ 'file ' => [$ file ],
152
+ 'title ' => 'ATitle ' ,
153
+ 'description ' => 'ADescription ' ,
154
+ 'activity_instance_id ' => $ activityInstance ->id
155
+ ]);
156
+ $ response ->assertStatus (200 );
157
+ $ response ->assertJsonCount (1 );
158
+
159
+ $ response ->assertJsonFragment ([
160
+ 'title ' => 'ATitle ' ,
161
+ 'description ' => 'ADescription ' ,
162
+ 'filename ' => 'filename.png ' ,
163
+ 'mime ' => 'image/png ' ,
164
+ 'activity_instance_id ' => $ activityInstance ->id
165
+ ]);
166
+ }
167
+
168
+ /** @test */
169
+ public function store_allows_multiple_files_to_be_uploaded ()
170
+ {
171
+ ModuleInstanceSetting::create ([
172
+ 'key ' => 'allowed_extensions ' , 'value ' => ['jpg ' , 'png ' ], 'module_instance_id ' => $ this ->getModuleInstance ()->id ()
173
+ ]);
174
+ $ activityInstance = factory (ActivityInstance::class)->create ();
175
+
176
+ $ this ->bypassAuthorization ();
177
+
178
+ Storage::fake ();
179
+
180
+ $ file1 = UploadedFile::fake ()->create ('filename.png ' , 58 , 'image/png ' );
181
+ $ file2 = UploadedFile::fake ()->create ('filename2.png ' , 58 , 'image/png ' );
182
+
183
+ $ response = $ this ->postJson ($ this ->adminApiUrl ('/file ' ), [
184
+ 'file ' => [$ file1 , $ file2 ],
185
+ 'title ' => 'ATitle ' ,
186
+ 'description ' => 'ADescription ' ,
187
+ 'activity_instance_id ' => $ activityInstance ->id
188
+ ]);
189
+ $ response ->assertStatus (200 );
190
+ $ response ->assertJsonCount (2 );
191
+ $ response ->assertJsonFragment ([
192
+ 'title ' => 'ATitle ' ,
193
+ 'description ' => 'ADescription ' ,
194
+ 'filename ' => 'filename.png ' ,
195
+ 'mime ' => 'image/png ' ,
196
+ 'activity_instance_id ' => $ activityInstance ->id
197
+ ]);
198
+ $ response ->assertJsonFragment ([
199
+ 'title ' => 'ATitle ' ,
200
+ 'description ' => 'ADescription ' ,
201
+ 'filename ' => 'filename2.png ' ,
202
+ 'mime ' => 'image/png ' ,
203
+ 'activity_instance_id ' => $ activityInstance ->id
204
+ ]);
205
+
206
+ $ this ->assertDatabaseHas ('uploadfile_files ' , [
207
+ 'title ' => 'ATitle ' ,
208
+ 'description ' => 'ADescription ' ,
209
+ 'filename ' => 'filename.png ' ,
210
+ 'mime ' => 'image/png ' ,
211
+ 'uploaded_by ' => $ this ->getControlUser ()->id (),
212
+ 'activity_instance_id ' => $ activityInstance ->id ,
213
+ 'module_instance_id ' => $ this ->getModuleInstance ()->id ()
214
+ ]);
215
+ $ this ->assertDatabaseHas ('uploadfile_files ' , [
216
+ 'title ' => 'ATitle ' ,
217
+ 'description ' => 'ADescription ' ,
218
+ 'filename ' => 'filename2.png ' ,
219
+ 'mime ' => 'image/png ' ,
220
+ 'uploaded_by ' => $ this ->getControlUser ()->id (),
221
+ 'activity_instance_id ' => $ activityInstance ->id ,
222
+ 'module_instance_id ' => $ this ->getModuleInstance ()->id ()
223
+ ]);
224
+
225
+ $ files = File::where ('title ' , 'ATitle ' )->get ();
226
+ $ this ->assertCount (2 , $ files );
227
+
228
+ $ this ->assertInstanceOf (File::class, $ files [0 ]);
229
+ Storage::assertExists ($ files [0 ]->path );
230
+ $ this ->assertInstanceOf (File::class, $ files [1 ]);
231
+ Storage::assertExists ($ files [1 ]->path );
232
+ }
233
+
234
+ /** @test */
235
+ public function store_returns_a_200_if_the_permission_owned ()
236
+ {
237
+ ModuleInstanceSetting::create ([
238
+ 'key ' => 'allowed_extensions ' , 'value ' => ['jpg ' , 'png ' ], 'module_instance_id ' => $ this ->getModuleInstance ()->id ()
239
+ ]);
240
+ $ activityInstance = factory (ActivityInstance::class)->create ();
241
+
242
+ $ this ->givePermissionTo ('uploadfile.admin.file.store ' );
243
+
244
+ Storage::fake ();
245
+ $ file = UploadedFile::fake ()->create ('filename.png ' , 58 , 'image/png ' );
246
+
247
+ $ response = $ this ->postJson ($ this ->adminApiUrl ('/file ' ), [
248
+ 'file ' => [$ file ],
249
+ 'title ' => 'ATitle ' ,
250
+ 'description ' => 'ADescription ' ,
251
+ 'activity_instance_id ' => $ activityInstance ->id
252
+ ]);
253
+
254
+ $ response ->assertStatus (200 );
255
+ }
256
+
257
+ /** @test */
258
+ public function store_returns_a_403_if_the_permission_is_not_owned ()
259
+ {
260
+ ModuleInstanceSetting::create ([
261
+ 'key ' => 'allowed_extensions ' , 'value ' => ['jpg ' , 'png ' ], 'module_instance_id ' => $ this ->getModuleInstance ()->id ()
262
+ ]);
263
+ $ activityInstance = factory (ActivityInstance::class)->create ();
264
+
265
+ $ this ->revokePermissionTo ('uploadfile.admin.file.store ' );
266
+
267
+ Storage::fake ();
268
+ $ file = UploadedFile::fake ()->create ('filename.png ' , 58 , 'image/png ' );
269
+
270
+ $ response = $ this ->postJson ($ this ->adminApiUrl ('/file ' ), [
271
+ 'file ' => [$ file ],
272
+ 'title ' => 'ATitle ' ,
273
+ 'description ' => 'ADescription ' ,
274
+ 'activity_instance_id ' => $ activityInstance ->id
275
+ ]);
276
+ $ response ->assertStatus (403 );
277
+ }
278
+
279
+ /** @test */
280
+ public function store_fires_an_event_when_a_file_is_uploaded ()
281
+ {
282
+ Event::fake (DocumentUploaded::class);
283
+ ModuleInstanceSetting::create ([
284
+ 'key ' => 'allowed_extensions ' , 'value ' => ['jpg ' , 'png ' ], 'module_instance_id ' => $ this ->getModuleInstance ()->id ()
285
+ ]);
286
+ $ activityInstance = factory (ActivityInstance::class)->create ();
287
+
288
+ $ this ->bypassAuthorization ();
289
+
290
+ Storage::fake ();
291
+ $ file = UploadedFile::fake ()->create ('filename.png ' , 58 , 'image/png ' );
292
+
293
+ $ response = $ this ->postJson ($ this ->adminApiUrl ('/file ' ), [
294
+ 'file ' => [$ file ],
295
+ 'title ' => 'ATitle ' ,
296
+ 'description ' => 'ADescription ' ,
297
+ 'activity_instance_id ' => $ activityInstance ->id
298
+ ]);
299
+ $ response ->assertStatus (200 );
300
+
301
+ $ fileMeta = File::where ('title ' , 'ATitle ' )->get ()->first ();
302
+ $ this ->assertInstanceOf (File::class, $ fileMeta );
303
+
304
+ Event::assertDispatched (DocumentUploaded::class, function ($ event ) use ($ fileMeta ) {
305
+ return $ event instanceof DocumentUploaded && $ event ->file ->is ($ fileMeta );
306
+ });
307
+ }
308
+
309
+ /** @test */
310
+ public function store_fires_an_event_for_each_file_uploaded ()
311
+ {
312
+ Event::fake (DocumentUploaded::class);
313
+ ModuleInstanceSetting::create ([
314
+ 'key ' => 'allowed_extensions ' , 'value ' => ['jpg ' , 'png ' ], 'module_instance_id ' => $ this ->getModuleInstance ()->id ()
315
+ ]);
316
+ $ activityInstance = factory (ActivityInstance::class)->create ();
317
+
318
+ $ this ->bypassAuthorization ();
319
+
320
+ Storage::fake ();
321
+
322
+ $ file1 = UploadedFile::fake ()->create ('filename.png ' , 58 , 'image/png ' );
323
+ $ file2 = UploadedFile::fake ()->create ('filename2.png ' , 58 , 'image/png ' );
324
+
325
+ $ response = $ this ->postJson ($ this ->adminApiUrl ('/file ' ), [
326
+ 'file ' => [$ file1 , $ file2 ],
327
+ 'title ' => 'ATitle ' ,
328
+ 'description ' => 'ADescription ' ,
329
+ 'activity_instance_id ' => $ activityInstance ->id
330
+ ]);
331
+ $ response ->assertStatus (200 );
332
+
333
+ $ files = File::where ('title ' , 'ATitle ' )->get ();
334
+ $ this ->assertCount (2 , $ files );
335
+
336
+ Event::assertDispatched (DocumentUploaded::class, function ($ event ) use ($ files ) {
337
+ return $ event instanceof DocumentUploaded && ($ event ->file ->is ($ files [0 ]) || $ event ->file ->is ($ files [1 ]));
338
+ });
339
+ }
340
+
341
+ /** @test */
342
+ public function store_returns_422_if_file_type_not_allowed (){
343
+ Event::fake (DocumentUploaded::class);
344
+ $ activityInstance = factory (ActivityInstance::class)->create ();
345
+ ModuleInstanceSetting::create ([
346
+ 'key ' => 'allowed_extensions ' , 'value ' => [], 'module_instance_id ' => $ this ->getModuleInstance ()->id ()
347
+ ]);
348
+
349
+ $ this ->bypassAuthorization ();
350
+
351
+ Storage::fake ();
352
+
353
+ $ file1 = UploadedFile::fake ()->create ('filename.png ' , 58 , 'image/png ' );
354
+ $ response = $ this ->postJson ($ this ->adminApiUrl ('/file ' ), [
355
+ 'file ' => [$ file1 ],
356
+ 'title ' => 'ATitle ' ,
357
+ 'description ' => 'ADescription ' ,
358
+ 'activity_instance_id ' => $ activityInstance ->id
359
+ ]);
360
+
361
+ $ response ->assertStatus (422 );
362
+
363
+ $ response ->assertJsonValidationErrors (['file.0 ' => 'file of type: ' ]);
364
+
365
+ }
366
+
89
367
}
0 commit comments