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