22
33namespace Tests \Feature \Api ;
44
5- use Faker \Factory as Faker ;
65use Illuminate \Foundation \Testing \DatabaseTransactions ;
7- use Illuminate \Http \Request ;
86use Illuminate \Http \UploadedFile ;
9- use Illuminate \Support \Facades \Auth ;
107use Illuminate \Support \Facades \Storage ;
8+ use ProcessMaker \Models \Media ;
119use ProcessMaker \Models \User ;
10+ use Spatie \BinaryUuid \HasBinaryUuid ;
1211use Tests \TestCase ;
1312use Tests \Feature \Shared \ApiCallWithUser ;
1413use Illuminate \Support \Facades \Hash ;
@@ -124,7 +123,7 @@ public function testGetFile()
124123 /**
125124 * Upload a file and associate it to a model
126125 */
127- public function testCreateFile ()
126+ public function testCreateFile ()
128127 {
129128 // We create a fake file to upload
130129 Storage::fake ('public ' );
@@ -141,14 +140,53 @@ public function testCreateFile()
141140 $ response = $ this ->actingAs ($ this ->user , 'api ' )
142141 ->json ('POST ' , self ::API_TEST_URL . '?model=user&model_uuid= ' . $ model ->uuid_text , $ data );
143142
143+ // Validate the header status code
144+ $ response ->assertStatus (200 );
145+
146+ // Validate that a file was created in the media directory
147+ $ mediaObj = json_decode ($ response ->getContent ());
148+ Storage::disk ('public ' )->assertExists ($ mediaObj ->uuid . '/test.txt ' );
149+ }
150+
151+ /**
152+ * Update a media with a new file
153+ */
154+ public function testUpdateFile ()
155+ {
156+ // We create a fake file to upload
157+ Storage::fake ('public ' );
158+ $ fileUploadInsert = UploadedFile::fake ()->create ('insertedFile.txt ' , 1 );
159+ $ fileUploadUpdate = UploadedFile::fake ()->create ('updatedFile.txt ' , 2 );
160+
161+ // We create a model (in this case a user) to whom the file will be associated
162+ $ model = factory (User::class)->create ();
163+ $ addedMedia = $ model ->addMedia ($ fileUploadInsert )->toMediaCollection ('local ' );
164+
165+ // In the data array add the file to update
166+ $ data = [
167+ 'file ' => $ fileUploadUpdate
168+ ];
169+
170+ $ response = $ this ->actingAs ($ this ->user , 'api ' )
171+ ->json ('PUT ' , self ::API_TEST_URL . '/ ' . $ addedMedia ->uuid_text , $ data );
172+
144173 // Validate the header status code
145174 $ response ->assertStatus (201 );
175+
176+ // Validate that the file was updated in the directory of the inserted media
177+ Storage::disk ('public ' )->assertExists ($ addedMedia ->uuid_text . '/updatedFile.txt ' );
178+
179+ // Validate that the media table has been updated
180+ $ modelId = HasBinaryUuid::encodeUuid ($ addedMedia ->uuid_text );
181+ $ updatedMediaModel = Media::find ($ modelId );
182+ $ this ->assertEquals ('updatedFile.txt ' , $ updatedMediaModel ->file_name );
183+ $ this ->assertEquals ('updatedFile ' , $ updatedMediaModel ->name );
146184 }
147185
148186 /**
149187 * Remove a file and its model associations
150188 */
151- public function testDestroyFile ()
189+ public function testDestroyFile ()
152190 {
153191 // We create a fake file to upload
154192 Storage::fake ('public ' );
0 commit comments