PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/Sabre/CalDAV/CalendarObjectTest.php

https://github.com/KOLANICH/SabreDAV
PHP | 359 lines | 233 code | 96 blank | 30 comment | 2 complexity | dc3fd205c0801c0717cd7957b0300cfb MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. namespace Sabre\CalDAV;
  3. use Sabre\DAVACL;
  4. require_once 'Sabre/CalDAV/TestUtil.php';
  5. class CalendarObjectTest extends \PHPUnit_Framework_TestCase {
  6. /**
  7. * @var Sabre\CalDAV\Backend_PDO
  8. */
  9. protected $backend;
  10. /**
  11. * @var Sabre\CalDAV\Calendar
  12. */
  13. protected $calendar;
  14. protected $principalBackend;
  15. function setup() {
  16. if (!SABRE_HASSQLITE) $this->markTestSkipped('SQLite driver is not available');
  17. $this->backend = TestUtil::getBackend();
  18. $calendars = $this->backend->getCalendarsForUser('principals/user1');
  19. $this->assertEquals(2,count($calendars));
  20. $this->calendar = new Calendar($this->backend, $calendars[0]);
  21. }
  22. function teardown() {
  23. unset($this->calendar);
  24. unset($this->backend);
  25. }
  26. function testSetup() {
  27. $children = $this->calendar->getChildren();
  28. $this->assertTrue($children[0] instanceof CalendarObject);
  29. $this->assertInternalType('string',$children[0]->getName());
  30. $this->assertInternalType('string',$children[0]->get());
  31. $this->assertInternalType('string',$children[0]->getETag());
  32. $this->assertEquals('text/calendar; charset=utf-8', $children[0]->getContentType());
  33. }
  34. /**
  35. * @expectedException InvalidArgumentException
  36. */
  37. function testInvalidArg1() {
  38. $obj = new CalendarObject(
  39. new Backend\Mock(array(),array()),
  40. array(),
  41. array()
  42. );
  43. }
  44. /**
  45. * @expectedException InvalidArgumentException
  46. */
  47. function testInvalidArg2() {
  48. $obj = new CalendarObject(
  49. new Backend\Mock(array(),array()),
  50. array(),
  51. array('calendarid' => '1')
  52. );
  53. }
  54. /**
  55. * @depends testSetup
  56. */
  57. function testPut() {
  58. $children = $this->calendar->getChildren();
  59. $this->assertTrue($children[0] instanceof CalendarObject);
  60. $newData = TestUtil::getTestCalendarData();
  61. $children[0]->put($newData);
  62. $this->assertEquals($newData, $children[0]->get());
  63. }
  64. /**
  65. * @depends testSetup
  66. */
  67. function testPutStream() {
  68. $children = $this->calendar->getChildren();
  69. $this->assertTrue($children[0] instanceof CalendarObject);
  70. $newData = TestUtil::getTestCalendarData();
  71. $stream = fopen('php://temp','r+');
  72. fwrite($stream, $newData);
  73. rewind($stream);
  74. $children[0]->put($stream);
  75. $this->assertEquals($newData, $children[0]->get());
  76. }
  77. /**
  78. * @depends testSetup
  79. */
  80. function testDelete() {
  81. $children = $this->calendar->getChildren();
  82. $this->assertTrue($children[0] instanceof CalendarObject);
  83. $obj = $children[0];
  84. $obj->delete();
  85. $children2 = $this->calendar->getChildren();
  86. $this->assertEquals(count($children)-1, count($children2));
  87. }
  88. /**
  89. * @depends testSetup
  90. */
  91. function testGetLastModified() {
  92. $children = $this->calendar->getChildren();
  93. $this->assertTrue($children[0] instanceof CalendarObject);
  94. $obj = $children[0];
  95. $lastMod = $obj->getLastModified();
  96. $this->assertTrue(is_int($lastMod) || ctype_digit($lastMod));
  97. }
  98. /**
  99. * @depends testSetup
  100. */
  101. function testGetSize() {
  102. $children = $this->calendar->getChildren();
  103. $this->assertTrue($children[0] instanceof CalendarObject);
  104. $obj = $children[0];
  105. $size = $obj->getSize();
  106. $this->assertInternalType('int', $size);
  107. }
  108. function testGetOwner() {
  109. $children = $this->calendar->getChildren();
  110. $this->assertTrue($children[0] instanceof CalendarObject);
  111. $obj = $children[0];
  112. $this->assertEquals('principals/user1', $obj->getOwner());
  113. }
  114. function testGetGroup() {
  115. $children = $this->calendar->getChildren();
  116. $this->assertTrue($children[0] instanceof CalendarObject);
  117. $obj = $children[0];
  118. $this->assertNull($obj->getGroup());
  119. }
  120. function testGetACL() {
  121. $expected = array(
  122. array(
  123. 'privilege' => '{DAV:}read',
  124. 'principal' => 'principals/user1',
  125. 'protected' => true,
  126. ),
  127. array(
  128. 'privilege' => '{DAV:}write',
  129. 'principal' => 'principals/user1',
  130. 'protected' => true,
  131. ),
  132. array(
  133. 'privilege' => '{DAV:}read',
  134. 'principal' => 'principals/user1/calendar-proxy-write',
  135. 'protected' => true,
  136. ),
  137. array(
  138. 'privilege' => '{DAV:}write',
  139. 'principal' => 'principals/user1/calendar-proxy-write',
  140. 'protected' => true,
  141. ),
  142. array(
  143. 'privilege' => '{DAV:}read',
  144. 'principal' => 'principals/user1/calendar-proxy-read',
  145. 'protected' => true,
  146. ),
  147. );
  148. $children = $this->calendar->getChildren();
  149. $this->assertTrue($children[0] instanceof CalendarObject);
  150. $obj = $children[0];
  151. $this->assertEquals($expected, $obj->getACL());
  152. }
  153. /**
  154. * @expectedException Sabre\DAV\Exception\MethodNotAllowed
  155. */
  156. function testSetACL() {
  157. $children = $this->calendar->getChildren();
  158. $this->assertTrue($children[0] instanceof CalendarObject);
  159. $obj = $children[0];
  160. $obj->setACL(array());
  161. }
  162. function testGet() {
  163. $children = $this->calendar->getChildren();
  164. $this->assertTrue($children[0] instanceof CalendarObject);
  165. $obj = $children[0];
  166. $expected = "BEGIN:VCALENDAR
  167. VERSION:2.0
  168. PRODID:-//Apple Inc.//iCal 4.0.1//EN
  169. CALSCALE:GREGORIAN
  170. BEGIN:VTIMEZONE
  171. TZID:Asia/Seoul
  172. BEGIN:DAYLIGHT
  173. TZOFFSETFROM:+0900
  174. RRULE:FREQ=YEARLY;UNTIL=19880507T150000Z;BYMONTH=5;BYDAY=2SU
  175. DTSTART:19870510T000000
  176. TZNAME:GMT+09:00
  177. TZOFFSETTO:+1000
  178. END:DAYLIGHT
  179. BEGIN:STANDARD
  180. TZOFFSETFROM:+1000
  181. DTSTART:19881009T000000
  182. TZNAME:GMT+09:00
  183. TZOFFSETTO:+0900
  184. END:STANDARD
  185. END:VTIMEZONE
  186. BEGIN:VEVENT
  187. CREATED:20100225T154229Z
  188. UID:39A6B5ED-DD51-4AFE-A683-C35EE3749627
  189. TRANSP:TRANSPARENT
  190. SUMMARY:Something here
  191. DTSTAMP:20100228T130202Z
  192. DTSTART;TZID=Asia/Seoul:20100223T060000
  193. DTEND;TZID=Asia/Seoul:20100223T070000
  194. ATTENDEE;PARTSTAT=NEEDS-ACTION:mailto:lisa@example.com
  195. SEQUENCE:2
  196. END:VEVENT
  197. END:VCALENDAR";
  198. $this->assertEquals($expected, $obj->get());
  199. }
  200. function testGetRefetch() {
  201. $backend = new Backend\Mock(array(), array(
  202. 1 => array(
  203. 'foo' => array(
  204. 'calendardata' => 'foo',
  205. 'uri' => 'foo'
  206. ),
  207. )
  208. ));
  209. $obj = new CalendarObject($backend, array(), array('calendarid' => 1, 'uri' => 'foo'));
  210. $this->assertEquals('foo', $obj->get());
  211. }
  212. function testGetEtag1() {
  213. $objectInfo = array(
  214. 'calendardata' => 'foo',
  215. 'uri' => 'foo',
  216. 'etag' => 'bar',
  217. 'calendarid' => 1
  218. );
  219. $backend = new Backend\Mock(array(), array());
  220. $obj = new CalendarObject($backend, array(), $objectInfo);
  221. $this->assertEquals('bar', $obj->getETag());
  222. }
  223. function testGetEtag2() {
  224. $objectInfo = array(
  225. 'calendardata' => 'foo',
  226. 'uri' => 'foo',
  227. 'calendarid' => 1
  228. );
  229. $backend = new Backend\Mock(array(), array());
  230. $obj = new CalendarObject($backend, array(), $objectInfo);
  231. $this->assertEquals('"' . md5('foo') . '"', $obj->getETag());
  232. }
  233. function testGetSupportedPrivilegesSet() {
  234. $objectInfo = array(
  235. 'calendardata' => 'foo',
  236. 'uri' => 'foo',
  237. 'calendarid' => 1
  238. );
  239. $backend = new Backend\Mock(array(), array());
  240. $obj = new CalendarObject($backend, array(), $objectInfo);
  241. $this->assertNull($obj->getSupportedPrivilegeSet());
  242. }
  243. function testGetSize1() {
  244. $objectInfo = array(
  245. 'calendardata' => 'foo',
  246. 'uri' => 'foo',
  247. 'calendarid' => 1
  248. );
  249. $backend = new Backend\Mock(array(), array());
  250. $obj = new CalendarObject($backend, array(), $objectInfo);
  251. $this->assertEquals(3, $obj->getSize());
  252. }
  253. function testGetSize2() {
  254. $objectInfo = array(
  255. 'uri' => 'foo',
  256. 'calendarid' => 1,
  257. 'size' => 4,
  258. );
  259. $backend = new Backend\Mock(array(), array());
  260. $obj = new CalendarObject($backend, array(), $objectInfo);
  261. $this->assertEquals(4, $obj->getSize());
  262. }
  263. }