Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show in search check for files #92

Merged
merged 13 commits into from
May 2, 2024
11 changes: 9 additions & 2 deletions src/Extensions/SearchFormFactoryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace SilverStripe\SearchService\Extensions;

use SilverStripe\Assets\Image;
use SilverStripe\Control\RequestHandler;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Extension;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\DatetimeField;
Expand All @@ -22,7 +22,14 @@ public function updateForm(
$fields = $form->Fields()->findOrMakeTab('Editor.Details');
$file = $context['Record'] ?? null;

if (!$fields || !$file || $file instanceof Image) {
$excludedClasses = Config::inst()->get(self::class, 'exclude_classes') ?? [];
blueo marked this conversation as resolved.
Show resolved Hide resolved
$excludeFileTypes = Config::inst()->get(self::class, 'exclude_file_extensions') ?? [];

if (!$fields
|| !$file
|| in_array($file->ClassName, $excludedClasses)
|| in_array($file->getExtension(), $excludeFileTypes)
) {
return;
}

Expand Down
30 changes: 22 additions & 8 deletions tests/DataObject/DataObjectDocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -760,15 +760,29 @@ public function testIndexDataObjectDocument(): void

public function testIndexDataObjectDocumentShowInSearch(): void
{
// Document should be NOT indexable (as its value is changed to ShowInSearch: 0)
$dataObject = $this->objFromFixture(DataObjectFakeVersioned::class, 'one');
$dataObject->ShowInSearch = false;
$dataObject->write();
$this->assertFalse($dataObject->shouldIndex());
$dataObject = $this->objFromFixture(DataObjectFakeVersioned::class, 'two');
$doc = DataObjectDocument::create($dataObject);

// Document should be indexable (as its value is now changed to ShowInSearch: 1)
$config = $this->mockConfig();
$config->set(
'getIndexesForDocument',
[
$doc->getIdentifier() => [
'index' => 'data',
],
]
);

// Should not index as ShowInSearch is false for this DataObject
$this->assertFalse($doc->shouldIndex());

// Should index as ShowInSearch is now set to true
$dataObject->ShowInSearch = true;
$dataObject->write();
$this->assertTrue($dataObject->shouldIndex());
$dataObject->publishRecursive();
blueo marked this conversation as resolved.
Show resolved Hide resolved

$doc = DataObjectDocument::create($dataObject);

$this->assertTrue($doc->shouldIndex());
}

}
31 changes: 2 additions & 29 deletions tests/Fake/DataObjectFakeVersioned.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\DataObject;
use SilverStripe\SearchService\Extensions\SearchServiceExtension;
use SilverStripe\SearchService\Interfaces\DocumentInterface;
use SilverStripe\Versioned\Versioned;

/**
Expand All @@ -14,7 +13,7 @@
* @mixin SearchServiceExtension
* @mixin Versioned
*/
class DataObjectFakeVersioned extends DataObject implements TestOnly, DocumentInterface
class DataObjectFakeVersioned extends DataObject implements TestOnly
{

private static string $table_name = 'DataObjectFakeVersioned';
Expand All @@ -26,38 +25,12 @@ class DataObjectFakeVersioned extends DataObject implements TestOnly, DocumentIn

private static array $db = [
'Title' => 'Varchar',
'ShowInSearch' => 'Boolean',
];

public function canView(mixed $member = null): bool
{
return true;
}

public function getIdentifier(): string
{
return (string) $this->ID;
}

public function shouldIndex(): bool
{
return (bool) $this->ShowInSearch;
}

public function markIndexed(): void
{
$this->ShowInSearch = 1;
}

public function toArray(): array
{
return [
'Title' => $this->Title,
];
}

public function getSourceClass(): string
{
return static::class;
}

}