1 /*
2   SPDX-FileCopyrightText: 2009 Constantin Berzan <exit3219@gmail.com>
3 
4   SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "multipartjobtest.h"
8 
9 #include <QDebug>
10 #include <QTest>
11 
12 #include <KMime/Content>
13 using namespace KMime;
14 
15 #include <MessageComposer/Composer>
16 #include <MessageComposer/GlobalPart>
17 #include <MessageComposer/MultipartJob>
18 #include <MessageComposer/SinglepartJob>
19 using namespace MessageComposer;
20 
QTEST_MAIN(MultipartJobTest)21 QTEST_MAIN(MultipartJobTest)
22 
23 void MultipartJobTest::testMultipartMixed()
24 {
25     Composer composer;
26     auto mjob = new MultipartJob(&composer);
27     mjob->setMultipartSubtype("mixed");
28 
29     QByteArray data1("one");
30     QByteArray data2("two");
31     QByteArray type1("text/plain");
32     QByteArray type2("application/x-mors-ontologica");
33 
34     {
35         auto cjob = new SinglepartJob(mjob);
36         cjob->setData(data1);
37         cjob->contentType()->setMimeType(type1);
38     }
39 
40     {
41         auto cjob = new SinglepartJob(mjob);
42         cjob->setData(data2);
43         cjob->contentType()->setMimeType(type2);
44     }
45 
46     QVERIFY(mjob->exec());
47     Content *result = mjob->content();
48     result->assemble();
49     qDebug() << result->encodedContent();
50 
51     QVERIFY(result->contentType(false));
52     QCOMPARE(result->contentType(false)->mimeType(), QByteArray("multipart/mixed"));
53     QCOMPARE(result->contents().count(), 2);
54 
55     {
56         Content *c = result->contents().at(0);
57         QCOMPARE(c->body(), data1);
58         QVERIFY(c->contentType(false));
59         QCOMPARE(c->contentType(false)->mimeType(), type1);
60     }
61 
62     {
63         Content *c = result->contents().at(1);
64         QCOMPARE(c->body(), data2);
65         QVERIFY(c->contentType(false));
66         QCOMPARE(c->contentType(false)->mimeType(), type2);
67     }
68     delete result;
69 }
70 
test8BitPropagation()71 void MultipartJobTest::test8BitPropagation()
72 {
73     // If a subpart is 8bit, its parent must be 8bit too.
74 
75     Composer composer;
76     composer.globalPart()->set8BitAllowed(true);
77     auto mjob = new MultipartJob(&composer);
78     mjob->setMultipartSubtype("mixed");
79     auto mjob2 = new MultipartJob(mjob);
80     mjob2->setMultipartSubtype("mixed");
81     auto cjob = new SinglepartJob(mjob2);
82     QByteArray data("time is so short and I'm sure there must be something more");
83     cjob->setData(data);
84     cjob->contentTransferEncoding()->setEncoding(Headers::CE8Bit);
85     QVERIFY(mjob->exec());
86     Content *content = mjob->content();
87     content->assemble();
88     qDebug() << content->encodedContent();
89     QVERIFY(content->contentTransferEncoding(false));
90     QCOMPARE(content->contentTransferEncoding(false)->encoding(), Headers::CE8Bit);
91     delete content;
92 }
93