diff --git a/lib/Search/PageContentProvider.php b/lib/Search/PageContentProvider.php index 840b981b4..cff172482 100644 --- a/lib/Search/PageContentProvider.php +++ b/lib/Search/PageContentProvider.php @@ -109,11 +109,6 @@ public function search(IUser $user, ISearchQuery $query): SearchResult { try { $collectiveRoot = $this->pageService->getCollectiveFolder($collective->getId(), $user->getUID()); $results = $this->indexedSearchService->searchCollective($collective, $query->getTerm()); - foreach ($results as $fileId => $fileData) { - $file = $collectiveRoot->getById($fileId); - $pages[$fileId] = reset($file); - $collectiveMap[$fileId] = $collective; - } } catch (FileSearchException|NotFoundException $e) { $this->logger->warning('Collectives file content search failed.', [ 'error' => $e->getMessage(), @@ -121,6 +116,13 @@ public function search(IUser $user, ISearchQuery $query): SearchResult { ]); continue; } + foreach ($results as $fileId => $fileData) { + $fileEntries = $collectiveRoot->getById($fileId); + if (!empty($fileEntries)) { + $pages[$fileId] = $fileEntries[0]; + $collectiveMap[$fileId] = $collective; + } + } } $highlighter = new Highlighter((new FileSearcher())->getTokenizer()); diff --git a/tests/Unit/Search/PageContentProviderTest.php b/tests/Unit/Search/PageContentProviderTest.php new file mode 100644 index 000000000..329a9e249 --- /dev/null +++ b/tests/Unit/Search/PageContentProviderTest.php @@ -0,0 +1,90 @@ +setId(123); + + /** @var IL10N&MockObject $l10n */ + $l10n = $this->createMock(IL10N::class); + /** @var IURLGenerator&MockObject $urlGenerator */ + $urlGenerator = $this->createMock(IURLGenerator::class); + /** @var CollectiveHelper&MockObject $collectiveHelper */ + $collectiveHelper = $this->createMock(CollectiveHelper::class); + $collectiveHelper->method('getCollectivesForUser') + ->willReturn([$collective]); + /** @var Folder&MockObject $folder */ + $folder = $this->createMock(Folder::class); + $folder->method('getById') + ->willReturn([]); + /** @var PageService&MockObject $pageService */ + $pageService = $this->createMock(PageService::class); + $pageService->method('getCollectiveFolder') + ->willReturn($folder); + /** @var SearchService&MockObject $indexedSearchService */ + $indexedSearchService = $this->createMock(SearchService::class); + $indexedSearchService->method('searchCollective') + ->willReturn(array(404 => 'fileData')); + /** @var LoggerInterface&MockObject $logger */ + $logger = $this->createMock(LoggerInterface::class); + /** @var IAppManager&MockObject $appManager */ + $appManager = $this->createMock(IAppManager::class); + $appManager->method('isEnabledForUser') + ->willReturn(true); + + $this->provider = new PageContentProvider( + $l10n, + $urlGenerator, + $collectiveHelper, + $pageService, + $indexedSearchService, + $logger, + $appManager + ); + } + + public function testId(): void { + $this->assertEquals('collectives-page-content', $this->provider->getId()); + } + + public function testSearchWithMissingFile(): void { + /** @var IUser&MockObject $user */ + $user = $this->createMock(IUser::class); + $user->method('getUID') + ->willReturn('jane'); + $query = new SearchQuery( + 'Search me!', + ISearchQuery::SORT_DATE_DESC, + SearchQuery::LIMIT_DEFAULT, + null, + 'collectives.' + ); + $response = json_encode($this->provider->search($user, $query)); + $result = json_decode($response, true); + + $this->assertEquals( + '[]', + json_encode($result['entries']) + ); + } +}