forked from BookStackApp/BookStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchApiTest.php
More file actions
121 lines (101 loc) · 4.37 KB
/
SearchApiTest.php
File metadata and controls
121 lines (101 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
namespace Tests\Api;
use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Bookshelf;
use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Models\Entity;
use BookStack\Entities\Models\Page;
use Tests\TestCase;
class SearchApiTest extends TestCase
{
use TestsApi;
protected string $baseEndpoint = '/api/search';
public function test_all_endpoint_returns_search_filtered_results_with_query()
{
$this->actingAsApiEditor();
$uniqueTerm = 'MySuperUniqueTermForSearching';
/** @var Entity $entityClass */
foreach ([Page::class, Chapter::class, Book::class, Bookshelf::class] as $entityClass) {
/** @var Entity $first */
$first = $entityClass::query()->first();
$first->update(['name' => $uniqueTerm]);
$first->indexForSearch();
}
$resp = $this->getJson($this->baseEndpoint . '?query=' . $uniqueTerm . '&count=5&page=1');
$resp->assertJsonCount(4, 'data');
$resp->assertJsonFragment(['name' => $uniqueTerm, 'type' => 'book']);
$resp->assertJsonFragment(['name' => $uniqueTerm, 'type' => 'chapter']);
$resp->assertJsonFragment(['name' => $uniqueTerm, 'type' => 'page']);
$resp->assertJsonFragment(['name' => $uniqueTerm, 'type' => 'bookshelf']);
}
public function test_all_endpoint_returns_entity_url()
{
$page = $this->entities->page();
$page->update(['name' => 'name with superuniquevalue within']);
$page->indexForSearch();
$resp = $this->actingAsApiAdmin()->getJson($this->baseEndpoint . '?query=superuniquevalue');
$resp->assertJsonFragment([
'type' => 'page',
'url' => $page->getUrl(),
]);
}
public function test_all_endpoint_returns_items_with_preview_html()
{
$book = $this->entities->book();
$book->forceFill(['name' => 'name with superuniquevalue within', 'description' => 'Description with superuniquevalue within'])->save();
$book->indexForSearch();
$resp = $this->actingAsApiAdmin()->getJson($this->baseEndpoint . '?query=superuniquevalue');
$resp->assertJsonFragment([
'type' => 'book',
'url' => $book->getUrl(),
'preview_html' => [
'name' => 'name with <strong>superuniquevalue</strong> within',
'content' => 'Description with <strong>superuniquevalue</strong> within',
],
]);
}
public function test_all_endpoint_requires_query_parameter()
{
$resp = $this->actingAsApiEditor()->get($this->baseEndpoint);
$resp->assertStatus(422);
$resp = $this->actingAsApiEditor()->get($this->baseEndpoint . '?query=myqueryvalue');
$resp->assertOk();
}
public function test_all_endpoint_includes_parent_details_where_visible()
{
$page = $this->entities->pageWithinChapter();
$chapter = $page->chapter;
$book = $page->book;
$page->update(['name' => 'name with superextrauniquevalue within']);
$page->indexForSearch();
$editor = $this->users->editor();
$this->actingAsApiEditor();
$resp = $this->getJson($this->baseEndpoint . '?query=superextrauniquevalue');
$resp->assertJsonFragment([
'id' => $page->id,
'type' => 'page',
'book' => [
'id' => $book->id,
'name' => $book->name,
'slug' => $book->slug,
],
'chapter' => [
'id' => $chapter->id,
'name' => $chapter->name,
'slug' => $chapter->slug,
],
]);
$this->permissions->disableEntityInheritedPermissions($chapter);
$this->permissions->setEntityPermissions($page, ['view'], [$editor->roles()->first()]);
$resp = $this->getJson($this->baseEndpoint . '?query=superextrauniquevalue');
$resp->assertOk();
$resp->assertJsonPath('data.0.id', $page->id);
$resp->assertJsonPath('data.0.book.name', $book->name);
$resp->assertJsonMissingPath('data.0.chapter');
$this->permissions->disableEntityInheritedPermissions($book);
$resp = $this->getJson($this->baseEndpoint . '?query=superextrauniquevalue');
$resp->assertOk();
$resp->assertJsonPath('data.0.id', $page->id);
$resp->assertJsonMissingPath('data.0.book.name');
}
}