5 use BookStack\Auth\Permissions\RolePermission;
6 use BookStack\Entities\Book;
9 class ApiListingTest extends TestCase
13 protected $endpoint = '/api/books';
15 public function test_count_parameter_limits_responses()
17 $this->actingAsApiEditor();
18 $bookCount = min(Book::visible()->count(), 100);
20 $resp = $this->get($this->endpoint);
21 $resp->assertJsonCount($bookCount, 'data');
23 $resp = $this->get($this->endpoint . '?count=1');
24 $resp->assertJsonCount(1, 'data');
27 public function test_offset_parameter()
29 $this->actingAsApiEditor();
30 $books = Book::visible()->orderBy('id')->take(3)->get();
32 $resp = $this->get($this->endpoint . '?count=1');
33 $resp->assertJsonMissing(['name' => $books[1]->name ]);
35 $resp = $this->get($this->endpoint . '?count=1&offset=1000');
36 $resp->assertJsonCount(0, 'data');
39 public function test_sort_parameter()
41 $this->actingAsApiEditor();
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()
50 foreach ($sortChecks as $sortOption => $result) {
51 $resp = $this->get($this->endpoint . '?count=1&sort=' . $sortOption);
52 $resp->assertJson(['data' => [
55 'name' => $result->name,
61 public function test_filter_parameter()
63 $this->actingAsApiEditor();
64 $book = Book::visible()->first();
65 $nameSubstr = substr($book->name, 0, 4);
66 $encodedNameSubstr = rawurlencode($nameSubstr);
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(),
77 // Test mulitple filters 'and' together
78 "filter[id]={$book->id}&filter[name]=random_non_existing_string" => 0,
81 foreach ($filterChecks as $filterOption => $resultCount) {
82 $resp = $this->get($this->endpoint . '?count=1&' . $filterOption);
83 $resp->assertJson(['total' => $resultCount]);