Skip to content

Commit

Permalink
Test adding and removing directly
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Erkelens committed Oct 16, 2023
1 parent ff78024 commit 6d98db3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
22 changes: 16 additions & 6 deletions src/Extensions/DataObjectElasticExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@

use Elastic\Elasticsearch\Exception\ClientResponseException;
use Elastic\Elasticsearch\Exception\ServerResponseException;
use Elastic\Elasticsearch\Response\Elasticsearch;
use Exception;
use Firesphere\ElasticSearch\Indexes\ElasticIndex;
use Firesphere\ElasticSearch\Services\ElasticCoreService;
use Http\Promise\Promise;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Log\LoggerInterface;
use SilverStripe\Core\Injector\Injector;
Expand Down Expand Up @@ -81,12 +83,15 @@ private function getDeleteQuery(mixed $index): array
}

/**
* @param ElasticCoreService $service
* @param array $deleteQuery
* @return Elasticsearch|Promise|bool
* @throws NotFoundExceptionInterface
*/
protected function executeQuery(ElasticCoreService $service, array $deleteQuery): void
protected function executeQuery(ElasticCoreService $service, array $deleteQuery)
{
try {
$service->getClient()->deleteByQuery($deleteQuery);
return $service->getClient()->deleteByQuery($deleteQuery);
} catch (Exception $e) {
// DirtyClass handling is a DataObject Search Core extension
$dirty = $this->owner->getDirtyClass('DELETE');
Expand All @@ -97,6 +102,8 @@ protected function executeQuery(ElasticCoreService $service, array $deleteQuery)
/** @var LoggerInterface $logger */
$logger = Injector::inst()->get(LoggerInterface::class);
$logger->error($e->getMessage(), $e->getTrace());

return false;
}
}

Expand All @@ -113,7 +120,7 @@ public function onAfterWrite()
!$this->owner->hasExtension(Versioned::class) ||
($this->owner->hasExtension(Versioned::class) && $this->owner->isPublished())
) {
$this->doIndex();
$this->pushToElastic();
}

// @codeCoverageIgnoreStart Elastic during tests isn't fast enough to pick this up properly
Expand All @@ -128,11 +135,12 @@ public function onAfterWrite()
* and query components.
* It can be called to add an object to the index immediately, without
* requiring a write.
* @throws NotFoundExceptionInterface
* @return array|void|bool
* @throws ClientResponseException
* @throws NotFoundExceptionInterface
* @throws ServerResponseException
*/
public function doIndex()
public function pushToElastic()
{
$list = ArrayList::create();
$list->push($this->owner);
Expand All @@ -143,8 +151,10 @@ public function doIndex()
$index = Injector::inst()->get($indexStr);
$idxConfig = ElasticIndex::config()->get($index->getIndexName());
if (in_array($this->owner->ClassName, $idxConfig['Classes'])) {
$service->updateIndex($index, $list);
$result = $service->updateIndex($index, $list);
}
}

return $result ?? false;
}
}
14 changes: 13 additions & 1 deletion tests/unit/Extensions/DataObjectElasticExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public function testOnAfterDelete()
$extension = new DataObjectElasticExtension();
foreach ($pages as $page) {
$extension->setOwner($page);
$page->delete();
$extension->onAfterDelete();
$page->delete();
}
$query = new ElasticQuery();
$query->addTerm('Page');
Expand All @@ -45,4 +45,16 @@ public function testOnAfterDelete()
// Even despite the wait in PHP, it doesn't help
$this->assertEquals(0, \Page::get()->count());
}

public function testAddRemoveFromElastic()
{

$page = \Page::create(['Title' => 'AddRemove Test']);
$extension = new DataObjectElasticExtension();
$extension->setOwner($page);
$indexCheck = $page->pushToElastic();
$this->assertNotFalse($indexCheck);
$removeCheck = $page->deleteFromElastic();
$this->assertNotFalse($removeCheck);
}
}

0 comments on commit 6d98db3

Please sign in to comment.