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 /** @var Page $page */
42 $page = Page::query()->first();
43 $page->update(['name' => 'name with superuniquevalue within']);
44 $page->indexForSearch();
46 $resp = $this->actingAsApiAdmin()->getJson($this->baseEndpoint . '?query=superuniquevalue');
47 $resp->assertJsonFragment([
49 'url' => $page->getUrl(),
53 public function test_all_endpoint_returns_items_with_preview_html()
55 /** @var Book $book */
56 $book = Book::query()->first();
57 $book->update(['name' => 'name with superuniquevalue within', 'description' => 'Description with superuniquevalue within']);
58 $book->indexForSearch();
60 $resp = $this->actingAsApiAdmin()->getJson($this->baseEndpoint . '?query=superuniquevalue');
61 $resp->assertJsonFragment([
63 'url' => $book->getUrl(),
65 'name' => 'name with <strong>superuniquevalue</strong> within',
66 'content' => 'Description with <strong>superuniquevalue</strong> within',
71 public function test_all_endpoint_requires_query_parameter()
73 $resp = $this->actingAsApiEditor()->get($this->baseEndpoint);
74 $resp->assertStatus(422);
76 $resp = $this->actingAsApiEditor()->get($this->baseEndpoint . '?query=myqueryvalue');