From d1fab61fe84f5c85b8a6872cf956f00bbbcab358 Mon Sep 17 00:00:00 2001 From: Jonathan White Date: Mon, 22 Oct 2018 17:13:25 -0400 Subject: [PATCH] Fix advanced search issues --- src/core/EntrySearcher.cpp | 15 ++++----- src/core/EntrySearcher.h | 2 +- tests/TestEntrySearcher.cpp | 66 +++++++++++++++++++++++-------------- tests/TestEntrySearcher.h | 2 +- 4 files changed, 50 insertions(+), 35 deletions(-) diff --git a/src/core/EntrySearcher.cpp b/src/core/EntrySearcher.cpp index c22b32ea8b..6614ab4637 100644 --- a/src/core/EntrySearcher.cpp +++ b/src/core/EntrySearcher.cpp @@ -28,17 +28,14 @@ EntrySearcher::EntrySearcher(bool caseSensitive) { } -QList EntrySearcher::search(const QString& searchString, const Group* group) +QList EntrySearcher::search(const QString& searchString, const Group* baseGroup, bool forceSearch) { - QList results; - - if (group->resolveSearchingEnabled()) { - results.append(searchEntries(searchString, group->entries())); - } + Q_ASSERT(baseGroup); - for (Group* childGroup : group->children()) { - if (childGroup->resolveSearchingEnabled()) { - results.append(searchEntries(searchString, childGroup->entries())); + QList results; + for (const auto group : baseGroup->groupsRecursive(true)) { + if (forceSearch || group->resolveSearchingEnabled()) { + results.append(searchEntries(searchString, group->entries())); } } diff --git a/src/core/EntrySearcher.h b/src/core/EntrySearcher.h index e68a03d94b..ec71a7ce1e 100644 --- a/src/core/EntrySearcher.h +++ b/src/core/EntrySearcher.h @@ -30,7 +30,7 @@ class EntrySearcher public: explicit EntrySearcher(bool caseSensitive = false); - QList search(const QString& searchString, const Group* group); + QList search(const QString& searchString, const Group* baseGroup, bool forceSearch = false); QList searchEntries(const QString& searchString, const QList& entries); void setCaseSensitive(bool state); diff --git a/tests/TestEntrySearcher.cpp b/tests/TestEntrySearcher.cpp index 43ef77bf4d..51be468b55 100644 --- a/tests/TestEntrySearcher.cpp +++ b/tests/TestEntrySearcher.cpp @@ -22,23 +22,32 @@ QTEST_GUILESS_MAIN(TestEntrySearcher) void TestEntrySearcher::initTestCase() { - m_groupRoot = new Group(); + m_rootGroup = new Group(); } void TestEntrySearcher::cleanupTestCase() { - delete m_groupRoot; + delete m_rootGroup; } void TestEntrySearcher::testSearch() { + /** + * Root + * - group1 (search disabled) + * - group11 + * - group2 + * - group21 + * - group211 + * - group2111 + */ Group* group1 = new Group(); Group* group2 = new Group(); Group* group3 = new Group(); - group1->setParent(m_groupRoot); - group2->setParent(m_groupRoot); - group3->setParent(m_groupRoot); + group1->setParent(m_rootGroup); + group2->setParent(m_rootGroup); + group3->setParent(m_rootGroup); Group* group11 = new Group(); @@ -55,15 +64,15 @@ void TestEntrySearcher::testSearch() group1->setSearchingEnabled(Group::Disable); Entry* eRoot = new Entry(); - eRoot->setNotes("test search term test"); - eRoot->setGroup(m_groupRoot); + eRoot->setTitle("test search term test"); + eRoot->setGroup(m_rootGroup); Entry* eRoot2 = new Entry(); eRoot2->setNotes("test term test"); - eRoot2->setGroup(m_groupRoot); + eRoot2->setGroup(m_rootGroup); Entry* e1 = new Entry(); - e1->setNotes("test search term test"); + e1->setUsername("test search term test"); e1->setGroup(group1); Entry* e11 = new Entry(); @@ -71,28 +80,36 @@ void TestEntrySearcher::testSearch() e11->setGroup(group11); Entry* e2111 = new Entry(); - e2111->setNotes("test search term test"); + e2111->setTitle("test search term test"); e2111->setGroup(group2111); Entry* e2111b = new Entry(); e2111b->setNotes("test search test"); + e2111b->setPassword("testpass"); e2111b->setGroup(group2111); Entry* e3 = new Entry(); - e3->setNotes("test search term test"); + e3->setUrl("test search term test"); e3->setGroup(group3); Entry* e3b = new Entry(); - e3b->setNotes("test search test"); + e3b->setTitle("test search test"); + e3b->setPassword("realpass"); e3b->setGroup(group3); - m_searchResult = m_entrySearcher.search("search term", m_groupRoot); - QCOMPARE(m_searchResult.count(), 2); + m_searchResult = m_entrySearcher.search("search", m_rootGroup); + QCOMPARE(m_searchResult.count(), 5); + + m_searchResult = m_entrySearcher.search("search term", m_rootGroup); + QCOMPARE(m_searchResult.count(), 3); m_searchResult = m_entrySearcher.search("search term", group211); QCOMPARE(m_searchResult.count(), 1); - // Parent group disabled search + m_searchResult = m_entrySearcher.search("password:testpass", m_rootGroup); + QCOMPARE(m_searchResult.count(), 1); + + // Parent group has search disabled m_searchResult = m_entrySearcher.search("search term", group11); QCOMPARE(m_searchResult.count(), 0); } @@ -102,38 +119,39 @@ void TestEntrySearcher::testAndConcatenationInSearch() Entry* entry = new Entry(); entry->setNotes("abc def ghi"); entry->setTitle("jkl"); - entry->setGroup(m_groupRoot); + entry->setGroup(m_rootGroup); - m_searchResult = m_entrySearcher.search("", m_groupRoot); + m_searchResult = m_entrySearcher.search("", m_rootGroup); QCOMPARE(m_searchResult.count(), 1); - m_searchResult = m_entrySearcher.search("def", m_groupRoot); + m_searchResult = m_entrySearcher.search("def", m_rootGroup); QCOMPARE(m_searchResult.count(), 1); - m_searchResult = m_entrySearcher.search(" abc ghi ", m_groupRoot); + m_searchResult = m_entrySearcher.search(" abc ghi ", m_rootGroup); QCOMPARE(m_searchResult.count(), 1); - m_searchResult = m_entrySearcher.search("ghi ef", m_groupRoot); + m_searchResult = m_entrySearcher.search("ghi ef", m_rootGroup); QCOMPARE(m_searchResult.count(), 1); - m_searchResult = m_entrySearcher.search("abc ef xyz", m_groupRoot); + m_searchResult = m_entrySearcher.search("abc ef xyz", m_rootGroup); QCOMPARE(m_searchResult.count(), 0); - m_searchResult = m_entrySearcher.search("abc kl", m_groupRoot); + m_searchResult = m_entrySearcher.search("abc kl", m_rootGroup); QCOMPARE(m_searchResult.count(), 1); } void TestEntrySearcher::testAllAttributesAreSearched() { Entry* entry = new Entry(); - entry->setGroup(m_groupRoot); + entry->setGroup(m_rootGroup); entry->setTitle("testTitle"); entry->setUsername("testUsername"); entry->setUrl("testUrl"); entry->setNotes("testNote"); - m_searchResult = m_entrySearcher.search("testTitle testUsername testUrl testNote", m_groupRoot); + // Default is to AND all terms together + m_searchResult = m_entrySearcher.search("testTitle testUsername testUrl testNote", m_rootGroup); QCOMPARE(m_searchResult.count(), 1); } diff --git a/tests/TestEntrySearcher.h b/tests/TestEntrySearcher.h index 095de4e6c7..f385d618e0 100644 --- a/tests/TestEntrySearcher.h +++ b/tests/TestEntrySearcher.h @@ -37,7 +37,7 @@ private slots: void testSearchTermParser(); private: - Group* m_groupRoot; + Group* m_rootGroup; EntrySearcher m_entrySearcher; QList m_searchResult; };