1 /*
2     This file is part of kcontacts.
3     SPDX-FileCopyrightText: 2016-2019 Laurent Montel <montel@kde.org>
4 
5     SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include "birthdaytest.h"
9 #include "addressee.h"
10 #include "vcard_p.h"
11 #include <QTest>
12 #include <vcardtool_p.h>
13 
BirthDayTest(QObject * parent)14 BirthDayTest::BirthDayTest(QObject *parent)
15     : QObject(parent)
16 {
17 }
18 
~BirthDayTest()19 BirthDayTest::~BirthDayTest()
20 {
21 }
22 
shouldParseBirthDay()23 void BirthDayTest::shouldParseBirthDay()
24 {
25     QByteArray vcarddata(
26         "BEGIN:VCARD\r\n"
27         "VERSION:4.0\r\n"
28         "BDAY:19760505T120505\r\n"
29         "EMAIL:foo@kde.org\r\n"
30         "EMAIL:bla@kde.org\r\n"
31         "N:;;;;\r\n"
32         "UID:testuid\r\n"
33         "END:VCARD\r\n\r\n");
34 
35     KContacts::VCardTool vcard;
36     const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
37     QCOMPARE(lst.count(), 1);
38     QDateTime dt(QDate(1976, 5, 5), QTime(12, 5, 5));
39     QCOMPARE(lst.at(0).birthday(), dt);
40     QCOMPARE(lst.at(0).birthdayHasTime(), true);
41 }
42 
shouldParseBirthDayWithoutTime()43 void BirthDayTest::shouldParseBirthDayWithoutTime()
44 {
45     QByteArray vcarddata(
46         "BEGIN:VCARD\r\n"
47         "VERSION:4.0\r\n"
48         "BDAY:19760505\r\n"
49         "EMAIL:foo@kde.org\r\n"
50         "EMAIL:bla@kde.org\r\n"
51         "N:;;;;\r\n"
52         "UID:testuid\r\n"
53         "END:VCARD\r\n\r\n");
54 
55     KContacts::VCardTool vcard;
56     const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
57     QCOMPARE(lst.count(), 1);
58     QDateTime dt(QDate(1976, 5, 5).startOfDay());
59 
60     QCOMPARE(lst.at(0).birthday(), dt);
61     QCOMPARE(lst.at(0).birthdayHasTime(), false);
62 }
63 
shouldParseBirthDayWithoutTimeAndYear()64 void BirthDayTest::shouldParseBirthDayWithoutTimeAndYear()
65 {
66     QByteArray vcarddata(
67         "BEGIN:VCARD\r\n"
68         "VERSION:4.0\r\n"
69         "BDAY:--0505\r\n"
70         "EMAIL:foo@kde.org\r\n"
71         "EMAIL:bla@kde.org\r\n"
72         "N:;;;;\r\n"
73         "UID:testuid\r\n"
74         "END:VCARD\r\n\r\n");
75 
76     KContacts::VCardTool vcard;
77     const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
78     QCOMPARE(lst.count(), 1);
79     QDateTime dt(QDate(-1, 5, 5).startOfDay());
80     QCOMPARE(lst.at(0).birthday(), dt);
81     QCOMPARE(lst.at(0).birthdayHasTime(), false);
82 }
83 
shouldExportVcard4()84 void BirthDayTest::shouldExportVcard4()
85 {
86     KContacts::AddresseeList lst;
87     KContacts::Addressee addr;
88     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org") << QStringLiteral("bla@kde.org"));
89     addr.setUid(QStringLiteral("testuid"));
90     const QDateTime dt(QDate(1976, 5, 5), QTime(12, 5, 5));
91     addr.setBirthday(dt);
92     lst << addr;
93     KContacts::VCardTool vcard;
94     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
95     QByteArray expected(
96         "BEGIN:VCARD\r\n"
97         "VERSION:4.0\r\n"
98         "BDAY:19760505T120505\r\n"
99         "EMAIL:foo@kde.org\r\n"
100         "EMAIL:bla@kde.org\r\n"
101         "N:;;;;\r\n"
102         "UID:testuid\r\n"
103         "END:VCARD\r\n\r\n");
104 
105     QCOMPARE(ba, expected);
106 }
107 
shouldExportVcard4WithoutTime()108 void BirthDayTest::shouldExportVcard4WithoutTime()
109 {
110     KContacts::AddresseeList lst;
111     KContacts::Addressee addr;
112     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org") << QStringLiteral("bla@kde.org"));
113     addr.setUid(QStringLiteral("testuid"));
114     const QDate d(1976, 5, 5);
115     addr.setBirthday(d);
116     lst << addr;
117     KContacts::VCardTool vcard;
118     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
119     QByteArray expected(
120         "BEGIN:VCARD\r\n"
121         "VERSION:4.0\r\n"
122         "BDAY:19760505\r\n"
123         "EMAIL:foo@kde.org\r\n"
124         "EMAIL:bla@kde.org\r\n"
125         "N:;;;;\r\n"
126         "UID:testuid\r\n"
127         "END:VCARD\r\n\r\n");
128 
129     QCOMPARE(ba, expected);
130 }
131 
shouldExportVcard4WithoutTimeAndWithoutYear()132 void BirthDayTest::shouldExportVcard4WithoutTimeAndWithoutYear()
133 {
134     KContacts::AddresseeList lst;
135     KContacts::Addressee addr;
136     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org") << QStringLiteral("bla@kde.org"));
137     addr.setUid(QStringLiteral("testuid"));
138     const QDate d(-1, 5, 5);
139     addr.setBirthday(d);
140     lst << addr;
141     KContacts::VCardTool vcard;
142     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
143     QByteArray expected(
144         "BEGIN:VCARD\r\n"
145         "VERSION:4.0\r\n"
146         "BDAY:--0505\r\n"
147         "EMAIL:foo@kde.org\r\n"
148         "EMAIL:bla@kde.org\r\n"
149         "N:;;;;\r\n"
150         "UID:testuid\r\n"
151         "END:VCARD\r\n\r\n");
152 
153     QCOMPARE(ba, expected);
154 }
155 
shouldExportVcard3()156 void BirthDayTest::shouldExportVcard3()
157 {
158     KContacts::AddresseeList lst;
159     KContacts::Addressee addr;
160     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org") << QStringLiteral("bla@kde.org"));
161     addr.setUid(QStringLiteral("testuid"));
162     const QDateTime dt(QDate(1976, 5, 5), QTime(12, 5, 5));
163     addr.setBirthday(dt);
164     lst << addr;
165     KContacts::VCardTool vcard;
166     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v3_0);
167     QByteArray expected(
168         "BEGIN:VCARD\r\n"
169         "VERSION:3.0\r\n"
170         "BDAY:1976-05-05T12:05:05\r\n"
171         "EMAIL:foo@kde.org\r\n"
172         "EMAIL:bla@kde.org\r\n"
173         "N:;;;;\r\n"
174         "UID:testuid\r\n"
175         "END:VCARD\r\n\r\n");
176 
177     QCOMPARE(ba, expected);
178 }
179 
shouldExportVcard3WithoutTimeAndWithoutYear()180 void BirthDayTest::shouldExportVcard3WithoutTimeAndWithoutYear()
181 {
182     KContacts::AddresseeList lst;
183     KContacts::Addressee addr;
184     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org") << QStringLiteral("bla@kde.org"));
185     addr.setUid(QStringLiteral("testuid"));
186     const QDate d(-1, 5, 5);
187     addr.setBirthday(d);
188     lst << addr;
189     KContacts::VCardTool vcard;
190     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v3_0);
191     QByteArray expected(
192         "BEGIN:VCARD\r\n"
193         "VERSION:3.0\r\n"
194         "BDAY:--05-05\r\n"
195         "EMAIL:foo@kde.org\r\n"
196         "EMAIL:bla@kde.org\r\n"
197         "N:;;;;\r\n"
198         "UID:testuid\r\n"
199         "END:VCARD\r\n\r\n");
200 
201     QCOMPARE(ba, expected);
202 }
203 
shouldExportVcard3WithoutTime()204 void BirthDayTest::shouldExportVcard3WithoutTime()
205 {
206     KContacts::AddresseeList lst;
207     KContacts::Addressee addr;
208     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org") << QStringLiteral("bla@kde.org"));
209     addr.setUid(QStringLiteral("testuid"));
210     const QDate d(1976, 5, 5);
211     addr.setBirthday(d);
212     lst << addr;
213     KContacts::VCardTool vcard;
214     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v3_0);
215     QByteArray expected(
216         "BEGIN:VCARD\r\n"
217         "VERSION:3.0\r\n"
218         "BDAY:1976-05-05\r\n"
219         "EMAIL:foo@kde.org\r\n"
220         "EMAIL:bla@kde.org\r\n"
221         "N:;;;;\r\n"
222         "UID:testuid\r\n"
223         "END:VCARD\r\n\r\n");
224 
225     QCOMPARE(ba, expected);
226 }
227 
228 QTEST_MAIN(BirthDayTest)
229