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