| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Tests\Api; | 
					
						
							| 
									
										
										
										
											2019-12-31 04:48:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 08:17:45 +08:00
										 |  |  | use BookStack\Entities\Models\Book; | 
					
						
							| 
									
										
										
										
											2020-04-04 08:16:05 +08:00
										 |  |  | use Tests\TestCase; | 
					
						
							| 
									
										
										
										
											2019-12-31 04:48:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-02 01:01:36 +08:00
										 |  |  | class ApiListingTest extends TestCase | 
					
						
							| 
									
										
										
										
											2019-12-31 04:48:23 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     use TestsApi; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected $endpoint = '/api/books'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_count_parameter_limits_responses() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->actingAsApiEditor(); | 
					
						
							|  |  |  |         $bookCount = min(Book::visible()->count(), 100); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->get($this->endpoint); | 
					
						
							|  |  |  |         $resp->assertJsonCount($bookCount, 'data'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->get($this->endpoint . '?count=1'); | 
					
						
							|  |  |  |         $resp->assertJsonCount(1, 'data'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_offset_parameter() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->actingAsApiEditor(); | 
					
						
							|  |  |  |         $books = Book::visible()->orderBy('id')->take(3)->get(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->get($this->endpoint . '?count=1'); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |         $resp->assertJsonMissing(['name' => $books[1]->name]); | 
					
						
							| 
									
										
										
										
											2019-12-31 04:48:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->get($this->endpoint . '?count=1&offset=1000'); | 
					
						
							|  |  |  |         $resp->assertJsonCount(0, 'data'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_sort_parameter() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->actingAsApiEditor(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $sortChecks = [ | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             '-id'   => Book::visible()->orderBy('id', 'desc')->first(), | 
					
						
							| 
									
										
										
										
											2019-12-31 04:48:23 +08:00
										 |  |  |             '+name' => Book::visible()->orderBy('name', 'asc')->first(), | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'name'  => Book::visible()->orderBy('name', 'asc')->first(), | 
					
						
							|  |  |  |             '-name' => Book::visible()->orderBy('name', 'desc')->first(), | 
					
						
							| 
									
										
										
										
											2019-12-31 04:48:23 +08:00
										 |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         foreach ($sortChecks as $sortOption => $result) { | 
					
						
							|  |  |  |             $resp = $this->get($this->endpoint . '?count=1&sort=' . $sortOption); | 
					
						
							|  |  |  |             $resp->assertJson(['data' => [ | 
					
						
							|  |  |  |                 [ | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |                     'id'   => $result->id, | 
					
						
							| 
									
										
										
										
											2019-12-31 04:48:23 +08:00
										 |  |  |                     'name' => $result->name, | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |                 ], | 
					
						
							| 
									
										
										
										
											2019-12-31 04:48:23 +08:00
										 |  |  |             ]]); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-02 00:33:47 +08:00
										 |  |  |     public function test_filter_parameter() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->actingAsApiEditor(); | 
					
						
							|  |  |  |         $book = Book::visible()->first(); | 
					
						
							|  |  |  |         $nameSubstr = substr($book->name, 0, 4); | 
					
						
							|  |  |  |         $encodedNameSubstr = rawurlencode($nameSubstr); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $filterChecks = [ | 
					
						
							|  |  |  |             // Test different types of filter
 | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             "filter[id]={$book->id}"                  => 1, | 
					
						
							|  |  |  |             "filter[id:ne]={$book->id}"               => Book::visible()->where('id', '!=', $book->id)->count(), | 
					
						
							|  |  |  |             "filter[id:gt]={$book->id}"               => Book::visible()->where('id', '>', $book->id)->count(), | 
					
						
							|  |  |  |             "filter[id:gte]={$book->id}"              => Book::visible()->where('id', '>=', $book->id)->count(), | 
					
						
							|  |  |  |             "filter[id:lt]={$book->id}"               => Book::visible()->where('id', '<', $book->id)->count(), | 
					
						
							| 
									
										
										
										
											2020-01-02 00:33:47 +08:00
										 |  |  |             "filter[name:like]={$encodedNameSubstr}%" => Book::visible()->where('name', 'like', $nameSubstr . '%')->count(), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // Test mulitple filters 'and' together
 | 
					
						
							|  |  |  |             "filter[id]={$book->id}&filter[name]=random_non_existing_string" => 0, | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         foreach ($filterChecks as $filterOption => $resultCount) { | 
					
						
							|  |  |  |             $resp = $this->get($this->endpoint . '?count=1&' . $filterOption); | 
					
						
							|  |  |  |             $resp->assertJson(['total' => $resultCount]); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-25 23:38:11 +08:00
										 |  |  |     public function test_total_on_results_shows_correctly() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->actingAsApiEditor(); | 
					
						
							|  |  |  |         $bookCount = Book::query()->count(); | 
					
						
							|  |  |  |         $resp = $this->get($this->endpoint . '?count=1'); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |         $resp->assertJson(['total' => $bookCount]); | 
					
						
							| 
									
										
										
										
											2020-04-25 23:38:11 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_total_on_results_shows_correctly_when_offset_provided() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->actingAsApiEditor(); | 
					
						
							|  |  |  |         $bookCount = Book::query()->count(); | 
					
						
							|  |  |  |         $resp = $this->get($this->endpoint . '?count=1&offset=1'); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |         $resp->assertJson(['total' => $bookCount]); | 
					
						
							| 
									
										
										
										
											2020-04-25 23:38:11 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | } |