5 use BookStack\Activity\Models\Tag;
6 use BookStack\Entities\Models\Book;
7 use BookStack\Entities\Models\Bookshelf;
8 use BookStack\Entities\Models\Chapter;
9 use BookStack\Entities\Models\Entity;
10 use BookStack\Entities\Models\Page;
13 class SearchApiTest extends TestCase
17 protected $baseEndpoint = '/api/search';
19 public function test_all_endpoint_returns_search_filtered_results_with_query()
21 $this->actingAsApiEditor();
22 $uniqueTerm = 'MySuperUniqueTermForSearching';
24 /** @var Entity $entityClass */
25 foreach ([Page::class, Chapter::class, Book::class, Bookshelf::class] as $entityClass) {
26 /** @var Entity $first */
27 $first = $entityClass::query()->first();
28 $first->update(['name' => $uniqueTerm]);
29 $first->indexForSearch();
32 $resp = $this->getJson($this->baseEndpoint . '?query=' . $uniqueTerm . '&count=5&page=1');
33 $resp->assertJsonCount(4, 'data');
34 $resp->assertJsonFragment(['name' => $uniqueTerm, 'type' => 'book']);
35 $resp->assertJsonFragment(['name' => $uniqueTerm, 'type' => 'chapter']);
36 $resp->assertJsonFragment(['name' => $uniqueTerm, 'type' => 'page']);
37 $resp->assertJsonFragment(['name' => $uniqueTerm, 'type' => 'bookshelf']);
40 public function test_all_endpoint_returns_entity_url()
42 $page = $this->entities->page();
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 $book = $this->entities->book();
56 $book->forceFill(['name' => 'name with superuniquevalue within', 'description' => 'Description with superuniquevalue within'])->save();
57 $book->indexForSearch();
59 $resp = $this->actingAsApiAdmin()->getJson($this->baseEndpoint . '?query=superuniquevalue');
60 $resp->assertJsonFragment([
62 'url' => $book->getUrl(),
64 'name' => 'name with <strong>superuniquevalue</strong> within',
65 'content' => 'Description with <strong>superuniquevalue</strong> within',
70 public function test_all_endpoint_requires_query_parameter()
72 $resp = $this->actingAsApiEditor()->get($this->baseEndpoint);
73 $resp->assertStatus(422);
75 $resp = $this->actingAsApiEditor()->get($this->baseEndpoint . '?query=myqueryvalue');
79 public function test_all_endpoint_includes_book_and_chapter_titles_when_requested()
81 $this->actingAsApiEditor();
83 $book = $this->entities->book();
84 $chapter = $this->entities->chapter();
85 $page = $this->entities->newPage();
87 $book->name = 'My Test Book';
90 $chapter->name = 'My Test Chapter';
91 $chapter->book_id = $book->id;
94 $page->name = 'My Test Page With UniqueSearchTerm';
95 $page->book_id = $book->id;
96 $page->chapter_id = $chapter->id;
99 $page->indexForSearch();
101 // Test without include parameter
102 $resp = $this->getJson($this->baseEndpoint . '?query=UniqueSearchTerm');
104 $resp->assertDontSee('book_title');
105 $resp->assertDontSee('chapter_title');
107 // Test with include parameter
108 $resp = $this->getJson($this->baseEndpoint . '?query=UniqueSearchTerm&include=titles');
110 $resp->assertJsonFragment([
111 'name' => 'My Test Page With UniqueSearchTerm',
112 'book_title' => 'My Test Book',
113 'chapter_title' => 'My Test Chapter',
118 public function test_all_endpoint_validates_include_parameter()
120 $this->actingAsApiEditor();
122 // Test invalid include value
123 $resp = $this->getJson($this->baseEndpoint . '?query=test&include=invalid');
125 $resp->assertDontSee('book_title');
127 // Test SQL injection attempt
128 $resp = $this->getJson($this->baseEndpoint . '?query=test&include=titles;DROP TABLE users');
129 $resp->assertStatus(422);
131 // Test multiple includes
132 $resp = $this->getJson($this->baseEndpoint . '?query=test&include=titles,tags');
136 public function test_all_endpoint_includes_tags_when_requested()
138 $this->actingAsApiEditor();
140 // Create a page and give it a unique name for search
141 $page = $this->entities->page();
142 $page->name = 'Page With UniqueSearchTerm';
145 // Save tags to the page using the existing saveTagsToEntity method
147 ['name' => 'SampleTag', 'value' => 'SampleValue']
149 app(\BookStack\Activity\TagRepo::class)->saveTagsToEntity($page, $tags);
151 // Ensure the page is indexed for search
152 $page->indexForSearch();
154 // Test without the "tags" include
155 $resp = $this->getJson($this->baseEndpoint . '?query=UniqueSearchTerm');
157 $resp->assertDontSee('tags');
159 // Test with the "tags" include
160 $resp = $this->getJson($this->baseEndpoint . '?query=UniqueSearchTerm&include=tags');
163 // Assert that tags are included in the response
164 $resp->assertJsonFragment([
165 'name' => 'SampleTag',
166 'value' => 'SampleValue',
169 // Optionally: check the structure to match the tag order as well
170 $resp->assertJsonStructure([