xref: /plugin/davcal/vendor/sabre/dav/tests/Sabre/CalDAV/Notifications/PluginTest.php (revision a1a3b6794e0e143a4a8b51d3185ce2d339be61ab)
1*a1a3b679SAndreas Boehler<?php
2*a1a3b679SAndreas Boehler
3*a1a3b679SAndreas Boehlernamespace Sabre\CalDAV\Notifications;
4*a1a3b679SAndreas Boehler
5*a1a3b679SAndreas Boehleruse Sabre\DAV;
6*a1a3b679SAndreas Boehleruse Sabre\DAVACL;
7*a1a3b679SAndreas Boehleruse Sabre\CalDAV;
8*a1a3b679SAndreas Boehleruse Sabre\CalDAV\Xml\Notification\SystemStatus;
9*a1a3b679SAndreas Boehleruse Sabre\HTTP;
10*a1a3b679SAndreas Boehleruse Sabre\HTTP\Request;
11*a1a3b679SAndreas Boehler
12*a1a3b679SAndreas Boehler
13*a1a3b679SAndreas Boehlerclass PluginTest extends \PHPUnit_Framework_TestCase {
14*a1a3b679SAndreas Boehler
15*a1a3b679SAndreas Boehler    /**
16*a1a3b679SAndreas Boehler     * @var Sabre\DAV\Server
17*a1a3b679SAndreas Boehler     */
18*a1a3b679SAndreas Boehler    protected $server;
19*a1a3b679SAndreas Boehler    /**
20*a1a3b679SAndreas Boehler     * @var Sabre\CalDAV\Plugin
21*a1a3b679SAndreas Boehler     */
22*a1a3b679SAndreas Boehler    protected $plugin;
23*a1a3b679SAndreas Boehler    protected $response;
24*a1a3b679SAndreas Boehler    /**
25*a1a3b679SAndreas Boehler     * @var Sabre\CalDAV\Backend\PDO
26*a1a3b679SAndreas Boehler     */
27*a1a3b679SAndreas Boehler    protected $caldavBackend;
28*a1a3b679SAndreas Boehler
29*a1a3b679SAndreas Boehler    function setup() {
30*a1a3b679SAndreas Boehler
31*a1a3b679SAndreas Boehler        $this->caldavBackend = new CalDAV\Backend\MockSharing();
32*a1a3b679SAndreas Boehler        $principalBackend = new DAVACL\PrincipalBackend\Mock();
33*a1a3b679SAndreas Boehler        $calendars = new CalDAV\CalendarRoot($principalBackend,$this->caldavBackend);
34*a1a3b679SAndreas Boehler        $principals = new CalDAV\Principal\Collection($principalBackend);
35*a1a3b679SAndreas Boehler
36*a1a3b679SAndreas Boehler        $root = new DAV\SimpleCollection('root');
37*a1a3b679SAndreas Boehler        $root->addChild($calendars);
38*a1a3b679SAndreas Boehler        $root->addChild($principals);
39*a1a3b679SAndreas Boehler
40*a1a3b679SAndreas Boehler        $this->server = new DAV\Server($root);
41*a1a3b679SAndreas Boehler        $this->server->sapi = new HTTP\SapiMock();
42*a1a3b679SAndreas Boehler        $this->server->debugExceptions = true;
43*a1a3b679SAndreas Boehler        $this->server->setBaseUri('/');
44*a1a3b679SAndreas Boehler        $this->plugin = new Plugin();
45*a1a3b679SAndreas Boehler        $this->server->addPlugin($this->plugin);
46*a1a3b679SAndreas Boehler
47*a1a3b679SAndreas Boehler
48*a1a3b679SAndreas Boehler        // Adding ACL plugin
49*a1a3b679SAndreas Boehler        $this->server->addPlugin(new DAVACL\Plugin());
50*a1a3b679SAndreas Boehler
51*a1a3b679SAndreas Boehler        // CalDAV is also required.
52*a1a3b679SAndreas Boehler        $this->server->addPlugin(new CalDAV\Plugin());
53*a1a3b679SAndreas Boehler        // Adding Auth plugin, and ensuring that we are logged in.
54*a1a3b679SAndreas Boehler        $authBackend = new DAV\Auth\Backend\Mock();
55*a1a3b679SAndreas Boehler        $authBackend->defaultUser = 'user1';
56*a1a3b679SAndreas Boehler        $authPlugin = new DAV\Auth\Plugin($authBackend, 'SabreDAV');
57*a1a3b679SAndreas Boehler        $this->server->addPlugin($authPlugin);
58*a1a3b679SAndreas Boehler
59*a1a3b679SAndreas Boehler        // This forces a login
60*a1a3b679SAndreas Boehler        $authPlugin->beforeMethod(new HTTP\Request(), new HTTP\Response());
61*a1a3b679SAndreas Boehler
62*a1a3b679SAndreas Boehler        $this->response = new HTTP\ResponseMock();
63*a1a3b679SAndreas Boehler        $this->server->httpResponse = $this->response;
64*a1a3b679SAndreas Boehler
65*a1a3b679SAndreas Boehler    }
66*a1a3b679SAndreas Boehler
67*a1a3b679SAndreas Boehler    function testSimple() {
68*a1a3b679SAndreas Boehler
69*a1a3b679SAndreas Boehler        $this->assertEquals([], $this->plugin->getFeatures());
70*a1a3b679SAndreas Boehler        $this->assertEquals('notifications', $this->plugin->getPluginName());
71*a1a3b679SAndreas Boehler        $this->assertEquals(
72*a1a3b679SAndreas Boehler            'notifications',
73*a1a3b679SAndreas Boehler            $this->plugin->getPluginInfo()['name']
74*a1a3b679SAndreas Boehler        );
75*a1a3b679SAndreas Boehler
76*a1a3b679SAndreas Boehler    }
77*a1a3b679SAndreas Boehler
78*a1a3b679SAndreas Boehler    function testPrincipalProperties() {
79*a1a3b679SAndreas Boehler
80*a1a3b679SAndreas Boehler        $httpRequest = new Request('GET', '/', ['Host' => 'sabredav.org']);
81*a1a3b679SAndreas Boehler        $this->server->httpRequest = $httpRequest;
82*a1a3b679SAndreas Boehler
83*a1a3b679SAndreas Boehler        $props = $this->server->getPropertiesForPath('/principals/user1',[
84*a1a3b679SAndreas Boehler            '{' . Plugin::NS_CALENDARSERVER . '}notification-URL',
85*a1a3b679SAndreas Boehler        ]);
86*a1a3b679SAndreas Boehler
87*a1a3b679SAndreas Boehler        $this->assertArrayHasKey(0,$props);
88*a1a3b679SAndreas Boehler        $this->assertArrayHasKey(200,$props[0]);
89*a1a3b679SAndreas Boehler
90*a1a3b679SAndreas Boehler
91*a1a3b679SAndreas Boehler        $this->assertArrayHasKey('{'.Plugin::NS_CALENDARSERVER .'}notification-URL',$props[0][200]);
92*a1a3b679SAndreas Boehler        $prop = $props[0][200]['{'.Plugin::NS_CALENDARSERVER .'}notification-URL'];
93*a1a3b679SAndreas Boehler        $this->assertTrue($prop instanceof DAV\Xml\Property\Href);
94*a1a3b679SAndreas Boehler        $this->assertEquals('calendars/user1/notifications/', $prop->getHref());
95*a1a3b679SAndreas Boehler
96*a1a3b679SAndreas Boehler    }
97*a1a3b679SAndreas Boehler
98*a1a3b679SAndreas Boehler    function testNotificationProperties() {
99*a1a3b679SAndreas Boehler
100*a1a3b679SAndreas Boehler        $notification = new Node(
101*a1a3b679SAndreas Boehler            $this->caldavBackend,
102*a1a3b679SAndreas Boehler            'principals/user1',
103*a1a3b679SAndreas Boehler            new SystemStatus('foo','"1"')
104*a1a3b679SAndreas Boehler        );
105*a1a3b679SAndreas Boehler        $propFind = new DAV\PropFind('calendars/user1/notifications', [
106*a1a3b679SAndreas Boehler            '{' . Plugin::NS_CALENDARSERVER . '}notificationtype',
107*a1a3b679SAndreas Boehler        ]);
108*a1a3b679SAndreas Boehler
109*a1a3b679SAndreas Boehler        $this->plugin->propFind($propFind, $notification);
110*a1a3b679SAndreas Boehler
111*a1a3b679SAndreas Boehler        $this->assertEquals(
112*a1a3b679SAndreas Boehler            $notification->getNotificationType(),
113*a1a3b679SAndreas Boehler            $propFind->get('{' . Plugin::NS_CALENDARSERVER . '}notificationtype')
114*a1a3b679SAndreas Boehler        );
115*a1a3b679SAndreas Boehler
116*a1a3b679SAndreas Boehler    }
117*a1a3b679SAndreas Boehler
118*a1a3b679SAndreas Boehler    function testNotificationGet() {
119*a1a3b679SAndreas Boehler
120*a1a3b679SAndreas Boehler        $notification = new Node(
121*a1a3b679SAndreas Boehler            $this->caldavBackend,
122*a1a3b679SAndreas Boehler            'principals/user1',
123*a1a3b679SAndreas Boehler            new SystemStatus('foo','"1"')
124*a1a3b679SAndreas Boehler        );
125*a1a3b679SAndreas Boehler
126*a1a3b679SAndreas Boehler        $server = new DAV\Server([$notification]);
127*a1a3b679SAndreas Boehler        $caldav = new Plugin();
128*a1a3b679SAndreas Boehler
129*a1a3b679SAndreas Boehler        $server->httpRequest = new Request('GET', '/foo.xml');
130*a1a3b679SAndreas Boehler        $httpResponse = new HTTP\ResponseMock();
131*a1a3b679SAndreas Boehler        $server->httpResponse = $httpResponse;
132*a1a3b679SAndreas Boehler
133*a1a3b679SAndreas Boehler        $server->addPlugin($caldav);
134*a1a3b679SAndreas Boehler
135*a1a3b679SAndreas Boehler        $caldav->httpGet($server->httpRequest, $server->httpResponse);
136*a1a3b679SAndreas Boehler
137*a1a3b679SAndreas Boehler        $this->assertEquals(200, $httpResponse->status);
138*a1a3b679SAndreas Boehler        $this->assertEquals([
139*a1a3b679SAndreas Boehler            'Content-Type' => ['application/xml'],
140*a1a3b679SAndreas Boehler            'ETag'         => ['"1"'],
141*a1a3b679SAndreas Boehler        ], $httpResponse->getHeaders());
142*a1a3b679SAndreas Boehler
143*a1a3b679SAndreas Boehler        $expected =
144*a1a3b679SAndreas Boehler'<?xml version="1.0" encoding="UTF-8"?>
145*a1a3b679SAndreas Boehler<cs:notification xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns" xmlns:cs="http://calendarserver.org/ns/">
146*a1a3b679SAndreas Boehler <cs:systemstatus type="high"/>
147*a1a3b679SAndreas Boehler</cs:notification>
148*a1a3b679SAndreas Boehler';
149*a1a3b679SAndreas Boehler
150*a1a3b679SAndreas Boehler        $this->assertXmlStringEqualsXmlString($expected, $httpResponse->getBodyAsString());
151*a1a3b679SAndreas Boehler
152*a1a3b679SAndreas Boehler    }
153*a1a3b679SAndreas Boehler
154*a1a3b679SAndreas Boehler    function testGETPassthrough() {
155*a1a3b679SAndreas Boehler
156*a1a3b679SAndreas Boehler        $server = new DAV\Server();
157*a1a3b679SAndreas Boehler        $caldav = new Plugin();
158*a1a3b679SAndreas Boehler
159*a1a3b679SAndreas Boehler        $httpResponse = new HTTP\ResponseMock();
160*a1a3b679SAndreas Boehler        $server->httpResponse = $httpResponse;
161*a1a3b679SAndreas Boehler
162*a1a3b679SAndreas Boehler        $server->addPlugin($caldav);
163*a1a3b679SAndreas Boehler
164*a1a3b679SAndreas Boehler        $this->assertNull($caldav->httpGet(new HTTP\Request('GET','/foozz'), $server->httpResponse));
165*a1a3b679SAndreas Boehler
166*a1a3b679SAndreas Boehler    }
167*a1a3b679SAndreas Boehler
168*a1a3b679SAndreas Boehler
169*a1a3b679SAndreas Boehler}
170