diff --git a/app/Search/SearchOptionSet.php b/app/Search/SearchOptionSet.php index f421abe2c..bd5e5a5b2 100644 --- a/app/Search/SearchOptionSet.php +++ b/app/Search/SearchOptionSet.php @@ -63,10 +63,20 @@ class SearchOptionSet } /** - * @return T[] + * @return self */ - public function negated(): array + public function negated(): self { - return array_values(array_filter($this->options, fn (SearchOption $option) => $option->negated)); + $values = array_values(array_filter($this->options, fn (SearchOption $option) => $option->negated)); + return new self($values); + } + + /** + * @return self + */ + public function nonNegated(): self + { + $values = array_values(array_filter($this->options, fn (SearchOption $option) => !$option->negated)); + return new self($values); } } diff --git a/app/Search/SearchOptions.php b/app/Search/SearchOptions.php index e362a8a01..7f9db2a64 100644 --- a/app/Search/SearchOptions.php +++ b/app/Search/SearchOptions.php @@ -244,9 +244,9 @@ class SearchOptions } // Negated items - array_push($options, ...$this->exacts->negated()); - array_push($options, ...$this->tags->negated()); - array_push($options, ...$this->filters->negated()); + array_push($options, ...$this->exacts->negated()->all()); + array_push($options, ...$this->tags->negated()->all()); + array_push($options, ...$this->filters->negated()->all()); return implode(' ', array_map(fn(SearchOption $o) => $o->toString(), $options)); } diff --git a/resources/views/search/all.blade.php b/resources/views/search/all.blade.php index 2a0d63a6e..ad437604b 100644 --- a/resources/views/search/all.blade.php +++ b/resources/views/search/all.blade.php @@ -9,7 +9,7 @@
{{ trans('entities.search_advanced') }}
@php - $filterMap = $options->filters->toValueMap(); + $filterMap = $options->filters->nonNegated()->toValueMap(); @endphp
{{ trans('entities.search_terms') }}
@@ -30,10 +30,10 @@
{{ trans('entities.search_exact_matches') }}
- @include('search.parts.term-list', ['type' => 'exact', 'currentList' => $options->exacts->toValueArray()]) + @include('search.parts.term-list', ['type' => 'exact', 'currentList' => $options->exacts->nonNegated()->toValueArray()])
{{ trans('entities.search_tags') }}
- @include('search.parts.term-list', ['type' => 'tags', 'currentList' => $options->tags->toValueArray()]) + @include('search.parts.term-list', ['type' => 'tags', 'currentList' => $options->tags->nonNegated()->toValueArray()]) @if(!user()->isGuest())
{{ trans('entities.search_options') }}
diff --git a/tests/Entity/EntitySearchTest.php b/tests/Entity/EntitySearchTest.php index bb1021a67..3a1a0a662 100644 --- a/tests/Entity/EntitySearchTest.php +++ b/tests/Entity/EntitySearchTest.php @@ -577,6 +577,14 @@ class EntitySearchTest extends TestCase $this->withHtml($resp)->assertFieldHasValue('extras', '{updated_by:dan} {created_by:dan} -"dog" -[a=b] -{viewed_by_me}'); } + public function test_negated_searches_dont_show_in_inputs() + { + $resp = $this->asEditor()->get('/search?term=' . urlencode('-{created_by:me} -[a=b] -"dog"')); + $this->withHtml($resp)->assertElementNotExists('input[name="tags[]"][value="a=b"]'); + $this->withHtml($resp)->assertElementNotExists('input[name="exact[]"][value="dog"]'); + $this->withHtml($resp)->assertElementNotExists('input[name="filters[created_by]"][value="me"][checked="checked"]'); + } + public function test_searches_with_user_filters_using_me_adds_them_into_advanced_search_form() { $resp = $this->asEditor()->get('/search?term=' . urlencode('test {updated_by:me} {created_by:me}')); diff --git a/tests/Entity/SearchOptionsTest.php b/tests/Entity/SearchOptionsTest.php index 543badcef..ae0f1e56a 100644 --- a/tests/Entity/SearchOptionsTest.php +++ b/tests/Entity/SearchOptionsTest.php @@ -123,7 +123,7 @@ class SearchOptionsTest extends TestCase $options = SearchOptions::fromRequest($request); $this->assertCount(2, $options->tags->all()); - $this->assertEquals('b=c', $options->tags->negated()[0]->value); + $this->assertEquals('b=c', $options->tags->negated()->all()[0]->value); $this->assertEquals('viewed_by_me', $options->filters->all()[0]->getKey()); $this->assertTrue($options->filters->all()[0]->negated); $this->assertEquals('dino', $options->exacts->all()[0]->value);