Skip to content

Commit 966ff91

Browse files
committed
Search: Prevented negated terms filling in UI inputs
Added test to cover.
1 parent cd84d08 commit 966ff91

File tree

5 files changed

+28
-10
lines changed

5 files changed

+28
-10
lines changed

app/Search/SearchOptionSet.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,20 @@ public function all(): array
6363
}
6464

6565
/**
66-
* @return T[]
66+
* @return self<T>
67+
*/
68+
public function negated(): self
69+
{
70+
$values = array_values(array_filter($this->options, fn (SearchOption $option) => $option->negated));
71+
return new self($values);
72+
}
73+
74+
/**
75+
* @return self<T>
6776
*/
68-
public function negated(): array
77+
public function nonNegated(): self
6978
{
70-
return array_values(array_filter($this->options, fn (SearchOption $option) => $option->negated));
79+
$values = array_values(array_filter($this->options, fn (SearchOption $option) => !$option->negated));
80+
return new self($values);
7181
}
7282
}

app/Search/SearchOptions.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,9 @@ public function getAdditionalOptionsString(): string
244244
}
245245

246246
// Negated items
247-
array_push($options, ...$this->exacts->negated());
248-
array_push($options, ...$this->tags->negated());
249-
array_push($options, ...$this->filters->negated());
247+
array_push($options, ...$this->exacts->negated()->all());
248+
array_push($options, ...$this->tags->negated()->all());
249+
array_push($options, ...$this->filters->negated()->all());
250250

251251
return implode(' ', array_map(fn(SearchOption $o) => $o->toString(), $options));
252252
}

resources/views/search/all.blade.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<h5>{{ trans('entities.search_advanced') }}</h5>
1010

1111
@php
12-
$filterMap = $options->filters->toValueMap();
12+
$filterMap = $options->filters->nonNegated()->toValueMap();
1313
@endphp
1414
<form method="get" action="{{ url('/search') }}">
1515
<h6>{{ trans('entities.search_terms') }}</h6>
@@ -30,10 +30,10 @@
3030
</div>
3131

3232
<h6>{{ trans('entities.search_exact_matches') }}</h6>
33-
@include('search.parts.term-list', ['type' => 'exact', 'currentList' => $options->exacts->toValueArray()])
33+
@include('search.parts.term-list', ['type' => 'exact', 'currentList' => $options->exacts->nonNegated()->toValueArray()])
3434

3535
<h6>{{ trans('entities.search_tags') }}</h6>
36-
@include('search.parts.term-list', ['type' => 'tags', 'currentList' => $options->tags->toValueArray()])
36+
@include('search.parts.term-list', ['type' => 'tags', 'currentList' => $options->tags->nonNegated()->toValueArray()])
3737

3838
@if(!user()->isGuest())
3939
<h6>{{ trans('entities.search_options') }}</h6>

tests/Entity/EntitySearchTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,14 @@ public function test_searches_with_terms_without_controls_includes_them_in_extra
577577
$this->withHtml($resp)->assertFieldHasValue('extras', '{updated_by:dan} {created_by:dan} -"dog" -[a=b] -{viewed_by_me}');
578578
}
579579

580+
public function test_negated_searches_dont_show_in_inputs()
581+
{
582+
$resp = $this->asEditor()->get('/search?term=' . urlencode('-{created_by:me} -[a=b] -"dog"'));
583+
$this->withHtml($resp)->assertElementNotExists('input[name="tags[]"][value="a=b"]');
584+
$this->withHtml($resp)->assertElementNotExists('input[name="exact[]"][value="dog"]');
585+
$this->withHtml($resp)->assertElementNotExists('input[name="filters[created_by]"][value="me"][checked="checked"]');
586+
}
587+
580588
public function test_searches_with_user_filters_using_me_adds_them_into_advanced_search_form()
581589
{
582590
$resp = $this->asEditor()->get('/search?term=' . urlencode('test {updated_by:me} {created_by:me}'));

tests/Entity/SearchOptionsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function test_from_request_properly_parses_out_extras_as_string()
123123

124124
$options = SearchOptions::fromRequest($request);
125125
$this->assertCount(2, $options->tags->all());
126-
$this->assertEquals('b=c', $options->tags->negated()[0]->value);
126+
$this->assertEquals('b=c', $options->tags->negated()->all()[0]->value);
127127
$this->assertEquals('viewed_by_me', $options->filters->all()[0]->getKey());
128128
$this->assertTrue($options->filters->all()[0]->negated);
129129
$this->assertEquals('dino', $options->exacts->all()[0]->value);

0 commit comments

Comments
 (0)