Merge pull request #5239 from BookStackApp/search_negation

Search term negation
This commit is contained in:
Dan Brown 2024-10-03 19:52:06 +01:00 committed by GitHub
commit 6646dcc24d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 545 additions and 225 deletions

View File

@ -0,0 +1,13 @@
<?php
namespace BookStack\Search\Options;
class ExactSearchOption extends SearchOption
{
public function toString(): string
{
$escaped = str_replace('\\', '\\\\', $this->value);
$escaped = str_replace('"', '\"', $escaped);
return ($this->negated ? '-' : '') . '"' . $escaped . '"';
}
}

View File

@ -0,0 +1,37 @@
<?php
namespace BookStack\Search\Options;
class FilterSearchOption extends SearchOption
{
protected string $name;
public function __construct(
string $value,
string $name,
bool $negated = false,
) {
parent::__construct($value, $negated);
$this->name = $name;
}
public function toString(): string
{
$valueText = ($this->value ? ':' . $this->value : '');
$filterBrace = '{' . $this->name . $valueText . '}';
return ($this->negated ? '-' : '') . $filterBrace;
}
public function getKey(): string
{
return $this->name;
}
public static function fromContentString(string $value, bool $negated = false): self
{
$explodedFilter = explode(':', $value, 2);
$filterValue = (count($explodedFilter) > 1) ? $explodedFilter[1] : '';
$filterName = $explodedFilter[0];
return new self($filterValue, $filterName, $negated);
}
}

View File

@ -0,0 +1,26 @@
<?php
namespace BookStack\Search\Options;
abstract class SearchOption
{
public function __construct(
public string $value,
public bool $negated = false,
) {
}
/**
* Get the key used for this option when used in a map.
* Null indicates to use the index of the containing array.
*/
public function getKey(): string|null
{
return null;
}
/**
* Get the search string representation for this search option.
*/
abstract public function toString(): string;
}

View File

@ -0,0 +1,37 @@
<?php
namespace BookStack\Search\Options;
class TagSearchOption extends SearchOption
{
/**
* Acceptable operators to be used within a tag search option.
*
* @var string[]
*/
protected array $queryOperators = ['<=', '>=', '=', '<', '>', 'like', '!='];
public function toString(): string
{
return ($this->negated ? '-' : '') . "[{$this->value}]";
}
/**
* @return array{name: string, operator: string, value: string}
*/
public function getParts(): array
{
$operatorRegex = implode('|', array_map(fn($op) => preg_quote($op), $this->queryOperators));
preg_match('/^(.*?)((' . $operatorRegex . ')(.*?))?$/', $this->value, $tagSplit);
$extractedOperator = count($tagSplit) > 2 ? $tagSplit[3] : '';
$tagOperator = in_array($extractedOperator, $this->queryOperators) ? $extractedOperator : '=';
$tagValue = count($tagSplit) > 3 ? $tagSplit[4] : '';
return [
'name' => $tagSplit[1],
'operator' => $tagOperator,
'value' => $tagValue,
];
}
}

View File

@ -0,0 +1,11 @@
<?php
namespace BookStack\Search\Options;
class TermSearchOption extends SearchOption
{
public function toString(): string
{
return $this->value;
}
}

View File

@ -0,0 +1,82 @@
<?php
namespace BookStack\Search;
use BookStack\Search\Options\SearchOption;
/**
* @template T of SearchOption
*/
class SearchOptionSet
{
/**
* @var T[]
*/
protected array $options = [];
public function __construct(array $options = [])
{
$this->options = $options;
}
public function toValueArray(): array
{
return array_map(fn(SearchOption $option) => $option->value, $this->options);
}
public function toValueMap(): array
{
$map = [];
foreach ($this->options as $index => $option) {
$key = $option->getKey() ?? $index;
$map[$key] = $option->value;
}
return $map;
}
public function merge(SearchOptionSet $set): self
{
return new self(array_merge($this->options, $set->options));
}
public function filterEmpty(): self
{
$filteredOptions = array_values(array_filter($this->options, fn (SearchOption $option) => !empty($option->value)));
return new self($filteredOptions);
}
/**
* @param class-string<SearchOption> $class
*/
public static function fromValueArray(array $values, string $class): self
{
$options = array_map(fn($val) => new $class($val), $values);
return new self($options);
}
/**
* @return T[]
*/
public function all(): array
{
return $this->options;
}
/**
* @return self<T>
*/
public function negated(): self
{
$values = array_values(array_filter($this->options, fn (SearchOption $option) => $option->negated));
return new self($values);
}
/**
* @return self<T>
*/
public function nonNegated(): self
{
$values = array_values(array_filter($this->options, fn (SearchOption $option) => !$option->negated));
return new self($values);
}
}

View File

@ -2,26 +2,39 @@
namespace BookStack\Search; namespace BookStack\Search;
use BookStack\Search\Options\ExactSearchOption;
use BookStack\Search\Options\FilterSearchOption;
use BookStack\Search\Options\SearchOption;
use BookStack\Search\Options\TagSearchOption;
use BookStack\Search\Options\TermSearchOption;
use Illuminate\Http\Request; use Illuminate\Http\Request;
class SearchOptions class SearchOptions
{ {
public array $searches = []; /** @var SearchOptionSet<TermSearchOption> */
public array $exacts = []; public SearchOptionSet $searches;
public array $tags = []; /** @var SearchOptionSet<ExactSearchOption> */
public array $filters = []; public SearchOptionSet $exacts;
/** @var SearchOptionSet<TagSearchOption> */
public SearchOptionSet $tags;
/** @var SearchOptionSet<FilterSearchOption> */
public SearchOptionSet $filters;
public function __construct()
{
$this->searches = new SearchOptionSet();
$this->exacts = new SearchOptionSet();
$this->tags = new SearchOptionSet();
$this->filters = new SearchOptionSet();
}
/** /**
* Create a new instance from a search string. * Create a new instance from a search string.
*/ */
public static function fromString(string $search): self public static function fromString(string $search): self
{ {
$decoded = static::decode($search); $instance = new self();
$instance = new SearchOptions(); $instance->addOptionsFromString($search);
foreach ($decoded as $type => $value) {
$instance->$type = $value;
}
return $instance; return $instance;
} }
@ -41,46 +54,64 @@ class SearchOptions
} }
$instance = new SearchOptions(); $instance = new SearchOptions();
$inputs = $request->only(['search', 'types', 'filters', 'exact', 'tags']); $inputs = $request->only(['search', 'types', 'filters', 'exact', 'tags', 'extras']);
$parsedStandardTerms = static::parseStandardTermString($inputs['search'] ?? ''); $parsedStandardTerms = static::parseStandardTermString($inputs['search'] ?? '');
$instance->searches = array_filter($parsedStandardTerms['terms']); $inputExacts = array_filter($inputs['exact'] ?? []);
$instance->exacts = array_filter($parsedStandardTerms['exacts']); $instance->searches = SearchOptionSet::fromValueArray(array_filter($parsedStandardTerms['terms']), TermSearchOption::class);
$instance->exacts = SearchOptionSet::fromValueArray(array_filter($parsedStandardTerms['exacts']), ExactSearchOption::class);
array_push($instance->exacts, ...array_filter($inputs['exact'] ?? [])); $instance->exacts = $instance->exacts->merge(SearchOptionSet::fromValueArray($inputExacts, ExactSearchOption::class));
$instance->tags = SearchOptionSet::fromValueArray(array_filter($inputs['tags'] ?? []), TagSearchOption::class);
$instance->tags = array_filter($inputs['tags'] ?? []);
$cleanedFilters = [];
foreach (($inputs['filters'] ?? []) as $filterKey => $filterVal) { foreach (($inputs['filters'] ?? []) as $filterKey => $filterVal) {
if (empty($filterVal)) { if (empty($filterVal)) {
continue; continue;
} }
$instance->filters[$filterKey] = $filterVal === 'true' ? '' : $filterVal; $cleanedFilterVal = $filterVal === 'true' ? '' : $filterVal;
$cleanedFilters[] = new FilterSearchOption($cleanedFilterVal, $filterKey);
} }
if (isset($inputs['types']) && count($inputs['types']) < 4) { if (isset($inputs['types']) && count($inputs['types']) < 4) {
$instance->filters['type'] = implode('|', $inputs['types']); $cleanedFilters[] = new FilterSearchOption(implode('|', $inputs['types']), 'types');
}
$instance->filters = new SearchOptionSet($cleanedFilters);
// Parse and merge in extras if provided
if (!empty($inputs['extras'])) {
$extras = static::fromString($inputs['extras']);
$instance->searches = $instance->searches->merge($extras->searches);
$instance->exacts = $instance->exacts->merge($extras->exacts);
$instance->tags = $instance->tags->merge($extras->tags);
$instance->filters = $instance->filters->merge($extras->filters);
} }
return $instance; return $instance;
} }
/** /**
* Decode a search string into an array of terms. * Decode a search string and add its contents to this instance.
*/ */
protected static function decode(string $searchString): array protected function addOptionsFromString(string $searchString): void
{ {
/** @var array<string, SearchOption[]> $terms */
$terms = [ $terms = [
'searches' => [],
'exacts' => [], 'exacts' => [],
'tags' => [], 'tags' => [],
'filters' => [], 'filters' => [],
]; ];
$patterns = [ $patterns = [
'exacts' => '/"((?:\\\\.|[^"\\\\])*)"/', 'exacts' => '/-?"((?:\\\\.|[^"\\\\])*)"/',
'tags' => '/\[(.*?)\]/', 'tags' => '/-?\[(.*?)\]/',
'filters' => '/\{(.*?)\}/', 'filters' => '/-?\{(.*?)\}/',
];
$constructors = [
'exacts' => fn(string $value, bool $negated) => new ExactSearchOption($value, $negated),
'tags' => fn(string $value, bool $negated) => new TagSearchOption($value, $negated),
'filters' => fn(string $value, bool $negated) => FilterSearchOption::fromContentString($value, $negated),
]; ];
// Parse special terms // Parse special terms
@ -88,34 +119,32 @@ class SearchOptions
$matches = []; $matches = [];
preg_match_all($pattern, $searchString, $matches); preg_match_all($pattern, $searchString, $matches);
if (count($matches) > 0) { if (count($matches) > 0) {
$terms[$termType] = $matches[1]; foreach ($matches[1] as $index => $value) {
$negated = str_starts_with($matches[0][$index], '-');
$terms[$termType][] = $constructors[$termType]($value, $negated);
}
$searchString = preg_replace($pattern, '', $searchString); $searchString = preg_replace($pattern, '', $searchString);
} }
} }
// Unescape exacts and backslash escapes // Unescape exacts and backslash escapes
foreach ($terms['exacts'] as $index => $exact) { foreach ($terms['exacts'] as $exact) {
$terms['exacts'][$index] = static::decodeEscapes($exact); $exact->value = static::decodeEscapes($exact->value);
} }
// Parse standard terms // Parse standard terms
$parsedStandardTerms = static::parseStandardTermString($searchString); $parsedStandardTerms = static::parseStandardTermString($searchString);
array_push($terms['searches'], ...$parsedStandardTerms['terms']); $this->searches = $this->searches
array_push($terms['exacts'], ...$parsedStandardTerms['exacts']); ->merge(SearchOptionSet::fromValueArray($parsedStandardTerms['terms'], TermSearchOption::class))
->filterEmpty();
$this->exacts = $this->exacts
->merge(new SearchOptionSet($terms['exacts']))
->merge(SearchOptionSet::fromValueArray($parsedStandardTerms['exacts'], ExactSearchOption::class))
->filterEmpty();
// Split filter values out // Add tags & filters
$splitFilters = []; $this->tags = $this->tags->merge(new SearchOptionSet($terms['tags']));
foreach ($terms['filters'] as $filter) { $this->filters = $this->filters->merge(new SearchOptionSet($terms['filters']));
$explodedFilter = explode(':', $filter, 2);
$splitFilters[$explodedFilter[0]] = (count($explodedFilter) > 1) ? $explodedFilter[1] : '';
}
$terms['filters'] = $splitFilters;
// Filter down terms where required
$terms['exacts'] = array_filter($terms['exacts']);
$terms['searches'] = array_filter($terms['searches']);
return $terms;
} }
/** /**
@ -175,7 +204,9 @@ class SearchOptions
*/ */
public function setFilter(string $filterName, string $filterValue = ''): void public function setFilter(string $filterName, string $filterValue = ''): void
{ {
$this->filters[$filterName] = $filterValue; $this->filters = $this->filters->merge(
new SearchOptionSet([new FilterSearchOption($filterValue, $filterName)])
);
} }
/** /**
@ -183,22 +214,40 @@ class SearchOptions
*/ */
public function toString(): string public function toString(): string
{ {
$parts = $this->searches; $options = [
...$this->searches->all(),
...$this->exacts->all(),
...$this->tags->all(),
...$this->filters->all(),
];
foreach ($this->exacts as $term) { $parts = array_map(fn(SearchOption $o) => $o->toString(), $options);
$escaped = str_replace('\\', '\\\\', $term);
$escaped = str_replace('"', '\"', $escaped);
$parts[] = '"' . $escaped . '"';
}
foreach ($this->tags as $term) {
$parts[] = "[{$term}]";
}
foreach ($this->filters as $filterName => $filterVal) {
$parts[] = '{' . $filterName . ($filterVal ? ':' . $filterVal : '') . '}';
}
return implode(' ', $parts); return implode(' ', $parts);
} }
/**
* Get the search options that don't have UI controls provided for.
* Provided back as a key => value array with the keys being expected
* input names for a search form, and values being the option value.
*/
public function getAdditionalOptionsString(): string
{
$options = [];
// Non-[created/updated]-by-me options
$userFilters = ['updated_by', 'created_by', 'owned_by'];
foreach ($this->filters->all() as $filter) {
if (in_array($filter->getKey(), $userFilters, true) && $filter->value !== null && $filter->value !== 'me') {
$options[] = $filter;
}
}
// Negated items
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));
}
} }

View File

@ -25,11 +25,12 @@ class SearchResultsFormatter
* Update the given entity model to set attributes used for previews of the item * Update the given entity model to set attributes used for previews of the item
* primarily within search result lists. * primarily within search result lists.
*/ */
protected function setSearchPreview(Entity $entity, SearchOptions $options) protected function setSearchPreview(Entity $entity, SearchOptions $options): void
{ {
$textProperty = $entity->textField; $textProperty = $entity->textField;
$textContent = $entity->$textProperty; $textContent = $entity->$textProperty;
$terms = array_merge($options->exacts, $options->searches); $relevantSearchOptions = $options->exacts->merge($options->searches);
$terms = $relevantSearchOptions->toValueArray();
$originalContentByNewAttribute = [ $originalContentByNewAttribute = [
'preview_name' => $entity->name, 'preview_name' => $entity->name,

View File

@ -7,6 +7,7 @@ use BookStack\Entities\Models\Entity;
use BookStack\Entities\Models\Page; use BookStack\Entities\Models\Page;
use BookStack\Entities\Queries\EntityQueries; use BookStack\Entities\Queries\EntityQueries;
use BookStack\Permissions\PermissionApplicator; use BookStack\Permissions\PermissionApplicator;
use BookStack\Search\Options\TagSearchOption;
use BookStack\Users\Models\User; use BookStack\Users\Models\User;
use Illuminate\Database\Connection; use Illuminate\Database\Connection;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
@ -16,31 +17,21 @@ use Illuminate\Database\Query\Builder;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use SplObjectStorage; use WeakMap;
class SearchRunner class SearchRunner
{ {
/**
* Acceptable operators to be used in a query.
*
* @var string[]
*/
protected array $queryOperators = ['<=', '>=', '=', '<', '>', 'like', '!='];
/** /**
* Retain a cache of score adjusted terms for specific search options. * Retain a cache of score adjusted terms for specific search options.
* From PHP>=8 this can be made into a WeakMap instead.
*
* @var SplObjectStorage
*/ */
protected $termAdjustmentCache; protected WeakMap $termAdjustmentCache;
public function __construct( public function __construct(
protected EntityProvider $entityProvider, protected EntityProvider $entityProvider,
protected PermissionApplicator $permissions, protected PermissionApplicator $permissions,
protected EntityQueries $entityQueries, protected EntityQueries $entityQueries,
) { ) {
$this->termAdjustmentCache = new SplObjectStorage(); $this->termAdjustmentCache = new WeakMap();
} }
/** /**
@ -55,10 +46,11 @@ class SearchRunner
$entityTypes = array_keys($this->entityProvider->all()); $entityTypes = array_keys($this->entityProvider->all());
$entityTypesToSearch = $entityTypes; $entityTypesToSearch = $entityTypes;
$filterMap = $searchOpts->filters->toValueMap();
if ($entityType !== 'all') { if ($entityType !== 'all') {
$entityTypesToSearch = [$entityType]; $entityTypesToSearch = [$entityType];
} elseif (isset($searchOpts->filters['type'])) { } elseif (isset($filterMap['type'])) {
$entityTypesToSearch = explode('|', $searchOpts->filters['type']); $entityTypesToSearch = explode('|', $filterMap['type']);
} }
$results = collect(); $results = collect();
@ -97,7 +89,8 @@ class SearchRunner
{ {
$opts = SearchOptions::fromString($searchString); $opts = SearchOptions::fromString($searchString);
$entityTypes = ['page', 'chapter']; $entityTypes = ['page', 'chapter'];
$entityTypesToSearch = isset($opts->filters['type']) ? explode('|', $opts->filters['type']) : $entityTypes; $filterMap = $opts->filters->toValueMap();
$entityTypesToSearch = isset($filterMap['type']) ? explode('|', $filterMap['type']) : $entityTypes;
$results = collect(); $results = collect();
foreach ($entityTypesToSearch as $entityType) { foreach ($entityTypesToSearch as $entityType) {
@ -161,24 +154,26 @@ class SearchRunner
$this->applyTermSearch($entityQuery, $searchOpts, $entityType); $this->applyTermSearch($entityQuery, $searchOpts, $entityType);
// Handle exact term matching // Handle exact term matching
foreach ($searchOpts->exacts as $inputTerm) { foreach ($searchOpts->exacts->all() as $exact) {
$entityQuery->where(function (EloquentBuilder $query) use ($inputTerm, $entityModelInstance) { $filter = function (EloquentBuilder $query) use ($exact, $entityModelInstance) {
$inputTerm = str_replace('\\', '\\\\', $inputTerm); $inputTerm = str_replace('\\', '\\\\', $exact->value);
$query->where('name', 'like', '%' . $inputTerm . '%') $query->where('name', 'like', '%' . $inputTerm . '%')
->orWhere($entityModelInstance->textField, 'like', '%' . $inputTerm . '%'); ->orWhere($entityModelInstance->textField, 'like', '%' . $inputTerm . '%');
}); };
$exact->negated ? $entityQuery->whereNot($filter) : $entityQuery->where($filter);
} }
// Handle tag searches // Handle tag searches
foreach ($searchOpts->tags as $inputTerm) { foreach ($searchOpts->tags->all() as $tagOption) {
$this->applyTagSearch($entityQuery, $inputTerm); $this->applyTagSearch($entityQuery, $tagOption);
} }
// Handle filters // Handle filters
foreach ($searchOpts->filters as $filterTerm => $filterValue) { foreach ($searchOpts->filters->all() as $filterOption) {
$functionName = Str::camel('filter_' . $filterTerm); $functionName = Str::camel('filter_' . $filterOption->getKey());
if (method_exists($this, $functionName)) { if (method_exists($this, $functionName)) {
$this->$functionName($entityQuery, $entityModelInstance, $filterValue); $this->$functionName($entityQuery, $entityModelInstance, $filterOption->value, $filterOption->negated);
} }
} }
@ -190,7 +185,7 @@ class SearchRunner
*/ */
protected function applyTermSearch(EloquentBuilder $entityQuery, SearchOptions $options, string $entityType): void protected function applyTermSearch(EloquentBuilder $entityQuery, SearchOptions $options, string $entityType): void
{ {
$terms = $options->searches; $terms = $options->searches->toValueArray();
if (count($terms) === 0) { if (count($terms) === 0) {
return; return;
} }
@ -209,8 +204,8 @@ class SearchRunner
$subQuery->where('entity_type', '=', $entityType); $subQuery->where('entity_type', '=', $entityType);
$subQuery->where(function (Builder $query) use ($terms) { $subQuery->where(function (Builder $query) use ($terms) {
foreach ($terms as $inputTerm) { foreach ($terms as $inputTerm) {
$inputTerm = str_replace('\\', '\\\\', $inputTerm); $escapedTerm = str_replace('\\', '\\\\', $inputTerm);
$query->orWhere('term', 'like', $inputTerm . '%'); $query->orWhere('term', 'like', $escapedTerm . '%');
} }
}); });
$subQuery->groupBy('entity_type', 'entity_id'); $subQuery->groupBy('entity_type', 'entity_id');
@ -264,7 +259,7 @@ class SearchRunner
$whenStatements = []; $whenStatements = [];
$whenBindings = []; $whenBindings = [];
foreach ($options->searches as $term) { foreach ($options->searches->toValueArray() as $term) {
$whenStatements[] = 'WHEN term LIKE ? THEN ?'; $whenStatements[] = 'WHEN term LIKE ? THEN ?';
$whenBindings[] = $term . '%'; $whenBindings[] = $term . '%';
$whenBindings[] = $term; $whenBindings[] = $term;
@ -310,179 +305,165 @@ class SearchRunner
} }
/** /**
* Get the available query operators as a regex escaped list. * Apply a tag search term onto an entity query.
*/ */
protected function getRegexEscapedOperators(): string protected function applyTagSearch(EloquentBuilder $query, TagSearchOption $option): void
{ {
$escapedOperators = []; $filter = function (EloquentBuilder $query) use ($option): void {
foreach ($this->queryOperators as $operator) { $tagParts = $option->getParts();
$escapedOperators[] = preg_quote($operator); if (empty($tagParts['operator']) || empty($tagParts['value'])) {
$query->where('name', '=', $tagParts['name']);
return;
} }
return implode('|', $escapedOperators); if (!empty($tagParts['name'])) {
$query->where('name', '=', $tagParts['name']);
} }
/** if (is_numeric($tagParts['value']) && $tagParts['operator'] !== 'like') {
* Apply a tag search term onto a entity query.
*/
protected function applyTagSearch(EloquentBuilder $query, string $tagTerm): EloquentBuilder
{
preg_match('/^(.*?)((' . $this->getRegexEscapedOperators() . ')(.*?))?$/', $tagTerm, $tagSplit);
$query->whereHas('tags', function (EloquentBuilder $query) use ($tagSplit) {
$tagName = $tagSplit[1];
$tagOperator = count($tagSplit) > 2 ? $tagSplit[3] : '';
$tagValue = count($tagSplit) > 3 ? $tagSplit[4] : '';
$validOperator = in_array($tagOperator, $this->queryOperators);
if (!empty($tagOperator) && !empty($tagValue) && $validOperator) {
if (!empty($tagName)) {
$query->where('name', '=', $tagName);
}
if (is_numeric($tagValue) && $tagOperator !== 'like') {
// We have to do a raw sql query for this since otherwise PDO will quote the value and MySQL will // We have to do a raw sql query for this since otherwise PDO will quote the value and MySQL will
// search the value as a string which prevents being able to do number-based operations // search the value as a string which prevents being able to do number-based operations
// on the tag values. We ensure it has a numeric value and then cast it just to be sure. // on the tag values. We ensure it has a numeric value and then cast it just to be sure.
/** @var Connection $connection */ /** @var Connection $connection */
$connection = $query->getConnection(); $connection = $query->getConnection();
$tagValue = (float) trim($connection->getPdo()->quote($tagValue), "'"); $quotedValue = (float) trim($connection->getPdo()->quote($tagParts['value']), "'");
$query->whereRaw("value {$tagOperator} {$tagValue}"); $query->whereRaw("value {$tagParts['operator']} {$quotedValue}");
} else if ($tagParts['operator'] === 'like') {
$query->where('value', $tagParts['operator'], str_replace('\\', '\\\\', $tagParts['value']));
} else { } else {
if ($tagOperator === 'like') { $query->where('value', $tagParts['operator'], $tagParts['value']);
$tagValue = str_replace('\\', '\\\\', $tagValue);
} }
$query->where('value', $tagOperator, $tagValue); };
}
} else {
$query->where('name', '=', $tagName);
}
});
return $query; $option->negated ? $query->whereDoesntHave('tags', $filter) : $query->whereHas('tags', $filter);
}
protected function applyNegatableWhere(EloquentBuilder $query, bool $negated, string $column, string $operator, mixed $value): void
{
if ($negated) {
$query->whereNot($column, $operator, $value);
} else {
$query->where($column, $operator, $value);
}
} }
/** /**
* Custom entity search filters. * Custom entity search filters.
*/ */
protected function filterUpdatedAfter(EloquentBuilder $query, Entity $model, $input): void protected function filterUpdatedAfter(EloquentBuilder $query, Entity $model, string $input, bool $negated): void
{ {
try {
$date = date_create($input); $date = date_create($input);
$query->where('updated_at', '>=', $date); $this->applyNegatableWhere($query, $negated, 'updated_at', '>=', $date);
} catch (\Exception $e) {
}
} }
protected function filterUpdatedBefore(EloquentBuilder $query, Entity $model, $input): void protected function filterUpdatedBefore(EloquentBuilder $query, Entity $model, string $input, bool $negated): void
{ {
try {
$date = date_create($input); $date = date_create($input);
$query->where('updated_at', '<', $date); $this->applyNegatableWhere($query, $negated, 'updated_at', '<', $date);
} catch (\Exception $e) {
}
} }
protected function filterCreatedAfter(EloquentBuilder $query, Entity $model, $input): void protected function filterCreatedAfter(EloquentBuilder $query, Entity $model, string $input, bool $negated): void
{ {
try {
$date = date_create($input); $date = date_create($input);
$query->where('created_at', '>=', $date); $this->applyNegatableWhere($query, $negated, 'created_at', '>=', $date);
} catch (\Exception $e) {
}
} }
protected function filterCreatedBefore(EloquentBuilder $query, Entity $model, $input) protected function filterCreatedBefore(EloquentBuilder $query, Entity $model, string $input, bool $negated)
{ {
try {
$date = date_create($input); $date = date_create($input);
$query->where('created_at', '<', $date); $this->applyNegatableWhere($query, $negated, 'created_at', '<', $date);
} catch (\Exception $e) {
}
} }
protected function filterCreatedBy(EloquentBuilder $query, Entity $model, $input) protected function filterCreatedBy(EloquentBuilder $query, Entity $model, string $input, bool $negated)
{ {
$userSlug = $input === 'me' ? user()->slug : trim($input); $userSlug = $input === 'me' ? user()->slug : trim($input);
$user = User::query()->where('slug', '=', $userSlug)->first(['id']); $user = User::query()->where('slug', '=', $userSlug)->first(['id']);
if ($user) { if ($user) {
$query->where('created_by', '=', $user->id); $this->applyNegatableWhere($query, $negated, 'created_by', '=', $user->id);
} }
} }
protected function filterUpdatedBy(EloquentBuilder $query, Entity $model, $input) protected function filterUpdatedBy(EloquentBuilder $query, Entity $model, string $input, bool $negated)
{ {
$userSlug = $input === 'me' ? user()->slug : trim($input); $userSlug = $input === 'me' ? user()->slug : trim($input);
$user = User::query()->where('slug', '=', $userSlug)->first(['id']); $user = User::query()->where('slug', '=', $userSlug)->first(['id']);
if ($user) { if ($user) {
$query->where('updated_by', '=', $user->id); $this->applyNegatableWhere($query, $negated, 'updated_by', '=', $user->id);
} }
} }
protected function filterOwnedBy(EloquentBuilder $query, Entity $model, $input) protected function filterOwnedBy(EloquentBuilder $query, Entity $model, string $input, bool $negated)
{ {
$userSlug = $input === 'me' ? user()->slug : trim($input); $userSlug = $input === 'me' ? user()->slug : trim($input);
$user = User::query()->where('slug', '=', $userSlug)->first(['id']); $user = User::query()->where('slug', '=', $userSlug)->first(['id']);
if ($user) { if ($user) {
$query->where('owned_by', '=', $user->id); $this->applyNegatableWhere($query, $negated, 'owned_by', '=', $user->id);
} }
} }
protected function filterInName(EloquentBuilder $query, Entity $model, $input) protected function filterInName(EloquentBuilder $query, Entity $model, string $input, bool $negated)
{ {
$query->where('name', 'like', '%' . $input . '%'); $this->applyNegatableWhere($query, $negated, 'name', 'like', '%' . $input . '%');
} }
protected function filterInTitle(EloquentBuilder $query, Entity $model, $input) protected function filterInTitle(EloquentBuilder $query, Entity $model, string $input, bool $negated)
{ {
$this->filterInName($query, $model, $input); $this->filterInName($query, $model, $input, $negated);
} }
protected function filterInBody(EloquentBuilder $query, Entity $model, $input) protected function filterInBody(EloquentBuilder $query, Entity $model, string $input, bool $negated)
{ {
$query->where($model->textField, 'like', '%' . $input . '%'); $this->applyNegatableWhere($query, $negated, $model->textField, 'like', '%' . $input . '%');
} }
protected function filterIsRestricted(EloquentBuilder $query, Entity $model, $input) protected function filterIsRestricted(EloquentBuilder $query, Entity $model, string $input, bool $negated)
{ {
$query->whereHas('permissions'); $negated ? $query->whereDoesntHave('permissions') : $query->whereHas('permissions');
} }
protected function filterViewedByMe(EloquentBuilder $query, Entity $model, $input) protected function filterViewedByMe(EloquentBuilder $query, Entity $model, string $input, bool $negated)
{ {
$query->whereHas('views', function ($query) { $filter = function ($query) {
$query->where('user_id', '=', user()->id); $query->where('user_id', '=', user()->id);
}); };
$negated ? $query->whereDoesntHave('views', $filter) : $query->whereHas('views', $filter);
} }
protected function filterNotViewedByMe(EloquentBuilder $query, Entity $model, $input) protected function filterNotViewedByMe(EloquentBuilder $query, Entity $model, string $input, bool $negated)
{ {
$query->whereDoesntHave('views', function ($query) { $filter = function ($query) {
$query->where('user_id', '=', user()->id); $query->where('user_id', '=', user()->id);
}); };
$negated ? $query->whereHas('views', $filter) : $query->whereDoesntHave('views', $filter);
} }
protected function filterIsTemplate(EloquentBuilder $query, Entity $model, $input) protected function filterIsTemplate(EloquentBuilder $query, Entity $model, string $input, bool $negated)
{ {
if ($model instanceof Page) { if ($model instanceof Page) {
$query->where('template', '=', true); $this->applyNegatableWhere($query, $negated, 'template', '=', true);
} }
} }
protected function filterSortBy(EloquentBuilder $query, Entity $model, $input) protected function filterSortBy(EloquentBuilder $query, Entity $model, string $input, bool $negated)
{ {
$functionName = Str::camel('sort_by_' . $input); $functionName = Str::camel('sort_by_' . $input);
if (method_exists($this, $functionName)) { if (method_exists($this, $functionName)) {
$this->$functionName($query, $model); $this->$functionName($query, $model, $negated);
} }
} }
/** /**
* Sorting filter options. * Sorting filter options.
*/ */
protected function sortByLastCommented(EloquentBuilder $query, Entity $model) protected function sortByLastCommented(EloquentBuilder $query, Entity $model, bool $negated)
{ {
$commentsTable = DB::getTablePrefix() . 'comments'; $commentsTable = DB::getTablePrefix() . 'comments';
$morphClass = str_replace('\\', '\\\\', $model->getMorphClass()); $morphClass = str_replace('\\', '\\\\', $model->getMorphClass());
$commentQuery = DB::raw('(SELECT c1.entity_id, c1.entity_type, c1.created_at as last_commented FROM ' . $commentsTable . ' c1 LEFT JOIN ' . $commentsTable . ' c2 ON (c1.entity_id = c2.entity_id AND c1.entity_type = c2.entity_type AND c1.created_at < c2.created_at) WHERE c1.entity_type = \'' . $morphClass . '\' AND c2.created_at IS NULL) as comments'); $commentQuery = DB::raw('(SELECT c1.entity_id, c1.entity_type, c1.created_at as last_commented FROM ' . $commentsTable . ' c1 LEFT JOIN ' . $commentsTable . ' c2 ON (c1.entity_id = c2.entity_id AND c1.entity_type = c2.entity_type AND c1.created_at < c2.created_at) WHERE c1.entity_type = \'' . $morphClass . '\' AND c2.created_at IS NULL) as comments');
$query->join($commentQuery, $model->getTable() . '.id', '=', 'comments.entity_id')->orderBy('last_commented', 'desc'); $query->join($commentQuery, $model->getTable() . '.id', '=', 'comments.entity_id')
->orderBy('last_commented', $negated ? 'asc' : 'desc');
} }
} }

View File

@ -8,15 +8,18 @@
<div> <div>
<h5>{{ trans('entities.search_advanced') }}</h5> <h5>{{ trans('entities.search_advanced') }}</h5>
@php
$filterMap = $options->filters->nonNegated()->toValueMap();
@endphp
<form method="get" action="{{ url('/search') }}"> <form method="get" action="{{ url('/search') }}">
<h6>{{ trans('entities.search_terms') }}</h6> <h6>{{ trans('entities.search_terms') }}</h6>
<input type="text" name="search" value="{{ implode(' ', $options->searches) }}"> <input type="text" name="search" value="{{ implode(' ', $options->searches->toValueArray()) }}">
<h6>{{ trans('entities.search_content_type') }}</h6> <h6>{{ trans('entities.search_content_type') }}</h6>
<div class="form-group"> <div class="form-group">
<?php <?php
$types = explode('|', $options->filters['type'] ?? ''); $types = explode('|', $filterMap['type'] ?? '');
$hasTypes = $types[0] !== ''; $hasTypes = $types[0] !== '';
?> ?>
@include('search.parts.type-filter', ['checked' => !$hasTypes || in_array('page', $types), 'entity' => 'page', 'transKey' => 'page']) @include('search.parts.type-filter', ['checked' => !$hasTypes || in_array('page', $types), 'entity' => 'page', 'transKey' => 'page'])
@ -27,47 +30,41 @@
</div> </div>
<h6>{{ trans('entities.search_exact_matches') }}</h6> <h6>{{ trans('entities.search_exact_matches') }}</h6>
@include('search.parts.term-list', ['type' => 'exact', 'currentList' => $options->exacts]) @include('search.parts.term-list', ['type' => 'exact', 'currentList' => $options->exacts->nonNegated()->toValueArray()])
<h6>{{ trans('entities.search_tags') }}</h6> <h6>{{ trans('entities.search_tags') }}</h6>
@include('search.parts.term-list', ['type' => 'tags', 'currentList' => $options->tags]) @include('search.parts.term-list', ['type' => 'tags', 'currentList' => $options->tags->nonNegated()->toValueArray()])
@if(!user()->isGuest()) @if(!user()->isGuest())
<h6>{{ trans('entities.search_options') }}</h6> <h6>{{ trans('entities.search_options') }}</h6>
@component('search.parts.boolean-filter', ['filters' => $options->filters, 'name' => 'viewed_by_me', 'value' => null]) @component('search.parts.boolean-filter', ['filters' => $filterMap, 'name' => 'viewed_by_me', 'value' => null])
{{ trans('entities.search_viewed_by_me') }} {{ trans('entities.search_viewed_by_me') }}
@endcomponent @endcomponent
@component('search.parts.boolean-filter', ['filters' => $options->filters, 'name' => 'not_viewed_by_me', 'value' => null]) @component('search.parts.boolean-filter', ['filters' => $filterMap, 'name' => 'not_viewed_by_me', 'value' => null])
{{ trans('entities.search_not_viewed_by_me') }} {{ trans('entities.search_not_viewed_by_me') }}
@endcomponent @endcomponent
@component('search.parts.boolean-filter', ['filters' => $options->filters, 'name' => 'is_restricted', 'value' => null]) @component('search.parts.boolean-filter', ['filters' => $filterMap, 'name' => 'is_restricted', 'value' => null])
{{ trans('entities.search_permissions_set') }} {{ trans('entities.search_permissions_set') }}
@endcomponent @endcomponent
@component('search.parts.boolean-filter', ['filters' => $options->filters, 'name' => 'created_by', 'value' => 'me']) @component('search.parts.boolean-filter', ['filters' => $filterMap, 'name' => 'created_by', 'value' => 'me'])
{{ trans('entities.search_created_by_me') }} {{ trans('entities.search_created_by_me') }}
@endcomponent @endcomponent
@component('search.parts.boolean-filter', ['filters' => $options->filters, 'name' => 'updated_by', 'value' => 'me']) @component('search.parts.boolean-filter', ['filters' => $filterMap, 'name' => 'updated_by', 'value' => 'me'])
{{ trans('entities.search_updated_by_me') }} {{ trans('entities.search_updated_by_me') }}
@endcomponent @endcomponent
@component('search.parts.boolean-filter', ['filters' => $options->filters, 'name' => 'owned_by', 'value' => 'me']) @component('search.parts.boolean-filter', ['filters' => $filterMap, 'name' => 'owned_by', 'value' => 'me'])
{{ trans('entities.search_owned_by_me') }} {{ trans('entities.search_owned_by_me') }}
@endcomponent @endcomponent
@endif @endif
<h6>{{ trans('entities.search_date_options') }}</h6> <h6>{{ trans('entities.search_date_options') }}</h6>
@include('search.parts.date-filter', ['name' => 'updated_after', 'filters' => $options->filters]) @include('search.parts.date-filter', ['name' => 'updated_after', 'filters' => $filterMap])
@include('search.parts.date-filter', ['name' => 'updated_before', 'filters' => $options->filters]) @include('search.parts.date-filter', ['name' => 'updated_before', 'filters' => $filterMap])
@include('search.parts.date-filter', ['name' => 'created_after', 'filters' => $options->filters]) @include('search.parts.date-filter', ['name' => 'created_after', 'filters' => $filterMap])
@include('search.parts.date-filter', ['name' => 'created_before', 'filters' => $options->filters]) @include('search.parts.date-filter', ['name' => 'created_before', 'filters' => $filterMap])
@if(isset($options->filters['created_by']) && $options->filters['created_by'] !== "me")
<input type="hidden" name="filters[created_by]" value="{{ $options->filters['created_by'] }}">
@endif
@if(isset($options->filters['updated_by']) && $options->filters['updated_by'] !== "me")
<input type="hidden" name="filters[updated_by]" value="{{ $options->filters['updated_by'] }}">
@endif
<input type="hidden" name="extras" value="{{ $options->getAdditionalOptionsString() }}">
<button type="submit" class="button">{{ trans('entities.search_update') }}</button> <button type="submit" class="button">{{ trans('entities.search_update') }}</button>
</form> </form>
@ -78,7 +75,8 @@
<h1 class="list-heading">{{ trans('entities.search_results') }}</h1> <h1 class="list-heading">{{ trans('entities.search_results') }}</h1>
<form action="{{ url('/search') }}" method="GET" class="search-box flexible hide-over-l"> <form action="{{ url('/search') }}" method="GET" class="search-box flexible hide-over-l">
<input value="{{$searchTerm}}" type="text" name="term" placeholder="{{ trans('common.search') }}"> <input value="{{$searchTerm}}" type="text" name="term"
placeholder="{{ trans('common.search') }}">
<button type="submit" <button type="submit"
aria-label="{{ trans('common.search') }}" aria-label="{{ trans('common.search') }}"
tabindex="-1">@icon('search')</button> tabindex="-1">@icon('search')</button>

View File

@ -118,6 +118,32 @@ class EntitySearchTest extends TestCase
$exactSearchB->assertStatus(200)->assertDontSee($page->name); $exactSearchB->assertStatus(200)->assertDontSee($page->name);
} }
public function test_negated_searches()
{
$page = $this->entities->newPage(['name' => 'My new test negation page', 'html' => '<p>An angry tortoise wore trumpeted plimsoles</p>']);
$page->tags()->saveMany([new Tag(['name' => 'DonkCount', 'value' => '500'])]);
$page->created_by = $this->users->admin()->id;
$page->save();
$editor = $this->users->editor();
$this->actingAs($editor);
$exactSearch = $this->get('/search?term=' . urlencode('negation -"tortoise"'));
$exactSearch->assertStatus(200)->assertDontSeeText($page->name);
$tagSearchA = $this->get('/search?term=' . urlencode('negation [DonkCount=500]'));
$tagSearchA->assertStatus(200)->assertSeeText($page->name);
$tagSearchB = $this->get('/search?term=' . urlencode('negation -[DonkCount=500]'));
$tagSearchB->assertStatus(200)->assertDontSeeText($page->name);
$filterSearchA = $this->get('/search?term=' . urlencode('negation -{created_by:me}'));
$filterSearchA->assertStatus(200)->assertSeeText($page->name);
$page->created_by = $editor->id;
$page->save();
$filterSearchB = $this->get('/search?term=' . urlencode('negation -{created_by:me}'));
$filterSearchB->assertStatus(200)->assertDontSeeText($page->name);
}
public function test_search_terms_with_delimiters_are_converted_to_exact_matches() public function test_search_terms_with_delimiters_are_converted_to_exact_matches()
{ {
$this->asEditor(); $this->asEditor();
@ -545,11 +571,18 @@ class EntitySearchTest extends TestCase
$search->assertSee($page->getUrl(), false); $search->assertSee($page->getUrl(), false);
} }
public function test_searches_with_user_filters_adds_them_into_advanced_search_form() public function test_searches_with_terms_without_controls_includes_them_in_extras()
{ {
$resp = $this->asEditor()->get('/search?term=' . urlencode('test {updated_by:dan} {created_by:dan}')); $resp = $this->asEditor()->get('/search?term=' . urlencode('test {updated_by:dan} {created_by:dan} -{viewed_by_me} -[a=b] -"dog"'));
$this->withHtml($resp)->assertElementExists('form input[name="filters[updated_by]"][value="dan"]'); $this->withHtml($resp)->assertFieldHasValue('extras', '{updated_by:dan} {created_by:dan} -"dog" -[a=b] -{viewed_by_me}');
$this->withHtml($resp)->assertElementExists('form input[name="filters[created_by]"][value="dan"]'); }
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() public function test_searches_with_user_filters_using_me_adds_them_into_advanced_search_form()

View File

@ -2,7 +2,12 @@
namespace Tests\Entity; namespace Tests\Entity;
use BookStack\Search\Options\ExactSearchOption;
use BookStack\Search\Options\FilterSearchOption;
use BookStack\Search\Options\TagSearchOption;
use BookStack\Search\Options\TermSearchOption;
use BookStack\Search\SearchOptions; use BookStack\Search\SearchOptions;
use BookStack\Search\SearchOptionSet;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Tests\TestCase; use Tests\TestCase;
@ -12,27 +17,57 @@ class SearchOptionsTest extends TestCase
{ {
$options = SearchOptions::fromString('cat "dog" [tag=good] {is_tree}'); $options = SearchOptions::fromString('cat "dog" [tag=good] {is_tree}');
$this->assertEquals(['cat'], $options->searches); $this->assertEquals(['cat'], $options->searches->toValueArray());
$this->assertEquals(['dog'], $options->exacts); $this->assertEquals(['dog'], $options->exacts->toValueArray());
$this->assertEquals(['tag=good'], $options->tags); $this->assertEquals(['tag=good'], $options->tags->toValueArray());
$this->assertEquals(['is_tree' => ''], $options->filters); $this->assertEquals(['is_tree' => ''], $options->filters->toValueMap());
}
public function test_from_string_parses_negations()
{
$options = SearchOptions::fromString('cat -"dog" -[tag=good] -{is_tree}');
$this->assertEquals(['cat'], $options->searches->toValueArray());
$this->assertTrue($options->exacts->all()[0]->negated);
$this->assertTrue($options->tags->all()[0]->negated);
$this->assertTrue($options->filters->all()[0]->negated);
} }
public function test_from_string_properly_parses_escaped_quotes() public function test_from_string_properly_parses_escaped_quotes()
{ {
$options = SearchOptions::fromString('"\"cat\"" surprise "\"\"" "\"donkey" "\"" "\\\\"'); $options = SearchOptions::fromString('"\"cat\"" surprise "\"\"" "\"donkey" "\"" "\\\\"');
$this->assertEquals(['"cat"', '""', '"donkey', '"', '\\'], $options->exacts); $this->assertEquals(['"cat"', '""', '"donkey', '"', '\\'], $options->exacts->toValueArray());
} }
public function test_to_string_includes_all_items_in_the_correct_format() public function test_to_string_includes_all_items_in_the_correct_format()
{ {
$expected = 'cat "dog" [tag=good] {is_tree}'; $expected = 'cat "dog" [tag=good] {is_tree} {beans:valid}';
$options = new SearchOptions(); $options = new SearchOptions();
$options->searches = ['cat']; $options->searches = SearchOptionSet::fromValueArray(['cat'], TermSearchOption::class);
$options->exacts = ['dog']; $options->exacts = SearchOptionSet::fromValueArray(['dog'], ExactSearchOption::class);
$options->tags = ['tag=good']; $options->tags = SearchOptionSet::fromValueArray(['tag=good'], TagSearchOption::class);
$options->filters = ['is_tree' => '']; $options->filters = new SearchOptionSet([
new FilterSearchOption('', 'is_tree'),
new FilterSearchOption('valid', 'beans'),
]);
$output = $options->toString();
foreach (explode(' ', $expected) as $term) {
$this->assertStringContainsString($term, $output);
}
}
public function test_to_string_handles_negations_as_expected()
{
$expected = 'cat -"dog" -[tag=good] -{is_tree}';
$options = new SearchOptions();
$options->searches = new SearchOptionSet([new TermSearchOption('cat')]);
$options->exacts = new SearchOptionSet([new ExactSearchOption('dog', true)]);
$options->tags = new SearchOptionSet([new TagSearchOption('tag=good', true)]);
$options->filters = new SearchOptionSet([
new FilterSearchOption('', 'is_tree', true),
]);
$output = $options->toString(); $output = $options->toString();
foreach (explode(' ', $expected) as $term) { foreach (explode(' ', $expected) as $term) {
@ -43,7 +78,7 @@ class SearchOptionsTest extends TestCase
public function test_to_string_escapes_as_expected() public function test_to_string_escapes_as_expected()
{ {
$options = new SearchOptions(); $options = new SearchOptions();
$options->exacts = ['"cat"', '""', '"donkey', '"', '\\', '\\"']; $options->exacts = SearchOptionSet::fromValueArray(['"cat"', '""', '"donkey', '"', '\\', '\\"'], ExactSearchOption::class);
$output = $options->toString(); $output = $options->toString();
$this->assertEquals('"\"cat\"" "\"\"" "\"donkey" "\"" "\\\\" "\\\\\""', $output); $this->assertEquals('"\"cat\"" "\"\"" "\"donkey" "\"" "\\\\" "\\\\\""', $output);
@ -57,14 +92,14 @@ class SearchOptionsTest extends TestCase
'is_tree' => '', 'is_tree' => '',
'name' => 'dan', 'name' => 'dan',
'cat' => 'happy', 'cat' => 'happy',
], $opts->filters); ], $opts->filters->toValueMap());
} }
public function test_it_cannot_parse_out_empty_exacts() public function test_it_cannot_parse_out_empty_exacts()
{ {
$options = SearchOptions::fromString('"" test ""'); $options = SearchOptions::fromString('"" test ""');
$this->assertEmpty($options->exacts); $this->assertEmpty($options->exacts->toValueArray());
$this->assertCount(1, $options->searches); $this->assertCount(1, $options->searches->toValueArray());
} }
public function test_from_request_properly_parses_exacts_from_search_terms() public function test_from_request_properly_parses_exacts_from_search_terms()
@ -74,7 +109,24 @@ class SearchOptionsTest extends TestCase
]); ]);
$options = SearchOptions::fromRequest($request); $options = SearchOptions::fromRequest($request);
$this->assertEquals(["biscuits"], $options->searches); $this->assertEquals(["biscuits"], $options->searches->toValueArray());
$this->assertEquals(['"cheese"', '""', '"baked', 'beans"'], $options->exacts); $this->assertEquals(['"cheese"', '""', '"baked', 'beans"'], $options->exacts->toValueArray());
}
public function test_from_request_properly_parses_out_extras_as_string()
{
$request = new Request([
'search' => '',
'tags' => ['a=b'],
'extras' => '-[b=c] -{viewed_by_me} -"dino"'
]);
$options = SearchOptions::fromRequest($request);
$this->assertCount(2, $options->tags->all());
$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);
$this->assertTrue($options->exacts->all()[0]->negated);
} }
} }