1--TEST--
2Bug #21027  Calendar support along with attachments and html images
3--SKIPIF--
4--FILE--
5<?php
6require_once('Mail/mime.php');
7
8$txtBody = 'Hi, this is Plain Text Body.';
9$htmlBody = '<div>This is HTML body.</div>';
10$icsText = 'BEGIN:VCALENDAR
11VERSION:2.0
12PRODID:-//icalcreator//NONSGML iCalcreator 2.22//
13METHOD:REQUEST
14BEGIN:VEVENT
15UID:77@localhost
16DTSTAMP:20160208T170811Z
17ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=
18 TRUE;CN=Jacob Alvarez:MAILTO:fake1@mailinator.com
19CREATED:20160208T170810Z
20DTSTART:20160215T180000Z
21DTEND:20160215T190000Z
22ORGANIZER;CN=-:MAILTO:fake2@mailinator.com
23SEQUENCE:1
24STATUS:CONFIRMED
25SUMMARY:Prueba 69
26TRANSP:OPAQUE
27URL:http://localhost/event/77
28END:VEVENT
29END:VCALENDAR';
30
31function printPartsStartAndEnd($body) {
32    $matches  = array();
33    preg_match_all('/--(=_[a-z0-9]+)--|Content-Type: ([^;\r\n]+)/', $body, $matches);
34    $tab = "    ";
35    foreach ($matches[0] as $match){
36        if (strpos($match, '--') === false) {
37            printf("%s%s\n", $tab, $match);
38            if (stripos($match, "multipart")) {
39                $tab .= "    ";
40            }
41        } else {
42            $tab = substr($tab, 0, -4);
43            printf("%sEnd part\n", $tab);
44        }
45    }
46}
47
48function printHeaderContentType($headers) {
49    $headerContentType = array();
50    preg_match('/([^;\r\n]+)/', $headers['Content-Type'], $headerContentType);
51    printf("Content-Type: %s\n", $headerContentType[0]);
52}
53
54print "TEST: text\n";
55$mime = new Mail_mime();
56$mime->setTXTBody($txtBody);
57$headers = $mime->headers();
58$body = $mime->get();
59printHeaderContentType($headers);
60printPartsStartAndEnd($body);
61print("\n");
62
63print "TEST: html\n";
64$mime = new Mail_mime();
65$mime->setHTMLBody($htmlBody);
66$headers = $mime->headers();
67$body = $mime->get();
68printHeaderContentType($headers);
69printPartsStartAndEnd($body);
70print("\n");
71
72print "TEST: attachments\n";
73$mime = new Mail_mime();
74$mime->addAttachment($icsText, 'application/ics', 'invite.ics', false);
75$headers = $mime->headers();
76$body = $mime->get();
77printHeaderContentType($headers);
78printPartsStartAndEnd($body);
79print("\n");
80
81print "TEST: text + attachments\n";
82$mime = new Mail_mime();
83$mime->setTXTBody($txtBody);
84$mime->addAttachment($icsText, 'application/ics', 'invite.ics', false);
85$headers = $mime->headers();
86$body = $mime->get();
87printHeaderContentType($headers);
88printPartsStartAndEnd($body);
89print("\n");
90
91print "TEST: html + attachments\n";
92$mime = new Mail_mime();
93$mime->setHTMLBody($htmlBody);
94$mime->addAttachment($icsText, 'application/ics', 'invite.ics', false);
95$headers = $mime->headers();
96$body = $mime->get();
97printHeaderContentType($headers);
98printPartsStartAndEnd($body);
99print("\n");
100
101print "TEST: html + inline images\n";
102$mime = new Mail_mime();
103$mime->setHTMLBody($htmlBody);
104$mime->addHTMLImage("aaaaaaaaaa", 'image/gif', 'image.gif', false, 'contentid');
105$headers = $mime->headers();
106$body = $mime->get();
107printHeaderContentType($headers);
108printPartsStartAndEnd($body);
109print("\n");
110
111print("TEST: txt, html and attachment\n");
112$mime = new Mail_mime();
113$mime->setTXTBody($txtBody);
114$mime->setHTMLBody($htmlBody);
115$mime->addAttachment("test", 'application/octet-stream', 'attachment', false);
116$headers = $mime->headers();
117$body = $mime->get();
118printHeaderContentType($headers);
119printPartsStartAndEnd($body);
120print("\n");
121
122print "TEST: calendar\n";
123$mime = new Mail_mime();
124$mime->setCalendarBody($icsText);
125$headers = $mime->headers();
126$body = $mime->get();
127printHeaderContentType($headers);
128printPartsStartAndEnd($body);
129print("\n");
130
131print "TEST: txt + calendar\n";
132$mime->setTXTBody($txtBody);
133$headers = $mime->headers();
134$body = $mime->get();
135printHeaderContentType($headers);
136printPartsStartAndEnd($body);
137print("\n");
138
139print "TEST: txt, html, calendar\n";
140$mime = new Mail_mime();
141$mime->setTXTBody($txtBody);
142$mime->setHTMLBody($htmlBody);
143$mime->setCalendarBody($icsText);
144$headers = $mime->headers();
145$body = $mime->get();
146printHeaderContentType($headers);
147printPartsStartAndEnd($body);
148print("\n");
149
150print "TEST: txt, html + html images, and calendar\n";
151$mime = new Mail_mime();
152$mime->setTXTBody($txtBody);
153$mime->setHTMLBody($htmlBody);
154$mime->addHTMLImage('testimage', 'image/gif', "bus.gif", false);
155$mime->setCalendarBody($icsText);
156$headers = $mime->headers();
157$body = $mime->get();
158printHeaderContentType($headers);
159printPartsStartAndEnd($body);
160print("\n");
161
162print("TEST: txt, html, calendar and attachment\n");
163$mime = new Mail_mime();
164$mime->setTXTBody($txtBody);
165$mime->setHTMLBody($htmlBody);
166$mime->setCalendarBody($icsText);
167$mime->addAttachment("test", 'application/octet-stream', 'attachment', false);
168$headers = $mime->headers();
169$body = $mime->get();
170printHeaderContentType($headers);
171printPartsStartAndEnd($body);
172print("\n");
173
174print "TEST: txt, html + html images, calendar, and attachment\n";
175$mime = new Mail_mime();
176$mime->setTXTBody($txtBody);
177$mime->setHTMLBody($htmlBody);
178$mime->addHTMLImage('testimage', 'image/gif', "bus.gif", false);
179$mime->setCalendarBody($icsText);
180$mime->addAttachment($icsText, 'application/ics', 'invite.ics', false);
181$headers = $mime->headers();
182$body = $mime->get();
183printHeaderContentType($headers);
184printPartsStartAndEnd($body);
185print("\n");
186?>
187--EXPECT--
188TEST: text
189Content-Type: text/plain
190
191TEST: html
192Content-Type: text/html
193
194TEST: attachments
195Content-Type: multipart/mixed
196    Content-Type: application/ics
197End part
198
199TEST: text + attachments
200Content-Type: multipart/mixed
201    Content-Type: text/plain
202    Content-Type: application/ics
203End part
204
205TEST: html + attachments
206Content-Type: multipart/mixed
207    Content-Type: text/html
208    Content-Type: application/ics
209End part
210
211TEST: html + inline images
212Content-Type: multipart/related
213    Content-Type: text/html
214    Content-Type: image/gif
215End part
216
217TEST: txt, html and attachment
218Content-Type: multipart/mixed
219    Content-Type: multipart/alternative
220        Content-Type: text/plain
221        Content-Type: text/html
222    End part
223    Content-Type: application/octet-stream
224End part
225
226TEST: calendar
227Content-Type: text/calendar
228
229TEST: txt + calendar
230Content-Type: multipart/alternative
231    Content-Type: text/plain
232    Content-Type: text/calendar
233End part
234
235TEST: txt, html, calendar
236Content-Type: multipart/alternative
237    Content-Type: text/plain
238    Content-Type: text/html
239    Content-Type: text/calendar
240End part
241
242TEST: txt, html + html images, and calendar
243Content-Type: multipart/alternative
244    Content-Type: text/plain
245    Content-Type: multipart/related
246        Content-Type: text/html
247        Content-Type: image/gif
248    End part
249    Content-Type: text/calendar
250End part
251
252TEST: txt, html, calendar and attachment
253Content-Type: multipart/mixed
254    Content-Type: multipart/alternative
255        Content-Type: text/plain
256        Content-Type: text/html
257        Content-Type: text/calendar
258    End part
259    Content-Type: application/octet-stream
260End part
261
262TEST: txt, html + html images, calendar, and attachment
263Content-Type: multipart/mixed
264    Content-Type: multipart/alternative
265        Content-Type: text/plain
266        Content-Type: multipart/related
267            Content-Type: text/html
268            Content-Type: image/gif
269        End part
270        Content-Type: text/calendar
271    End part
272    Content-Type: application/ics
273End part
274