2021-11-14 16:28:01 +00:00
|
|
|
<?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;
|
|
|
|
|
2024-12-03 13:47:45 +00:00
|
|
|
protected string $baseEndpoint = '/api/search';
|
2021-11-14 16:28:01 +00:00
|
|
|
|
|
|
|
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']);
|
|
|
|
}
|
|
|
|
|
2021-12-06 20:42:04 +00:00
|
|
|
public function test_all_endpoint_returns_entity_url()
|
|
|
|
{
|
2022-09-29 17:31:38 +01:00
|
|
|
$page = $this->entities->page();
|
2021-12-06 20:42:04 +00:00
|
|
|
$page->update(['name' => 'name with superuniquevalue within']);
|
|
|
|
$page->indexForSearch();
|
|
|
|
|
|
|
|
$resp = $this->actingAsApiAdmin()->getJson($this->baseEndpoint . '?query=superuniquevalue');
|
|
|
|
$resp->assertJsonFragment([
|
|
|
|
'type' => 'page',
|
2024-10-21 02:42:49 +05:30
|
|
|
'url' => $page->getUrl(),
|
2021-12-06 20:42:04 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_all_endpoint_returns_items_with_preview_html()
|
|
|
|
{
|
2022-09-29 17:31:38 +01:00
|
|
|
$book = $this->entities->book();
|
2023-12-20 17:21:09 +00:00
|
|
|
$book->forceFill(['name' => 'name with superuniquevalue within', 'description' => 'Description with superuniquevalue within'])->save();
|
2021-12-06 20:42:04 +00:00
|
|
|
$book->indexForSearch();
|
|
|
|
|
|
|
|
$resp = $this->actingAsApiAdmin()->getJson($this->baseEndpoint . '?query=superuniquevalue');
|
|
|
|
$resp->assertJsonFragment([
|
2024-10-21 02:42:49 +05:30
|
|
|
'type' => 'book',
|
|
|
|
'url' => $book->getUrl(),
|
2021-12-06 20:42:04 +00:00
|
|
|
'preview_html' => [
|
2024-10-21 02:42:49 +05:30
|
|
|
'name' => 'name with <strong>superuniquevalue</strong> within',
|
2021-12-06 20:42:04 +00:00
|
|
|
'content' => 'Description with <strong>superuniquevalue</strong> within',
|
2021-12-15 13:49:20 +00:00
|
|
|
],
|
2021-12-06 20:42:04 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2021-11-14 16:28:01 +00:00
|
|
|
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();
|
|
|
|
}
|
2024-12-03 13:47:45 +00:00
|
|
|
|
|
|
|
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->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->assertJsonPath('data.0.id', $page->id);
|
|
|
|
$resp->assertJsonMissingPath('data.0.book.name');
|
|
|
|
}
|
2021-11-14 16:28:01 +00:00
|
|
|
}
|