ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
JsonTest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Sabre\VObject\Parser;
4 
6 use Sabre\VObject;
7 
8 class JsonTest extends TestCase {
9 
10  function testRoundTripJCard() {
11 
12  $input = [
13  "vcard",
14  [
15  [
16  "version",
17  new \StdClass(),
18  "text",
19  "4.0"
20  ],
21  [
22  "prodid",
23  new \StdClass(),
24  "text",
25  "-//Sabre//Sabre VObject " . VObject\Version::VERSION . "//EN",
26  ],
27  [
28  "uid",
29  new \StdClass(),
30  "text",
31  "foo",
32  ],
33  [
34  "bday",
35  new \StdClass(),
36  "date-and-or-time",
37  "1985-04-07",
38  ],
39  [
40  "bday",
41  (object)[
42  'x-param' => [1,2],
43  ],
44  "date",
45  "1979-12-25",
46  ],
47  [
48  "bday",
49  new \StdClass(),
50  "date-time",
51  "1979-12-25T02:00:00",
52  ],
53  [
54  "rev",
55  new \StdClass(),
56  "timestamp",
57  "1995-10-31T22:27:10Z",
58  ],
59  [
60  "lang",
61  new \StdClass(),
62  "language-tag",
63  "nl",
64  ],
65  [
66  "n",
67  new \StdClass(),
68  "text",
69  ["Last", "First", "Middle", "", ""],
70  ],
71  [
72  "tel",
73  (object)[
74  "group" => "item1",
75  ],
76  "text",
77  "+1 555 123456",
78  ],
79  [
80  "x-ab-label",
81  (object)[
82  "group" => "item1",
83  ],
84  "unknown",
85  "Walkie Talkie",
86  ],
87  [
88  "adr",
89  new \StdClass(),
90  "text",
91  [
92  "",
93  "",
94  ["My Street", "Left Side", "Second Shack"],
95  "Hometown",
96  "PA",
97  "18252",
98  "U.S.A",
99  ],
100  ],
101 
102  [
103  "x-truncated",
104  new \StdClass(),
105  "date",
106  "--12-25",
107  ],
108  [
109  "x-time-local",
110  new \StdClass(),
111  "time",
112  "12:30:00"
113  ],
114  [
115  "x-time-utc",
116  new \StdClass(),
117  "time",
118  "12:30:00Z"
119  ],
120  [
121  "x-time-offset",
122  new \StdClass(),
123  "time",
124  "12:30:00-08:00"
125  ],
126  [
127  "x-time-reduced",
128  new \StdClass(),
129  "time",
130  "23"
131  ],
132  [
133  "x-time-truncated",
134  new \StdClass(),
135  "time",
136  "--30"
137  ],
138  [
139  "x-karma-points",
140  new \StdClass(),
141  "integer",
142  42
143  ],
144  [
145  "x-grade",
146  new \StdClass(),
147  "float",
148  1.3
149  ],
150  [
151  "tz",
152  new \StdClass(),
153  "utc-offset",
154  "-05:00",
155  ],
156  ],
157  ];
158 
159  $parser = new Json(json_encode($input));
160  $vobj = $parser->parse();
161 
163 
164  $result = $vobj->serialize();
165  $expected = <<<VCF
166 BEGIN:VCARD
167 VERSION:4.0
168 PRODID:-//Sabre//Sabre VObject $version//EN
169 UID:foo
170 BDAY:1985-04-07
171 BDAY;X-PARAM=1,2;VALUE=DATE:1979-12-25
172 BDAY;VALUE=DATE-TIME:1979-12-25T02:00:00
173 REV:1995-10-31T22:27:10Z
174 LANG:nl
175 N:Last;First;Middle;;
176 item1.TEL:+1 555 123456
177 item1.X-AB-LABEL:Walkie Talkie
178 ADR:;;My Street,Left Side,Second Shack;Hometown;PA;18252;U.S.A
179 X-TRUNCATED;VALUE=DATE:--12-25
180 X-TIME-LOCAL;VALUE=TIME:123000
181 X-TIME-UTC;VALUE=TIME:123000Z
182 X-TIME-OFFSET;VALUE=TIME:123000-0800
183 X-TIME-REDUCED;VALUE=TIME:23
184 X-TIME-TRUNCATED;VALUE=TIME:--30
185 X-KARMA-POINTS;VALUE=INTEGER:42
186 X-GRADE;VALUE=FLOAT:1.3
187 TZ;VALUE=UTC-OFFSET:-0500
188 END:VCARD
189 
190 VCF;
191  $this->assertEquals($expected, str_replace("\r", "", $result));
192 
193  $this->assertEquals(
194  $input,
195  $vobj->jsonSerialize()
196  );
197 
198  }
199 
200  function testRoundTripJCal() {
201 
202  $input = [
203  "vcalendar",
204  [
205  [
206  "version",
207  new \StdClass(),
208  "text",
209  "2.0"
210  ],
211  [
212  "prodid",
213  new \StdClass(),
214  "text",
215  "-//Sabre//Sabre VObject " . VObject\Version::VERSION . "//EN",
216  ],
217  [
218  "calscale",
219  new \StdClass(),
220  "text",
221  "GREGORIAN"
222  ],
223  ],
224  [
225  ["vevent",
226  [
227  [
228  "uid", new \StdClass(), "text", "foo",
229  ],
230  [
231  "dtstart", new \StdClass(), "date", "2013-05-26",
232  ],
233  [
234  "duration", new \StdClass(), "duration", "P1D",
235  ],
236  [
237  "categories", new \StdClass(), "text", "home", "testing",
238  ],
239  [
240  "created", new \StdClass(), "date-time", "2013-05-26T18:10:00Z",
241  ],
242  [
243  "attach", new \StdClass(), "binary", base64_encode('attachment')
244  ],
245  [
246  "attendee", new \StdClass(), "cal-address", "mailto:armin@example.org",
247  ],
248  [
249  "attendee",
250  (object)[
251  "cn" => "Dominik",
252  "partstat" => "DECLINED",
253  ],
254  "cal-address",
255  "mailto:dominik@example.org"
256  ],
257  [
258  "geo", new \StdClass(), "float", [51.96668, 7.61876],
259  ],
260  [
261  "sequence", new \StdClass(), "integer", 5
262  ],
263  [
264  "freebusy", new \StdClass(), "period", ["2013-05-26T21:02:13", "PT1H"], ["2013-06-26T12:00:00", "2013-06-26T13:00:00"],
265  ],
266  [
267  "url", new \StdClass(), "uri", "http://example.org/",
268  ],
269  [
270  "tzoffsetfrom", new \StdClass(), "utc-offset", "+05:00",
271  ],
272  [
273  "rrule", new \StdClass(), "recur", [
274  'freq' => 'WEEKLY',
275  'byday' => ['MO', 'TU'],
276  ],
277  ],
278  [
279  "x-bool", new \StdClass(), "boolean", true
280  ],
281  [
282  "x-time", new \StdClass(), "time", "08:00:00",
283  ],
284  [
285  "request-status",
286  new \StdClass(),
287  "text",
288  ["2.0", "Success"],
289  ],
290  [
291  "request-status",
292  new \StdClass(),
293  "text",
294  ["3.7", "Invalid Calendar User", "ATTENDEE:mailto:jsmith@example.org"],
295  ],
296  ],
297  [
298  ["valarm",
299  [
300  [
301  "action", new \StdClass(), "text", "DISPLAY",
302  ],
303  ],
304  [],
305  ],
306  ],
307  ]
308  ],
309  ];
310 
311  $parser = new Json(json_encode($input));
312  $vobj = $parser->parse();
313  $result = $vobj->serialize();
314 
316 
317  $expected = <<<VCF
318 BEGIN:VCALENDAR
319 VERSION:2.0
320 PRODID:-//Sabre//Sabre VObject $version//EN
321 CALSCALE:GREGORIAN
322 BEGIN:VEVENT
323 UID:foo
324 DTSTART;VALUE=DATE:20130526
325 DURATION:P1D
326 CATEGORIES:home,testing
327 CREATED:20130526T181000Z
328 ATTACH;VALUE=BINARY:YXR0YWNobWVudA==
329 ATTENDEE:mailto:armin@example.org
330 ATTENDEE;CN=Dominik;PARTSTAT=DECLINED:mailto:dominik@example.org
331 GEO:51.96668;7.61876
332 SEQUENCE:5
333 FREEBUSY:20130526T210213/PT1H,20130626T120000/20130626T130000
334 URL;VALUE=URI:http://example.org/
335 TZOFFSETFROM:+0500
336 RRULE:FREQ=WEEKLY;BYDAY=MO,TU
337 X-BOOL;VALUE=BOOLEAN:TRUE
338 X-TIME;VALUE=TIME:080000
339 REQUEST-STATUS:2.0;Success
340 REQUEST-STATUS:3.7;Invalid Calendar User;ATTENDEE:mailto:jsmith@example.org
341 BEGIN:VALARM
342 ACTION:DISPLAY
343 END:VALARM
344 END:VEVENT
345 END:VCALENDAR
346 
347 VCF;
348  $this->assertEquals($expected, str_replace("\r", "", $result));
349 
350  $this->assertEquals(
351  $input,
352  $vobj->jsonSerialize()
353  );
354 
355  }
356 
357  function testParseStreamArg() {
358 
359  $input = [
360  "vcard",
361  [
362  [
363  "FN", new \StdClass(), 'text', "foo",
364  ],
365  ],
366  ];
367 
368  $stream = fopen('php://memory', 'r+');
369  fwrite($stream, json_encode($input));
370  rewind($stream);
371 
373  $this->assertEquals('foo', $result->FN->getValue());
374 
375  }
376 
380  function testParseInvalidData() {
381 
382  $json = new Json();
383  $input = [
384  "vlist",
385  [
386  [
387  "FN", new \StdClass(), 'text', "foo",
388  ],
389  ],
390  ];
391 
392  $json->parse(json_encode($input), 0);
393 
394  }
395 }
$result
Json Parser.
Definition: Json.php:19
if($argc< 3) $input
const VERSION
Full version number.
Definition: Version.php:17
$stream
PHP stream implementation.
$version
Definition: build.php:27
static http()
Fetches the global http state from ILIAS.
$vobj
Definition: rrulebench.php:21
static readJson($data, $options=0)
Parses a jCard or jCal object, and returns the top component.
Definition: Reader.php:67
$parser
Definition: BPMN2Parser.php:23