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