Commit ba720712 authored by Florian Schöppe's avatar Florian Schöppe Committed by Stefan Bürk
Browse files

[TASK] Add functional tests for indexed_search

Added tests for:
- indexing contents and words
- a basic search
- metaphone search

Resolves: #97732
Releases: main, 11.5, 10.4
Change-Id: Id56e09f129de4bd13092f584e97eedc9fd326b5c
parent 3d1b0a08
Pipeline #27769 passed with stages
in 20 minutes and 40 seconds
<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CMS\IndexedSearch\Tests\Functional\Fixtures\Configurations;
use TYPO3\CMS\IndexedSearch\Indexer;
class IndexerTestConfiguration
{
public static function getLoremIpsumIndexer(): Indexer
{
return self::getIndexer(
'Lorem Ipsum',
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut luctus fringilla tortor sit amet feugiat. Sed mattis semper sapien, in eleifend quam condimentum vel. Aliquam pellentesque feugiat ipsum sed posuere. Morbi pulvinar, eros at fermentum ullamcorper, mauris sem viverra eros, aliquet consequat nulla augue eu sem. Ut placerat, leo sed sagittis suscipit, magna lacus venenatis neque, quis venenatis neque lorem non leo. Sed ullamcorper lorem id ullamcorper commodo. Cras a hendrerit neque. Proin vehicula pretium neque, vitae feugiat justo consequat aliquam. Donec fringilla dolor ac fringilla scelerisque. Suspendisse condimentum egestas odio, vel rutrum neque aliquet sed. Phasellus in sapien quam. Nullam luctus hendrerit dignissim.'
);
}
private static function getIndexer(string $title, string $body): Indexer
{
$indexerConfig = [
'id' => 1,
'type' => 0,
'MP' => '',
'staticPageArguments' => null,
'sys_language_uid' => 0,
'gr_list' => '0,-1',
'recordUid' => null,
'freeIndexUid' => null,
'freeIndexSetId' => null,
'index_descrLgd' => 200,
'index_metatags' => true,
'index_externals' => false,
'mtime' => time(),
'crdate' => time(),
'content' =>
"<html>
<head>
<title>$title</title>
</head>
<body>
$body
</body>
</html>",
'indexedDocTitle' => '',
];
$indexer = new Indexer();
$indexer->init($indexerConfig);
$indexer->indexerConfig['debugMode'] = false;
return $indexer;
}
}
<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CMS\IndexedSearch\Tests\Functional;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Context\UserAspect;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\IndexedSearch\Domain\Repository\IndexSearchRepository;
use TYPO3\CMS\IndexedSearch\Tests\Functional\Fixtures\Configurations\IndexerTestConfiguration;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
/**
* @covers \TYPO3\CMS\IndexedSearch\Domain\Repository\IndexSearchRepository
*/
class IndexSearchRepositoryTest extends FunctionalTestCase
{
protected array $coreExtensionsToLoad = [
'indexed_search',
];
/**
* Indexes a "Lorem Ipsum"-page
* and adds a UserAspect to the context providing a "grlist" used in
* @see IndexSearchRepository::$frontendUserGroupList.
*/
protected function setUp(): void
{
/** @noinspection PhpUnhandledExceptionInspection */
parent::setUp();
IndexerTestConfiguration::getLoremIpsumIndexer()->indexTypo3PageContent();
GeneralUtility::makeInstance(Context::class)->setAspect('frontend.user', new UserAspect(null, [0, -1]));
}
/**
* @test
*/
public function doSearchReturnsLoremIpsumResults(): void
{
$searchRepository = $this->getSearchRepository();
$searchResults = $searchRepository->doSearch([['sword' => 'lorem']], -1);
self::assertTrue(isset($searchResults['resultRows']));
self::assertCount(1, $searchResults['resultRows']);
self::assertStringContainsStringIgnoringCase('lorem', $searchResults['resultRows'][0]['item_description']);
}
/**
* @test
*/
public function doSearchProperlyQuotesSearchWord(): void
{
$searchRepository = $this->getSearchRepository();
$searchResults = $searchRepository->doSearch([['sword' => 'l%rem']], -1);
self::assertFalse(isset($searchResults['resultRows']));
}
/**
* @test
*/
public function doSearchReturnsLurimIpasomResultsWithMetaphoneSearch(): void
{
$searchRepository = $this->getSearchRepository(10);
$searchResults = $searchRepository->doSearch([['sword' => 'lurim']], -1);
self::assertTrue(isset($searchResults['resultRows']));
self::assertCount(1, $searchResults['resultRows']);
self::assertStringContainsStringIgnoringCase('lorem', $searchResults['resultRows'][0]['item_description']);
}
private function getSearchRepository($searchType = 1): IndexSearchRepository
{
$searchRepository = GeneralUtility::makeInstance(IndexSearchRepository::class);
$searchRepositoryDefaultOptions = [
'defaultOperand' => 0,
'sections' => 0,
'mediaType' => -1,
'sortOrder' => 'rank_flag',
'languageUid' => 'current',
'sortDesc' => 1,
'searchType' => $searchType,
'extResume' => 1,
];
$searchRepository->initialize([], $searchRepositoryDefaultOptions, [], -1);
return $searchRepository;
}
}
<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CMS\IndexedSearch\Tests\Functional;
use TYPO3\CMS\IndexedSearch\Tests\Functional\Fixtures\Configurations\IndexerTestConfiguration;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
/**
* @covers \TYPO3\CMS\IndexedSearch\Indexer
*/
class IndexerTest extends FunctionalTestCase
{
protected array $coreExtensionsToLoad = [
'indexed_search',
];
/**
* @test
*/
public function indexerIndexesLoremIpsumContent(): void
{
IndexerTestConfiguration::getLoremIpsumIndexer()->indexTypo3PageContent();
$indexWords = $this->findAllIndexWords();
self::assertCount(69, $indexWords);
$indexWordsLorem = array_filter($indexWords, static fn ($indexWord) => $indexWord['baseword'] === 'lorem');
$indexWordLorem = array_shift($indexWordsLorem);
self::assertNotNull($indexWordLorem);
$indexPhashes = $this->findAllIndexPhash();
self::assertCount(1, $indexPhashes);
$indexRels = $this->findAllIndexRel();
self::assertCount(69, $indexRels);
$indexRelsLorem = array_filter($indexRels, static fn ($indexRel) => $indexRel['wid'] === $indexWordLorem['wid']);
$indexRelLorem = array_shift($indexRelsLorem);
self::assertSame(4, $indexRelLorem['count']);
self::assertSame(0, $indexRelLorem['first']);
self::assertSame(12075, $indexRelLorem['freq']);
self::assertSame(128, $indexRelLorem['flags']);
$indexPhash = $indexPhashes[0]['phash'];
$indexRelsPhash = array_unique(array_column($indexRels, 'phash'))[0];
self::assertSame($indexPhash, $indexRelsPhash);
}
private function findAllIndexWords(): array
{
$queryBuilder = $this->getConnectionPool()->getQueryBuilderForTable('index_words');
return $queryBuilder
->select('*')
->from('index_words')
->executeQuery()
->fetchAllAssociative();
}
private function findAllIndexPhash(): array
{
$queryBuilder = $this->getConnectionPool()->getQueryBuilderForTable('index_phash');
return $queryBuilder
->select('*')
->from('index_phash')
->executeQuery()
->fetchAllAssociative();
}
private function findAllIndexRel(): array
{
$queryBuilder = $this->getConnectionPool()->getQueryBuilderForTable('index_rel');
return $queryBuilder
->select('*')
->from('index_rel')
->executeQuery()
->fetchAllAssociative();
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment