Skip to content

Commit b71501f

Browse files
authored
Update for scrutinizer issues. pt. 1 (#65)
* Update for scrutinizer issues. pt. 1 * Add check for Synonyms being pushed/deleted in Elastic * Ensure the set-up of synonym sets before testing * The rule ID is not the same as the set ID * Better testing of CRUD for synonyms
1 parent 87db121 commit b71501f

File tree

5 files changed

+99
-27
lines changed

5 files changed

+99
-27
lines changed

composer.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@
1515
"email": "github@casa-laguna.net",
1616
"homepage": "https://firesphere.dev",
1717
"role": "Lead developer"
18-
},
19-
{
20-
"name": "Marco `Sheepy` Hermo",
21-
"email": "marco@silverstripe.com",
22-
"role": "Lead developer"
2318
}
2419
],
2520
"require": {

src/Extensions/ElasticSynonymExtension.php

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Firesphere\ElasticSearch\Models\SynonymSet;
1717
use Firesphere\ElasticSearch\Services\ElasticCoreService;
1818
use Firesphere\SearchBackend\Models\SearchSynonym;
19+
use Psr\Container\NotFoundExceptionInterface;
1920
use SilverStripe\Core\Injector\Injector;
2021
use SilverStripe\Forms\FieldList;
2122
use SilverStripe\ORM\DataExtension;
@@ -27,48 +28,42 @@
2728
*/
2829
class ElasticSynonymExtension extends DataExtension
2930
{
30-
public function updateCMSFields(FieldList $fields)
31-
{
32-
$fields->removeByName('SynonymSets');
33-
parent::updateCMSFields($fields);
34-
}
35-
3631
/**
3732
* Add or update this synonym in Elastic
3833
*
39-
* @throws ClientResponseException
40-
* @throws ServerResponseException
41-
* @throws MissingParameterException
34+
* @throws NotFoundExceptionInterface
4235
*/
4336
public function onAfterWrite()
4437
{
4538
$service = Injector::inst()->get(ElasticCoreService::class);
39+
/** @var SearchSynonym|ElasticSynonymExtension $owner */
40+
$owner = $this->owner;
4641
$syn = $service->getClient()->synonyms();
4742
/** @var SynonymSet $set */
4843
$set = SynonymSet::get()->first();
4944
$syn->putSynonymRule([
5045
'set_id' => $set->Key,
51-
'rule_id' => $this->owner->getModifiedID(),
46+
'rule_id' => $owner->getModifiedID(),
5247
'body' => [
53-
'synonyms' => $this->owner->getCombinedSynonym()
48+
'synonyms' => $owner->getCombinedSynonym()
5449
]
5550
]);
5651
}
5752

5853
/**
5954
* When deleting a synonym from the CMS, delete it as a rule
6055
*
61-
* @throws ClientResponseException
62-
* @throws ServerResponseException
63-
* @throws MissingParameterException
56+
* @throws NotFoundExceptionInterface
6457
*/
6558
public function onAfterDelete()
6659
{
6760
$service = Injector::inst()->get(ElasticCoreService::class);
6861
$syn = $service->getClient()->synonyms();
62+
/** @var SearchSynonym $owner */
63+
$owner = $this->owner;
6964
/** @var SynonymSet $set */
7065
$set = SynonymSet::get()->first();
71-
$syn->deleteSynonymRule(['set_id' => $set->Key, 'rule_id' => $this->owner->getModifiedId()]);
66+
$syn->deleteSynonymRule(['set_id' => $set->Key, 'rule_id' => $owner->getModifiedId()]);
7267
parent::onAfterDelete();
7368
}
7469
}

src/Tasks/ElasticIndexTask.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ class ElasticIndexTask extends BuildTask
8888
*/
8989
protected $index;
9090

91+
/**
92+
* @var int
93+
*/
94+
protected $groups = 0;
95+
9196
/**
9297
* ElasticIndexTask constructor. Sets up the document factory
9398
*
@@ -142,7 +147,7 @@ public function run($request)
142147
$time = gmdate('H:i:s', (time() - $start));
143148
$this->getLogger()->info(sprintf('Time taken: %s', $time));
144149

145-
return $groups;
150+
$this->groups = $groups;
146151
}
147152

148153
/**
@@ -217,15 +222,18 @@ public function setDebug(bool $debug, bool $force = false): self
217222
return $this;
218223
}
219224

225+
public function getGroups(): int
226+
{
227+
return $this->groups;
228+
}
229+
220230
/**
221231
* Index a single class for a given index. {@link static::indexClassForIndex()}
222232
*
223233
* @param bool $isGroup Is a specific group indexed
224234
* @param string $class Class to index
225235
* @param int $group Group to index
226236
* @return int|bool
227-
* @throws HTTPException
228-
* @throws ValidationException
229237
*/
230238
private function indexClass(bool $isGroup, string $class, int $group)
231239
{
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace Firesphere\ElasticSearch\Tests\unit\Extensions;
4+
5+
use Elastic\Elasticsearch\Client;
6+
use Elastic\Elasticsearch\Exception\ClientResponseException;
7+
use Firesphere\ElasticSearch\Extensions\ElasticSynonymExtension;
8+
use Firesphere\ElasticSearch\Models\SynonymSet;
9+
use Firesphere\ElasticSearch\Services\ElasticCoreService;
10+
use Firesphere\ElasticSearch\Tasks\ElasticConfigureSynonymsTask;
11+
use Firesphere\SearchBackend\Models\SearchSynonym;
12+
use SilverStripe\Control\HTTPRequest;
13+
use SilverStripe\Core\Injector\Injector;
14+
use SilverStripe\Dev\SapphireTest;
15+
16+
class ElasticSynonymExtensionTest extends SapphireTest
17+
{
18+
19+
public function testWriteUpdateDelete()
20+
{
21+
(new SynonymSet())->requireDefaultRecords();
22+
$request = new HTTPRequest('GET', 'dev/tasks/ElasticSynonymTask');
23+
$task = new ElasticConfigureSynonymsTask();
24+
25+
$task->run($request);
26+
27+
SynonymSet::singleton()->requireDefaultRecords();
28+
/** @var SynonymSet $set */
29+
$set = SynonymSet::get()->first();
30+
/** @var SearchSynonym $synonym */
31+
$synonym = SearchSynonym::create(['Keyword' => 'Simon', 'Synonym' => 'Firesphere']);
32+
$extension = new ElasticSynonymExtension();
33+
$extension->setOwner($synonym);
34+
35+
$synonym->write();
36+
37+
/** @var Client $client */
38+
$client = Injector::inst()->get(ElasticCoreService::class)->getClient();
39+
$synonymCheck = $client->synonyms()->getSynonymRule([
40+
'set_id' => $set->Key,
41+
'rule_id' => $synonym->getModifiedID()
42+
]);
43+
44+
$check = $synonymCheck->asArray();
45+
46+
$this->assertEquals(['id' => $synonym->getModifiedID(), 'synonyms' => $synonym->getCombinedSynonym()], $check);
47+
48+
$synonym->Synonym = 'Firesphere,Hans';
49+
$synonym->write();
50+
51+
$synonymCheck = $client->synonyms()->getSynonymRule([
52+
'set_id' => $set->Key,
53+
'rule_id' => $synonym->getModifiedID()
54+
]);
55+
56+
$check = $synonymCheck->asArray();
57+
58+
$this->assertEquals(['id' => $synonym->getModifiedID(), 'synonyms' => $synonym->getCombinedSynonym()], $check);
59+
60+
61+
$synonym->delete();
62+
63+
try {
64+
$client->synonyms()->getSynonymRule([
65+
'set_id' => $set->Key,
66+
'rule_id' => $synonym->getModifiedID()
67+
]);
68+
} catch (ClientResponseException $e) {
69+
$this->assertEquals(404, $e->getCode());
70+
}
71+
72+
73+
}
74+
}

tests/unit/Tasks/ElasticIndexTaskTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ public function testRun()
3333
$page->publishSingle();
3434
$task = new ElasticIndexTask();
3535
$request = new HTTPRequest('GET', 'dev/tasks/ElasticIndexTask');
36-
$result = $task->run($request);
36+
$task->run($request);
3737

38-
$this->assertGreaterThan(0, $result);
38+
$this->assertGreaterThan(0, $task->getGroups());
3939
$this->assertinstanceOf(ElasticIndex::class, $task->getIndex());
4040
$request = new HTTPRequest('GET', 'dev/tasks/ElasticIndexTask', ['clear' => true]);
41-
$result = $task->run($request);
41+
$task->run($request);
4242

43-
$this->assertGreaterThan(0, $result);
43+
$this->assertGreaterThan(0, $task->getGroups());
4444
$this->assertinstanceOf(ElasticIndex::class, $task->getIndex());
4545
}
4646
}

0 commit comments

Comments
 (0)