1<?php
2
3namespace Sabre\CalDAV;
4
5use Sabre\DAV;
6use Sabre\DAVACL;
7use Sabre\HTTP;
8
9require_once 'Sabre/CalDAV/Backend/Mock.php';
10require_once 'Sabre/HTTP/ResponseMock.php';
11
12class FreeBusyReportTest extends \PHPUnit_Framework_TestCase {
13
14    /**
15     * @var Sabre\CalDAV\Plugin
16     */
17    protected $plugin;
18    /**
19     * @var Sabre\DAV\Server
20     */
21    protected $server;
22
23    function setUp() {
24
25        $obj1 = <<<ics
26BEGIN:VCALENDAR
27BEGIN:VEVENT
28DTSTART:20111005T120000Z
29DURATION:PT1H
30END:VEVENT
31END:VCALENDAR
32ics;
33
34        $obj2 = fopen('php://memory','r+');
35        fwrite($obj2,<<<ics
36BEGIN:VCALENDAR
37BEGIN:VEVENT
38DTSTART:20121005T120000Z
39DURATION:PT1H
40END:VEVENT
41END:VCALENDAR
42ics
43        );
44        rewind($obj2);
45
46        $obj3 = <<<ics
47BEGIN:VCALENDAR
48BEGIN:VEVENT
49DTSTART:20111006T120000
50DURATION:PT1H
51END:VEVENT
52END:VCALENDAR
53ics;
54
55        $calendarData = [
56            1 => [
57                'obj1' => [
58                    'calendarid' => 1,
59                    'uri' => 'event1.ics',
60                    'calendardata' => $obj1,
61                ],
62                'obj2' => [
63                    'calendarid' => 1,
64                    'uri' => 'event2.ics',
65                    'calendardata' => $obj2
66                ],
67                'obj3' => [
68                    'calendarid' => 1,
69                    'uri' => 'event3.ics',
70                    'calendardata' => $obj3
71                ]
72            ],
73        ];
74
75
76        $caldavBackend = new Backend\Mock([], $calendarData);
77
78        $calendar = new Calendar($caldavBackend, [
79            'id' => 1,
80            'uri' => 'calendar',
81            'principaluri' => 'principals/user1',
82            '{' . Plugin::NS_CALDAV . '}calendar-timezone' => "BEGIN:VCALENDAR\r\nBEGIN:VTIMEZONE\r\nTZID:Europe/Berlin\r\nEND:VTIMEZONE\r\nEND:VCALENDAR",
83        ]);
84
85        $this->server = new DAV\Server([$calendar]);
86
87        $request = HTTP\Sapi::createFromServerArray([
88            'REQUEST_URI' => '/calendar',
89        ]);
90        $this->server->httpRequest = $request;
91        $this->server->httpResponse = new HTTP\ResponseMock();
92
93        $this->plugin = new Plugin();
94        $this->server->addPlugin($this->plugin);
95
96    }
97
98    function testFreeBusyReport() {
99
100        $reportXML = <<<XML
101<?xml version="1.0"?>
102<c:free-busy-query xmlns:c="urn:ietf:params:xml:ns:caldav">
103    <c:time-range start="20111001T000000Z" end="20111101T000000Z" />
104</c:free-busy-query>
105XML;
106
107        $report = $this->server->xml->parse($reportXML, null, $rootElem);
108        $this->plugin->report($rootElem, $report);
109
110        $this->assertEquals(200, $this->server->httpResponse->status);
111        $this->assertEquals('text/calendar', $this->server->httpResponse->getHeader('Content-Type'));
112        $this->assertTrue(strpos($this->server->httpResponse->body, 'BEGIN:VFREEBUSY')!==false);
113        $this->assertTrue(strpos($this->server->httpResponse->body, '20111005T120000Z/20111005T130000Z')!==false);
114        $this->assertTrue(strpos($this->server->httpResponse->body, '20111006T100000Z/20111006T110000Z')!==false);
115
116    }
117
118    /**
119     * @expectedException Sabre\DAV\Exception\BadRequest
120     */
121    function testFreeBusyReportNoTimeRange() {
122
123        $reportXML = <<<XML
124<?xml version="1.0"?>
125<c:free-busy-query xmlns:c="urn:ietf:params:xml:ns:caldav">
126</c:free-busy-query>
127XML;
128
129        $report = $this->server->xml->parse($reportXML, null, $rootElem);
130        $this->plugin->report($rootElem, $report);
131
132    }
133
134    /**
135     * @expectedException Sabre\DAV\Exception\NotImplemented
136     */
137    function testFreeBusyReportWrongNode() {
138
139        $request = HTTP\Sapi::createFromServerArray(array(
140            'REQUEST_URI' => '/',
141        ));
142        $this->server->httpRequest = $request;
143
144        $reportXML = <<<XML
145<?xml version="1.0"?>
146<c:free-busy-query xmlns:c="urn:ietf:params:xml:ns:caldav">
147    <c:time-range start="20111001T000000Z" end="20111101T000000Z" />
148</c:free-busy-query>
149XML;
150
151        $report = $this->server->xml->parse($reportXML, null, $rootElem);
152        $this->plugin->report($rootElem, $report);
153
154    }
155
156    /**
157     * @expectedException Sabre\DAV\Exception
158     */
159    function testFreeBusyReportNoACLPlugin() {
160
161        $this->server = new DAV\Server();
162        $this->plugin = new Plugin();
163        $this->server->addPlugin($this->plugin);
164
165        $reportXML = <<<XML
166<?xml version="1.0"?>
167<c:free-busy-query xmlns:c="urn:ietf:params:xml:ns:caldav">
168    <c:time-range start="20111001T000000Z" end="20111101T000000Z" />
169</c:free-busy-query>
170XML;
171
172        $report = $this->server->xml->parse($reportXML, null, $rootElem);
173        $this->plugin->report($rootElem, $report);
174
175    }
176}
177