]> BookStack Code Mirror - bookstack/blob - tests/Api/SearchApiTest.php
Lexical: Added about button/view
[bookstack] / tests / Api / SearchApiTest.php
1 <?php
2
3 namespace Tests\Api;
4
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;
10 use Tests\TestCase;
11
12 class SearchApiTest extends TestCase
13 {
14     use TestsApi;
15
16     protected string $baseEndpoint = '/api/search';
17
18     public function test_all_endpoint_returns_search_filtered_results_with_query()
19     {
20         $this->actingAsApiEditor();
21         $uniqueTerm = 'MySuperUniqueTermForSearching';
22
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();
29         }
30
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']);
37     }
38
39     public function test_all_endpoint_returns_entity_url()
40     {
41         $page = $this->entities->page();
42         $page->update(['name' => 'name with superuniquevalue within']);
43         $page->indexForSearch();
44
45         $resp = $this->actingAsApiAdmin()->getJson($this->baseEndpoint . '?query=superuniquevalue');
46         $resp->assertJsonFragment([
47             'type' => 'page',
48             'url' => $page->getUrl(),
49         ]);
50     }
51
52     public function test_all_endpoint_returns_items_with_preview_html()
53     {
54         $book = $this->entities->book();
55         $book->forceFill(['name' => 'name with superuniquevalue within', 'description' => 'Description with superuniquevalue within'])->save();
56         $book->indexForSearch();
57
58         $resp = $this->actingAsApiAdmin()->getJson($this->baseEndpoint . '?query=superuniquevalue');
59         $resp->assertJsonFragment([
60             'type' => 'book',
61             'url' => $book->getUrl(),
62             'preview_html' => [
63                 'name' => 'name with <strong>superuniquevalue</strong> within',
64                 'content' => 'Description with <strong>superuniquevalue</strong> within',
65             ],
66         ]);
67     }
68
69     public function test_all_endpoint_requires_query_parameter()
70     {
71         $resp = $this->actingAsApiEditor()->get($this->baseEndpoint);
72         $resp->assertStatus(422);
73
74         $resp = $this->actingAsApiEditor()->get($this->baseEndpoint . '?query=myqueryvalue');
75         $resp->assertOk();
76     }
77
78     public function test_all_endpoint_includes_parent_details_where_visible()
79     {
80         $page = $this->entities->pageWithinChapter();
81         $chapter = $page->chapter;
82         $book = $page->book;
83
84         $page->update(['name' => 'name with superextrauniquevalue within']);
85         $page->indexForSearch();
86
87         $editor = $this->users->editor();
88         $this->actingAsApiEditor();
89         $resp = $this->getJson($this->baseEndpoint . '?query=superextrauniquevalue');
90         $resp->assertJsonFragment([
91             'id' => $page->id,
92             'type' => 'page',
93             'book' => [
94                 'id' => $book->id,
95                 'name' => $book->name,
96                 'slug' => $book->slug,
97             ],
98             'chapter' => [
99                 'id' => $chapter->id,
100                 'name' => $chapter->name,
101                 'slug' => $chapter->slug,
102             ],
103         ]);
104
105         $this->permissions->disableEntityInheritedPermissions($chapter);
106         $this->permissions->setEntityPermissions($page, ['view'], [$editor->roles()->first()]);
107
108         $resp = $this->getJson($this->baseEndpoint . '?query=superextrauniquevalue');
109         $resp->assertJsonPath('data.0.id', $page->id);
110         $resp->assertJsonPath('data.0.book.name', $book->name);
111         $resp->assertJsonMissingPath('data.0.chapter');
112
113         $this->permissions->disableEntityInheritedPermissions($book);
114
115         $resp = $this->getJson($this->baseEndpoint . '?query=superextrauniquevalue');
116         $resp->assertJsonPath('data.0.id', $page->id);
117         $resp->assertJsonMissingPath('data.0.book.name');
118     }
119 }