]> BookStack Code Mirror - bookstack/blob - tests/Api/ApiListingTest.php
Fixed test class names + add perm. check to api session auth
[bookstack] / tests / Api / ApiListingTest.php
1 <?php
2
3 namespace Tests;
4
5 use BookStack\Auth\Permissions\RolePermission;
6 use BookStack\Entities\Book;
7 use Carbon\Carbon;
8
9 class ApiListingTest extends TestCase
10 {
11     use TestsApi;
12
13     protected $endpoint = '/api/books';
14
15     public function test_count_parameter_limits_responses()
16     {
17         $this->actingAsApiEditor();
18         $bookCount = min(Book::visible()->count(), 100);
19
20         $resp = $this->get($this->endpoint);
21         $resp->assertJsonCount($bookCount, 'data');
22
23         $resp = $this->get($this->endpoint . '?count=1');
24         $resp->assertJsonCount(1, 'data');
25     }
26
27     public function test_offset_parameter()
28     {
29         $this->actingAsApiEditor();
30         $books = Book::visible()->orderBy('id')->take(3)->get();
31
32         $resp = $this->get($this->endpoint . '?count=1');
33         $resp->assertJsonMissing(['name' => $books[1]->name ]);
34
35         $resp = $this->get($this->endpoint . '?count=1&offset=1000');
36         $resp->assertJsonCount(0, 'data');
37     }
38
39     public function test_sort_parameter()
40     {
41         $this->actingAsApiEditor();
42
43         $sortChecks = [
44             '-id' => Book::visible()->orderBy('id', 'desc')->first(),
45             '+name' => Book::visible()->orderBy('name', 'asc')->first(),
46             'name' => Book::visible()->orderBy('name', 'asc')->first(),
47             '-name' => Book::visible()->orderBy('name', 'desc')->first()
48         ];
49
50         foreach ($sortChecks as $sortOption => $result) {
51             $resp = $this->get($this->endpoint . '?count=1&sort=' . $sortOption);
52             $resp->assertJson(['data' => [
53                 [
54                     'id' => $result->id,
55                     'name' => $result->name,
56                 ]
57             ]]);
58         }
59     }
60
61     public function test_filter_parameter()
62     {
63         $this->actingAsApiEditor();
64         $book = Book::visible()->first();
65         $nameSubstr = substr($book->name, 0, 4);
66         $encodedNameSubstr = rawurlencode($nameSubstr);
67
68         $filterChecks = [
69             // Test different types of filter
70             "filter[id]={$book->id}" => 1,
71             "filter[id:ne]={$book->id}" => Book::visible()->where('id', '!=', $book->id)->count(),
72             "filter[id:gt]={$book->id}" => Book::visible()->where('id', '>', $book->id)->count(),
73             "filter[id:gte]={$book->id}" => Book::visible()->where('id', '>=', $book->id)->count(),
74             "filter[id:lt]={$book->id}" => Book::visible()->where('id', '<', $book->id)->count(),
75             "filter[name:like]={$encodedNameSubstr}%" => Book::visible()->where('name', 'like', $nameSubstr . '%')->count(),
76
77             // Test mulitple filters 'and' together
78             "filter[id]={$book->id}&filter[name]=random_non_existing_string" => 0,
79         ];
80
81         foreach ($filterChecks as $filterOption => $resultCount) {
82             $resp = $this->get($this->endpoint . '?count=1&' . $filterOption);
83             $resp->assertJson(['total' => $resultCount]);
84         }
85     }
86
87 }