1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2026-01-03 23:42:28 +03:00

respective book and chapter structure added.

This commit is contained in:
Rashad
2024-10-27 22:50:20 +05:30
parent 90a8070518
commit f606711463
5 changed files with 103 additions and 246 deletions

View File

@@ -2,7 +2,6 @@
namespace Tests\Api;
use BookStack\Activity\Models\Tag;
use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Bookshelf;
use BookStack\Entities\Models\Chapter;
@@ -75,112 +74,4 @@ class SearchApiTest extends TestCase
$resp = $this->actingAsApiEditor()->get($this->baseEndpoint . '?query=myqueryvalue');
$resp->assertOk();
}
public function test_all_endpoint_includes_book_and_chapter_titles_when_requested()
{
$this->actingAsApiEditor();
$book = $this->entities->book();
$chapter = $this->entities->chapter();
$page = $this->entities->newPage();
$book->name = 'My Test Book';
$book->save();
$chapter->name = 'My Test Chapter';
$chapter->book_id = $book->id;
$chapter->save();
$page->name = 'My Test Page With UniqueSearchTerm';
$page->book_id = $book->id;
$page->chapter_id = $chapter->id;
$page->save();
$page->indexForSearch();
// Test without include parameter
$resp = $this->getJson($this->baseEndpoint . '?query=UniqueSearchTerm');
$resp->assertOk();
$resp->assertDontSee('book_title');
$resp->assertDontSee('chapter_title');
// Test with include parameter
$resp = $this->getJson($this->baseEndpoint . '?query=UniqueSearchTerm&include=titles');
$resp->assertOk();
$resp->assertJsonFragment([
'name' => 'My Test Page With UniqueSearchTerm',
'book_title' => 'My Test Book',
'chapter_title' => 'My Test Chapter',
'type' => 'page'
]);
}
public function test_all_endpoint_validates_include_parameter()
{
$this->actingAsApiEditor();
// Test invalid include value
$resp = $this->getJson($this->baseEndpoint . '?query=test&include=invalid');
$resp->assertOk();
$resp->assertDontSee('book_title');
// Test SQL injection attempt
$resp = $this->getJson($this->baseEndpoint . '?query=test&include=titles;DROP TABLE users');
$resp->assertStatus(422);
// Test multiple includes
$resp = $this->getJson($this->baseEndpoint . '?query=test&include=titles,tags');
$resp->assertOk();
}
public function test_all_endpoint_includes_tags_when_requested()
{
$this->actingAsApiEditor();
// Create a page and give it a unique name for search
$page = $this->entities->page();
$page->name = 'Page With UniqueSearchTerm';
$page->save();
// Save tags to the page using the existing saveTagsToEntity method
$tags = [
['name' => 'SampleTag', 'value' => 'SampleValue']
];
app(\BookStack\Activity\TagRepo::class)->saveTagsToEntity($page, $tags);
// Ensure the page is indexed for search
$page->indexForSearch();
// Test without the "tags" include
$resp = $this->getJson($this->baseEndpoint . '?query=UniqueSearchTerm');
$resp->assertOk();
$resp->assertDontSee('tags');
// Test with the "tags" include
$resp = $this->getJson($this->baseEndpoint . '?query=UniqueSearchTerm&include=tags');
$resp->assertOk();
// Assert that tags are included in the response
$resp->assertJsonFragment([
'name' => 'SampleTag',
'value' => 'SampleValue',
]);
// Optionally: check the structure to match the tag order as well
$resp->assertJsonStructure([
'data' => [
'*' => [
'tags' => [
'*' => [
'name',
'value',
'order',
],
],
],
],
]);
}
}