From 4fd2269e7d05c86481e31eadbaf87750ea8a9ab1 Mon Sep 17 00:00:00 2001 From: Leane Hecht Date: Thu, 27 Jul 2023 08:52:02 +0200 Subject: [PATCH 1/7] Resolves #621 by checking value type --- app/Rules/Utf8.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/Rules/Utf8.php b/app/Rules/Utf8.php index 13610e30f..f2a9bdf83 100644 --- a/app/Rules/Utf8.php +++ b/app/Rules/Utf8.php @@ -15,7 +15,8 @@ class Utf8 implements Rule */ public function passes($attribute, $value) { - return mb_detect_encoding($value->get(), 'UTF-8', true) !== false; + $value = is_string($value) ? $value : $value->get(); + return mb_detect_encoding($value, 'UTF-8', true) !== false; } /** From 3896c836bc58ade05e8fa9806e89fdfaef762b64 Mon Sep 17 00:00:00 2001 From: Leane Hecht Date: Fri, 28 Jul 2023 09:30:39 +0200 Subject: [PATCH 2/7] Add test for metadata as string --- .../Api/Volumes/MetadataControllerTest.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/php/Http/Controllers/Api/Volumes/MetadataControllerTest.php b/tests/php/Http/Controllers/Api/Volumes/MetadataControllerTest.php index 6b78f0c4f..278c2fa47 100644 --- a/tests/php/Http/Controllers/Api/Volumes/MetadataControllerTest.php +++ b/tests/php/Http/Controllers/Api/Volumes/MetadataControllerTest.php @@ -76,6 +76,19 @@ public function testStoreImageMetadata() $this->assertEmpty($png->metadata); } + public function testStoreStringMetadata() + { + $id = $this->volume()->id; + + $this->beAdmin(); + + $this->postJson("/api/v1/volumes/{$id}/metadata", ['metadata_csv' => "metadata_string"]) + ->assertStatus(422); + + $this->postJson("/api/v1/volumes/{$id}/metadata", ['metadata_text' => "metadata_string"]) + ->assertStatus(422); + } + public function testStoreDeprecatedFileAttribute() { $id = $this->volume()->id; From 2a2cc363feeaf895bbca27c9c64f8255c484092e Mon Sep 17 00:00:00 2001 From: Leane Hecht Date: Tue, 8 Aug 2023 09:11:22 +0200 Subject: [PATCH 3/7] Remove utf8 test for metadata_text object --- .../Api/Volumes/MetadataControllerTest.php | 72 +++++++++---------- 1 file changed, 35 insertions(+), 37 deletions(-) diff --git a/tests/php/Http/Controllers/Api/Volumes/MetadataControllerTest.php b/tests/php/Http/Controllers/Api/Volumes/MetadataControllerTest.php index 278c2fa47..fe0be2116 100644 --- a/tests/php/Http/Controllers/Api/Volumes/MetadataControllerTest.php +++ b/tests/php/Http/Controllers/Api/Volumes/MetadataControllerTest.php @@ -23,7 +23,7 @@ public function testStoreImageMetadata() $this->doTestApiRoute('POST', "/api/v1/volumes/{$id}/metadata"); - $csv = new UploadedFile(__DIR__."/../../../../../files/image-metadata.csv", 'image-metadata.csv', 'text/csv', null, true); + $csv = new UploadedFile(__DIR__.'/../../../../../files/image-metadata.csv', 'image-metadata.csv', 'text/csv', null, true); $this->beEditor(); // no permissions $this->postJson("/api/v1/volumes/{$id}/metadata", ['metadata_csv' => $csv]) @@ -82,11 +82,9 @@ public function testStoreStringMetadata() $this->beAdmin(); - $this->postJson("/api/v1/volumes/{$id}/metadata", ['metadata_csv' => "metadata_string"]) - ->assertStatus(422); + $this->postJson("/api/v1/volumes/{$id}/metadata", ['metadata_csv' => 'metadata_string']) + ->assertStatus(422); - $this->postJson("/api/v1/volumes/{$id}/metadata", ['metadata_text' => "metadata_string"]) - ->assertStatus(422); } public function testStoreDeprecatedFileAttribute() @@ -102,7 +100,7 @@ public function testStoreDeprecatedFileAttribute() ]], ]); - $csv = new UploadedFile(__DIR__."/../../../../../files/image-metadata.csv", 'metadata.csv', 'text/csv', null, true); + $csv = new UploadedFile(__DIR__.'/../../../../../files/image-metadata.csv', 'metadata.csv', 'text/csv', null, true); $this->beAdmin(); $this->postJson("/api/v1/volumes/{$id}/metadata", ['file' => $csv]) @@ -153,7 +151,7 @@ public function testStoreVideoMetadataCsv() ]], ]); - $csv = new UploadedFile(__DIR__."/../../../../../files/video-metadata.csv", 'metadata.csv', 'text/csv', null, true); + $csv = new UploadedFile(__DIR__.'/../../../../../files/video-metadata.csv', 'metadata.csv', 'text/csv', null, true); $this->beAdmin(); $this->postJson("/api/v1/volumes/{$id}/metadata", ['file' => $csv]) @@ -181,10 +179,10 @@ public function testStoreVideoMetadataText() ]); $text = <<beAdmin(); $this->postJson("/api/v1/volumes/{$id}/metadata", [ @@ -215,10 +213,10 @@ public function testStoreVideoMetadataMerge() ]); $text = <<beAdmin(); $this->postJson("/api/v1/volumes/{$id}/metadata", [ @@ -255,12 +253,12 @@ public function testStoreVideoMetadataFillOneVideoButNotTheOther() ]); $text = <<beAdmin(); $this->postJson("/api/v1/volumes/{$id}/metadata", [ @@ -313,10 +311,10 @@ public function testStoreVideoMetadataCannotUpdateBasicWithTimestamped() ]); $text = <<beAdmin(); // The video has basic metadata. There is no way the new area data with @@ -338,9 +336,9 @@ public function testStoreVideoMetadataZerosSingle() ]); $text = <<beAdmin(); $this->postJson("/api/v1/volumes/{$id}/metadata", [ @@ -363,10 +361,10 @@ public function testStoreVideoMetadataZeros() ]); $text = <<beAdmin(); $this->postJson("/api/v1/volumes/{$id}/metadata", [ @@ -388,7 +386,7 @@ public function testStoreVideoMetadataIncorrectEncoding() 'volume_id' => $id, ]); - $csv = new UploadedFile(__DIR__."/../../../../../files/video-metadata-incorrect-encoding.csv", 'metadata.csv', 'text/csv', null, true); + $csv = new UploadedFile(__DIR__.'/../../../../../files/video-metadata-incorrect-encoding.csv', 'metadata.csv', 'text/csv', null, true); $this->beAdmin(); $this->postJson("/api/v1/volumes/{$id}/metadata", ['file' => $csv]) @@ -399,7 +397,7 @@ public function testStoreImageIfdoFile() { $id = $this->volume()->id; $this->beAdmin(); - $file = new UploadedFile(__DIR__."/../../../../../files/image-ifdo.yaml", 'ifdo.yaml', 'application/yaml', null, true); + $file = new UploadedFile(__DIR__.'/../../../../../files/image-ifdo.yaml', 'ifdo.yaml', 'application/yaml', null, true); Storage::fake('ifdos'); @@ -417,7 +415,7 @@ public function testStoreVideoIfdoFile() $this->volume()->media_type_id = MediaType::videoId(); $this->volume()->save(); $this->beAdmin(); - $file = new UploadedFile(__DIR__."/../../../../../files/video-ifdo.yaml", 'ifdo.yaml', 'application/yaml', null, true); + $file = new UploadedFile(__DIR__.'/../../../../../files/video-ifdo.yaml', 'ifdo.yaml', 'application/yaml', null, true); Storage::fake('ifdos'); @@ -433,7 +431,7 @@ public function testStoreVideoIfdoFileForImageVolume() { $id = $this->volume()->id; $this->beAdmin(); - $file = new UploadedFile(__DIR__."/../../../../../files/video-ifdo.yaml", 'ifdo.yaml', 'application/yaml', null, true); + $file = new UploadedFile(__DIR__.'/../../../../../files/video-ifdo.yaml', 'ifdo.yaml', 'application/yaml', null, true); $this->postJson("/api/v1/volumes/{$id}/metadata", ['ifdo_file' => $file]) ->assertStatus(422); @@ -445,7 +443,7 @@ public function testStoreImageIfdoFileForVideoVolume() $this->volume()->media_type_id = MediaType::videoId(); $this->volume()->save(); $this->beAdmin(); - $file = new UploadedFile(__DIR__."/../../../../../files/image-ifdo.yaml", 'ifdo.yaml', 'application/yaml', null, true); + $file = new UploadedFile(__DIR__.'/../../../../../files/image-ifdo.yaml', 'ifdo.yaml', 'application/yaml', null, true); $this->postJson("/api/v1/volumes/{$id}/metadata", ['ifdo_file' => $file]) ->assertStatus(422); From c335799fb858a0259a88fca9352377e19a0cb1c5 Mon Sep 17 00:00:00 2001 From: Leane Hecht Date: Tue, 8 Aug 2023 09:30:12 +0200 Subject: [PATCH 4/7] Add bail property to rules --- app/Http/Requests/StoreVolumeMetadata.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Http/Requests/StoreVolumeMetadata.php b/app/Http/Requests/StoreVolumeMetadata.php index d99c7a082..ae16a1e6b 100644 --- a/app/Http/Requests/StoreVolumeMetadata.php +++ b/app/Http/Requests/StoreVolumeMetadata.php @@ -40,12 +40,12 @@ public function rules() { return [ 'metadata_csv' => [ - 'required_without_all:metadata_text,ifdo_file', + 'bail:required_without_all:metadata_text,ifdo_file', 'file', 'mimetypes:text/plain,text/csv,application/csv', new Utf8, ], - 'metadata_text' => 'required_without_all:metadata_csv,ifdo_file', + 'metadata_text' => 'bail:required_without_all:metadata_csv,ifdo_file', 'ifdo_file' => 'required_without_all:metadata_csv,metadata_text|file', 'metadata' => 'filled', ]; From ae6e7efc334f3c46faa7949c541e5e88081f0eb6 Mon Sep 17 00:00:00 2001 From: Leane Hecht Date: Wed, 9 Aug 2023 08:21:21 +0200 Subject: [PATCH 5/7] Add bail property as separate array element --- app/Http/Requests/StoreVolumeMetadata.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/Http/Requests/StoreVolumeMetadata.php b/app/Http/Requests/StoreVolumeMetadata.php index ae16a1e6b..3cc7fe2bc 100644 --- a/app/Http/Requests/StoreVolumeMetadata.php +++ b/app/Http/Requests/StoreVolumeMetadata.php @@ -40,12 +40,13 @@ public function rules() { return [ 'metadata_csv' => [ - 'bail:required_without_all:metadata_text,ifdo_file', + 'bail', + 'required_without_all:metadata_text,ifdo_file', 'file', 'mimetypes:text/plain,text/csv,application/csv', new Utf8, ], - 'metadata_text' => 'bail:required_without_all:metadata_csv,ifdo_file', + 'metadata_text' => 'required_without_all:metadata_csv,ifdo_file', 'ifdo_file' => 'required_without_all:metadata_csv,metadata_text|file', 'metadata' => 'filled', ]; From fc3789484ae00dd5dd35c5f797e05a9b28edf290 Mon Sep 17 00:00:00 2001 From: Leane Hecht Date: Wed, 9 Aug 2023 08:23:50 +0200 Subject: [PATCH 6/7] Revert "Remove utf8 test for metadata_text object" This reverts commit 2a2cc363feeaf895bbca27c9c64f8255c484092e. --- .../Api/Volumes/MetadataControllerTest.php | 72 ++++++++++--------- 1 file changed, 37 insertions(+), 35 deletions(-) diff --git a/tests/php/Http/Controllers/Api/Volumes/MetadataControllerTest.php b/tests/php/Http/Controllers/Api/Volumes/MetadataControllerTest.php index fe0be2116..278c2fa47 100644 --- a/tests/php/Http/Controllers/Api/Volumes/MetadataControllerTest.php +++ b/tests/php/Http/Controllers/Api/Volumes/MetadataControllerTest.php @@ -23,7 +23,7 @@ public function testStoreImageMetadata() $this->doTestApiRoute('POST', "/api/v1/volumes/{$id}/metadata"); - $csv = new UploadedFile(__DIR__.'/../../../../../files/image-metadata.csv', 'image-metadata.csv', 'text/csv', null, true); + $csv = new UploadedFile(__DIR__."/../../../../../files/image-metadata.csv", 'image-metadata.csv', 'text/csv', null, true); $this->beEditor(); // no permissions $this->postJson("/api/v1/volumes/{$id}/metadata", ['metadata_csv' => $csv]) @@ -82,9 +82,11 @@ public function testStoreStringMetadata() $this->beAdmin(); - $this->postJson("/api/v1/volumes/{$id}/metadata", ['metadata_csv' => 'metadata_string']) - ->assertStatus(422); + $this->postJson("/api/v1/volumes/{$id}/metadata", ['metadata_csv' => "metadata_string"]) + ->assertStatus(422); + $this->postJson("/api/v1/volumes/{$id}/metadata", ['metadata_text' => "metadata_string"]) + ->assertStatus(422); } public function testStoreDeprecatedFileAttribute() @@ -100,7 +102,7 @@ public function testStoreDeprecatedFileAttribute() ]], ]); - $csv = new UploadedFile(__DIR__.'/../../../../../files/image-metadata.csv', 'metadata.csv', 'text/csv', null, true); + $csv = new UploadedFile(__DIR__."/../../../../../files/image-metadata.csv", 'metadata.csv', 'text/csv', null, true); $this->beAdmin(); $this->postJson("/api/v1/volumes/{$id}/metadata", ['file' => $csv]) @@ -151,7 +153,7 @@ public function testStoreVideoMetadataCsv() ]], ]); - $csv = new UploadedFile(__DIR__.'/../../../../../files/video-metadata.csv', 'metadata.csv', 'text/csv', null, true); + $csv = new UploadedFile(__DIR__."/../../../../../files/video-metadata.csv", 'metadata.csv', 'text/csv', null, true); $this->beAdmin(); $this->postJson("/api/v1/volumes/{$id}/metadata", ['file' => $csv]) @@ -179,10 +181,10 @@ public function testStoreVideoMetadataText() ]); $text = <<beAdmin(); $this->postJson("/api/v1/volumes/{$id}/metadata", [ @@ -213,10 +215,10 @@ public function testStoreVideoMetadataMerge() ]); $text = <<beAdmin(); $this->postJson("/api/v1/volumes/{$id}/metadata", [ @@ -253,12 +255,12 @@ public function testStoreVideoMetadataFillOneVideoButNotTheOther() ]); $text = <<beAdmin(); $this->postJson("/api/v1/volumes/{$id}/metadata", [ @@ -311,10 +313,10 @@ public function testStoreVideoMetadataCannotUpdateBasicWithTimestamped() ]); $text = <<beAdmin(); // The video has basic metadata. There is no way the new area data with @@ -336,9 +338,9 @@ public function testStoreVideoMetadataZerosSingle() ]); $text = <<beAdmin(); $this->postJson("/api/v1/volumes/{$id}/metadata", [ @@ -361,10 +363,10 @@ public function testStoreVideoMetadataZeros() ]); $text = <<beAdmin(); $this->postJson("/api/v1/volumes/{$id}/metadata", [ @@ -386,7 +388,7 @@ public function testStoreVideoMetadataIncorrectEncoding() 'volume_id' => $id, ]); - $csv = new UploadedFile(__DIR__.'/../../../../../files/video-metadata-incorrect-encoding.csv', 'metadata.csv', 'text/csv', null, true); + $csv = new UploadedFile(__DIR__."/../../../../../files/video-metadata-incorrect-encoding.csv", 'metadata.csv', 'text/csv', null, true); $this->beAdmin(); $this->postJson("/api/v1/volumes/{$id}/metadata", ['file' => $csv]) @@ -397,7 +399,7 @@ public function testStoreImageIfdoFile() { $id = $this->volume()->id; $this->beAdmin(); - $file = new UploadedFile(__DIR__.'/../../../../../files/image-ifdo.yaml', 'ifdo.yaml', 'application/yaml', null, true); + $file = new UploadedFile(__DIR__."/../../../../../files/image-ifdo.yaml", 'ifdo.yaml', 'application/yaml', null, true); Storage::fake('ifdos'); @@ -415,7 +417,7 @@ public function testStoreVideoIfdoFile() $this->volume()->media_type_id = MediaType::videoId(); $this->volume()->save(); $this->beAdmin(); - $file = new UploadedFile(__DIR__.'/../../../../../files/video-ifdo.yaml', 'ifdo.yaml', 'application/yaml', null, true); + $file = new UploadedFile(__DIR__."/../../../../../files/video-ifdo.yaml", 'ifdo.yaml', 'application/yaml', null, true); Storage::fake('ifdos'); @@ -431,7 +433,7 @@ public function testStoreVideoIfdoFileForImageVolume() { $id = $this->volume()->id; $this->beAdmin(); - $file = new UploadedFile(__DIR__.'/../../../../../files/video-ifdo.yaml', 'ifdo.yaml', 'application/yaml', null, true); + $file = new UploadedFile(__DIR__."/../../../../../files/video-ifdo.yaml", 'ifdo.yaml', 'application/yaml', null, true); $this->postJson("/api/v1/volumes/{$id}/metadata", ['ifdo_file' => $file]) ->assertStatus(422); @@ -443,7 +445,7 @@ public function testStoreImageIfdoFileForVideoVolume() $this->volume()->media_type_id = MediaType::videoId(); $this->volume()->save(); $this->beAdmin(); - $file = new UploadedFile(__DIR__.'/../../../../../files/image-ifdo.yaml', 'ifdo.yaml', 'application/yaml', null, true); + $file = new UploadedFile(__DIR__."/../../../../../files/image-ifdo.yaml", 'ifdo.yaml', 'application/yaml', null, true); $this->postJson("/api/v1/volumes/{$id}/metadata", ['ifdo_file' => $file]) ->assertStatus(422); From 375da037996775e0b50ff9f9092dbe1ebd430006 Mon Sep 17 00:00:00 2001 From: Leane Hecht Date: Wed, 9 Aug 2023 08:25:03 +0200 Subject: [PATCH 7/7] Remove utf8 test for metadata_text object --- .../Http/Controllers/Api/Volumes/MetadataControllerTest.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/php/Http/Controllers/Api/Volumes/MetadataControllerTest.php b/tests/php/Http/Controllers/Api/Volumes/MetadataControllerTest.php index 278c2fa47..9237a93bf 100644 --- a/tests/php/Http/Controllers/Api/Volumes/MetadataControllerTest.php +++ b/tests/php/Http/Controllers/Api/Volumes/MetadataControllerTest.php @@ -83,10 +83,7 @@ public function testStoreStringMetadata() $this->beAdmin(); $this->postJson("/api/v1/volumes/{$id}/metadata", ['metadata_csv' => "metadata_string"]) - ->assertStatus(422); - - $this->postJson("/api/v1/volumes/{$id}/metadata", ['metadata_text' => "metadata_string"]) - ->assertStatus(422); + ->assertStatus(422); } public function testStoreDeprecatedFileAttribute()