File indexing completed on 2024-11-10 04:40:15

0001 /*
0002     SPDX-FileCopyrightText: 2007 Volker Krause <vkrause@kde.org>
0003     SPDX-FileCopyrightText: 2014 Daniel Vrátil <dvratil@redhat.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "searchjobtest.h"
0009 #include "qtest_akonadi.h"
0010 
0011 #include "collection.h"
0012 #include "collectiondeletejob.h"
0013 #include "collectionfetchjob.h"
0014 #include "collectionmodifyjob.h"
0015 #include "collectionutils.h"
0016 #include "itemfetchjob.h"
0017 #include "persistentsearchattribute.h"
0018 #include "searchcreatejob.h"
0019 #include "searchquery.h"
0020 
0021 QTEST_AKONADIMAIN(SearchJobTest)
0022 
0023 using namespace Akonadi;
0024 
0025 void SearchJobTest::initTestCase()
0026 {
0027     AkonadiTest::checkTestIsIsolated();
0028 }
0029 
0030 void SearchJobTest::testCreateDeleteSearch()
0031 {
0032     Akonadi::SearchQuery query;
0033     query.addTerm(Akonadi::SearchTerm(QStringLiteral("plugin"), 1));
0034     query.addTerm(Akonadi::SearchTerm(QStringLiteral("resource"), 2));
0035     query.addTerm(Akonadi::SearchTerm(QStringLiteral("plugin"), 3));
0036     query.addTerm(Akonadi::SearchTerm(QStringLiteral("resource"), 4));
0037 
0038     // Create collection
0039     SearchCreateJob *create = new SearchCreateJob(QStringLiteral("search123456"), query, this);
0040     create->setRemoteSearchEnabled(false);
0041     AKVERIFYEXEC(create);
0042     const Collection created = create->createdCollection();
0043     QVERIFY(created.isValid());
0044 
0045     // Fetch "Search" collection, check the search collection has been created
0046     CollectionFetchJob *list = new CollectionFetchJob(Collection(1), CollectionFetchJob::Recursive, this);
0047     AKVERIFYEXEC(list);
0048     const Collection::List cols = list->collections();
0049     Collection col;
0050     for (const auto &c : cols) {
0051         if (c.name() == QLatin1StringView("search123456")) {
0052             col = c;
0053             break;
0054         }
0055     }
0056     QVERIFY(col == created);
0057     QCOMPARE(col.parentCollection().id(), 1LL);
0058     QVERIFY(col.isVirtual());
0059 
0060     // Fetch items in the search collection, check whether they are there
0061     ItemFetchJob *fetch = new ItemFetchJob(created, this);
0062     AKVERIFYEXEC(fetch);
0063     const Item::List items = fetch->items();
0064     QCOMPARE(items.count(), 2);
0065 
0066     CollectionDeleteJob *delJob = new CollectionDeleteJob(col, this);
0067     AKVERIFYEXEC(delJob);
0068 }
0069 
0070 void SearchJobTest::testModifySearch()
0071 {
0072     Akonadi::SearchQuery query;
0073     query.addTerm(Akonadi::SearchTerm(QStringLiteral("plugin"), 1));
0074     query.addTerm(Akonadi::SearchTerm(QStringLiteral("resource"), 2));
0075 
0076     // make sure there is a virtual collection
0077     SearchCreateJob *create = new SearchCreateJob(QStringLiteral("search123456"), query, this);
0078     AKVERIFYEXEC(create);
0079     Collection created = create->createdCollection();
0080     QVERIFY(created.isValid());
0081     QVERIFY(created.hasAttribute<PersistentSearchAttribute>());
0082 
0083     auto attr = created.attribute<PersistentSearchAttribute>();
0084     QVERIFY(!attr->isRecursive());
0085     QVERIFY(!attr->isRemoteSearchEnabled());
0086     QCOMPARE(attr->queryCollections(), QList<qint64>{0});
0087     const QString oldQueryString = attr->queryString();
0088 
0089     // Change the attributes
0090     attr->setRecursive(true);
0091     attr->setRemoteSearchEnabled(true);
0092     attr->setQueryCollections(QList<qint64>{1});
0093     Akonadi::SearchQuery newQuery;
0094     newQuery.addTerm(Akonadi::SearchTerm(QStringLiteral("plugin"), 3));
0095     newQuery.addTerm(Akonadi::SearchTerm(QStringLiteral("resource"), 4));
0096     attr->setQueryString(QString::fromUtf8(newQuery.toJSON()));
0097 
0098     auto modify = new CollectionModifyJob(created, this);
0099     AKVERIFYEXEC(modify);
0100 
0101     auto fetch = new CollectionFetchJob(created, CollectionFetchJob::Base, this);
0102     AKVERIFYEXEC(fetch);
0103     QCOMPARE(fetch->collections().size(), 1);
0104 
0105     const auto col = fetch->collections().first();
0106     QVERIFY(col.hasAttribute<PersistentSearchAttribute>());
0107     attr = col.attribute<PersistentSearchAttribute>();
0108 
0109     QVERIFY(attr->isRecursive());
0110     QVERIFY(attr->isRemoteSearchEnabled());
0111     QCOMPARE(attr->queryCollections(), QList<qint64>{1});
0112     QVERIFY(attr->queryString() != oldQueryString);
0113 
0114     auto delJob = new CollectionDeleteJob(col, this);
0115     AKVERIFYEXEC(delJob);
0116 }
0117 
0118 #include "moc_searchjobtest.cpp"