-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathorg-caldav-tests.el
1204 lines (1085 loc) · 41.9 KB
/
org-caldav-tests.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;; Test suite for org-caldav.el
;; Copyright, authorship, license: see org-caldav.el.
;; Run it from the org-caldav directory like this:
;; TZ="Europe/Berlin" emacs -Q -L . --eval '(setq org-caldav-url "CALDAV-URL")' -l org-caldav-testsuite.el -f ert
;;
;; On the server, there must already exist two calendars "test1" and "test2".
;; These will completely wiped by running this test!
;;
;; Hint: In case you need a test server, one lightweight option is:
;; docker run -v /path/to/data:/data tomsquest/docker-radicale
;; Then, you can create the test1 calendar from Thunderbird like so:
;; Thunderbird -> New Calendar -> Network -> Location:
;; http://localhost:5232/test/test1/ (the trailing slash is
;; important), with username "test" and blank password. Then add an
;; event from Thunderbird to make sure the calendar exists.
(require 'ert)
(require 'org)
(require 'org-caldav)
(require 'cl-lib)
(when (org-caldav-use-oauth2)
(org-caldav-check-oauth2 org-caldav-url)
(org-caldav-retrieve-oauth2-token org-caldav-url))
(defvar org-caldav-test-calendar-names '("test1" "test2"))
(setq org-caldav-debug-level (max 2 org-caldav-debug-level))
(setq org-caldav-delete-calendar-entries 'always)
(setq org-caldav-backup-file nil)
(setq org-caldav-test-preamble
"BEGIN:VCALENDAR
VERSION:2.0
CALSCALE:GREGORIAN
X-WR-TIMEZONE:Europe/Berlin
X-WR-CALDESC:
BEGIN:VTIMEZONE
TZID:Europe/Berlin
X-LIC-LOCATION:Europe/Berlin
BEGIN:DAYLIGHT
TZOFFSETFROM:+0100
TZOFFSETTO:+0200
TZNAME:CEST
DTSTART:19700329T020000
RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
END:DAYLIGHT
BEGIN:STANDARD
TZOFFSETFROM:+0200
TZOFFSETTO:+0100
TZNAME:CET
DTSTART:19701025T030000
RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
END:STANDARD
END:VTIMEZONE
")
;; First test event in calendar
(setq org-caldav-test-ics1
"BEGIN:VEVENT
DTSTART;VALUE=DATE:20121220
DTEND;VALUE=DATE:20121221
DTSTAMP:20121218T212132Z
UID:orgcaldavtest@cal1
CREATED:20121216T205929Z
DESCRIPTION:A first test
LAST-MODIFIED:20121218T212132Z
LOCATION:
SUMMARY:Test appointment Number 1
END:VEVENT
")
;; How it should end up in Org
(setq org-caldav-test-ics1-org
"\\* Test appointment Number 1
\\s-*:PROPERTIES:
\\s-*:ID:\\s-*orgcaldavtest@cal1
\\s-*:END:
\\s-*<2012-12-20 Thu>
\\s-*A first test")
;; Dto., second one
(setq org-caldav-test-ics2
"BEGIN:VEVENT
DTSTART;TZID=Europe/Berlin:20121205T190000
DTEND;TZID=Europe/Berlin:20121205T200000
DTSTAMP:20121219T213352Z
UID:orgcaldavtest-cal2
CREATED:20121219T213352Z
DESCRIPTION:A second test
LAST-MODIFIED:20121219T213352Z
LOCATION:
SUMMARY:Test appointment Number 2
END:VEVENT
")
(setq org-caldav-test-ics2-org
"\\* Test appointment Number 2
\\s-*:PROPERTIES:
\\s-*:ID:\\s-*orgcaldavtest-cal2
\\s-*:END:
\\s-*<2012-12-05 Wed 19:00-20:00>
\\s-*A second test")
;; First test task in calendar
(setq org-caldav-test-ics3
"BEGIN:VTODO
UID:orgcaldavtest@cal3
DTSTAMP:20220828T161432Z
DTSTART;VALUE=DATE:20121223
SUMMARY:A test task from iCal
DESCRIPTION:ical test task 1
PRIORITY:0
STATUS:NEEDS-ACTION
END:VTODO
")
(setq org-caldav-test-ics3-org
"\\* TODO A test task from iCal
\\s-*SCHEDULED: <2012-12-23 Sun>
\\s-*:PROPERTIES:
\\s-*:ID: orgcaldavtest@cal3
\\s-*:END:
\\s-*ical test task 1")
;; Second test task in calendar
(setq org-caldav-test-ics4
"BEGIN:VTODO
UID:orgcaldavtest-cal4
DTSTAMP:20220828T161432Z
DTSTART;VALUE=DATE:20121219
DUE;VALUE=DATE:20121223
SUMMARY:Another test task from iCal
DESCRIPTION:ical test task 2
PRIORITY:5
STATUS:NEEDS-ACTION
END:VTODO
")
(setq org-caldav-test-ics4-org
"\\* TODO \\[#B\\] Another test task from iCal
\\s-*DEADLINE: <2012-12-23 Sun> SCHEDULED: <2012-12-19 Wed>
\\s-*:PROPERTIES:
\\s-*:ID: orgcaldavtest-cal4
\\s-*:END:
\\s-*ical test task 2")
;; First test entry in Org which should end up in calendar
(setq org-caldav-test-org1
"* This is a test
:PROPERTIES:
:ID: orgcaldavtest@org1
:END:
<2012-12-23 Sun 20:00-21:00>
Foo Bar Baz
")
;; Dto., second one
(setq org-caldav-test-org2
"* This is another test
:PROPERTIES:
:ID: orgcaldavtest-org2
:END:
<2012-12-19 Wed 19:00-21:00>
Baz Bar Foo
")
(setq org-caldav-test-org3
"* This is a test with a tag :sometag:
:PROPERTIES:
:ID: orgcaldavtest-org3
:END:
<2012-12-20 Thu 19:00-21:00>
moose
")
;; First test task in Org which should end up in calendar
(setq org-caldav-test-org4
"* TODO A test task from Org
SCHEDULED: <2012-12-23 Sun>
:PROPERTIES:
:ID: orgcaldavtest@org4
:END:
Org task 1
")
;; Dto., second one
(setq org-caldav-test-org5
"* TODO [#B] Another test task from Org
DEADLINE: <2012-12-23 Sun> SCHEDULED: <2012-12-19 Wed>
:PROPERTIES:
:ID: orgcaldavtest-org5
:END:
Org task 2
")
;; All events after sync.
(setq org-caldav-test-allevents
'("orgcaldavtest@org1" "orgcaldavtest-org2" "orgcaldavtest@cal1" "orgcaldavtest-cal2"))
(setq org-caldav-test-alltodos
'("orgcaldavtest@org4" "orgcaldavtest-org5" "orgcaldavtest@cal3" "orgcaldavtest-cal4"))
(setq org-caldav-test-sync-result
'(("test1" "orgcaldavtest@cal1" new-in-cal cal->org)
("test1" "orgcaldavtest-cal2" new-in-cal cal->org)))
;; Test files.
(defun org-caldav-test-calendar-empty-p ()
"Check if calendar is empty."
(let ((output (org-caldav-url-dav-get-properties
(org-caldav-events-url) "resourcetype")))
(unless (eq (plist-get (cdar output) 'DAV:status) 200)
(error "Could not query CalDAV URL %s: %s." (org-caldav-events-url) (prin1-to-string output)))
(= (length output) 1)))
(defun org-caldav-test-set-up ()
"Make a clean slate."
(message "SET UP")
(unless (org-caldav-test-calendar-empty-p)
(dolist (cur (org-caldav-get-event-etag-list))
(message "Deleting %s" (car cur))
(org-caldav-delete-event (car cur))))
(should (org-caldav-test-calendar-empty-p)))
(defun org-caldav-test-put-events ()
"Put initial events to calendar."
(message "PUT")
(let ((org-caldav-calendar-preamble org-caldav-test-preamble)
events)
(with-temp-buffer
(insert org-caldav-test-ics1)
(should (org-caldav-put-event (current-buffer)))
(erase-buffer)
(insert org-caldav-test-ics2)
(should (org-caldav-put-event (current-buffer))))
(should
(setq events
(org-caldav-get-event-etag-list)))
(should (assoc "orgcaldavtest@cal1" events))
(should (assoc "orgcaldavtest-cal2" events))))
(defun org-caldav-test-setup-temp-files ()
(let ((tmpdir (make-temp-file "org-caldav-test-" t)))
(message "Using tempdir %s" tmpdir)
(setq org-caldav-save-directory (expand-file-name "org-caldav-savedir" tmpdir)
org-caldav-backup-file (expand-file-name "org-caldav-test-backup.org" tmpdir)
org-caldav-test-orgfile (expand-file-name "test.org" tmpdir)
org-caldav-test-second-orgfile (expand-file-name "test-second.org" tmpdir)
org-caldav-test-inbox (expand-file-name "inbox.org" tmpdir)
org-id-locations-file (expand-file-name "org-id-locations" tmpdir)
org-id-locations nil
org-id-files nil
org-caldav-test-current-tempdir tmpdir))
(make-directory org-caldav-save-directory)
(with-current-buffer (find-file-noselect org-caldav-test-inbox)
(save-buffer)))
(defun org-caldav-test-cleanup ()
(dolist (name '("test.org" "test-second.org" "inbox.org" "org-id-locations"))
(let ((buf (get-buffer name)))
(when buf
(with-current-buffer buf
(set-buffer-modified-p nil)
(kill-buffer)))))
(setq org-id-locations nil)
(setq org-caldav-event-list nil)
(when org-caldav-test-current-tempdir
(delete-directory org-caldav-test-current-tempdir t)
(setq org-caldav-test-current-tempdir nil)))
;; This is one, big, big test, since pretty much everything depends on
;; the current calendar/org state and I cannot easily split it.
(ert-deftest org-caldav-01-sync-test ()
(message "Setting up temporary files")
(org-caldav-test-setup-temp-files)
(setq org-caldav-calendar-id (car org-caldav-test-calendar-names))
;; Set up orgfile.
(with-current-buffer (find-file-noselect org-caldav-test-orgfile)
(insert org-caldav-test-org1 "\n")
(insert org-caldav-test-org2 "\n")
(save-buffer))
;; Set up data for org-caldav.
(setq org-caldav-files (list org-caldav-test-orgfile))
(setq org-caldav-inbox org-caldav-test-inbox)
(message "Cleaning up upstream calendars")
(org-caldav-test-set-up)
(message "Putting events")
(org-caldav-test-put-events)
(message "1st SYNC")
;; Do the sync.
(org-caldav-sync)
;; Check result.
(should (member `(,org-caldav-calendar-id "orgcaldavtest@cal1" new-in-cal cal->org)
org-caldav-sync-result))
(should (member `(,org-caldav-calendar-id "orgcaldavtest-cal2" new-in-cal cal->org)
org-caldav-sync-result))
(should (member `(,org-caldav-calendar-id "orgcaldavtest@org1" new-in-org org->cal)
org-caldav-sync-result))
(should (member `(,org-caldav-calendar-id "orgcaldavtest-org2" new-in-org org->cal)
org-caldav-sync-result))
;; State file should exist now.
(should (file-exists-p
(org-caldav-sync-state-filename org-caldav-calendar-id)))
(let ((calevents (org-caldav-get-event-etag-list)))
(should (= (length calevents) (length org-caldav-test-allevents)))
;; Org events should be in cal.
(dolist (cur org-caldav-test-allevents)
(should (assoc cur calevents))))
;; Cal events should be in Org.
(with-current-buffer (find-file-noselect org-caldav-test-inbox)
(goto-char (point-min))
(should (re-search-forward org-caldav-test-ics1-org nil t))
(goto-char (point-min))
(should (re-search-forward org-caldav-test-ics2-org nil t)))
(message "2nd SYNC")
;; Sync again.
(org-caldav-sync)
;; Nothing should have happened.
(should-not org-caldav-sync-result)
(message "Changing org events")
;; Now change events in Org
(with-current-buffer (find-buffer-visiting org-caldav-test-orgfile)
(goto-char (point-min))
(search-forward "This is another test")
(replace-match "This is a changed test heading")
(search-forward "<2012-12-19 Wed 19:00-21:00>")
(replace-match "<2012-12-19 Thu 20:00-22:00>"))
(with-current-buffer (find-buffer-visiting org-caldav-test-inbox)
(goto-char (point-min))
(search-forward "Test appointment Number 2")
(replace-match "Appointment number 2 was changed!")
(search-forward "<2012-12-05 Wed 19:00-20:00>")
(replace-match "<2012-12-04 Tue 18:00-19:00>"))
;; And sync...
(message "3rd SYNC")
(org-caldav-sync)
(should (equal `((,org-caldav-calendar-id "orgcaldavtest-cal2" changed-in-org org->cal)
(,org-caldav-calendar-id "orgcaldavtest-org2" changed-in-org org->cal))
org-caldav-sync-result))
;; Check if those events correctly end up in calendar.
(with-current-buffer (org-caldav-get-event "orgcaldavtest-cal2")
(goto-char (point-min))
(save-excursion
(should (search-forward "SUMMARY:Appointment number 2 was changed!")))
(save-excursion
(should (re-search-forward "DTSTART.*:20121204T\\(170000Z\\|180000\\)" nil t)))
(save-excursion
(should (re-search-forward "DTEND.*:20121204T\\(180000Z\\|190000\\)" nil t))))
(with-current-buffer (org-caldav-get-event "orgcaldavtest-org2")
(goto-char (point-min))
(save-excursion
(should (search-forward "SUMMARY:This is a changed test heading")))
(save-excursion
(should (re-search-forward "DTSTART.*:20121219T\\(190000Z\\|200000\\)" nil t)))
(save-excursion
(should (re-search-forward "DTEND.*:20121219T\\(210000Z\\|220000\\)" nil t))))
;; Now change events in Cal
(message "Changing events in calendar")
(with-current-buffer (org-caldav-get-event "orgcaldavtest@cal1")
(goto-char (point-min))
(save-excursion
(search-forward "SUMMARY:Test appointment Number 1")
(replace-match "SUMMARY:Changed test appointment Number 1"))
(save-excursion
(re-search-forward "DTSTART\\(;.*\\)?:\\(20121220\\)")
(replace-match "20121212" nil nil nil 2))
(save-excursion
(re-search-forward "DTEND\\(;.*\\)?:\\(20121221\\)")
(replace-match "20121213" nil nil nil 2))
(save-excursion
(when (re-search-forward "SEQUENCE:\\s-*\\([0-9]+\\)" nil t)
(replace-match (number-to-string
(1+ (string-to-number (match-string 1))))
nil t nil 1)))
(message "PUTting first changed event")
(should (org-caldav-save-resource
(concat (org-caldav-events-url) (url-hexify-string "[email protected]"))
(encode-coding-string (buffer-string) 'utf-8))))
(with-current-buffer (org-caldav-get-event "orgcaldavtest@org1")
(goto-char (point-min))
(save-excursion
(search-forward "SUMMARY:This is a test")
(replace-match "SUMMARY:This is a changed test"))
(save-excursion
(if (re-search-forward "DTSTART\\(;.*\\)?:\\(20121223T190000Z\\)" nil t)
(replace-match "20121213T180000Z" nil nil nil 2)
(re-search-forward "DTSTART\\(;.*\\)?:\\(20121223T200000\\)")
(replace-match "20121213T190000" nil nil nil 2)))
(save-excursion
(if (re-search-forward "DTEND\\(;.*\\)?:\\(20121223T200000Z\\)" nil t)
(replace-match "20121213T190000Z" nil nil nil 2)
(re-search-forward "DTEND\\(;.*\\)?:\\(20121223T210000\\)")
(replace-match "20121213T200000" nil nil nil 2)))
(save-excursion
(when (re-search-forward "SEQUENCE:\\s-*\\([0-9]+\\)" nil t)
(replace-match (number-to-string
(1+ (string-to-number (match-string 1))))
nil t nil 1)))
(message "PUTting second changed event")
(should (org-caldav-save-resource
(concat (org-caldav-events-url) "[email protected]")
(encode-coding-string (buffer-string) 'utf-8))))
;; Aaaand sync!
(message "4th SYNC")
(org-caldav-sync)
(should (equal `((,org-caldav-calendar-id "orgcaldavtest@cal1" changed-in-cal cal->org)
(,org-caldav-calendar-id "orgcaldavtest@org1" changed-in-cal cal->org))
org-caldav-sync-result))
(with-current-buffer (find-file-noselect org-caldav-test-inbox)
(goto-char (point-min))
(should (re-search-forward
"* Changed test appointment Number 1
\\s-*:PROPERTIES:
\\s-*:ID:\\s-*orgcaldavtest@cal1
\\s-*:END:
\\s-*<2012-12-12 Wed>
\\s-*A first test")))
(message "Deleting event in Org")
(with-current-buffer (find-file-noselect org-caldav-test-orgfile)
(goto-char (point-min))
(should (re-search-forward
"* This is a changed test
\\s-*:PROPERTIES:
\\s-*:ID:\\s-*orgcaldavtest@org1
\\s-*:END:
\\s-*<2012-12-13 Thu 19:00-20:00>
\\s-*Foo Bar Baz"))
;; Delete this event in Org
(replace-match ""))
;; Sync
(message "6th SYNC")
(org-caldav-sync)
;; Event should be deleted in calendar
(let ((calevents (org-caldav-get-event-etag-list)))
(should (= (length calevents) 3))
(should-not (assoc '"orgcaldavtest@org1" calevents)))
(should
(equal org-caldav-sync-result
`((,org-caldav-calendar-id "orgcaldavtest@org1" deleted-in-org removed-from-cal))))
(should-not
(assoc '"orgcaldavtest@org1" org-caldav-event-list))
;; Delete event in calendar
(message "Delete event in calendar")
(should (org-caldav-delete-event "orgcaldavtest-org2"))
;; Sync one last time
(message "7th SYNC")
(let ((org-caldav-delete-org-entries 'always))
(org-caldav-sync))
(should
(equal org-caldav-sync-result
`((,org-caldav-calendar-id "orgcaldavtest-org2" deleted-in-cal removed-from-org))))
;; There shouldn't be anything left in that buffer
(with-current-buffer (find-file-noselect org-caldav-test-orgfile)
(goto-char (point-min))
(should-not (re-search-forward "[:alnum:]" nil t)))
(should-not
(assoc '"orgcaldavtest-org2" org-caldav-event-list))
(org-caldav-test-cleanup)
)
(ert-deftest org-caldav-02-change-heading-test ()
(with-current-buffer (get-buffer-create "headingtest")
(erase-buffer)
(insert "* This is a test without timestamp in headline\n"
" <2009-08-08 Sat 10:00>\n whatever\n foo\n bar\n")
(insert "* This is a test <2009-08-08 Sat> \n"
" whatever\n foo\n bar\n")
(insert "* <2009-08-08 Sat 14:00> This is another test\n"
" whatever\n foo\n bar\n")
(org-mode)
(goto-char (point-min))
(save-excursion
(org-caldav-change-heading "first changed heading"))
(should (looking-at "^\\* first changed heading$"))
(search-forward "*" nil t 2)
(beginning-of-line)
(save-excursion
(org-caldav-change-heading "second changed heading"))
(should (looking-at "^\\* second changed heading <2009-08-08 Sat> $"))
(search-forward "*" nil t 2)
(beginning-of-line)
(save-excursion
(org-caldav-change-heading "third changed heading"))
(should (looking-at "^\\* <2009-08-08 Sat 14:00> third changed heading\n"))
))
(ert-deftest org-caldav-03-insert-org-entry ()
"Make sure that `org-caldav-insert-org-event-or-todo' works fine."
(let ((entry '((start-d . "01 01 2015")
(start-t . "19:00")
(end-d ."01 01 2015")
(end-t . "20:00")
(summary . "The summary")
(description . "The description")
(location . "location")))
(org-caldav-select-tags ""))
(cl-flet ((write-entry (uid level)
(with-temp-buffer
(org-mode) ; useful to set org-mode's
; internal variables
(org-caldav-insert-org-event-or-todo
(append entry `((uid . ,uid)
(level . ,level))))
(buffer-string))))
(should (string-match (concat
"\\*\\s-+The summary\n"
"\\s-*:PROPERTIES:\n"
"\\s-*:LOCATION: location\n"
"\\s-*:END:\n"
"\\s-*<2015-01-01 Thu 19:00-20:00>\n"
"\\s-*The description\n")
(write-entry nil nil)))
(should (string-match (concat
"\\*\\*\\s-+The summary\n"
"\\s-*:PROPERTIES:\n"
"\\s-*:LOCATION: location\n"
"\\s-*:END:\n"
"\\s-*<2015-01-01 Thu 19:00-20:00>\n"
"\\s-*The description\n")
(write-entry nil 2)))
(should (string-match (concat "\\*\\s-+The summary\n"
"\\s-*:PROPERTIES:\n"
"\\s-*:ID:\\s-*1\n"
"\\s-*:LOCATION: location\n"
"\\s-*:END:\n"
"\\s-*<2015-01-01 Thu 19:00-20:00>\n"
"\\s-*The description\n")
(write-entry "1" nil))))))
(ert-deftest org-caldav-04-multiple-calendars ()
(org-caldav-test-setup-temp-files)
(with-current-buffer (find-file-noselect org-caldav-test-orgfile)
(insert org-caldav-test-org1)
(save-buffer))
(with-current-buffer (find-file-noselect org-caldav-test-second-orgfile)
(insert org-caldav-test-org2)
(save-buffer))
;; Delete calendar contents
(let ((org-caldav-calendar-id (car org-caldav-test-calendar-names)))
(org-caldav-test-set-up))
(let ((org-caldav-calendar-id (nth 1 org-caldav-test-calendar-names)))
(org-caldav-test-set-up))
(let
((org-caldav-calendars
`((:calendar-id ,(car org-caldav-test-calendar-names)
:url ,org-caldav-url
:files (,org-caldav-test-orgfile)
:inbox ,org-caldav-test-orgfile)
(:calendar-id ,(nth 1 org-caldav-test-calendar-names)
:url ,org-caldav-url
:files (,org-caldav-test-second-orgfile)
:inbox ,org-caldav-test-second-orgfile))))
(org-caldav-sync))
;; Check that each calendar has one event
(let
((org-caldav-calendar-id (car org-caldav-test-calendar-names)))
(should (org-caldav-get-event "orgcaldavtest@org1"))
(should-error (org-caldav-get-event "orgcaldavtest-org2")))
(let
((org-caldav-calendar-id (nth 1 org-caldav-test-calendar-names)))
(should-error (org-caldav-get-event "orgcaldavtest@org1"))
(should (org-caldav-get-event "orgcaldavtest-org2")))
(org-caldav-test-cleanup)
)
(ert-deftest org-caldav-05-multiple-calendars-agenda-skip-function ()
(org-caldav-test-setup-temp-files)
(with-current-buffer (find-file-noselect org-caldav-test-orgfile)
(insert org-caldav-test-org1)
(insert org-caldav-test-org2)
(insert org-caldav-test-org3)
(save-buffer))
;; Delete calendar contents
(let ((org-caldav-calendar-id (car org-caldav-test-calendar-names)))
(org-caldav-test-set-up))
(let ((org-caldav-calendar-id (nth 1 org-caldav-test-calendar-names)))
(org-caldav-test-set-up))
(message "Starting sync")
(let
((org-caldav-calendars
`((:calendar-id ,(car org-caldav-test-calendar-names)
:url ,org-caldav-url
:skip-conditions (regexp ":sometag:")
:files (,org-caldav-test-orgfile)
:inbox ,org-caldav-test-orgfile)
(:calendar-id ,(nth 1 org-caldav-test-calendar-names)
:url ,org-caldav-url
:skip-conditions (notregexp ":sometag:")
:files (,org-caldav-test-orgfile)
:inbox ,org-caldav-test-orgfile))))
(org-caldav-sync))
;; Check that each calendar has one event
(let
((org-caldav-calendar-id (car org-caldav-test-calendar-names)))
(should (org-caldav-get-event "orgcaldavtest@org1"))
(should (org-caldav-get-event "orgcaldavtest-org2"))
(should-error (org-caldav-get-event "orgcaldavtest-org3")))
(let
((org-caldav-calendar-id (nth 1 org-caldav-test-calendar-names)))
(should (org-caldav-get-event "orgcaldavtest-org3"))
(should-error (org-caldav-get-event "orgcaldavtest@org1"))
(should-error (org-caldav-get-event "orgcaldavtest-org2")))
;; Make sure org-agenda-skip-function-global is not set permanently
(should-not org-agenda-skip-function-global)
(org-caldav-test-cleanup)
)
;; Make sure setting org-caldav-files to 'nil' does not
;; do anything weird.
(ert-deftest org-caldav-06-org-caldav-files-nil ()
(message "Setting up temporary files")
(org-caldav-test-setup-temp-files)
(setq org-caldav-calendar-id (car org-caldav-test-calendar-names))
;; Set org-caldav-files to nil
(setq org-caldav-files nil)
(setq org-caldav-inbox org-caldav-test-inbox)
(message "Setting up upstream calendar")
(org-caldav-test-set-up)
(message "Putting events")
(org-caldav-test-put-events)
(org-caldav-sync)
;; Events must still be in calendar
(should (org-caldav-get-event "orgcaldavtest@cal1"))
(should (org-caldav-get-event "orgcaldavtest-cal2"))
;; Sync result
(should (or (equal org-caldav-test-sync-result
org-caldav-sync-result)
(equal (reverse org-caldav-test-sync-result)
org-caldav-sync-result)))
(org-caldav-test-cleanup))
;; Check that we are able to detect when an Org file was removed from
;; org-caldav-files between syncs.
(ert-deftest org-caldav-07-detect-removed-file ()
(message "Setting up temporary files")
(org-caldav-test-setup-temp-files)
(with-current-buffer (find-file-noselect org-caldav-test-orgfile)
(insert org-caldav-test-org1)
(save-buffer))
(with-current-buffer (find-file-noselect org-caldav-test-second-orgfile)
(insert org-caldav-test-org2)
(save-buffer))
(setq org-caldav-calendar-id (car org-caldav-test-calendar-names))
;; Set org-caldav-files to nil
(setq org-caldav-files (list org-caldav-test-orgfile org-caldav-test-second-orgfile))
(setq org-caldav-inbox org-caldav-test-inbox)
(message "Setting up upstream calendar")
(org-caldav-test-set-up)
(message "Putting events")
(org-caldav-test-put-events)
(message "1st sync")
(org-caldav-sync)
;; Remove one of the files
(setq org-caldav-files (list org-caldav-test-second-orgfile))
;; Sync again, binding yes-or-no-p to our test
(setq org-caldav-test-seen-prompt nil)
(let (octest-seen-prompt)
(cl-letf (((symbol-function 'yes-or-no-p)
(lambda (prompt)
(setq octest-seen-prompt prompt) nil)))
(message "2nd sync")
(should-error (org-caldav-sync))
(should (string-match "WARNING: Previously synced"
octest-seen-prompt))))
(org-caldav-test-cleanup))
(ert-deftest org-caldav-test-multiline-location ()
(with-temp-buffer
(org-mode)
(insert org-caldav-test-org1)
(goto-char (point-min))
(let ((orig-id (alist-get "ID" (org-entry-properties) nil nil #'string=)))
(org-caldav-change-location "multi\nline")
(let ((props (org-entry-properties)))
(should (string= (alist-get "ID" props nil nil #'string=) orig-id))
(should (string-match-p "multi" (alist-get "LOCATION" props nil nil #'string=)))
(should (string-match-p "line" (alist-get "LOCATION" props nil nil #'string=)))))))
(ert-deftest org-caldav-08-test-setting-sync-direction ()
(org-caldav-test-setup-temp-files)
(with-current-buffer (find-file-noselect org-caldav-test-orgfile)
(insert org-caldav-test-org1)
(insert org-caldav-test-org2)
(insert org-caldav-test-org3)
(save-buffer))
;; Delete calendar contents
(let ((org-caldav-calendar-id (car org-caldav-test-calendar-names)))
(org-caldav-test-set-up))
(let ((org-caldav-calendar-id (nth 1 org-caldav-test-calendar-names)))
(org-caldav-test-set-up))
(message "Starting sync")
(let
((org-caldav-calendars
;; First only syncs Org to calendar
`((:calendar-id ,(car org-caldav-test-calendar-names)
:url ,org-caldav-url
:files (,org-caldav-test-orgfile)
:inbox nil ;; No inbox needed
:sync-direction org->cal)
;; Second only syncs Calendar to Org inbox
(:calendar-id ,(nth 1 org-caldav-test-calendar-names)
:url ,org-caldav-url
:files nil ;; No files needed
:inbox ,org-caldav-test-inbox
:sync-direction cal->org))))
(org-caldav-sync)
;; First calendar should sync everything
(let
((org-caldav-calendar-id (car org-caldav-test-calendar-names)))
(should (org-caldav-get-event "orgcaldavtest@org1"))
(should (org-caldav-get-event "orgcaldavtest-org2"))
(should (org-caldav-get-event "orgcaldavtest-org3")))
;; Second calendar syncs nothing from org to cal
(let
((org-caldav-calendar-id (nth 1 org-caldav-test-calendar-names)))
(should-error (org-caldav-get-event "orgcaldavtest-org3"))
(should-error (org-caldav-get-event "orgcaldavtest@org1"))
(should-error (org-caldav-get-event "orgcaldavtest-org2")))
;; Put calendar events in both calendars
(let ((org-caldav-calendar-id (car org-caldav-test-calendar-names)))
(org-caldav-test-put-events))
(let ((org-caldav-calendar-id (nth 1 org-caldav-test-calendar-names)))
(org-caldav-test-put-events))
;; Sync again
(org-caldav-sync)
;; Events are still there in first
(let
((org-caldav-calendar-id (car org-caldav-test-calendar-names)))
(should (org-caldav-get-event "orgcaldavtest@org1"))
(should (org-caldav-get-event "orgcaldavtest-org2"))
(should (org-caldav-get-event "orgcaldavtest-org3")))
;; Events still not there in second
(let
((org-caldav-calendar-id (nth 1 org-caldav-test-calendar-names)))
(should-error (org-caldav-get-event "orgcaldavtest-org3"))
(should-error (org-caldav-get-event "orgcaldavtest@org1"))
(should-error (org-caldav-get-event "orgcaldavtest-org2")))
;; But second should have new events in inbox
(with-current-buffer (find-file-noselect org-caldav-test-inbox)
(goto-char (point-min))
(should (re-search-forward org-caldav-test-ics1-org nil t))
(goto-char (point-min))
(should (re-search-forward org-caldav-test-ics2-org nil t))))
(org-caldav-test-cleanup)
)
;; Based on org-caldav-01-sync-test, but modified for todos
(ert-deftest org-caldav-09-sync-test-todo ()
(let ((org-caldav-sync-todo t)
(org-icalendar-include-todo 'all))
(message "Setting up temporary files")
(org-caldav-test-setup-temp-files)
(setq org-caldav-calendar-id (car org-caldav-test-calendar-names))
;; Set up data for org-caldav.
(setq org-caldav-files (list org-caldav-test-orgfile))
(setq org-caldav-inbox org-caldav-test-inbox)
(message "Cleaning up upstream calendars")
(org-caldav-test-set-up)
;; Set up orgfile.
(with-current-buffer (find-file-noselect org-caldav-test-orgfile)
(insert org-caldav-test-org4 "\n")
(insert org-caldav-test-org5 "\n")
(save-buffer))
(message "Putting events")
(let ((org-caldav-calendar-preamble org-caldav-test-preamble)
events)
(with-temp-buffer
(insert org-caldav-test-ics3)
(should (org-caldav-put-event (current-buffer)))
(erase-buffer)
(insert org-caldav-test-ics4)
(should (org-caldav-put-event (current-buffer))))
(let ((events (org-caldav-get-event-etag-list)))
(should (assoc "orgcaldavtest@cal3" events))
(should (assoc "orgcaldavtest-cal4" events))))
(message "1st SYNC")
;; Do the sync.
(org-caldav-sync)
;;;; Check result.
(should (= (length (org-caldav-get-event-etag-list)) 4))
(should (member `(,org-caldav-calendar-id "orgcaldavtest@cal3" new-in-cal cal->org)
org-caldav-sync-result))
(should (member `(,org-caldav-calendar-id "orgcaldavtest-cal4" new-in-cal cal->org)
org-caldav-sync-result))
(should (member `(,org-caldav-calendar-id "orgcaldavtest@org4" new-in-org org->cal)
org-caldav-sync-result))
(should (member `(,org-caldav-calendar-id "orgcaldavtest-org5" new-in-org org->cal)
org-caldav-sync-result))
;; State file should exist now.
(should (file-exists-p
(org-caldav-sync-state-filename org-caldav-calendar-id)))
(let ((calevents (org-caldav-get-event-etag-list)))
(should (= (length calevents) (length org-caldav-test-alltodos)))
;; Org events should be in cal.
(dolist (cur org-caldav-test-alltodos)
(should (assoc cur calevents))))
;; Cal events should be in Org.
(with-current-buffer (find-file-noselect org-caldav-test-inbox)
(goto-char (point-min))
(should (re-search-forward org-caldav-test-ics3-org nil t))
(goto-char (point-min))
(should (re-search-forward org-caldav-test-ics4-org nil t)))
(message "2nd SYNC")
;; Sync again.
(org-caldav-sync)
;; Nothing should have happened.
(should-not org-caldav-sync-result)
(message "Changing org events")
;; Now change events in Org
(with-current-buffer (find-buffer-visiting org-caldav-test-orgfile)
(goto-char (point-min))
(search-forward "TODO A test task from Org")
(replace-match "DONE Finished test task from Org")
(search-forward "SCHEDULED:")
(replace-match "CLOSED: [2012-12-24 Mon 00:00] SCHEDULED:" t))
(with-current-buffer (find-buffer-visiting org-caldav-test-inbox)
(goto-char (point-min))
(search-forward "TODO [#B] Another test task from iCal")
(replace-match "DONE [#C] Another test task from iCal was finished!")
(search-forward "DEADLINE:")
(replace-match "CLOSED: [2012-12-20 Thu 00:00] DEADLINE:" t))
;; And sync...
(message "3rd SYNC")
(org-caldav-sync)
(should (equal `((,org-caldav-calendar-id "orgcaldavtest-cal4" changed-in-org org->cal)
(,org-caldav-calendar-id "orgcaldavtest@org4" changed-in-org org->cal))
org-caldav-sync-result))
;; Check if those events correctly end up in calendar.
(with-current-buffer (org-caldav-get-event "orgcaldavtest-cal4")
(goto-char (point-min))
(save-excursion
(should (search-forward "SUMMARY:Another test task from iCal was finished!")))
(save-excursion
(should (search-forward "PRIORITY:9")))
(save-excursion
(should (search-forward "STATUS:COMPLETED")))
(save-excursion
(should (re-search-forward "COMPLETED.*:20121220T000000"))))
(with-current-buffer (org-caldav-get-event "orgcaldavtest@org4")
(goto-char (point-min))
(save-excursion
(should (search-forward "SUMMARY:Finished test task from Org")))
(save-excursion
(should (search-forward "PRIORITY:0")))
(save-excursion
(should (search-forward "STATUS:COMPLETED")))
(save-excursion
(should (re-search-forward "COMPLETED.*:20121224T000000"))))
;; Now change events in Cal
(message "Changing events in calendar")
(with-current-buffer (org-caldav-get-event "orgcaldavtest@cal3")
(goto-char (point-min))
(save-excursion
(search-forward "SUMMARY:A test task from iCal")
(replace-match "SUMMARY:Changed A test task from iCal"))
(save-excursion
(re-search-forward "DTSTART\\(;.*\\)?:\\(20121223\\)")
(replace-match "20121224" nil nil nil 2))
(save-excursion
(when (re-search-forward "SEQUENCE:\\s-*\\([0-9]+\\)" nil t)
(replace-match (number-to-string
(1+ (string-to-number (match-string 1))))
nil t nil 1)))
(message "PUTting first changed event")
(should (org-caldav-save-resource
(concat (org-caldav-events-url) (url-hexify-string "[email protected]"))
(encode-coding-string (buffer-string) 'utf-8))))
(with-current-buffer (org-caldav-get-event "orgcaldavtest-org5")
(goto-char (point-min))
(save-excursion
(search-forward "SUMMARY:Another test task from Org")
(replace-match "SUMMARY:Changed Another test task from Org"))
(save-excursion
(search-forward "STATUS:NEEDS-ACTION")
(replace-match "STATUS:COMPLETED\nCOMPLETED:20121224T000000"))
(save-excursion
(search-forward "PERCENT-COMPLETE:0")
(replace-match "PERCENT-COMPLETE:100"))
(save-excursion
(when (re-search-forward "SEQUENCE:\\s-*\\([0-9]+\\)" nil t)
(replace-match (number-to-string
(1+ (string-to-number (match-string 1))))
nil t nil 1)))
(message "PUTting second changed event")
(should (org-caldav-save-resource
(concat (org-caldav-events-url) "orgcaldavtest-org5.ics")
(encode-coding-string (buffer-string) 'utf-8))))
;; Aaaand sync!
(message "4th SYNC")
(org-caldav-sync)
(should (equal `((,org-caldav-calendar-id "orgcaldavtest@cal3" changed-in-cal cal->org)
(,org-caldav-calendar-id "orgcaldavtest-org5" changed-in-cal cal->org))
org-caldav-sync-result))
(with-current-buffer (find-file-noselect org-caldav-test-inbox)
(goto-char (point-min))
(should (re-search-forward
"* TODO Changed A test task from iCal
\\s-*SCHEDULED: <2012-12-24 Mon>
\\s-*:PROPERTIES:
\\s-*:ID:\\s-*orgcaldavtest@cal3
\\s-*:END:
\\s-*ical test task 1")))
(message "Changing description in icalendar")
(with-current-buffer (org-caldav-get-event "orgcaldavtest@cal3")
(goto-char (point-min))
(save-excursion
(search-forward "DESCRIPTION:ical test task 1")
(replace-match "DESCRIPTION:ical test task 1 modified"))
(message "PUTting changed description")
(should (org-caldav-save-resource
(concat (org-caldav-events-url) (url-hexify-string "[email protected]"))
(encode-coding-string (buffer-string) 'utf-8))))
(message "5th SYNC")
(let ((org-caldav-sync-changes-to-org 'all))
(org-caldav-sync))
(should (equal `((,org-caldav-calendar-id "orgcaldavtest@cal3"
changed-in-cal cal->org))
org-caldav-sync-result))
(with-current-buffer (find-file-noselect org-caldav-test-inbox)
(goto-char (point-min))
(should (re-search-forward
"* TODO Changed A test task from iCal
\\s-*SCHEDULED: <2012-12-24 Mon>
\\s-*:PROPERTIES:
\\s-*:ID:\\s-*orgcaldavtest@cal3
\\s-*:END:
\\s-*ical test task 1 modified")))
(message "Deleting event in Org")
(with-current-buffer (find-file-noselect org-caldav-test-orgfile)
(goto-char (point-min))
(message (buffer-string))
(should (search-forward
"* DONE [#B] Changed Another test task from Org :test:
CLOSED: [2012-12-24 Mon 00:00] DEADLINE: <2012-12-23 Sun> SCHEDULED: <2012-12-19 Wed>
:PROPERTIES:
:ID: orgcaldavtest-org5
:END:
Org task 2
"))
;; Delete this event in Org
(replace-match ""))
;; Sync