From 5e2bff813292bcec1da3d2709fca62304f776ae0 Mon Sep 17 00:00:00 2001 From: Helder Magalhaes Date: Fri, 10 Jan 2025 10:36:29 +0100 Subject: [PATCH] Allow search to filter by book_id and chapter_id --- app/Search/SearchRunner.php | 14 ++++++++++++++ tests/Entity/EntitySearchTest.php | 19 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/app/Search/SearchRunner.php b/app/Search/SearchRunner.php index 9716f8053be..ad317f76655 100644 --- a/app/Search/SearchRunner.php +++ b/app/Search/SearchRunner.php @@ -454,6 +454,20 @@ protected function filterSortBy(EloquentBuilder $query, Entity $model, string $i } } + protected function filterBookId(EloquentBuilder $query, Entity $model, string $input, bool $negated) + { + if ($model instanceof Page || $model instanceof Chapter) { + $this->applyNegatableWhere($query, $negated, 'book_id', '=', $input); + } + } + + protected function filterChapterId(EloquentBuilder $query, Entity $model, string $input, bool $negated) + { + if ($model instanceof Page) { + $this->applyNegatableWhere($query, $negated, 'chapter_id', '=', $input); + } + } + /** * Sorting filter options. */ diff --git a/tests/Entity/EntitySearchTest.php b/tests/Entity/EntitySearchTest.php index 5ace70e3ab2..9668193a3e2 100644 --- a/tests/Entity/EntitySearchTest.php +++ b/tests/Entity/EntitySearchTest.php @@ -30,6 +30,25 @@ public function test_bookshelf_search() $search->assertSeeText($shelf->name, true); } + public function test_book_id_search() + { + $book = Book::first(); + + $search = $this->asEditor()->get('/search?query={type:page}{book_id:' . $book->id . '}'); + $search->assertSee('Search Results'); + $search->assertSeeText($book->id, true); + } + + public function test_chapter_id_search() + { + $book = Book::first(); + $chapter = $book->chapters->last(); + + $search = $this->asEditor()->get('/search?query={type:page}{chapter_id:' . $chapter->id . '}'); + $search->assertSee('Search Results'); + $search->assertSeeText($chapter->id, true); + } + public function test_invalid_page_search() { $resp = $this->asEditor()->get('/search?term=' . urlencode('

test

'));