BookStack/tests/Api/SearchApiTest.php
Dan Brown 6f1bdbf771
Added API search endpoint
Is a little awkward, emulates a 'list' API endpoint but has unstable
paging and does not support filters/sort. This is detailed on the
endpoint though.

Made some updates to the docs system to better support parameters
and examples on GET requests.

Includes tests to cover.

For #909
2021-11-14 16:28:01 +00:00

48 lines
1.6 KiB
PHP

<?php
namespace Tests\Api;
use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Bookshelf;
use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Models\Entity;
use BookStack\Entities\Models\Page;
use Tests\TestCase;
class SearchApiTest extends TestCase
{
use TestsApi;
protected $baseEndpoint = '/api/search';
public function test_all_endpoint_returns_search_filtered_results_with_query()
{
$this->actingAsApiEditor();
$uniqueTerm = 'MySuperUniqueTermForSearching';
/** @var Entity $entityClass */
foreach ([Page::class, Chapter::class, Book::class, Bookshelf::class] as $entityClass) {
/** @var Entity $first */
$first = $entityClass::query()->first();
$first->update(['name' => $uniqueTerm]);
$first->indexForSearch();
}
$resp = $this->getJson($this->baseEndpoint . '?query=' . $uniqueTerm . '&count=5&page=1');
$resp->assertJsonCount(4, 'data');
$resp->assertJsonFragment(['name' => $uniqueTerm, 'type' => 'book']);
$resp->assertJsonFragment(['name' => $uniqueTerm, 'type' => 'chapter']);
$resp->assertJsonFragment(['name' => $uniqueTerm, 'type' => 'page']);
$resp->assertJsonFragment(['name' => $uniqueTerm, 'type' => 'bookshelf']);
}
public function test_all_endpoint_requires_query_parameter()
{
$resp = $this->actingAsApiEditor()->get($this->baseEndpoint);
$resp->assertStatus(422);
$resp = $this->actingAsApiEditor()->get($this->baseEndpoint . '?query=myqueryvalue');
$resp->assertOk();
}
}