1 <?php namespace Tests\Api;
3 use BookStack\Entities\Models\Book;
6 class ApiListingTest extends TestCase
10 protected $endpoint = '/api/books';
12 public function test_count_parameter_limits_responses()
14 $this->actingAsApiEditor();
15 $bookCount = min(Book::visible()->count(), 100);
17 $resp = $this->get($this->endpoint);
18 $resp->assertJsonCount($bookCount, 'data');
20 $resp = $this->get($this->endpoint . '?count=1');
21 $resp->assertJsonCount(1, 'data');
24 public function test_offset_parameter()
26 $this->actingAsApiEditor();
27 $books = Book::visible()->orderBy('id')->take(3)->get();
29 $resp = $this->get($this->endpoint . '?count=1');
30 $resp->assertJsonMissing(['name' => $books[1]->name ]);
32 $resp = $this->get($this->endpoint . '?count=1&offset=1000');
33 $resp->assertJsonCount(0, 'data');
36 public function test_sort_parameter()
38 $this->actingAsApiEditor();
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()
47 foreach ($sortChecks as $sortOption => $result) {
48 $resp = $this->get($this->endpoint . '?count=1&sort=' . $sortOption);
49 $resp->assertJson(['data' => [
52 'name' => $result->name,
58 public function test_filter_parameter()
60 $this->actingAsApiEditor();
61 $book = Book::visible()->first();
62 $nameSubstr = substr($book->name, 0, 4);
63 $encodedNameSubstr = rawurlencode($nameSubstr);
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(),
74 // Test mulitple filters 'and' together
75 "filter[id]={$book->id}&filter[name]=random_non_existing_string" => 0,
78 foreach ($filterChecks as $filterOption => $resultCount) {
79 $resp = $this->get($this->endpoint . '?count=1&' . $filterOption);
80 $resp->assertJson(['total' => $resultCount]);
84 public function test_total_on_results_shows_correctly()
86 $this->actingAsApiEditor();
87 $bookCount = Book::query()->count();
88 $resp = $this->get($this->endpoint . '?count=1');
89 $resp->assertJson(['total' => $bookCount ]);
92 public function test_total_on_results_shows_correctly_when_offset_provided()
94 $this->actingAsApiEditor();
95 $bookCount = Book::query()->count();
96 $resp = $this->get($this->endpoint . '?count=1&offset=1');
97 $resp->assertJson(['total' => $bookCount ]);