1 /*
2  * SPDX-FileCopyrightText: 2012 Christian Mollekopf <mollekopf@kolabsys.com>
3  *
4  * SPDX-License-Identifier: LGPL-3.0-or-later
5  */
6 
7 #include "kcalconversiontest.h"
8 #include <contact.h>
9 
10 #include <KCalendarCore/Recurrence>
11 #include <KContacts/Addressee>
12 #include <QTest>
13 #include <kolabcontact.h>
14 
15 #include "conversion/kabcconversion.h"
16 #include "conversion/kcalconversion.cpp"
17 #include "conversion/kcalconversion.h"
18 #include "testhelpers.h"
19 
20 using namespace Kolab::Conversion;
21 
comparePointerVectors(const QVector<T> & list,const QVector<T> & other)22 template<typename T> void comparePointerVectors(const QVector<T> &list, const QVector<T> &other)
23 {
24     QCOMPARE(list.size(), other.size());
25     for (int i = 0; i < list.size(); i++) {
26         QCOMPARE(*list.at(i), *other.at(i));
27     }
28 }
29 
compareAttendeesVectors(const KCalendarCore::Attendee::List & list,const KCalendarCore::Attendee::List & other)30 void compareAttendeesVectors(const KCalendarCore::Attendee::List &list, const KCalendarCore::Attendee::List &other)
31 {
32     QCOMPARE(list.size(), other.size());
33     for (int i = 0; i < list.size(); i++) {
34         KCalendarCore::Attendee at1 = list.at(i);
35         at1.setUid(QString());
36         KCalendarCore::Attendee at2 = other.at(i);
37         at2.setUid(QString());
38         QCOMPARE(at1, at2);
39     }
40 }
41 
initTestCase()42 void KCalConversionTest::initTestCase()
43 {
44 }
45 
testDate_data()46 void KCalConversionTest::testDate_data()
47 {
48     QTest::addColumn<Kolab::cDateTime>("input");
49     QTest::addColumn<QDateTime>("result");
50 
51     QTest::newRow("datetime with tz") << Kolab::cDateTime("Europe/Zurich", 2006, 1, 8, 12, 0, 0)
52                                       << QDateTime(QDate(2006, 1, 8), QTime(12, 0, 0), QTimeZone("Europe/Zurich"));
53     QTest::newRow("floating datetime") << Kolab::cDateTime(2006, 1, 8, 12, 0, 0, false) << QDateTime(QDate(2006, 1, 8), QTime(12, 0, 0));
54     QTest::newRow("utc datetime") << Kolab::cDateTime(2006, 1, 8, 12, 0, 0, true) << QDateTime(QDate(2006, 1, 8), QTime(12, 0, 0), Qt::UTC);
55     QTest::newRow("date only") << Kolab::cDateTime(2006, 1, 8) << QDateTime(QDate(2006, 1, 8), {});
56 }
57 
testDate()58 void KCalConversionTest::testDate()
59 {
60     QFETCH(Kolab::cDateTime, input);
61     QFETCH(QDateTime, result);
62 
63     const QDateTime &r = Kolab::Conversion::toDate(input);
64     QCOMPARE(r, result);
65 
66     const Kolab::cDateTime &r2 = Kolab::Conversion::fromDate(result, input.isDateOnly());
67     QCOMPARE(r2, input);
68 }
69 
testDuration_data()70 void KCalConversionTest::testDuration_data()
71 {
72     QTest::addColumn<Kolab::Duration>("input");
73     QTest::addColumn<KCalendarCore::Duration>("result");
74     QTest::addColumn<Kolab::Duration>("fromResult");
75 
76     QTest::newRow("seconds") << Kolab::Duration(0, 0, 0, 30, false) << KCalendarCore::Duration(30, KCalendarCore::Duration::Seconds)
77                              << Kolab::Duration(0, 0, 0, 30, false);
78     QTest::newRow("minutes") << Kolab::Duration(0, 0, 1, 30, false) << KCalendarCore::Duration(90, KCalendarCore::Duration::Seconds)
79                              << Kolab::Duration(0, 0, 0, 90, false);
80     QTest::newRow("hours") << Kolab::Duration(0, 1, 1, 30, false) << KCalendarCore::Duration(60 * 60 + 90, KCalendarCore::Duration::Seconds)
81                            << Kolab::Duration(0, 0, 0, 60 * 60 + 90, false);
82     QTest::newRow("days") << Kolab::Duration(1, 1, 1, 30, false) << KCalendarCore::Duration(24 * 60 * 60 + 60 * 60 + 90, KCalendarCore::Duration::Seconds)
83                           << Kolab::Duration(0, 0, 0, 24 * 60 * 60 + 60 * 60 + 90, false);
84     QTest::newRow("daysonly") << Kolab::Duration(30, 0, 0, 0, false) << KCalendarCore::Duration(30, KCalendarCore::Duration::Days)
85                               << Kolab::Duration(30, 0, 0, 0, false);
86     QTest::newRow("weeks") << Kolab::Duration(30, false) << KCalendarCore::Duration(30 * 7, KCalendarCore::Duration::Days)
87                            << Kolab::Duration(30 * 7, 0, 0, 0, false);
88 }
89 
testDuration()90 void KCalConversionTest::testDuration()
91 {
92     QFETCH(Kolab::Duration, input);
93     QFETCH(KCalendarCore::Duration, result);
94     QFETCH(Kolab::Duration, fromResult);
95 
96     const KCalendarCore::Duration &r = Kolab::Conversion::toDuration(input);
97     QCOMPARE(r, result);
98 
99     const Kolab::Duration &r2 = Kolab::Conversion::fromDuration(result);
100     QCOMPARE(r2, fromResult);
101 }
102 
testDateTZ_data()103 void KCalConversionTest::testDateTZ_data()
104 {
105     QTest::addColumn<Kolab::cDateTime>("input");
106     QTest::addColumn<QDateTime>("result");
107 
108     QTest::newRow("berlin") << Kolab::cDateTime("Europe/Berlin", 2006, 1, 8, 12, 0, 0)
109                             << QDateTime(QDate(2006, 1, 8), QTime(12, 0, 0), QTimeZone("Europe/Berlin"));
110 }
111 
testDateTZ()112 void KCalConversionTest::testDateTZ()
113 {
114     QFETCH(Kolab::cDateTime, input);
115     QFETCH(QDateTime, result);
116 
117     const QDateTime &r = Kolab::Conversion::toDate(input);
118     QCOMPARE(QString::fromUtf8(result.timeZone().id()), QString::fromStdString(input.timezone()));
119     QCOMPARE(r.timeZone().offsetFromUtc(QDateTime::currentDateTimeUtc()), result.timeZone().offsetFromUtc(QDateTime::currentDateTimeUtc()));
120 
121     const Kolab::cDateTime &r2 = Kolab::Conversion::fromDate(result, input.isDateOnly());
122     QCOMPARE(QString::fromStdString(r2.timezone()), QString::fromUtf8(result.timeZone().id()));
123 }
124 
testConversion_data()125 void KCalConversionTest::testConversion_data()
126 {
127     QTest::addColumn<KCalendarCore::Event>("kcal");
128     QTest::addColumn<Kolab::Event>("kolab");
129 
130     Kolab::cDateTime date(2011, 2, 2, 12, 11, 10, true);
131     Kolab::cDateTime date2(2011, 2, 2, 12, 12, 10, true);
132     Kolab::cDateTime date3(2012, 2, 2, 12, 12, 10, true);
133     std::vector<int> intVector;
134     intVector.push_back(1);
135     intVector.push_back(-3);
136     intVector.push_back(2);
137     std::vector<std::string> stringVector;
138     stringVector.push_back("cat1");
139     stringVector.push_back("cat2");
140     stringVector.push_back("parent/child");
141 
142     {
143         KCalendarCore::Event kcal;
144         kcal.setUid(QStringLiteral("uid"));
145         kcal.setCreated(toDate(date));
146         kcal.setLastModified(toDate(date));
147         kcal.setRevision(3);
148         kcal.setSecrecy(KCalendarCore::Incidence::SecrecyConfidential);
149         kcal.setCategories(toStringList(stringVector));
150         kcal.setDtStart(toDate(date));
151         kcal.setDtEnd(toDate(date2));
152         kcal.setAllDay(date.isDateOnly());
153         kcal.setTransparency(KCalendarCore::Event::Transparent);
154 
155         kcal.setRecurrenceId(toDate(date2)); // TODO THISANDFUTURE
156         kcal.recurrence()->setDaily(3);
157         kcal.recurrence()->setDuration(5);
158         kcal.recurrence()->addRDateTime(toDate(date2));
159         kcal.recurrence()->addRDate(toDate(date2).date());
160         kcal.recurrence()->addExDateTime(toDate(date3));
161         kcal.recurrence()->addExDate(toDate(date3).date());
162 
163         KCalendarCore::RecurrenceRule *rr = kcal.recurrence()->defaultRRule(true);
164         QList<int> intList(intVector.cbegin(), intVector.cend());
165         rr->setBySeconds(intList);
166         rr->setByMinutes(intList);
167         rr->setByHours(intList);
168         rr->setByDays(QList<KCalendarCore::RecurrenceRule::WDayPos>()
169                       << KCalendarCore::RecurrenceRule::WDayPos(3, 1) << KCalendarCore::RecurrenceRule::WDayPos(5, 4));
170         rr->setByMonthDays(intList);
171         rr->setByYearDays(intList);
172         rr->setByMonths(intList);
173         rr->setByWeekNumbers(intList);
174 
175         kcal.setSummary(QStringLiteral("summary"));
176         kcal.setDescription(QStringLiteral("description"));
177         kcal.setPriority(3);
178         kcal.setStatus(KCalendarCore::Incidence::StatusConfirmed);
179         kcal.setLocation(QStringLiteral("location"));
180         kcal.setOrganizer(KCalendarCore::Person(QStringLiteral("organizer"), QStringLiteral("organizer@email")));
181         // Url
182         kcal.setNonKDECustomProperty("X-KOLAB-URL", QStringLiteral("http://test.org"));
183         KCalendarCore::Attendee att(QStringLiteral("attendee"),
184                                     QStringLiteral("attendee@email"),
185                                     false,
186                                     KCalendarCore::Attendee::NeedsAction,
187                                     KCalendarCore::Attendee::ReqParticipant);
188         att.setDelegate(QStringLiteral("mailto:delegatee<delegatee@email>"));
189         att.setDelegator(QStringLiteral("mailto:delegator<delegator@email>"));
190         kcal.addAttendee(att);
191         kcal.addAttachment(KCalendarCore::Attachment(QStringLiteral("uri"), QStringLiteral("mimetype/mime")));
192         KCalendarCore::Alarm::Ptr alarm = KCalendarCore::Alarm::Ptr(new KCalendarCore::Alarm(&kcal));
193         KCalendarCore::Person::List addressees;
194         addressees.append(KCalendarCore::Person(QStringLiteral("name"), QStringLiteral("email@email")));
195         alarm->setEmailAlarm(QStringLiteral("subject"), QStringLiteral("text"), addressees, QStringList()); // No support for attachments
196         kcal.addAlarm(alarm);
197         // TODO alarms
198 
199         kcal.setNonKDECustomProperty("X-KOLAB-key1", QStringLiteral("value1"));
200         kcal.setNonKDECustomProperty("X-KOLAB-key2", QStringLiteral("value2"));
201         kcal.setCustomProperty("SOMEOTHERAPP", "key2", QStringLiteral("value2"));
202         Q_ASSERT(kcal.nonKDECustomProperty("X-KOLAB-key1") == QLatin1String("value1"));
203 
204         Kolab::Event kolab;
205         kolab.setUid("uid");
206         kolab.setCreated(date);
207         kolab.setLastModified(date);
208         kolab.setSequence(3);
209         kolab.setClassification(Kolab::ClassConfidential);
210         kolab.setCategories(stringVector);
211         kolab.setStart(date);
212         kolab.setEnd(date2);
213         kolab.setTransparency(true);
214 
215         Kolab::RecurrenceRule rrule;
216         rrule.setInterval(3);
217         rrule.setFrequency(Kolab::RecurrenceRule::Daily);
218         rrule.setCount(5);
219         rrule.setBysecond(intVector);
220         rrule.setByminute(intVector);
221         rrule.setByhour(intVector);
222         rrule.setByday(std::vector<Kolab::DayPos>() << Kolab::DayPos(3, Kolab::Monday) << Kolab::DayPos(5, Kolab::Thursday));
223         rrule.setBymonthday(intVector);
224         rrule.setByyearday(intVector);
225         rrule.setByweekno(intVector);
226         rrule.setBymonth(intVector);
227 
228         kolab.setRecurrenceRule(rrule);
229         kolab.setRecurrenceID(date2, true);
230         kolab.setRecurrenceDates(std::vector<Kolab::cDateTime>() << date2 << Kolab::cDateTime(date2.year(), date2.month(), date2.day()));
231         kolab.setExceptionDates(std::vector<Kolab::cDateTime>() << date3 << Kolab::cDateTime(date3.year(), date3.month(), date3.day()));
232 
233         kolab.setSummary("summary");
234         kolab.setDescription("description");
235         kolab.setPriority(3);
236         kolab.setStatus(Kolab::StatusConfirmed);
237         kolab.setLocation("location");
238         kolab.setOrganizer(Kolab::ContactReference(Kolab::ContactReference::EmailReference, "organizer@email", "organizer")); // TODO uid
239         kolab.setUrl("http://test.org");
240 
241         Kolab::Attendee a(Kolab::ContactReference(Kolab::ContactReference::EmailReference, "attendee@email", "attendee")); // TODO uid
242         a.setDelegatedTo(std::vector<Kolab::ContactReference>()
243                          << Kolab::ContactReference(Kolab::ContactReference::EmailReference, "delegatee@email", "delegatee"));
244         a.setDelegatedFrom(std::vector<Kolab::ContactReference>()
245                            << Kolab::ContactReference(Kolab::ContactReference::EmailReference, "delegator@email", "delegator"));
246         a.setCutype(Kolab::CutypeIndividual);
247         kolab.setAttendees(std::vector<Kolab::Attendee>() << a);
248 
249         Kolab::Attachment attach;
250         attach.setUri("uri", "mimetype/mime");
251         kolab.setAttachments(std::vector<Kolab::Attachment>() << attach);
252 
253         //     std::vector<std::string> receipents;
254         //     receipents.push_back("email@email");
255         //     Kolab::Alarm alarm2("summary", "description", receipents);
256         //     kolab.setAlarms(std::vector<Kolab::Alarm>() << alarm2);
257 
258         // The sorting is random, just sort them here how we think they should arrive so we don't have to sort during compare (due to laziness).
259         std::vector<Kolab::CustomProperty> customproperties;
260         customproperties.push_back(Kolab::CustomProperty("X-KDE-SOMEOTHERAPP-key2", "value2"));
261         customproperties.push_back(Kolab::CustomProperty("key1", "value1"));
262         customproperties.push_back(Kolab::CustomProperty("key2", "value2"));
263 
264         kolab.setCustomProperties(customproperties);
265 
266         QTest::newRow("with endDate and recurrence duration") << kcal << kolab;
267     }
268     {
269         KCalendarCore::Event kcal;
270         kcal.setUid(QStringLiteral("uid"));
271         kcal.setCreated(toDate(date));
272         kcal.setLastModified(toDate(date));
273         kcal.setRevision(3);
274         kcal.setDtStart(toDate(date));
275         kcal.setAllDay(date.isDateOnly());
276         kcal.setDuration(KCalendarCore::Duration(toDate(date), toDate(date2)));
277         kcal.recurrence()->setDaily(3);
278         kcal.recurrence()->setEndDateTime(toDate(date3));
279 
280         Kolab::Event kolab;
281         kolab.setUid("uid");
282         kolab.setCreated(date);
283         kolab.setLastModified(date);
284         kolab.setSequence(3);
285         kolab.setStart(date);
286         kolab.setDuration(Kolab::Duration(0, 0, 1, 0));
287         Kolab::RecurrenceRule rrule;
288         rrule.setInterval(3);
289         rrule.setFrequency(Kolab::RecurrenceRule::Daily);
290         rrule.setEnd(date3);
291         kolab.setRecurrenceRule(rrule);
292 
293         QTest::newRow("with duration and recurrence endDate") << kcal << kolab;
294     }
295     {
296         Kolab::cDateTime start(2011, 1, 1);
297         Kolab::cDateTime end(2011, 1, 3);
298 
299         KCalendarCore::Event kcal;
300         kcal.setUid(QStringLiteral("uid"));
301         kcal.setCreated(toDate(date));
302         kcal.setLastModified(toDate(date));
303         kcal.setDtStart(toDate(start));
304         kcal.setDtEnd(toDate(end));
305         kcal.setAllDay(start.isDateOnly());
306         kcal.recurrence()->setDaily(3);
307         kcal.recurrence()->setEndDateTime(toDate(end));
308 
309         Kolab::Event kolab;
310         kolab.setUid("uid");
311         kolab.setCreated(date);
312         kolab.setLastModified(date);
313         kolab.setStart(start);
314         kolab.setEnd(end);
315         Kolab::RecurrenceRule rrule;
316         rrule.setInterval(3);
317         rrule.setFrequency(Kolab::RecurrenceRule::Daily);
318         rrule.setEnd(end);
319         kolab.setRecurrenceRule(rrule);
320 
321         QTest::newRow("date only dates") << kcal << kolab;
322     }
323     {
324         KCalendarCore::Event kcal;
325         kcal.setUid(QStringLiteral("uid"));
326         kcal.setCreated(toDate(date));
327         kcal.setLastModified(toDate(date));
328         kcal.setDtStart(toDate(date));
329         kcal.setAllDay(date.isDateOnly());
330         kcal.setSummary(QStringLiteral("äöü%@$£é¤¼²°€Š�"));
331 
332         Kolab::Event kolab;
333         kolab.setUid("uid");
334         kolab.setCreated(date);
335         kolab.setLastModified(date);
336         kolab.setStart(date);
337         kolab.setSummary(std::string(QStringLiteral("äöü%@$£é¤¼²°€Š�").toUtf8().constData()));
338 
339         QTest::newRow("latin1+Unicode") << kcal << kolab;
340     }
341 }
342 
testConversion()343 void KCalConversionTest::testConversion()
344 {
345     QFETCH(KCalendarCore::Event, kcal);
346     QFETCH(Kolab::Event, kolab);
347 
348     KCalendarCore::Event::Ptr e = toKCalendarCore(kolab);
349     const Kolab::Event &b = fromKCalendarCore(kcal);
350 
351     QCOMPARE(e->uid(), kcal.uid());
352     QCOMPARE(e->created(), kcal.created());
353     QCOMPARE(e->lastModified(), kcal.lastModified());
354     QCOMPARE(e->revision(), kcal.revision());
355     QCOMPARE(e->secrecy(), kcal.secrecy());
356     QCOMPARE(e->categories(), kcal.categories());
357     QCOMPARE(e->dtStart(), kcal.dtStart());
358     QCOMPARE(e->dtEnd(), kcal.dtEnd());
359     QCOMPARE(e->duration(), kcal.duration());
360     QCOMPARE(e->transparency(), kcal.transparency());
361     QCOMPARE(*e->recurrence(), *kcal.recurrence());
362     QCOMPARE(e->recurrenceId(), kcal.recurrenceId());
363     QCOMPARE(e->recurrenceType(), kcal.recurrenceType());
364     QCOMPARE(e->summary(), kcal.summary());
365     QCOMPARE(e->description(), kcal.description());
366     QCOMPARE(e->priority(), kcal.priority());
367     QCOMPARE(e->status(), kcal.status());
368     QCOMPARE(e->location(), kcal.location());
369     QCOMPARE(e->organizer().name(), kcal.organizer().name());
370     QCOMPARE(e->organizer().email(), kcal.organizer().email());
371     QCOMPARE(e->nonKDECustomProperty("X-KOLAB-URL"), kcal.nonKDECustomProperty("X-KOLAB-URL"));
372     // otherwise we'd break the customProperties comparison
373     e->removeNonKDECustomProperty("X-KOLAB-URL");
374     kcal.removeNonKDECustomProperty("X-KOLAB-URL");
375     compareAttendeesVectors(e->attendees(), kcal.attendees());
376     QCOMPARE(e->attachments(), kcal.attachments());
377 
378     //     QCOMPARE(e->alarms(), kcal.alarms()); //TODO
379     QCOMPARE(e->customProperties(), kcal.customProperties());
380 
381     //     QBENCHMARK {
382     //         toKCalendarCore(kolab);
383     //     }
384 
385     QCOMPARE(b.uid(), kolab.uid());
386     QCOMPARE(b.created(), kolab.created());
387     QCOMPARE(b.lastModified(), kolab.lastModified());
388     QCOMPARE(b.sequence(), kolab.sequence());
389     QCOMPARE(b.classification(), kolab.classification());
390     QCOMPARE(b.categories(), kolab.categories());
391     QCOMPARE(b.start(), kolab.start());
392     QCOMPARE(b.end(), kolab.end());
393     QCOMPARE(b.duration(), kolab.duration());
394     QCOMPARE(b.transparency(), kolab.transparency());
395 
396     QCOMPARE(b.recurrenceRule(), kolab.recurrenceRule());
397     QCOMPARE(b.recurrenceID(), kolab.recurrenceID());
398     QCOMPARE(b.recurrenceDates(), kolab.recurrenceDates());
399     QCOMPARE(b.exceptionDates(), kolab.exceptionDates());
400 
401     QCOMPARE(b.summary(), kolab.summary());
402     QCOMPARE(b.description(), kolab.description());
403     QCOMPARE(b.status(), kolab.status());
404     QCOMPARE(b.location(), kolab.location());
405     QCOMPARE(b.organizer(), kolab.organizer());
406     QCOMPARE(b.url(), kolab.url());
407     QCOMPARE(b.attendees(), kolab.attendees());
408     QCOMPARE(b.attachments(), kolab.attachments());
409     QCOMPARE(b.customProperties(), kolab.customProperties());
410 }
411 
testTodoConversion_data()412 void KCalConversionTest::testTodoConversion_data()
413 {
414     QTest::addColumn<KCalendarCore::Todo>("kcal");
415     QTest::addColumn<Kolab::Todo>("kolab");
416 
417     Kolab::cDateTime date(2011, 2, 2, 12, 11, 10, true);
418     Kolab::cDateTime date2(2011, 2, 2, 12, 12, 10, true);
419 
420     {
421         KCalendarCore::Todo kcal;
422         kcal.setUid(QStringLiteral("uid"));
423         kcal.setDtStart(toDate(date));
424         kcal.setDtDue(toDate(date2));
425         kcal.setAllDay(date.isDateOnly());
426         kcal.setRelatedTo(QStringLiteral("uid2"), KCalendarCore::Incidence::RelTypeParent);
427 
428         Kolab::Todo kolab;
429         kolab.setUid("uid");
430         kolab.setStart(date);
431         kolab.setDue(date2);
432         std::vector<std::string> relateds;
433         relateds.push_back("uid2");
434         kolab.setRelatedTo(relateds);
435 
436         QTest::newRow("todo") << kcal << kolab;
437     }
438 }
439 
testTodoConversion()440 void KCalConversionTest::testTodoConversion()
441 {
442     QFETCH(KCalendarCore::Todo, kcal);
443     QFETCH(Kolab::Todo, kolab);
444 
445     const KCalendarCore::Todo::Ptr e = toKCalendarCore(kolab);
446 
447     QCOMPARE(e->uid(), kcal.uid());
448     QCOMPARE(e->dtStart(), kcal.dtStart());
449     QCOMPARE(e->dtDue(), kcal.dtDue());
450     QCOMPARE(e->relatedTo(KCalendarCore::Incidence::RelTypeParent), kcal.relatedTo(KCalendarCore::Incidence::RelTypeParent));
451 
452     const Kolab::Todo &b = fromKCalendarCore(kcal);
453     QCOMPARE(b.uid(), kolab.uid());
454     QCOMPARE(b.start(), kolab.start());
455     QCOMPARE(b.due(), kolab.due());
456     QCOMPARE(b.relatedTo(), kolab.relatedTo());
457 }
458 
testJournalConversion_data()459 void KCalConversionTest::testJournalConversion_data()
460 {
461     QTest::addColumn<KCalendarCore::Journal>("kcal");
462     QTest::addColumn<Kolab::Journal>("kolab");
463 
464     Kolab::cDateTime date(2011, 2, 2, 12, 11, 10, true);
465     Kolab::cDateTime date2(2011, 2, 2, 12, 12, 10, true);
466 
467     {
468         KCalendarCore::Journal kcal;
469         kcal.setUid(QStringLiteral("uid"));
470         kcal.setDtStart(toDate(date));
471         kcal.setSummary(QStringLiteral("summary"));
472 
473         Kolab::Journal kolab;
474         kolab.setUid("uid");
475         kolab.setStart(date);
476         kolab.setSummary("summary");
477 
478         QTest::newRow("journal") << kcal << kolab;
479     }
480 }
481 
testJournalConversion()482 void KCalConversionTest::testJournalConversion()
483 {
484     QFETCH(KCalendarCore::Journal, kcal);
485     QFETCH(Kolab::Journal, kolab);
486 
487     const KCalendarCore::Journal::Ptr e = toKCalendarCore(kolab);
488 
489     QCOMPARE(e->uid(), kcal.uid());
490     QCOMPARE(e->dtStart(), kcal.dtStart());
491     QCOMPARE(e->summary(), kcal.summary());
492 
493     const Kolab::Journal &b = fromKCalendarCore(kcal);
494     QCOMPARE(b.uid(), kolab.uid());
495     QCOMPARE(b.start(), kolab.start());
496     QCOMPARE(b.summary(), kolab.summary());
497 }
498 
testContactConversion_data()499 void KCalConversionTest::testContactConversion_data()
500 {
501     QTest::addColumn<KContacts::Addressee>("kcal");
502     QTest::addColumn<Kolab::Contact>("kolab");
503 
504     {
505         KContacts::Addressee kcal;
506         kcal.setUid(QStringLiteral("uid"));
507         kcal.setFormattedName(QStringLiteral("name"));
508 
509         Kolab::Contact kolab;
510         kolab.setUid("uid");
511         kolab.setName("name");
512 
513         QTest::newRow("basic") << kcal << kolab;
514     }
515     {
516         KContacts::Addressee kcal;
517         kcal.setUid(QStringLiteral("uid"));
518         kcal.setFormattedName(QStringLiteral("name"));
519         kcal.setBirthday(QDate(2012, 2, 2).startOfDay());
520 
521         // Because QDateTime doesn't know date-only values we always end up with a date-time
522         Kolab::Contact kolab;
523         kolab.setUid("uid");
524         kolab.setName("name");
525         kolab.setBDay(Kolab::cDateTime(2012, 2, 2, 0, 0, 0));
526 
527         QTest::newRow("bday") << kcal << kolab;
528     }
529     {
530         KContacts::Addressee kcal;
531         kcal.setUid(QStringLiteral("uid"));
532         // The first address is always the preferred
533         kcal.setEmails(QStringList() << QStringLiteral("email1@example.org") << QStringLiteral("email2@example.org"));
534         kcal.insertCustom(QStringLiteral("KOLAB"), QStringLiteral("EmailTypesemail1@example.org"), QStringLiteral("home,work"));
535 
536         Kolab::Contact kolab;
537         kolab.setUid("uid");
538 
539         Kolab::Email email1("email1@example.org", Kolab::Email::Work | Kolab::Email::Home);
540         Kolab::Email email2("email2@example.org");
541 
542         std::vector<Kolab::Email> emails;
543         emails.push_back(email1);
544         emails.push_back(email2);
545         kolab.setEmailAddresses(emails, 0);
546 
547         QTest::newRow("emailTypesAndPreference") << kcal << kolab;
548     }
549 }
550 
testContactConversion()551 void KCalConversionTest::testContactConversion()
552 {
553     QFETCH(KContacts::Addressee, kcal);
554     QFETCH(Kolab::Contact, kolab);
555 
556     const KContacts::Addressee &e = toKABC(kolab);
557 
558     QCOMPARE(e.uid(), kcal.uid());
559     QCOMPARE(e.formattedName(), kcal.formattedName());
560     QCOMPARE(e.emails(), kcal.emails());
561     QCOMPARE(e.preferredEmail(), kcal.preferredEmail());
562     const auto mails{e.emails()};
563     for (const QString &mail : mails) {
564         QCOMPARE(e.custom(QLatin1String("KOLAB"), QString::fromLatin1("EmailTypes%1").arg(mail)),
565                  kcal.custom(QLatin1String("KOLAB"), QString::fromLatin1("EmailTypes%1").arg(mail)));
566     }
567     QCOMPARE(e.birthday(), kcal.birthday());
568 
569     const Kolab::Contact &b = fromKABC(kcal);
570     QCOMPARE(b.uid(), kolab.uid());
571     QCOMPARE(b.name(), kolab.name());
572     QCOMPARE(b.emailAddresses(), kolab.emailAddresses());
573     QCOMPARE(b.emailAddressPreferredIndex(), kolab.emailAddressPreferredIndex());
574     QCOMPARE(b.bDay(), kolab.bDay());
575 }
576 
577 // void KCalConversionTest::BenchmarkRoundtripKCAL()
578 // {
579 //     const Kolab::Event &event = Kolab::readEvent(TEST_DATA_PATH "/testfiles/icalEvent.xml", true);
580 //     std::string result = Kolab::writeEvent(event);
581 //     QBENCHMARK {
582 //         Kolab::Conversion::toKCalendarCore(Kolab::readEvent(result, false));
583 //     }
584 // }
585 
586 QTEST_MAIN(KCalConversionTest)
587