From 2cd5debf977c8e1e3893cf645da9624ed73782ae Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Fri, 7 Jul 2017 16:44:57 +0200 Subject: [PATCH] BUGFIX: Fix broken getRecord method Also add test covering method. --- Classes/Domain/Index/TcaIndexer.php | 43 ++++++++++--------- .../Elasticsearch/IndexTcaTableTest.php | 23 ++++++++++ 2 files changed, 46 insertions(+), 20 deletions(-) diff --git a/Classes/Domain/Index/TcaIndexer.php b/Classes/Domain/Index/TcaIndexer.php index 18444ce..7923565 100644 --- a/Classes/Domain/Index/TcaIndexer.php +++ b/Classes/Domain/Index/TcaIndexer.php @@ -22,6 +22,7 @@ namespace Codappix\SearchCore\Domain\Index; use Codappix\SearchCore\Connection\ConnectionInterface; use TYPO3\CMS\Core\Database\ConnectionPool; +use TYPO3\CMS\Core\Database\Query\QueryBuilder; use TYPO3\CMS\Core\Utility\GeneralUtility; /** @@ -53,21 +54,11 @@ class TcaIndexer extends AbstractIndexer */ protected function getRecords($offset, $limit) { - $queryBuilder = $this->getDatabaseConnection()->getQueryBuilderForTable($this->tcaTableService->getTableName()); - $where = $this->tcaTableService->getWhereClause(); - $query = $queryBuilder->select(... $this->tcaTableService->getFields()) - ->from($this->tcaTableService->getTableClause()) - ->where($where->getStatement()) - ->setParameters($where->getParameters()) + $records = $this->getQuery() ->setFirstResult($offset) - ->setMaxResults($limit); - - foreach ($this->tcaTableService->getJoins() as $join) { - $query->from($join->getTable()); - $query->andWhere($join->getCondition()); - } - - $records = $query->execute()->fetchAll(); + ->setMaxResults($limit) + ->execute() + ->fetchAll(); if ($records === null) { return null; @@ -88,12 +79,7 @@ class TcaIndexer extends AbstractIndexer */ protected function getRecord($identifier) { - $record = $this->getDatabaseConnection()->exec_SELECTgetSingleRow( - $this->tcaTableService->getFields(), - $this->tcaTableService->getTableClause(), - $this->tcaTableService->getWhereClause() - . ' AND ' . $this->tcaTableService->getTableName() . '.uid = ' . (int) $identifier - ); + $record = $this->getQuery()->execute()->fetch(); if ($record === false || $record === null) { throw new NoRecordFoundException( @@ -114,6 +100,23 @@ class TcaIndexer extends AbstractIndexer return $this->tcaTableService->getTableName(); } + protected function getQuery() : QueryBuilder + { + $queryBuilder = $this->getDatabaseConnection()->getQueryBuilderForTable($this->tcaTableService->getTableName()); + $where = $this->tcaTableService->getWhereClause(); + $query = $queryBuilder->select(... $this->tcaTableService->getFields()) + ->from($this->tcaTableService->getTableClause()) + ->where($where->getStatement()) + ->setParameters($where->getParameters()); + + foreach ($this->tcaTableService->getJoins() as $join) { + $query->from($join->getTable()); + $query->andWhere($join->getCondition()); + } + + return $query; + } + protected function getDatabaseConnection() { return GeneralUtility::makeInstance(ConnectionPool::class); diff --git a/Tests/Functional/Connection/Elasticsearch/IndexTcaTableTest.php b/Tests/Functional/Connection/Elasticsearch/IndexTcaTableTest.php index 8a76c77..7210e01 100644 --- a/Tests/Functional/Connection/Elasticsearch/IndexTcaTableTest.php +++ b/Tests/Functional/Connection/Elasticsearch/IndexTcaTableTest.php @@ -59,6 +59,29 @@ class IndexTcaTableTest extends AbstractFunctionalTestCase ); } + /** + * @test + */ + public function indexSingleBasicTtContent() + { + \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(ObjectManager::class) + ->get(IndexerFactory::class) + ->getIndexer('tt_content') + ->indexDocument(6) + ; + + $response = $this->client->request('typo3content/_search?q=*:*'); + + $this->assertTrue($response->isOK(), 'Elastica did not answer with ok code.'); + $this->assertSame($response->getData()['hits']['total'], 1, 'Not exactly 1 document was indexed.'); + $this->assertArraySubset( + ['_source' => ['header' => 'indexed content element']], + $response->getData()['hits']['hits'][0], + false, + 'Record was not indexed.' + ); + } + /** * @test * @expectedException \Codappix\SearchCore\Domain\Index\IndexingException