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