Skip to content

Commit

Permalink
Add check for Synonyms being pushed/deleted in Elastic
Browse files Browse the repository at this point in the history
  • Loading branch information
Firesphere committed Oct 20, 2023
1 parent f49a25d commit baeab8d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
5 changes: 0 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@
"email": "[email protected]",
"homepage": "https://firesphere.dev",
"role": "Lead developer"
},
{
"name": "Marco `Sheepy` Hermo",
"email": "[email protected]",
"role": "Lead developer"
}
],
"require": {
Expand Down
7 changes: 0 additions & 7 deletions src/Tasks/ElasticIndexTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,20 +227,13 @@ public function getGroups(): int
return $this->groups;
}

public function setGroups(int $groups): void
{
$this->groups = $groups;
}

/**
* Index a single class for a given index. {@link static::indexClassForIndex()}
*
* @param bool $isGroup Is a specific group indexed
* @param string $class Class to index
* @param int $group Group to index
* @return int|bool
* @throws HTTPException
* @throws ValidationException
*/
private function indexClass(bool $isGroup, string $class, int $group)
{
Expand Down
50 changes: 50 additions & 0 deletions tests/unit/Extensions/ElasticSynonymExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Firesphere\ElasticSearch\Tests\unit\Extensions;

use Elastic\Elasticsearch\Client;
use Firesphere\ElasticSearch\Extensions\ElasticSynonymExtension;
use Firesphere\ElasticSearch\Models\SynonymSet;
use Firesphere\ElasticSearch\Services\ElasticCoreService;
use Firesphere\SearchBackend\Models\SearchSynonym;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\SapphireTest;

class ElasticSynonymExtensionTest extends SapphireTest
{

public function testOnAfterWrite()
{
SynonymSet::singleton()->requireDefaultRecords();
/** @var SynonymSet $set */
$set = SynonymSet::get()->first();
/** @var SearchSynonym $synonym */
$synonym = SearchSynonym::create(['Keyword' => 'Simon', 'Synonym' => 'Firesphere']);
$extension = new ElasticSynonymExtension();
$extension->setOwner($synonym);

$synonym->write();
$extension->onAfterWrite();

/** @var Client $client */
$client = Injector::inst()->get(ElasticCoreService::class)->getClient();
$synonymCheck = $client->synonyms()->getSynonymRule([
'set_id' => $set->Key,
'rule_id' => $synonym->getModifiedID()
]);

$check = $synonymCheck->asArray();

$this->assertEquals(['id' => $set->Key, 'synonyms' => $synonym->getCombinedSynonym()], $check);

$synonym->onAfterDelete();
$extension->onAfterDelete();
$synonymCheck = $client->synonyms()->getSynonymRule([
'set_id' => $set->Key,
'rule_id' => $synonym->getModifiedID()
]);

$this->assertEquals(404, $synonymCheck->getStatusCode());

}
}

0 comments on commit baeab8d

Please sign in to comment.