File indexing completed on 2024-11-10 04:40:12
0001 /* 0002 SPDX-FileCopyrightText: 2009 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "collection.h" 0008 #include "collectionpathresolver.h" 0009 #include "control.h" 0010 #include "itemdeletejob.h" 0011 #include "itemfetchjob.h" 0012 #include "itemmodifyjob.h" 0013 #include "monitor.h" 0014 #include "qtest_akonadi.h" 0015 #include "resourceselectjob_p.h" 0016 #include "tagcreatejob.h" 0017 #include "transactionjobs.h" 0018 0019 #include <QObject> 0020 0021 #include <sys/types.h> 0022 0023 using namespace Akonadi; 0024 0025 class ItemDeleteTest : public QObject 0026 { 0027 Q_OBJECT 0028 private Q_SLOTS: 0029 void initTestCase() 0030 { 0031 AkonadiTest::checkTestIsIsolated(); 0032 Control::start(); 0033 } 0034 0035 void testIllegalDelete() 0036 { 0037 auto djob = new ItemDeleteJob(Item(INT_MAX), this); 0038 QVERIFY(!djob->exec()); 0039 0040 // make sure a failed delete doesn't leave a transaction open (the kpilot bug) 0041 auto tjob = new TransactionRollbackJob(this); 0042 QVERIFY(!tjob->exec()); 0043 } 0044 0045 void testDelete() 0046 { 0047 auto monitor = AkonadiTest::getTestMonitor(); 0048 QSignalSpy spy(monitor.get(), &Monitor::itemsRemoved); 0049 0050 auto fjob = new ItemFetchJob(Item(1), this); 0051 AKVERIFYEXEC(fjob); 0052 QCOMPARE(fjob->items().count(), 1); 0053 0054 auto djob = new ItemDeleteJob(Item(1), this); 0055 AKVERIFYEXEC(djob); 0056 0057 fjob = new ItemFetchJob(Item(1), this); 0058 QVERIFY(!fjob->exec()); 0059 0060 QTRY_COMPARE(spy.count(), 1); 0061 auto items = spy.at(0).at(0).value<Akonadi::Item::List>(); 0062 QCOMPARE(items.count(), 1); 0063 QCOMPARE(items.at(0).id(), 1); 0064 QVERIFY(items.at(0).parentCollection().isValid()); 0065 } 0066 0067 void testDeleteFromUnselectedCollection() 0068 { 0069 auto monitor = AkonadiTest::getTestMonitor(); 0070 QSignalSpy spy(monitor.get(), &Monitor::itemsRemoved); 0071 0072 const QString path = QLatin1StringView("res1") + CollectionPathResolver::pathDelimiter() + QLatin1StringView("foo"); 0073 auto rjob = new CollectionPathResolver(path, this); 0074 AKVERIFYEXEC(rjob); 0075 0076 auto fjob = new ItemFetchJob(Collection(rjob->collection()), this); 0077 AKVERIFYEXEC(fjob); 0078 0079 const Item::List items = fjob->items(); 0080 QVERIFY(!items.isEmpty()); 0081 0082 fjob = new ItemFetchJob(items[0], this); 0083 AKVERIFYEXEC(fjob); 0084 QCOMPARE(fjob->items().count(), 1); 0085 0086 auto djob = new ItemDeleteJob(items[0], this); 0087 AKVERIFYEXEC(djob); 0088 0089 fjob = new ItemFetchJob(items[0], this); 0090 QVERIFY(!fjob->exec()); 0091 0092 QTRY_COMPARE(spy.count(), 1); 0093 auto ntfItems = spy.at(0).at(0).value<Akonadi::Item::List>(); 0094 QCOMPARE(ntfItems.count(), 1); 0095 QCOMPARE(ntfItems.at(0).id(), items[0].id()); 0096 QVERIFY(ntfItems.at(0).parentCollection().isValid()); 0097 } 0098 0099 void testRidDelete() 0100 { 0101 auto monitor = AkonadiTest::getTestMonitor(); 0102 QSignalSpy spy(monitor.get(), &Monitor::itemsRemoved); 0103 0104 { 0105 auto select = new ResourceSelectJob(QStringLiteral("akonadi_knut_resource_0")); 0106 AKVERIFYEXEC(select); 0107 } 0108 const Collection col(AkonadiTest::collectionIdFromPath(QStringLiteral("res1/foo"))); 0109 QVERIFY(col.isValid()); 0110 0111 Item i; 0112 i.setRemoteId(QStringLiteral("C")); 0113 0114 auto fjob = new ItemFetchJob(i, this); 0115 fjob->setCollection(col); 0116 AKVERIFYEXEC(fjob); 0117 auto items = fjob->items(); 0118 QCOMPARE(items.count(), 1); 0119 0120 auto djob = new ItemDeleteJob(i, this); 0121 AKVERIFYEXEC(djob); 0122 0123 QTRY_COMPARE(spy.count(), 1); 0124 auto ntfItems = spy.at(0).at(0).value<Akonadi::Item::List>(); 0125 QCOMPARE(ntfItems.count(), 1); 0126 QCOMPARE(ntfItems.at(0).id(), items[0].id()); 0127 QVERIFY(ntfItems.at(0).parentCollection().isValid()); 0128 0129 fjob = new ItemFetchJob(i, this); 0130 fjob->setCollection(col); 0131 QVERIFY(!fjob->exec()); 0132 { 0133 auto select = new ResourceSelectJob(QStringLiteral("")); 0134 AKVERIFYEXEC(select); 0135 } 0136 } 0137 0138 void testTagDelete() 0139 { 0140 auto monitor = AkonadiTest::getTestMonitor(); 0141 QSignalSpy spy(monitor.get(), &Monitor::itemsRemoved); 0142 0143 // Create tag 0144 Tag tag; 0145 tag.setName(QStringLiteral("Tag1")); 0146 tag.setGid("Tag1"); 0147 auto tjob = new TagCreateJob(tag, this); 0148 AKVERIFYEXEC(tjob); 0149 tag = tjob->tag(); 0150 0151 const Collection col(AkonadiTest::collectionIdFromPath(QStringLiteral("res1/foo"))); 0152 QVERIFY(col.isValid()); 0153 0154 Item i; 0155 i.setRemoteId(QStringLiteral("D")); 0156 0157 auto fjob = new ItemFetchJob(i, this); 0158 fjob->setCollection(col); 0159 AKVERIFYEXEC(fjob); 0160 QCOMPARE(fjob->items().count(), 1); 0161 0162 i = fjob->items().first(); 0163 i.setTag(tag); 0164 auto mjob = new ItemModifyJob(i, this); 0165 AKVERIFYEXEC(mjob); 0166 0167 // Delete the tagged item 0168 auto djob = new ItemDeleteJob(tag, this); 0169 AKVERIFYEXEC(djob); 0170 0171 QTRY_COMPARE(spy.count(), 1); 0172 auto ntfItems = spy.at(0).at(0).value<Akonadi::Item::List>(); 0173 QCOMPARE(ntfItems.count(), 1); 0174 QCOMPARE(ntfItems.at(0).id(), i.id()); 0175 QVERIFY(ntfItems.at(0).parentCollection().isValid()); 0176 0177 // Try to fetch the item again, there should be none 0178 fjob = new ItemFetchJob(i, this); 0179 QVERIFY(!fjob->exec()); 0180 } 0181 0182 void testCollectionDelete() 0183 { 0184 auto monitor = AkonadiTest::getTestMonitor(); 0185 QSignalSpy spy(monitor.get(), &Monitor::itemsRemoved); 0186 0187 const Collection col(AkonadiTest::collectionIdFromPath(QStringLiteral("res1/foo"))); 0188 auto fjob = new ItemFetchJob(col, this); 0189 AKVERIFYEXEC(fjob); 0190 auto items = fjob->items(); 0191 QVERIFY(items.count() > 0); 0192 0193 // delete from non-empty collection 0194 auto djob = new ItemDeleteJob(col, this); 0195 AKVERIFYEXEC(djob); 0196 0197 QTRY_COMPARE(spy.count(), 1); 0198 auto ntfItems = spy.at(0).at(0).value<Akonadi::Item::List>(); 0199 QCOMPARE(ntfItems.count(), items.count()); 0200 if (!ntfItems.isEmpty()) { 0201 QVERIFY(ntfItems.at(0).parentCollection().isValid()); 0202 } 0203 0204 fjob = new ItemFetchJob(col, this); 0205 AKVERIFYEXEC(fjob); 0206 QCOMPARE(fjob->items().count(), 0); 0207 0208 // delete from empty collection 0209 djob = new ItemDeleteJob(col, this); 0210 QVERIFY(!djob->exec()); // error: no items found 0211 0212 fjob = new ItemFetchJob(col, this); 0213 AKVERIFYEXEC(fjob); 0214 QCOMPARE(fjob->items().count(), 0); 0215 } 0216 }; 0217 0218 QTEST_AKONADIMAIN(ItemDeleteTest) 0219 0220 #include "itemdeletetest.moc"