5 use BookStack\Entities\Book;
7 class ApiListingTest extends TestCase
11 protected $endpoint = '/api/books';
13 public function test_count_parameter_limits_responses()
15 $this->actingAsApiEditor();
16 $bookCount = min(Book::visible()->count(), 100);
18 $resp = $this->get($this->endpoint);
19 $resp->assertJsonCount($bookCount, 'data');
21 $resp = $this->get($this->endpoint . '?count=1');
22 $resp->assertJsonCount(1, 'data');
25 public function test_offset_parameter()
27 $this->actingAsApiEditor();
28 $books = Book::visible()->orderBy('id')->take(3)->get();
30 $resp = $this->get($this->endpoint . '?count=1');
31 $resp->assertJsonMissing(['name' => $books[1]->name ]);
33 $resp = $this->get($this->endpoint . '?count=1&offset=1000');
34 $resp->assertJsonCount(0, 'data');
37 public function test_sort_parameter()
39 $this->actingAsApiEditor();
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()
48 foreach ($sortChecks as $sortOption => $result) {
49 $resp = $this->get($this->endpoint . '?count=1&sort=' . $sortOption);
50 $resp->assertJson(['data' => [
53 'name' => $result->name,
59 public function test_filter_parameter()
61 $this->actingAsApiEditor();
62 $book = Book::visible()->first();
63 $nameSubstr = substr($book->name, 0, 4);
64 $encodedNameSubstr = rawurlencode($nameSubstr);
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(),
75 // Test mulitple filters 'and' together
76 "filter[id]={$book->id}&filter[name]=random_non_existing_string" => 0,
79 foreach ($filterChecks as $filterOption => $resultCount) {
80 $resp = $this->get($this->endpoint . '?count=1&' . $filterOption);
81 $resp->assertJson(['total' => $resultCount]);