99
1010class SingleFileUploadTest extends TestCase
1111{
12- /** @test */
1312 public function test_normal_file_upload ()
1413 {
1514 $ tmpPath = config ('filepond.temporary_files_path ' , 'filepond ' );
@@ -33,4 +32,94 @@ public function test_normal_file_upload()
3332
3433 Storage::disk ($ diskName )->assertExists ($ pathFromServerId );
3534 }
35+
36+ public function test_it_initializes_a_chunked_upload ()
37+ {
38+ $ tmpPath = config ('filepond.temporary_files_path ' , 'filepond ' );
39+ $ diskName = config ('filepond.temporary_files_disk ' , 'local ' );
40+
41+ Storage::fake ($ diskName );
42+
43+ $ response = $ this ->postJson ('/filepond/api/process ' , [], [
44+ 'Upload-Name ' => 'chunked.txt ' ,
45+ ]);
46+
47+ $ response ->assertStatus (200 );
48+
49+ /** @var Filepond $filepond */
50+ $ filepond = app (Filepond::class);
51+ $ pathFromServerId = $ filepond ->getPathFromServerId ($ response ->content ());
52+
53+ $ this ->assertStringStartsWith ($ tmpPath . DIRECTORY_SEPARATOR . 'chunked- ' , $ pathFromServerId );
54+ $ this ->assertStringEndsWith ('.txt ' , $ pathFromServerId );
55+ Storage::disk ($ diskName )->assertExists ($ pathFromServerId );
56+ }
57+
58+ public function test_it_persists_a_chunked_upload_when_all_chunks_have_arrived ()
59+ {
60+ $ diskName = config ('filepond.temporary_files_disk ' , 'local ' );
61+
62+ Storage::fake ($ diskName );
63+
64+ $ initializeResponse = $ this ->postJson ('/filepond/api/process ' , [], [
65+ 'Upload-Name ' => 'chunked.txt ' ,
66+ ]);
67+
68+ $ initializeResponse ->assertStatus (200 );
69+ $ serverId = $ initializeResponse ->content ();
70+
71+ $ response = $ this ->call ('PATCH ' , '/filepond/api ' , [
72+ 'patch ' => $ serverId ,
73+ ], [], [], [
74+ 'HTTP_UPLOAD_OFFSET ' => '0 ' ,
75+ 'HTTP_UPLOAD_LENGTH ' => '11 ' ,
76+ ], 'hello world ' );
77+
78+ $ response ->assertStatus (204 );
79+
80+ /** @var Filepond $filepond */
81+ $ filepond = app (Filepond::class);
82+ $ pathFromServerId = $ filepond ->getPathFromServerId ($ serverId );
83+
84+ Storage::disk ($ diskName )->assertExists ($ pathFromServerId );
85+ $ this ->assertSame ('hello world ' , Storage::disk ($ diskName )->get ($ pathFromServerId ));
86+ Storage::disk ($ diskName )->assertMissing (config ('filepond.chunks_path ' ) . DIRECTORY_SEPARATOR . basename ($ pathFromServerId ));
87+ }
88+
89+ public function test_it_rejects_chunk_uploads_with_invalid_encrypted_ids ()
90+ {
91+ $ response = $ this ->call ('PATCH ' , '/filepond/api ' , [
92+ 'patch ' => 'not-a-valid-server-id ' ,
93+ ], [], [], [
94+ 'HTTP_UPLOAD_OFFSET ' => '0 ' ,
95+ 'HTTP_UPLOAD_LENGTH ' => '11 ' ,
96+ ], 'hello world ' );
97+
98+ $ response ->assertStatus (400 );
99+ }
100+
101+ public function test_it_deletes_an_uploaded_file_folder_from_its_server_id ()
102+ {
103+ $ diskName = config ('filepond.temporary_files_disk ' , 'local ' );
104+ $ path = config ('filepond.temporary_files_path ' , 'filepond ' ) . DIRECTORY_SEPARATOR . 'delete-me ' . DIRECTORY_SEPARATOR . 'test.txt ' ;
105+
106+ Storage::fake ($ diskName );
107+ Storage::disk ($ diskName )->put ($ path , 'delete me ' );
108+
109+ /** @var Filepond $filepond */
110+ $ filepond = app (Filepond::class);
111+ $ serverId = $ filepond ->getServerIdFromPath ($ path );
112+
113+ $ response = $ this ->call ('DELETE ' , '/filepond/api/process ' , [], [], [], [], $ serverId );
114+
115+ $ response ->assertStatus (200 );
116+ Storage::disk ($ diskName )->assertMissing ($ path );
117+ }
118+
119+ public function test_it_rejects_delete_requests_with_invalid_encrypted_ids ()
120+ {
121+ $ response = $ this ->call ('DELETE ' , '/filepond/api/process ' , [], [], [], [], 'not-a-valid-server-id ' );
122+
123+ $ response ->assertStatus (400 );
124+ }
36125}
0 commit comments