Skip to content

Commit

Permalink
Add tests for SolrSearch
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver-stoehr committed May 19, 2023
1 parent 6676f03 commit aeb34f8
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions Tests/Functional/Common/SolrSearchTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

/**
* (c) Kitodo. Key to digital objects e.V. <[email protected]>
*
* This file is part of the Kitodo and TYPO3 projects.
*
* @license GNU General Public License version 3 or later.
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/

namespace Kitodo\Dlf\Tests\Functional\Common;

use Kitodo\Dlf\Common\Solr;
use Kitodo\Dlf\Common\SolrSearch;
use Kitodo\Dlf\Domain\Repository\DocumentRepository;
use Kitodo\Dlf\Domain\Repository\SolrCoreRepository;
use Kitodo\Dlf\Tests\Functional\FunctionalTestCase;
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;

class SolrSearchTest extends FunctionalTestCase
{
static array $databaseFixtures = [
__DIR__ . '/../../Fixtures/Common/solrcores.xml'
];

static array $solrFixtures = [
__DIR__ . '/../../Fixtures/Common/documents_1.solr.json'
];

public function setUp(): void
{
parent::setUp();
$this->setUpData(self::$databaseFixtures);
$this->setUpSolr(4, 0, self::$solrFixtures);
}

/**
* @test
*/
public function canPrepareAndSubmit()
{
$documentRepository = $this->initializeRepository(DocumentRepository::class, 0);
$settings = ['solrcore' => 4];

$params1 = [];
$search = new SolrSearch($documentRepository, null, $settings, $params1);
$search->prepare();
$this->assertEquals(33, $search->getNumFound());
$this->assertEquals(3, $search->getSolrResults()['numberOfToplevels']);
$this->assertCount(15, $search->getSolrResults()['documents']);

$params2 = ['query' => '10 Keyboard pieces'];
$search2 = new SolrSearch($documentRepository, null, $settings, $params2);
$search2->prepare();
$this->assertEquals(1, $search2->getNumFound());
$this->assertEquals(1, $search2->getSolrResults()['numberOfToplevels']);
$this->assertCount(1, $search2->getSolrResults()['documents']);

$params3 = ['query' => 'foobar'];
$search3 = new SolrSearch($documentRepository, null, $settings, $params3);
$search3->prepare();
$this->assertEquals(0, $search3->getNumFound());
$this->assertEquals(0, $search3->getSolrResults()['numberOfToplevels']);
$this->assertCount(0, $search3->getSolrResults()['documents']);
}

protected function setUpData($databaseFixtures): void
{
foreach ($databaseFixtures as $filePath) {
$this->importDataSet($filePath);
}
$this->persistenceManager = $this->objectManager->get(PersistenceManager::class);
$this->initializeRepository(DocumentRepository::class, 0);
}

protected function setUpSolr($uid, $storagePid, $solrFixtures)
{
$this->solrCoreRepository = $this->initializeRepository(SolrCoreRepository::class, $storagePid);

// Setup Solr only once for all tests in this suite
static $solr = null;

if ($solr === null) {
$coreName = Solr::createCore();
$solr = Solr::getInstance($coreName);
foreach ($solrFixtures as $filePath) {
$this->importSolrDocuments($solr, $filePath);
}
}

$coreModel = $this->solrCoreRepository->findByUid($uid);
$coreModel->setIndexName($solr->core);
$this->solrCoreRepository->update($coreModel);
$this->persistenceManager->persistAll();
return $solr;
}
}

0 comments on commit aeb34f8

Please sign in to comment.