Skip to content

Commit

Permalink
Merge branch 'release/3.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
janhenckens committed Mar 15, 2024
2 parents 0d46893 + 0a47413 commit 954680a
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 18 deletions.
13 changes: 3 additions & 10 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
APP_ENV: testing

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Set up MySQL
run: |
Expand All @@ -50,15 +50,8 @@ jobs:
run: |
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction
- name: Run eventHandlersTest
run: ./vendor/bin/codecept run unit EventHandlersTest
- name: Run all tests
run: ./vendor/bin/codecept run

- name: Run AbstractEngineTest
run: ./vendor/bin/codecept run unit AbstractEngineTest

- name: Run ScoutVariableTest
run: ./vendor/bin/codecept run unit ScoutVariableTest

- name: Run SearchableBehaviorTest
run: ./vendor/bin/codecept run unit SearchableBehaviorTest

4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to this project will be documented in this file.

## 3.4.0 - 2024-03-15
## Added
- Related element types are now configurable ([#273](https://github.com/studioespresso/craft-scout/pull/273))

## 3.3.3 - 2024-03-01
### Fixed
- Fixed an issues with updating and deleting split elements ([#259](https://github.com/studioespresso/craft-scout/issues/259) & [#281](https://github.com/studioespresso/craft-scout/issues/281))
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ return [
* Disabling this can improve performance on larger sites that have lots of relations.
*/
'indexRelations' => true,

/**
* The element types to look for when indexRelations is enabled.
* By default, all Craft elements are checked for relations.
* Use this to avoid unnecessary queries to Elements that aren't
* used by your indices or to check custom Elements that may be
* related to your indices
*/
'relatedElementTypes' => [],

/*
* The Algolia Application ID, this id can be found in your Algolia Account
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "studioespresso/craft-scout",
"description": "Craft Scout provides a simple solution for adding full-text search to your entries. Scout will automatically keep your search indexes in sync with your entries.",
"type": "craft-plugin",
"version": "3.3.3",
"version": "3.4.0",
"keywords": [
"craft",
"cms",
Expand Down
9 changes: 9 additions & 0 deletions config/scout.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,13 @@
* Make this false to use the single item itself.
*/
'useOriginalRecordIfSplitValueIsArrayOfOne' => true,

/**
* The element types to look for when indexRelations is enabled.
* By default, all Craft elements are checked for relations.
* Use this to avoid unnecessary queries to Elements that aren't
* used by your indices or to check custom Elements that may be
* related to your indices
*/
'relatedElementTypes' => [],
];
11 changes: 10 additions & 1 deletion src/behaviors/SearchableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,19 @@ public function searchableRelations(): void

public function getRelatedElements(): Collection
{
if (!Scout::$plugin->getSettings()->sync) {
$settings = Scout::$plugin->getSettings();

if (!$settings->sync) {
return new Collection();
}

if (!empty($settings->relatedElementTypes)) {
return Collection::make($settings->relatedElementTypes)
->flatMap(function($className) {
return $className::find()->relatedTo($this->owner)->site('*')->all();
});
}

$assets = Asset::find()->relatedTo($this->owner)->site('*')->all();
$categories = Category::find()->relatedTo($this->owner)->site('*')->all();
$entries = Entry::find()->relatedTo($this->owner)->site('*')->all();
Expand Down
3 changes: 3 additions & 0 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ class Settings extends Model
/** @var bool */
public $useOriginalRecordIfSplitValueIsArrayOfOne = true;

/** @var string[] An array of ::class strings */
public $relatedElementTypes = [];

public function fields(): array
{
$fields = parent::fields();
Expand Down
111 changes: 106 additions & 5 deletions tests/unit/AbstractEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,113 @@ public function it_handles_when_the_object_field_isnt_an_array()
],
];

$this->assertEqualsCanonicalizing([
'save' => [
['objectID' => 1, 'title' => 'One', 'body' => 'Paragraph 1', 'distinctID' => 1],
$this->assertEqualsCanonicalizing(
[
'save' => [
[
'objectID' => 1,
'title' => 'One',
'body' => 'Paragraph 1',
'distinctID' => 1,
],
],
'delete' => [
[
'objectID' => 1,
'title' => 'One',
'body' => 'Paragraph 1',
'distinctID' => 1,
],
],
],
'delete' => [],
], $engine->splitObjects($objects));
$engine->splitObjects($objects)
);
}

/** @test * */
public function it_handles_a_single_split_value()
{
$settings = Scout::$plugin->getSettings();
$settings->useOriginalRecordIfSplitValueIsArrayOfOne = false;

/** @var FakeEngine $engine */
$engine = $settings->getEngines()[0];

$objects = [
[
'objectID' => 1,
'title' => 'One',
'body' => [
'Paragraph 1'
],
],
];

$this->assertEqualsCanonicalizing(
[
'save' => [
[
'objectID' => 1,
'title' => 'One',
'body' => 'Paragraph 1',
'distinctID' => 1,
],
],
'delete' => [
[
'objectID' => 1,
'title' => 'One',
'body' => [
'Paragraph 1',
],
],
],
],
$engine->splitObjects($objects)
);
}

/** @test * */
public function it_handles_a_single_split_value_and_dont_use_original_value()
{
$settings = Scout::$plugin->getSettings();
$settings->useOriginalRecordIfSplitValueIsArrayOfOne = false;

/** @var FakeEngine $engine */
$engine = $settings->getEngines()[0];

$objects = [
[
'objectID' => 1,
'title' => 'One',
'body' => [
'Paragraph 1'
],
],
];

$this->assertEqualsCanonicalizing(
[
'save' => [
[
'objectID' => 1,
'title' => 'One',
'body' => 'Paragraph 1',
'distinctID' => 1,
],
],
'delete' => [
[
'objectID' => 1,
'title' => 'One',
'body' => [
'Paragraph 1',
],
],
],
],
$engine->splitObjects($objects)
);
}

/** @test * */
Expand Down
27 changes: 26 additions & 1 deletion tests/unit/EventHandlersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Codeception\Test\Unit;
use Craft;
use craft\elements\Entry;
use craft\fieldlayoutelements\CustomField;
use craft\fields\Entries;
use craft\models\Section;
use craft\models\Section_SiteSettings;
Expand Down Expand Up @@ -124,14 +125,38 @@ public function it_attaches_to_the_element_save_event_once()
/** @test * */
public function it_also_updates_related_elements()
{
// (very) Verbose adding the new field to the entry type field layout
// so that the relation can be saved through the Element.

// Define a Relation field and persist to DB
$relationField = new Entries([
'name' => 'Entry field',
'handle' => 'entryField',
'groupId' => Craft::$app->getFields()->getAllGroups()[0]->id,
]);
Craft::$app->getFields()->saveField($relationField);

Craft::$app->getRelations()->saveRelations($relationField, $this->element, [$this->element2->id]);
// Get the field layout...
$field_layout = $this->element->getType()
->getFieldLayout();
$current_tabs = $field_layout->getTabs();
// ... and get the current fields on the first tab...
$elements = $current_tabs[0]->getElements();
// ... and add the previously created Relation field to the layout ...
$elements[] = new CustomField(
$relationField,
);
$current_tabs[0]->setElements($elements);
$field_layout->setTabs($current_tabs);

// ... and persist the updated layout to the DB.
Craft::$app->getFields()->saveLayout($field_layout);

// Now can define the relationship through attribute on Element
// and persist the relation to the DB whilst also having the
// relation defined on the current Element instance.
$this->element->entryField = [$this->element2->id];
Craft::$app->getElements()->saveElement($this->element);

Craft::$app->getCache()->set("scout-Blog-{$this->element->id}-updateCalled", 0);
Craft::$app->getCache()->set("scout-Blog-{$this->element2->id}-updateCalled", 0);
Expand Down

0 comments on commit 954680a

Please sign in to comment.