summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSean Molenaar <sean@seanmolenaar.eu>2021-04-10 10:52:34 +0200
committerBenjamin Brahmer <info@b-brahmer.de>2021-04-10 15:00:30 +0200
commit15244f6347d73c5908a813a93588bfdc152b4376 (patch)
treed464a49a8e9c0938b3f38b150e5fb312c4d7e628
parent6e845124df1d9238d28875c8f745868be1f765b7 (diff)
downloadnextcloud-news-rebase/fix/folder_search.tar.gz
nextcloud-news-rebase/fix/folder_search.tar.xz
Search: Fix and test folder searchrebase/fix/folder_search
Signed-off-by: Sean Molenaar <sean@seanmolenaar.eu>
-rw-r--r--AUTHORS.md3
-rw-r--r--CHANGELOG.md1
-rw-r--r--lib/Search/FolderSearchProvider.php17
-rw-r--r--tests/Unit/Search/FolderSearchProviderTest.php132
4 files changed, 145 insertions, 8 deletions
diff --git a/AUTHORS.md b/AUTHORS.md
index 56f3aceb1..e6d7dca80 100644
--- a/AUTHORS.md
+++ b/AUTHORS.md
@@ -16,10 +16,10 @@
* [Bernhard Posselt](mailto:bep@foryouandyourcustomers.com)
* [Thomas Müller](mailto:thomas.mueller@tmit.eu)
* [Hoàng Đức Hiếu](mailto:hdhoang@zahe.me)
+* [rakekniven](mailto:mark.ziegler@rakekniven.de)
* [Daniel Opitz](mailto:git@copynpaste.de)
* [Marco Nassabain](mailto:marco.nassabain@hotmail.com)
* [Sean Molenaar](mailto:sean@m2mobi.com)
-* [rakekniven](mailto:mark.ziegler@rakekniven.de)
* [David-Development](mailto:david-dev@live.de)
* [IBBoard](mailto:dev@ibboard.co.uk)
* [Koen Martens](mailto:gmc@sonologic.nl)
@@ -79,6 +79,7 @@
* [Carlos Silva](mailto:r3pek@r3pek.org)
* [Cesar Enrique Garcia Dabo](mailto:enrique@engarda.org)
* [Chris Aumann](mailto:me@chr4.org)
+* [Chris Danser](mailto:skiingwiz@gmail.com)
* [Chris Noxz](mailto:chris@noxz.tech)
* [Christoph Wurst](mailto:ChristophWurst@users.noreply.github.com)
* [Clemens](mailto:clemens@clemensknost.de)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 349e3e9a6..cf5f29691 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,7 @@ The format is almost based on [Keep a Changelog](https://keepachangelog.com/en/1
### Fixed
- Check category label for null (#1282)
+- Do not return non-matching search items
## [15.4.0-beta3] - 2021-04-03
### Fixed
diff --git a/lib/Search/FolderSearchProvider.php b/lib/Search/FolderSearchProvider.php
index 1d961a699..3e8126245 100644
--- a/lib/Search/FolderSearchProvider.php
+++ b/lib/Search/FolderSearchProvider.php
@@ -49,7 +49,7 @@ class FolderSearchProvider implements IProvider
public function getOrder(string $route, array $routeParameters): int
{
- if (strpos($route, Application::NAME . '.') === 0) {
+ if ($route === 'news.view.index') {
// Active app, prefer my results
return -1;
}
@@ -59,19 +59,22 @@ class FolderSearchProvider implements IProvider
public function search(IUser $user, ISearchQuery $query): SearchResult
{
- $term = $query->getTerm();
- $list = array_map(function (Folder $folder) use ($term): ?SearchResultEntry {
- if (strpos($folder->getName(), $term) === false) {
- return null;
+ $list = [];
+ $term = strtolower($query->getTerm());
+
+ foreach ($this->service->findAllForUser($user->getUID()) as $folder) {
+ if (strpos(strtolower($folder->getName()), $term) === false) {
+ continue;
}
- return new SearchResultEntry(
+ $list[] = new SearchResultEntry(
$this->urlGenerator->imagePath('core', 'filetypes/folder.svg'),
$folder->getName(),
'',
$this->urlGenerator->linkToRoute('news.view.index') . '#/items/folders/' . $folder->getId()
);
- }, $this->service->findAllForUser($user->getUID()));
+ }
+
return SearchResult::complete($this->l10n->t('News'), $list);
}
}
diff --git a/tests/Unit/Search/FolderSearchProviderTest.php b/tests/Unit/Search/FolderSearchProviderTest.php
new file mode 100644
index 000000000..b7aff7587
--- /dev/null
+++ b/tests/Unit/Search/FolderSearchProviderTest.php
@@ -0,0 +1,132 @@
+<?php
+
+namespace OCA\News\Search;
+
+use OCA\News\Db\Folder;
+use OCA\News\Service\FeedServiceV2;
+use OCA\News\Service\FolderServiceV2;
+use OCA\News\Service\OpmlService;
+use OCA\News\Utility\OPMLExporter;
+use OCP\IL10N;
+use OCP\IURLGenerator;
+use OCP\IUser;
+use OCP\Search\ISearchQuery;
+use PHPUnit\Framework\TestCase;
+
+class FolderSearchProviderTest extends TestCase
+{
+
+ /**
+ * @var \PHPUnit\Framework\MockObject\MockObject|FolderServiceV2
+ */
+ private $folderService;
+
+ /**
+ * @var \PHPUnit\Framework\MockObject\MockObject|IL10N
+ */
+ private $l10n;
+
+ /**
+ * @var \PHPUnit\Framework\MockObject\MockObject|IURLGenerator
+ */
+ private $generator;
+
+ /**
+ * @var FolderSearchProvider
+ */
+ private $class;
+
+ protected function setUp(): void
+ {
+ $this->l10n = $this->getMockBuilder(IL10N::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->generator = $this->getMockBuilder(IURLGenerator::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->folderService = $this->getMockBuilder(FolderServiceV2::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $this->class = new FolderSearchProvider(
+ $this->l10n,
+ $this->generator,
+ $this->folderService
+ );
+ }
+
+ public function testGetId()
+ {
+ $this->assertSame('news_folder', $this->class->getId());
+ }
+
+ public function testGetName()
+ {
+ $this->l10n->expects($this->once())
+ ->method('t')
+ ->with('News folders')
+ ->willReturnArgument(0);
+
+ $this->assertSame('News folders', $this->class->getName());
+ }
+
+ public function testGetOrderExternal()
+ {
+ $this->assertSame(55, $this->class->getOrder('contacts.Page.index', []));
+ }
+
+ public function testGetOrderInternal()
+ {
+ $this->assertSame(-1, $this->class->getOrder('news.view.index', []));
+ }
+
+ public function testSearch()
+ {
+ $user = $this->getMockBuilder(IUser::class)
+ ->getMock();
+ $query = $this->getMockBuilder(ISearchQuery::class)
+ ->getMock();
+
+ $user->expects($this->once())
+ ->method('getUID')
+ ->willReturn('user');
+
+ $query->expects($this->once())
+ ->method('getTerm')
+ ->willReturn('Term');
+
+ $folders = [
+ Folder::fromRow(['id' => 1,'name' => 'some_tErm']),
+ Folder::fromRow(['id' => 2,'name' => 'nothing'])
+ ];
+
+ $this->folderService->expects($this->once())
+ ->method('findAllForUser')
+ ->with('user')
+ ->willReturn($folders);
+
+ $this->l10n->expects($this->once())
+ ->method('t')
+ ->with('News')
+ ->willReturnArgument(0);
+
+ $this->generator->expects($this->once())
+ ->method('imagePath')
+ ->with('core', 'filetypes/folder.svg')
+ ->willReturn('folderpath.svg');
+
+ $this->generator->expects($this->once())
+ ->method('linkToRoute')
+ ->with('news.view.index')
+ ->willReturn('/news');
+
+
+ $result = $this->class->search($user, $query)->jsonSerialize();
+ $entry = $result['entries'][0]->jsonSerialize();
+ $this->assertSame('News', $result['name']);
+ $this->assertSame('some_tErm', $entry['title']);
+ $this->assertSame('folderpath.svg', $entry['thumbnailUrl']);
+ $this->assertSame('', $entry['subline']);
+ $this->assertSame('/news#/items/folders/1', $entry['resourceUrl']);
+ }
+}