]> BookStack Code Mirror - bookstack/blob - tests/Api/TestsApi.php
Started work on details/summary blocks
[bookstack] / tests / Api / TestsApi.php
1 <?php
2
3 namespace Tests\Api;
4
5 trait TestsApi
6 {
7     protected $apiTokenId = 'apitoken';
8     protected $apiTokenSecret = 'password';
9
10     /**
11      * Set the API editor role as the current user via the API driver.
12      */
13     protected function actingAsApiEditor()
14     {
15         $this->actingAs($this->getEditor(), 'api');
16
17         return $this;
18     }
19
20     /**
21      * Set the API admin role as the current user via the API driver.
22      */
23     protected function actingAsApiAdmin()
24     {
25         $this->actingAs($this->getAdmin(), 'api');
26
27         return $this;
28     }
29
30     /**
31      * Format the given items into a standardised error format.
32      */
33     protected function errorResponse(string $message, int $code): array
34     {
35         return ['error' => ['code' => $code, 'message' => $message]];
36     }
37
38     /**
39      * Format the given (field_name => ["messages"]) array
40      * into a standard validation response format.
41      */
42     protected function validationResponse(array $messages): array
43     {
44         $err = $this->errorResponse('The given data was invalid.', 422);
45         $err['error']['validation'] = $messages;
46
47         return $err;
48     }
49
50     /**
51      * Get an approved API auth header.
52      */
53     protected function apiAuthHeader(): array
54     {
55         return [
56             'Authorization' => "Token {$this->apiTokenId}:{$this->apiTokenSecret}",
57         ];
58     }
59 }