]> BookStack Code Mirror - bookstack/commitdiff
Added missing validation.file message
authorDan Brown <redacted>
Sun, 6 Feb 2022 14:47:33 +0000 (14:47 +0000)
committerDan Brown <redacted>
Sun, 6 Feb 2022 14:48:33 +0000 (14:48 +0000)
- Included test to cover
- Also applied StyleCI fixes

Closes #3248

resources/lang/en/validation.php
tests/Api/AttachmentsApiTest.php
tests/Entity/PageContentTest.php

index 1963b0df2f9a9bb3d2586fc71fbc2ca04e02f97d..2a676c7c4cce0130591a929cefd8a21e7765be1c 100644 (file)
@@ -32,6 +32,7 @@ return [
     'digits_between'       => 'The :attribute must be between :min and :max digits.',
     'email'                => 'The :attribute must be a valid email address.',
     'ends_with' => 'The :attribute must end with one of the following: :values',
+    'file'                 => 'The :attribute must be provided as a valid file.',
     'filled'               => 'The :attribute field is required.',
     'gt'                   => [
         'numeric' => 'The :attribute must be greater than :value.',
index 79b4ae98ce8f3c1ff9d7c6cdb5aed05a8cc27508..d7625c93823b9cc00f35b314ce69a788c022d046 100644 (file)
@@ -122,7 +122,7 @@ class AttachmentsApiTest extends TestCase
         $resp = $this->call('POST', $this->baseEndpoint, $details, [], ['file' => $file]);
         $resp->assertStatus(422);
         $resp->assertJson($this->validationResponse([
-            "file" => ["The file may not be greater than 1000 kilobytes."]
+            'file' => ['The file may not be greater than 1000 kilobytes.'],
         ]));
     }
 
@@ -139,15 +139,7 @@ class AttachmentsApiTest extends TestCase
 
         $resp = $this->postJson($this->baseEndpoint, $details);
         $resp->assertStatus(422);
-        $resp->assertJson([
-            'error' => [
-                'message'    => 'The given data was invalid.',
-                'validation' => [
-                    'name' => ['The name field is required.'],
-                ],
-                'code' => 422,
-            ],
-        ]);
+        $resp->assertJson($this->validationResponse(['name' => ['The name field is required.']]));
     }
 
     public function test_link_or_file_needed_to_create()
@@ -163,16 +155,27 @@ class AttachmentsApiTest extends TestCase
 
         $resp = $this->postJson($this->baseEndpoint, $details);
         $resp->assertStatus(422);
-        $resp->assertJson([
-            'error' => [
-                'message'    => 'The given data was invalid.',
-                'validation' => [
-                    'file' => ['The file field is required when link is not present.'],
-                    'link' => ['The link field is required when file is not present.'],
-                ],
-                'code' => 422,
-            ],
-        ]);
+        $resp->assertJson($this->validationResponse([
+            'file' => ['The file field is required when link is not present.'],
+            'link' => ['The link field is required when file is not present.'],
+        ]));
+    }
+
+    public function test_message_shown_if_file_is_not_a_valid_file()
+    {
+        $this->actingAsApiAdmin();
+        /** @var Page $page */
+        $page = Page::query()->first();
+
+        $details = [
+            'name'        => 'my attachment',
+            'uploaded_to' => $page->id,
+            'file'        => 'cat',
+        ];
+
+        $resp = $this->postJson($this->baseEndpoint, $details);
+        $resp->assertStatus(422);
+        $resp->assertJson($this->validationResponse(['file' => ['The file must be provided as a valid file.']]));
     }
 
     public function test_read_endpoint_for_link_attachment()
index cf1ecd84d44782d48ca5e7958edc210598ef4002..20cde049a1938d46e4aa55fe48a6da33fd90bb80 100644 (file)
@@ -659,14 +659,14 @@ class PageContentTest extends TestCase
 
     public function test_markdown_base64_extract_not_limited_by_pcre_limits()
     {
-        $pcreBacktrackLimit = ini_get("pcre.backtrack_limit");
-        $pcreRecursionLimit = ini_get("pcre.recursion_limit");
+        $pcreBacktrackLimit = ini_get('pcre.backtrack_limit');
+        $pcreRecursionLimit = ini_get('pcre.recursion_limit');
 
         $this->asEditor();
         $page = Page::query()->first();
 
-        ini_set("pcre.backtrack_limit", "500");
-        ini_set("pcre.recursion_limit", "500");
+        ini_set('pcre.backtrack_limit', '500');
+        ini_set('pcre.recursion_limit', '500');
 
         $content = str_repeat('a', 5000);
         $base64Content = base64_encode($content);
@@ -686,8 +686,8 @@ class PageContentTest extends TestCase
         $this->assertEquals($content, file_get_contents($imageFile));
 
         $this->deleteImage($imagePath);
-        ini_set("pcre.backtrack_limit", $pcreBacktrackLimit);
-        ini_set("pcre.recursion_limit", $pcreRecursionLimit);
+        ini_set('pcre.backtrack_limit', $pcreBacktrackLimit);
+        ini_set('pcre.recursion_limit', $pcreRecursionLimit);
     }
 
     public function test_base64_images_within_markdown_blanked_if_not_supported_extension_for_extract()