-
-
Notifications
You must be signed in to change notification settings - Fork 540
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow transforming of search index values (#2462)
- Loading branch information
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace Tests\Search; | ||
|
||
use Statamic\Facades\Entry; | ||
use Statamic\Search\Searchables; | ||
use Tests\TestCase; | ||
|
||
class SearchablesTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function it_transforms_values_set_in_the_config_file() | ||
{ | ||
config()->set('statamic.search.indexes.default', [ | ||
'fields' => [ | ||
'title', | ||
], | ||
'transformers' => [ | ||
'title' => function ($value) { | ||
return strtoupper($value); | ||
}, | ||
], | ||
]); | ||
|
||
$index = app(\Statamic\Search\Comb\Index::class, [ | ||
'name' => 'default', | ||
'config' => config('statamic.search.indexes.default'), | ||
]); | ||
|
||
$searchable = Entry::make()->data(['title' => 'Hello']); | ||
$searchables = new Searchables($index); | ||
|
||
$this->assertEquals([ | ||
'title' => 'HELLO', | ||
], $searchables->fields($searchable)); | ||
} | ||
|
||
/** @test */ | ||
public function if_a_transformer_returns_an_array_it_gets_combined_into_the_results() | ||
{ | ||
config()->set('statamic.search.indexes.default', [ | ||
'fields' => [ | ||
'title', | ||
], | ||
'transformers' => [ | ||
'title' => function ($value) { | ||
return [ | ||
'title' => $value, | ||
'title_upper' => strtoupper($value), | ||
]; | ||
}, | ||
], | ||
]); | ||
|
||
$index = app(\Statamic\Search\Comb\Index::class, [ | ||
'name' => 'default', | ||
'config' => config('statamic.search.indexes.default'), | ||
]); | ||
|
||
$searchable = Entry::make()->data(['title' => 'Hello']); | ||
$searchables = new Searchables($index); | ||
|
||
$this->assertEquals([ | ||
'title' => 'Hello', | ||
'title_upper' => 'HELLO', | ||
], $searchables->fields($searchable)); | ||
} | ||
} |