]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/dolphinquerytest.cpp
[search] Fix corner cases when using quotes in filenames
[dolphin.git] / src / tests / dolphinquerytest.cpp
1 /***************************************************************************
2 * Copyright (C) 2019 by Ismael Asensio <isma.af@gmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "search/dolphinquery.h"
21
22 #include <QTest>
23
24 #include <QJsonDocument>
25 #include <QJsonObject>
26 #include <QStringList>
27 #include <QUrl>
28 #include <QUrlQuery>
29
30 class DolphinSearchBoxTest : public QObject
31 {
32 Q_OBJECT
33
34 private slots:
35 void testBalooSearchParsing_data();
36 void testBalooSearchParsing();
37 };
38
39 /**
40 * Helper function to compose the baloo query URL used for searching
41 */
42 QUrl balooQueryUrl(const QString& searchString)
43 {
44 const QJsonObject jsonObject {
45 {"searchString", searchString}
46 };
47
48 const QJsonDocument doc(jsonObject);
49 const QString queryString = QString::fromUtf8(doc.toJson(QJsonDocument::Compact));
50
51 QUrlQuery urlQuery;
52 urlQuery.addQueryItem(QStringLiteral("json"), queryString);
53
54 QUrl searchUrl;
55 searchUrl.setScheme(QLatin1String("baloosearch"));
56 searchUrl.setQuery(urlQuery);
57
58 return searchUrl;
59 }
60
61 /**
62 * Defines the parameters for the test cases in testBalooSearchParsing()
63 */
64 void DolphinSearchBoxTest::testBalooSearchParsing_data()
65 {
66
67 QTest::addColumn<QUrl>("searchUrl");
68 QTest::addColumn<QString>("expectedText");
69 QTest::addColumn<QStringList>("expectedTerms");
70 QTest::addColumn<bool>("hasContent");
71 QTest::addColumn<bool>("hasFileName");
72
73 const QString text = QStringLiteral("abc");
74 const QString textS = QStringLiteral("abc xyz");
75 const QString textQ = QStringLiteral("\"abc xyz\"");
76 const QString textM = QStringLiteral("\"abc xyz\" tuv");
77
78 const QString filename = QStringLiteral("filename:\"%1\"").arg(text);
79 const QString filenameS = QStringLiteral("filename:\"%1\"").arg(textS);
80 const QString filenameQ = QStringLiteral("filename:\"%1\"").arg(textQ);
81 const QString filenameM = QStringLiteral("filename:\"%1\"").arg(textM);
82
83 const QString rating = QStringLiteral("rating>=2");
84 const QString modified = QStringLiteral("modified>=2019-08-07");
85
86 const QString tag = QStringLiteral("tag:tagA");
87 const QString tagS = QStringLiteral("tag:\"tagB with spaces\""); // in search url
88 const QString tagR = QStringLiteral("tag:tagB with spaces"); // in result term
89
90 // Test for "Content"
91 QTest::newRow("content") << balooQueryUrl(text) << text << QStringList() << true << false;
92 QTest::newRow("content/space") << balooQueryUrl(textS) << textS << QStringList() << true << false;
93 QTest::newRow("content/quoted") << balooQueryUrl(textQ) << textS << QStringList() << true << false;
94 QTest::newRow("content/empty") << balooQueryUrl("") << "" << QStringList() << false << false;
95 QTest::newRow("content/single_quote") << balooQueryUrl("\"") << "\"" << QStringList() << true << false;
96 QTest::newRow("content/double_quote") << balooQueryUrl("\"\"") << "" << QStringList() << false << false;
97
98 // Test for "FileName"
99 QTest::newRow("filename") << balooQueryUrl(filename) << text << QStringList() << false << true;
100 QTest::newRow("filename/space") << balooQueryUrl(filenameS) << textS << QStringList() << false << true;
101 QTest::newRow("filename/quoted") << balooQueryUrl(filenameQ) << textQ << QStringList() << false << true;
102 QTest::newRow("filename/mixed") << balooQueryUrl(filenameM) << textM << QStringList() << false << true;
103 QTest::newRow("filename/empty") << balooQueryUrl("filename:") << "" << QStringList() << false << false;
104 QTest::newRow("filename/single_quote") << balooQueryUrl("filename:\"") << "\"" << QStringList() << false << true;
105 QTest::newRow("filename/double_quote") << balooQueryUrl("filename:\"\"") << "" << QStringList() << false << false;
106
107 // Combined content and filename search
108 QTest::newRow("content+filename")
109 << balooQueryUrl(text + " " + filename)
110 << text + " " + filename << QStringList() << true << true;
111
112 QTest::newRow("content+filename/quoted")
113 << balooQueryUrl(textQ + " " + filenameQ)
114 << textS + " " + filenameQ << QStringList() << true << true;
115
116 // Test for rating
117 QTest::newRow("rating") << balooQueryUrl(rating) << "" << QStringList({rating}) << false << false;
118 QTest::newRow("rating+content") << balooQueryUrl(rating + " " + text) << text << QStringList({rating}) << true << false;
119 QTest::newRow("rating+filename") << balooQueryUrl(rating + " " + filename) << text << QStringList({rating}) << false << true;
120
121 // Test for modified date
122 QTest::newRow("modified") << balooQueryUrl(modified) << "" << QStringList({modified}) << false << false;
123 QTest::newRow("modified+content") << balooQueryUrl(modified + " " + text) << text << QStringList({modified}) << true << false;
124 QTest::newRow("modified+filename") << balooQueryUrl(modified + " " + filename) << text << QStringList({modified}) << false << true;
125
126 // Test for tags
127 QTest::newRow("tag") << balooQueryUrl(tag) << "" << QStringList({tag}) << false << false;
128 QTest::newRow("tag/space" ) << balooQueryUrl(tagS) << "" << QStringList({tagR}) << false << false;
129 QTest::newRow("tag/double") << balooQueryUrl(tag + " " + tagS) << "" << QStringList({tag, tagR}) << false << false;
130 QTest::newRow("tag+content") << balooQueryUrl(tag + " " + text) << text << QStringList({tag}) << true << false;
131 QTest::newRow("tag+filename") << balooQueryUrl(tag + " " + filename) << text << QStringList({tag}) << false << true;
132
133 // Combined search terms
134 QTest::newRow("searchTerms")
135 << balooQueryUrl(rating + " AND " + modified + " AND " + tag + " AND " + tagS)
136 << "" << QStringList({modified, rating, tag, tagR}) << false << false;
137
138 QTest::newRow("searchTerms+content")
139 << balooQueryUrl(rating + " AND " + modified + " " + text + " " + tag + " AND " + tagS)
140 << text << QStringList({modified, rating, tag, tagR}) << true << false;
141
142 QTest::newRow("searchTerms+filename")
143 << balooQueryUrl(rating + " AND " + modified + " " + filename + " " + tag + " AND " + tagS)
144 << text << QStringList({modified, rating, tag, tagR}) << false << true;
145
146 QTest::newRow("allTerms")
147 << balooQueryUrl(text + " " + filename + " " + rating + " AND " + modified + " AND " + tag)
148 << text + " " + filename << QStringList({modified, rating, tag}) << true << true;
149
150 QTest::newRow("allTerms/space")
151 << balooQueryUrl(textS + " " + filenameS + " " + rating + " AND " + modified + " AND " + tagS)
152 << textS + " " + filenameS << QStringList({modified, rating, tagR}) << true << true;
153 }
154
155 /**
156 * The test verifies whether the different terms search URL (e.g. "baloosearch:/") are
157 * properly handled by the searchbox, and only "user" or filename terms are added to the
158 * text bar of the searchbox.
159 */
160 void DolphinSearchBoxTest::testBalooSearchParsing()
161 {
162 QFETCH(QUrl, searchUrl);
163 QFETCH(QString, expectedText);
164 QFETCH(QStringList, expectedTerms);
165 QFETCH(bool, hasContent);
166 QFETCH(bool, hasFileName);
167
168 const DolphinQuery query = DolphinQuery::fromSearchUrl(searchUrl);
169
170 // Checkt that the URL is supported
171 QVERIFY(DolphinQuery::supportsScheme(searchUrl.scheme()));
172
173 // Check for parsed text (would be displayed on the input search bar)
174 QCOMPARE(query.text(), expectedText);
175
176 // Check for parsed search terms (would be displayed by the facetsWidget)
177 QStringList searchTerms = query.searchTerms();
178 searchTerms.sort();
179
180 QCOMPARE(searchTerms.count(), expectedTerms.count());
181 for (int i = 0; i < expectedTerms.count(); i++) {
182 QCOMPARE(searchTerms.at(i), expectedTerms.at(i));
183 }
184
185 // Check for filename and content detection
186 QCOMPARE(query.hasContentSearch(), hasContent);
187 QCOMPARE(query.hasFileName(), hasFileName);
188 }
189
190 QTEST_MAIN(DolphinSearchBoxTest)
191
192 #include "dolphinquerytest.moc"