]> BookStack Code Mirror - bookstack/blobdiff - tests/Api/TestsApi.php
Fixes padding issues of the sidebar's items
[bookstack] / tests / Api / TestsApi.php
index 623fa6969a7128bf9652f9e50e16fa710d29ac54..97ca82ea71c914bf5597408f8dd5fc5aec2087b2 100644 (file)
@@ -1,8 +1,9 @@
-<?php namespace Tests\Api;
+<?php
+
+namespace Tests\Api;
 
 trait TestsApi
 {
-
     protected $apiTokenId = 'apitoken';
     protected $apiTokenSecret = 'password';
 
@@ -12,6 +13,17 @@ trait TestsApi
     protected function actingAsApiEditor()
     {
         $this->actingAs($this->getEditor(), 'api');
+
+        return $this;
+    }
+
+    /**
+     * Set the API admin role as the current user via the API driver.
+     */
+    protected function actingAsApiAdmin()
+    {
+        $this->actingAs($this->getAdmin(), 'api');
+
         return $this;
     }
 
@@ -20,7 +32,19 @@ trait TestsApi
      */
     protected function errorResponse(string $message, int $code): array
     {
-        return ["error" => ["code" => $code, "message" => $message]];
+        return ['error' => ['code' => $code, 'message' => $message]];
+    }
+
+    /**
+     * Format the given (field_name => ["messages"]) array
+     * into a standard validation response format.
+     */
+    protected function validationResponse(array $messages): array
+    {
+        $err = $this->errorResponse('The given data was invalid.', 422);
+        $err['error']['validation'] = $messages;
+
+        return $err;
     }
 
     /**
@@ -29,8 +53,7 @@ trait TestsApi
     protected function apiAuthHeader(): array
     {
         return [
-            "Authorization" => "Token {$this->apiTokenId}:{$this->apiTokenSecret}"
+            'Authorization' => "Token {$this->apiTokenId}:{$this->apiTokenSecret}",
         ];
     }
-
-}
\ No newline at end of file
+}