1<?php 2 3namespace Sabre\CalDAV; 4 5use Sabre\DAV\PropPatch; 6 7require_once 'Sabre/CalDAV/TestUtil.php'; 8 9class CalendarTest extends \PHPUnit_Framework_TestCase { 10 11 /** 12 * @var Sabre\CalDAV\Backend\PDO 13 */ 14 protected $backend; 15 protected $principalBackend; 16 /** 17 * @var Sabre\CalDAV\Calendar 18 */ 19 protected $calendar; 20 /** 21 * @var array 22 */ 23 protected $calendars; 24 25 function setup() { 26 27 $this->backend = TestUtil::getBackend(); 28 29 $this->calendars = $this->backend->getCalendarsForUser('principals/user1'); 30 $this->assertEquals(2, count($this->calendars)); 31 $this->calendar = new Calendar($this->backend, $this->calendars[0]); 32 33 34 } 35 36 function teardown() { 37 38 unset($this->backend); 39 40 } 41 42 function testSimple() { 43 44 $this->assertEquals($this->calendars[0]['uri'], $this->calendar->getName()); 45 46 } 47 48 /** 49 * @depends testSimple 50 */ 51 function testUpdateProperties() { 52 53 $propPatch = new PropPatch([ 54 '{DAV:}displayname' => 'NewName', 55 ]); 56 57 $result = $this->calendar->propPatch($propPatch); 58 $result = $propPatch->commit(); 59 60 $this->assertEquals(true, $result); 61 62 $calendars2 = $this->backend->getCalendarsForUser('principals/user1'); 63 $this->assertEquals('NewName', $calendars2[0]['{DAV:}displayname']); 64 65 } 66 67 /** 68 * @depends testSimple 69 */ 70 function testGetProperties() { 71 72 $question = [ 73 '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set', 74 ]; 75 76 $result = $this->calendar->getProperties($question); 77 78 foreach ($question as $q) $this->assertArrayHasKey($q, $result); 79 80 $this->assertEquals(['VEVENT', 'VTODO'], $result['{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set']->getValue()); 81 82 } 83 84 /** 85 * @expectedException Sabre\DAV\Exception\NotFound 86 * @depends testSimple 87 */ 88 function testGetChildNotFound() { 89 90 $this->calendar->getChild('randomname'); 91 92 } 93 94 /** 95 * @depends testSimple 96 */ 97 function testGetChildren() { 98 99 $children = $this->calendar->getChildren(); 100 $this->assertEquals(1, count($children)); 101 102 $this->assertTrue($children[0] instanceof CalendarObject); 103 104 } 105 106 /** 107 * @depends testGetChildren 108 */ 109 function testChildExists() { 110 111 $this->assertFalse($this->calendar->childExists('foo')); 112 113 $children = $this->calendar->getChildren(); 114 $this->assertTrue($this->calendar->childExists($children[0]->getName())); 115 } 116 117 118 119 /** 120 * @expectedException Sabre\DAV\Exception\MethodNotAllowed 121 */ 122 function testCreateDirectory() { 123 124 $this->calendar->createDirectory('hello'); 125 126 } 127 128 /** 129 * @expectedException Sabre\DAV\Exception\MethodNotAllowed 130 */ 131 function testSetName() { 132 133 $this->calendar->setName('hello'); 134 135 } 136 137 function testGetLastModified() { 138 139 $this->assertNull($this->calendar->getLastModified()); 140 141 } 142 143 function testCreateFile() { 144 145 $file = fopen('php://memory', 'r+'); 146 fwrite($file, TestUtil::getTestCalendarData()); 147 rewind($file); 148 149 $this->calendar->createFile('hello', $file); 150 151 $file = $this->calendar->getChild('hello'); 152 $this->assertTrue($file instanceof CalendarObject); 153 154 } 155 156 function testCreateFileNoSupportedComponents() { 157 158 $file = fopen('php://memory', 'r+'); 159 fwrite($file, TestUtil::getTestCalendarData()); 160 rewind($file); 161 162 $calendar = new Calendar($this->backend, $this->calendars[1]); 163 $calendar->createFile('hello', $file); 164 165 $file = $calendar->getChild('hello'); 166 $this->assertTrue($file instanceof CalendarObject); 167 168 } 169 170 function testDelete() { 171 172 $this->calendar->delete(); 173 174 $calendars = $this->backend->getCalendarsForUser('principals/user1'); 175 $this->assertEquals(1, count($calendars)); 176 } 177 178 function testGetOwner() { 179 180 $this->assertEquals('principals/user1', $this->calendar->getOwner()); 181 182 } 183 184 function testGetGroup() { 185 186 $this->assertNull($this->calendar->getGroup()); 187 188 } 189 190 function testGetACL() { 191 192 $expected = [ 193 [ 194 'privilege' => '{DAV:}read', 195 'principal' => 'principals/user1', 196 'protected' => true, 197 ], 198 [ 199 'privilege' => '{DAV:}read', 200 'principal' => 'principals/user1/calendar-proxy-write', 201 'protected' => true, 202 ], 203 [ 204 'privilege' => '{DAV:}read', 205 'principal' => 'principals/user1/calendar-proxy-read', 206 'protected' => true, 207 ], 208 [ 209 'privilege' => '{' . Plugin::NS_CALDAV . '}read-free-busy', 210 'principal' => '{DAV:}authenticated', 211 'protected' => true, 212 ], 213 [ 214 'privilege' => '{DAV:}write', 215 'principal' => 'principals/user1', 216 'protected' => true, 217 ], 218 [ 219 'privilege' => '{DAV:}write', 220 'principal' => 'principals/user1/calendar-proxy-write', 221 'protected' => true, 222 ], 223 ]; 224 $this->assertEquals($expected, $this->calendar->getACL()); 225 226 } 227 228 /** 229 * @expectedException \Sabre\DAV\Exception\Forbidden 230 */ 231 function testSetACL() { 232 233 $this->calendar->setACL([]); 234 235 } 236 237 function testGetSyncToken() { 238 239 $this->assertNull($this->calendar->getSyncToken()); 240 241 } 242 243 function testGetSyncTokenNoSyncSupport() { 244 245 $calendar = new Calendar(new Backend\Mock([], []), []); 246 $this->assertNull($calendar->getSyncToken()); 247 248 } 249 250 function testGetChanges() { 251 252 $this->assertNull($this->calendar->getChanges(1, 1)); 253 254 } 255 256} 257