5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Bookshelf;
7 use BookStack\Entities\Models\Chapter;
8 use BookStack\Entities\Models\Entity;
9 use BookStack\Entities\Models\Page;
12 class SearchApiTest extends TestCase
16 protected $baseEndpoint = '/api/search';
18 public function test_all_endpoint_returns_search_filtered_results_with_query()
20 $this->actingAsApiEditor();
21 $uniqueTerm = 'MySuperUniqueTermForSearching';
23 /** @var Entity $entityClass */
24 foreach ([Page::class, Chapter::class, Book::class, Bookshelf::class] as $entityClass) {
25 /** @var Entity $first */
26 $first = $entityClass::query()->first();
27 $first->update(['name' => $uniqueTerm]);
28 $first->indexForSearch();
31 $resp = $this->getJson($this->baseEndpoint . '?query=' . $uniqueTerm . '&count=5&page=1');
32 $resp->assertJsonCount(4, 'data');
33 $resp->assertJsonFragment(['name' => $uniqueTerm, 'type' => 'book']);
34 $resp->assertJsonFragment(['name' => $uniqueTerm, 'type' => 'chapter']);
35 $resp->assertJsonFragment(['name' => $uniqueTerm, 'type' => 'page']);
36 $resp->assertJsonFragment(['name' => $uniqueTerm, 'type' => 'bookshelf']);
39 public function test_all_endpoint_returns_entity_url()
41 $page = $this->entities->page();
42 $page->update(['name' => 'name with superuniquevalue within']);
43 $page->indexForSearch();
45 $resp = $this->actingAsApiAdmin()->getJson($this->baseEndpoint . '?query=superuniquevalue');
46 $resp->assertJsonFragment([
48 'url' => $page->getUrl(),
52 public function test_all_endpoint_returns_items_with_preview_html()
54 $book = $this->entities->book();
55 $book->forceFill(['name' => 'name with superuniquevalue within', 'description' => 'Description with superuniquevalue within'])->save();
56 $book->indexForSearch();
58 $resp = $this->actingAsApiAdmin()->getJson($this->baseEndpoint . '?query=superuniquevalue');
59 $resp->assertJsonFragment([
61 'url' => $book->getUrl(),
63 'name' => 'name with <strong>superuniquevalue</strong> within',
64 'content' => 'Description with <strong>superuniquevalue</strong> within',
69 public function test_all_endpoint_requires_query_parameter()
71 $resp = $this->actingAsApiEditor()->get($this->baseEndpoint);
72 $resp->assertStatus(422);
74 $resp = $this->actingAsApiEditor()->get($this->baseEndpoint . '?query=myqueryvalue');