diff --git a/composer.json b/composer.json index b3e01fa..32b18ef 100644 --- a/composer.json +++ b/composer.json @@ -15,11 +15,6 @@ "email": "github@casa-laguna.net", "homepage": "https://firesphere.dev", "role": "Lead developer" - }, - { - "name": "Marco `Sheepy` Hermo", - "email": "marco@silverstripe.com", - "role": "Lead developer" } ], "require": { diff --git a/src/Extensions/ElasticSynonymExtension.php b/src/Extensions/ElasticSynonymExtension.php index 3899c40..8fe3cd7 100644 --- a/src/Extensions/ElasticSynonymExtension.php +++ b/src/Extensions/ElasticSynonymExtension.php @@ -16,6 +16,7 @@ use Firesphere\ElasticSearch\Models\SynonymSet; use Firesphere\ElasticSearch\Services\ElasticCoreService; use Firesphere\SearchBackend\Models\SearchSynonym; +use Psr\Container\NotFoundExceptionInterface; use SilverStripe\Core\Injector\Injector; use SilverStripe\Forms\FieldList; use SilverStripe\ORM\DataExtension; @@ -27,30 +28,24 @@ */ class ElasticSynonymExtension extends DataExtension { - public function updateCMSFields(FieldList $fields) - { - $fields->removeByName('SynonymSets'); - parent::updateCMSFields($fields); - } - /** * Add or update this synonym in Elastic * - * @throws ClientResponseException - * @throws ServerResponseException - * @throws MissingParameterException + * @throws NotFoundExceptionInterface */ public function onAfterWrite() { $service = Injector::inst()->get(ElasticCoreService::class); + /** @var SearchSynonym|ElasticSynonymExtension $owner */ + $owner = $this->owner; $syn = $service->getClient()->synonyms(); /** @var SynonymSet $set */ $set = SynonymSet::get()->first(); $syn->putSynonymRule([ 'set_id' => $set->Key, - 'rule_id' => $this->owner->getModifiedID(), + 'rule_id' => $owner->getModifiedID(), 'body' => [ - 'synonyms' => $this->owner->getCombinedSynonym() + 'synonyms' => $owner->getCombinedSynonym() ] ]); } @@ -58,17 +53,17 @@ public function onAfterWrite() /** * When deleting a synonym from the CMS, delete it as a rule * - * @throws ClientResponseException - * @throws ServerResponseException - * @throws MissingParameterException + * @throws NotFoundExceptionInterface */ public function onAfterDelete() { $service = Injector::inst()->get(ElasticCoreService::class); $syn = $service->getClient()->synonyms(); + /** @var SearchSynonym $owner */ + $owner = $this->owner; /** @var SynonymSet $set */ $set = SynonymSet::get()->first(); - $syn->deleteSynonymRule(['set_id' => $set->Key, 'rule_id' => $this->owner->getModifiedId()]); + $syn->deleteSynonymRule(['set_id' => $set->Key, 'rule_id' => $owner->getModifiedId()]); parent::onAfterDelete(); } } diff --git a/src/Tasks/ElasticIndexTask.php b/src/Tasks/ElasticIndexTask.php index 4ca51be..c7f4a32 100644 --- a/src/Tasks/ElasticIndexTask.php +++ b/src/Tasks/ElasticIndexTask.php @@ -88,6 +88,11 @@ class ElasticIndexTask extends BuildTask */ protected $index; + /** + * @var int + */ + protected $groups = 0; + /** * ElasticIndexTask constructor. Sets up the document factory * @@ -142,7 +147,7 @@ public function run($request) $time = gmdate('H:i:s', (time() - $start)); $this->getLogger()->info(sprintf('Time taken: %s', $time)); - return $groups; + $this->groups = $groups; } /** @@ -217,6 +222,11 @@ public function setDebug(bool $debug, bool $force = false): self return $this; } + public function getGroups(): int + { + return $this->groups; + } + /** * Index a single class for a given index. {@link static::indexClassForIndex()} * @@ -224,8 +234,6 @@ public function setDebug(bool $debug, bool $force = false): self * @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) { diff --git a/tests/unit/Extensions/ElasticSynonymExtensionTest.php b/tests/unit/Extensions/ElasticSynonymExtensionTest.php new file mode 100644 index 0000000..89af00e --- /dev/null +++ b/tests/unit/Extensions/ElasticSynonymExtensionTest.php @@ -0,0 +1,74 @@ +requireDefaultRecords(); + $request = new HTTPRequest('GET', 'dev/tasks/ElasticSynonymTask'); + $task = new ElasticConfigureSynonymsTask(); + + $task->run($request); + + 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(); + + /** @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' => $synonym->getModifiedID(), 'synonyms' => $synonym->getCombinedSynonym()], $check); + + $synonym->Synonym = 'Firesphere,Hans'; + $synonym->write(); + + $synonymCheck = $client->synonyms()->getSynonymRule([ + 'set_id' => $set->Key, + 'rule_id' => $synonym->getModifiedID() + ]); + + $check = $synonymCheck->asArray(); + + $this->assertEquals(['id' => $synonym->getModifiedID(), 'synonyms' => $synonym->getCombinedSynonym()], $check); + + + $synonym->delete(); + + try { + $client->synonyms()->getSynonymRule([ + 'set_id' => $set->Key, + 'rule_id' => $synonym->getModifiedID() + ]); + } catch (ClientResponseException $e) { + $this->assertEquals(404, $e->getCode()); + } + + + } +} diff --git a/tests/unit/Tasks/ElasticIndexTaskTest.php b/tests/unit/Tasks/ElasticIndexTaskTest.php index 6653864..790c706 100644 --- a/tests/unit/Tasks/ElasticIndexTaskTest.php +++ b/tests/unit/Tasks/ElasticIndexTaskTest.php @@ -33,14 +33,14 @@ public function testRun() $page->publishSingle(); $task = new ElasticIndexTask(); $request = new HTTPRequest('GET', 'dev/tasks/ElasticIndexTask'); - $result = $task->run($request); + $task->run($request); - $this->assertGreaterThan(0, $result); + $this->assertGreaterThan(0, $task->getGroups()); $this->assertinstanceOf(ElasticIndex::class, $task->getIndex()); $request = new HTTPRequest('GET', 'dev/tasks/ElasticIndexTask', ['clear' => true]); - $result = $task->run($request); + $task->run($request); - $this->assertGreaterThan(0, $result); + $this->assertGreaterThan(0, $task->getGroups()); $this->assertinstanceOf(ElasticIndex::class, $task->getIndex()); } }