forked from silverstripe/silverstripe-search-service
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show in search check for files (silverstripe#92)
* Index or remove from index based on ShowInSearch value * Add Show in search and last indexed field to file details tab * Add tests and revert onAfterPublish code * Add DataObject search content controls * Generic SearchFormFactoryExtension and fixing tests * Add config and update tests * Default config and update doc * Update ShowInSearch if files is not set to index * Add tests for SearchFormFactoryExtension * Fix broken tests * Linting fixes * Show in search remove duplicated field for SiteTree main tab * Update settings for SiteTree objects
- Loading branch information
Showing
10 changed files
with
247 additions
and
7 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
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,63 @@ | ||
<?php | ||
|
||
namespace SilverStripe\SearchService\Extensions; | ||
|
||
use SilverStripe\Control\RequestHandler; | ||
use SilverStripe\Core\Config\Config; | ||
use SilverStripe\Core\Extension; | ||
use SilverStripe\Forms\CheckboxField; | ||
use SilverStripe\Forms\DatetimeField; | ||
use SilverStripe\Forms\Form; | ||
use SilverStripe\Forms\FormFactory; | ||
|
||
class SearchFormFactoryExtension extends Extension | ||
{ | ||
|
||
public function updateForm( | ||
Form $form, | ||
?RequestHandler $controller = null, | ||
string $name = FormFactory::DEFAULT_NAME, | ||
array $context = [] | ||
): void { | ||
$fields = $form->Fields()->findOrMakeTab('Editor.Details'); | ||
$file = $context['Record'] ?? null; | ||
|
||
$excludedClasses = Config::inst()->get(self::class, 'exclude_classes') ?? []; | ||
$excludeFileTypes = Config::inst()->get(self::class, 'exclude_file_extensions') ?? []; | ||
|
||
if (!$fields || !$file) { | ||
return; | ||
} | ||
|
||
if (in_array($file->ClassName, $excludedClasses) || in_array($file->getExtension(), $excludeFileTypes)) { | ||
if ($file->ShowInSearch) { | ||
$file->ShowInSearch = false; | ||
$file->write(); | ||
} | ||
|
||
return; | ||
} | ||
|
||
$fields->push( | ||
CheckboxField::create( | ||
'ShowInSearch', | ||
_t( | ||
'SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.SHOWINSEARRCH', | ||
'Show in search?' | ||
) | ||
) | ||
); | ||
|
||
$fields->push( | ||
DatetimeField::create( | ||
'SearchIndexed', | ||
_t( | ||
'SilverStripe\\SearchService\\Extensions\\SearchServiceExtension.LastIndexed', | ||
'Last indexed in search' | ||
) | ||
) | ||
->setReadonly(true) | ||
); | ||
} | ||
|
||
} |
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
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 SilverStripe\SearchService\Tests\Extensions; | ||
|
||
use SilverStripe\Assets\File; | ||
use SilverStripe\Assets\Image; | ||
use SilverStripe\Core\Config\Config; | ||
use SilverStripe\Dev\SapphireTest; | ||
use SilverStripe\Forms\FieldList; | ||
use SilverStripe\Forms\Form; | ||
use SilverStripe\Forms\TabSet; | ||
use SilverStripe\SearchService\Extensions\SearchFormFactoryExtension; | ||
|
||
class SearchFormFactoryExtensionTest extends SapphireTest | ||
{ | ||
|
||
protected static $fixture_file = [ // phpcs:ignore | ||
'../fixtures.yml', | ||
'../pages.yml', | ||
]; | ||
|
||
public function testDefaultConfigValues(): void | ||
{ | ||
$expected = ['SilverStripe\Assets\Image']; | ||
$actual = Config::inst()->get(SearchFormFactoryExtension::class, 'exclude_classes'); | ||
$this->assertEquals($expected, $actual); | ||
} | ||
|
||
public function testImageAndFileInclusionInShowInSearch(): void | ||
{ | ||
$form = Form::create(); | ||
$fields = new FieldList(new TabSet('Editor')); | ||
$form->setFields($fields); | ||
|
||
$image = $this->objFromFixture(Image::class, 'image'); | ||
// Every file has default ShowInSearch value of 1 | ||
// (https://github.com/silverstripe/silverstripe-assets/blob/2/src/File.php#L163) | ||
$this->assertEquals(1, $image->ShowInSearch); | ||
|
||
$searchFormFactoryExtension = new SearchFormFactoryExtension(); | ||
$searchFormFactoryExtension->updateForm($form, null, 'Form', ['Record' => $image]); | ||
// By default, `SilverStripe\Assets\Image` is excluded from the search - see `_config/extensions.yml` | ||
$this->assertEquals(0, $image->ShowInSearch); | ||
|
||
$file = $this->objFromFixture(File::class, 'pdf-file'); | ||
$searchFormFactoryExtension->updateForm($form, null, 'Form', ['Record' => $file]); | ||
$this->assertEquals(1, $file->ShowInSearch); | ||
} | ||
|
||
public function testExcludedFileExtensionShowInSearch(): void | ||
{ | ||
// Modify config to exclude pdf files from search | ||
Config::modify()->set(SearchFormFactoryExtension::class, 'exclude_file_extensions', ['pdf']); | ||
|
||
$file = $this->objFromFixture(File::class, 'pdf-file'); | ||
// Default ShowInSearch value of 1 | ||
$this->assertEquals(1, $file->ShowInSearch); | ||
|
||
$form = Form::create(); | ||
$fields = new FieldList(new TabSet('Editor')); | ||
$form->setFields($fields); | ||
|
||
$searchFormFactoryExtension = new SearchFormFactoryExtension(); | ||
$searchFormFactoryExtension->updateForm($form, null, 'Form', ['Record' => $file]); | ||
$this->assertEquals(0, $file->ShowInSearch); | ||
} | ||
|
||
} |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.