VAvailabilityTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <?php
  2. namespace Sabre\VObject\Component;
  3. use Sabre\VObject;
  4. use Sabre\VObject\Reader;
  5. use Sabre\VObject\Component;
  6. use Sabre\VObject\Component\VAvailability;
  7. /**
  8. * We use `RFCxxx` has a placeholder for the
  9. * https://tools.ietf.org/html/draft-daboo-calendar-availability-05 name.
  10. */
  11. class VAvailabilityTest extends \PHPUnit_Framework_TestCase {
  12. function testVAvailabilityComponent() {
  13. $vcal = <<<VCAL
  14. BEGIN:VCALENDAR
  15. BEGIN:VAVAILABILITY
  16. END:VAVAILABILITY
  17. END:VCALENDAR
  18. VCAL;
  19. $document = Reader::read($vcal);
  20. $this->assertInstanceOf(__NAMESPACE__ . '\VAvailability', $document->VAVAILABILITY);
  21. }
  22. function testRFCxxxSection3_1_availabilityprop_required() {
  23. // UID and DTSTAMP are present.
  24. $this->assertIsValid(Reader::read(
  25. <<<VCAL
  26. BEGIN:VCALENDAR
  27. VERSION:2.0
  28. PRODID:-//id
  29. BEGIN:VAVAILABILITY
  30. UID:foo@test
  31. DTSTAMP:20111005T133225Z
  32. END:VAVAILABILITY
  33. END:VCALENDAR
  34. VCAL
  35. ));
  36. // UID and DTSTAMP are missing.
  37. $this->assertIsNotValid(Reader::read(
  38. <<<VCAL
  39. BEGIN:VCALENDAR
  40. VERSION:2.0
  41. PRODID:-//id
  42. BEGIN:VAVAILABILITY
  43. END:VAVAILABILITY
  44. END:VCALENDAR
  45. VCAL
  46. ));
  47. // DTSTAMP is missing.
  48. $this->assertIsNotValid(Reader::read(
  49. <<<VCAL
  50. BEGIN:VCALENDAR
  51. VERSION:2.0
  52. PRODID:-//id
  53. BEGIN:VAVAILABILITY
  54. UID:foo@test
  55. END:VAVAILABILITY
  56. END:VCALENDAR
  57. VCAL
  58. ));
  59. // UID is missing.
  60. $this->assertIsNotValid(Reader::read(
  61. <<<VCAL
  62. BEGIN:VCALENDAR
  63. VERSION:2.0
  64. PRODID:-//id
  65. BEGIN:VAVAILABILITY
  66. DTSTAMP:20111005T133225Z
  67. END:VAVAILABILITY
  68. END:VCALENDAR
  69. VCAL
  70. ));
  71. }
  72. function testRFCxxxSection3_1_availabilityprop_optional_once() {
  73. $properties = array(
  74. 'BUSYTYPE:BUSY',
  75. 'CLASS:PUBLIC',
  76. 'CREATED:20111005T135125Z',
  77. 'DESCRIPTION:Long bla bla',
  78. 'DTSTART:20111005T020000',
  79. 'LAST-MODIFIED:20111005T135325Z',
  80. 'ORGANIZER:mailto:foo@example.com',
  81. 'PRIORITY:1',
  82. 'SEQUENCE:0',
  83. 'SUMMARY:Bla bla',
  84. 'URL:http://example.org/'
  85. );
  86. // They are all present, only once.
  87. $this->assertIsValid(Reader::read($this->template($properties)));
  88. // We duplicate each one to see if it fails.
  89. foreach ($properties as $property) {
  90. $this->assertIsNotValid(Reader::read($this->template(array(
  91. $property,
  92. $property
  93. ))));
  94. }
  95. }
  96. function testRFCxxxSection3_1_availabilityprop_dtend_duration() {
  97. // Only DTEND.
  98. $this->assertIsValid(Reader::read($this->template(array(
  99. 'DTEND:21111005T133225Z'
  100. ))));
  101. // Only DURATION.
  102. $this->assertIsValid(Reader::read($this->template(array(
  103. 'DURATION:PT1H'
  104. ))));
  105. // Both (not allowed).
  106. $this->assertIsNotValid(Reader::read($this->template(array(
  107. 'DTEND:21111005T133225Z',
  108. 'DURATION:PT1H'
  109. ))));
  110. }
  111. function testAvailableSubComponent() {
  112. $vcal = <<<VCAL
  113. BEGIN:VCALENDAR
  114. BEGIN:VAVAILABILITY
  115. BEGIN:AVAILABLE
  116. END:AVAILABLE
  117. END:VAVAILABILITY
  118. END:VCALENDAR
  119. VCAL;
  120. $document = Reader::read($vcal);
  121. $this->assertInstanceOf(__NAMESPACE__, $document->VAVAILABILITY->AVAILABLE);
  122. }
  123. function testRFCxxxSection3_1_availableprop_required() {
  124. // UID, DTSTAMP and DTSTART are present.
  125. $this->assertIsValid(Reader::read(
  126. <<<VCAL
  127. BEGIN:VCALENDAR
  128. VERSION:2.0
  129. PRODID:-//id
  130. BEGIN:VAVAILABILITY
  131. UID:foo@test
  132. DTSTAMP:20111005T133225Z
  133. BEGIN:AVAILABLE
  134. UID:foo@test
  135. DTSTAMP:20111005T133225Z
  136. DTSTART:20111005T133225Z
  137. END:AVAILABLE
  138. END:VAVAILABILITY
  139. END:VCALENDAR
  140. VCAL
  141. ));
  142. // UID, DTSTAMP and DTSTART are missing.
  143. $this->assertIsNotValid(Reader::read(
  144. <<<VCAL
  145. BEGIN:VCALENDAR
  146. VERSION:2.0
  147. PRODID:-//id
  148. BEGIN:VAVAILABILITY
  149. UID:foo@test
  150. DTSTAMP:20111005T133225Z
  151. BEGIN:AVAILABLE
  152. END:AVAILABLE
  153. END:VAVAILABILITY
  154. END:VCALENDAR
  155. VCAL
  156. ));
  157. // UID is missing.
  158. $this->assertIsNotValid(Reader::read(
  159. <<<VCAL
  160. BEGIN:VCALENDAR
  161. VERSION:2.0
  162. PRODID:-//id
  163. BEGIN:VAVAILABILITY
  164. UID:foo@test
  165. DTSTAMP:20111005T133225Z
  166. BEGIN:AVAILABLE
  167. DTSTAMP:20111005T133225Z
  168. DTSTART:20111005T133225Z
  169. END:AVAILABLE
  170. END:VAVAILABILITY
  171. END:VCALENDAR
  172. VCAL
  173. ));
  174. // DTSTAMP is missing.
  175. $this->assertIsNotValid(Reader::read(
  176. <<<VCAL
  177. BEGIN:VCALENDAR
  178. VERSION:2.0
  179. PRODID:-//id
  180. BEGIN:VAVAILABILITY
  181. UID:foo@test
  182. DTSTAMP:20111005T133225Z
  183. BEGIN:AVAILABLE
  184. UID:foo@test
  185. DTSTART:20111005T133225Z
  186. END:AVAILABLE
  187. END:VAVAILABILITY
  188. END:VCALENDAR
  189. VCAL
  190. ));
  191. // DTSTART is missing.
  192. $this->assertIsNotValid(Reader::read(
  193. <<<VCAL
  194. BEGIN:VCALENDAR
  195. VERSION:2.0
  196. PRODID:-//id
  197. BEGIN:VAVAILABILITY
  198. UID:foo@test
  199. DTSTAMP:20111005T133225Z
  200. BEGIN:AVAILABLE
  201. UID:foo@test
  202. DTSTAMP:20111005T133225Z
  203. END:AVAILABLE
  204. END:VAVAILABILITY
  205. END:VCALENDAR
  206. VCAL
  207. ));
  208. }
  209. function testRFCxxxSection3_1_available_dtend_duration() {
  210. // Only DTEND.
  211. $this->assertIsValid(Reader::read($this->templateAvailable(array(
  212. 'DTEND:21111005T133225Z'
  213. ))));
  214. // Only DURATION.
  215. $this->assertIsValid(Reader::read($this->templateAvailable(array(
  216. 'DURATION:PT1H'
  217. ))));
  218. // Both (not allowed).
  219. $this->assertIsNotValid(Reader::read($this->templateAvailable(array(
  220. 'DTEND:21111005T133225Z',
  221. 'DURATION:PT1H'
  222. ))));
  223. }
  224. function testRFCxxxSection3_1_available_optional_once() {
  225. $properties = array(
  226. 'CREATED:20111005T135125Z',
  227. 'DESCRIPTION:Long bla bla',
  228. 'LAST-MODIFIED:20111005T135325Z',
  229. 'RECURRENCE-ID;RANGE=THISANDFUTURE:19980401T133000Z',
  230. 'RRULE:FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR',
  231. 'SUMMARY:Bla bla'
  232. );
  233. // They are all present, only once.
  234. $this->assertIsValid(Reader::read($this->templateAvailable($properties)));
  235. // We duplicate each one to see if it fails.
  236. foreach ($properties as $property) {
  237. $this->assertIsNotValid(Reader::read($this->templateAvailable(array(
  238. $property,
  239. $property
  240. ))));
  241. }
  242. }
  243. function testRFCxxxSection3_2() {
  244. $this->assertEquals(
  245. 'BUSY',
  246. Reader::read($this->templateAvailable(array(
  247. 'BUSYTYPE:BUSY'
  248. )))
  249. ->VAVAILABILITY
  250. ->AVAILABLE
  251. ->BUSYTYPE
  252. ->getValue()
  253. );
  254. $this->assertEquals(
  255. 'BUSY-UNAVAILABLE',
  256. Reader::read($this->templateAvailable(array(
  257. 'BUSYTYPE:BUSY-UNAVAILABLE'
  258. )))
  259. ->VAVAILABILITY
  260. ->AVAILABLE
  261. ->BUSYTYPE
  262. ->getValue()
  263. );
  264. $this->assertEquals(
  265. 'BUSY-TENTATIVE',
  266. Reader::read($this->templateAvailable(array(
  267. 'BUSYTYPE:BUSY-TENTATIVE'
  268. )))
  269. ->VAVAILABILITY
  270. ->AVAILABLE
  271. ->BUSYTYPE
  272. ->getValue()
  273. );
  274. }
  275. protected function assertIsValid(VObject\Document $document) {
  276. $this->assertEmpty($document->validate());
  277. }
  278. protected function assertIsNotValid(VObject\Document $document) {
  279. $this->assertNotEmpty($document->validate());
  280. }
  281. protected function template(array $properties) {
  282. return $this->_template(
  283. <<<VCAL
  284. BEGIN:VCALENDAR
  285. VERSION:2.0
  286. PRODID:-//id
  287. BEGIN:VAVAILABILITY
  288. UID:foo@test
  289. DTSTAMP:20111005T133225Z
  290. END:VAVAILABILITY
  291. END:VCALENDAR
  292. VCAL
  293. ,
  294. $properties
  295. );
  296. }
  297. protected function templateAvailable(array $properties) {
  298. return $this->_template(
  299. <<<VCAL
  300. BEGIN:VCALENDAR
  301. VERSION:2.0
  302. PRODID:-//id
  303. BEGIN:VAVAILABILITY
  304. UID:foo@test
  305. DTSTAMP:20111005T133225Z
  306. BEGIN:AVAILABLE
  307. UID:foo@test
  308. DTSTAMP:20111005T133225Z
  309. DTSTART:20111005T133225Z
  310. END:AVAILABLE
  311. END:VAVAILABILITY
  312. END:VCALENDAR
  313. VCAL
  314. ,
  315. $properties
  316. );
  317. }
  318. protected function _template($template, array $properties) {
  319. return str_replace('…', implode("\r\n", $properties), $template);
  320. }
  321. }