]> BookStack Code Mirror - bookstack/blob - tests/Api/SearchApiTest.php
Merge branch 'fix/markdown-export' into development
[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 $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 }