"Fossies" - the Fresh Open Source Software Archive

Member "dav-4.0.3/tests/Sabre/CalDAV/Schedule/FreeBusyRequestTest.php" (10 Jan 2020, 15977 Bytes) of package /linux/www/dav-4.0.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "FreeBusyRequestTest.php": 4.0.2_vs_4.0.3.

    1 <?php
    2 
    3 declare(strict_types=1);
    4 
    5 namespace Sabre\CalDAV\Schedule;
    6 
    7 use Sabre\CalDAV;
    8 use Sabre\CalDAV\Xml\Property\ScheduleCalendarTransp;
    9 use Sabre\DAV;
   10 use Sabre\DAVACL;
   11 use Sabre\HTTP;
   12 
   13 class FreeBusyRequestTest extends \PHPUnit\Framework\TestCase
   14 {
   15     protected $plugin;
   16     protected $server;
   17     protected $aclPlugin;
   18     protected $request;
   19     protected $authPlugin;
   20     protected $caldavBackend;
   21 
   22     public function setUp()
   23     {
   24         $caldavNS = '{'.CalDAV\Plugin::NS_CALDAV.'}';
   25         $calendars = [
   26             [
   27                 'principaluri' => 'principals/user2',
   28                 'id' => 1,
   29                 'uri' => 'calendar1',
   30                 $caldavNS.'calendar-timezone' => "BEGIN:VCALENDAR\r\nBEGIN:VTIMEZONE\r\nTZID:Europe/Berlin\r\nEND:VTIMEZONE\r\nEND:VCALENDAR",
   31             ],
   32             [
   33                 'principaluri' => 'principals/user2',
   34                 'id' => 2,
   35                 'uri' => 'calendar2',
   36                 $caldavNS.'schedule-calendar-transp' => new ScheduleCalendarTransp(ScheduleCalendarTransp::TRANSPARENT),
   37             ],
   38         ];
   39         $calendarobjects = [
   40             1 => ['1.ics' => [
   41                 'uri' => '1.ics',
   42                 'calendardata' => 'BEGIN:VCALENDAR
   43 BEGIN:VEVENT
   44 DTSTART:20110101T130000
   45 DURATION:PT1H
   46 END:VEVENT
   47 END:VCALENDAR',
   48                 'calendarid' => 1,
   49             ]],
   50             2 => ['2.ics' => [
   51                 'uri' => '2.ics',
   52                 'calendardata' => 'BEGIN:VCALENDAR
   53 BEGIN:VEVENT
   54 DTSTART:20110101T080000
   55 DURATION:PT1H
   56 END:VEVENT
   57 END:VCALENDAR',
   58                 'calendarid' => 2,
   59             ]],
   60         ];
   61 
   62         $principalBackend = new DAVACL\PrincipalBackend\Mock();
   63         $this->caldavBackend = new CalDAV\Backend\MockScheduling($calendars, $calendarobjects);
   64 
   65         $tree = [
   66             new DAVACL\PrincipalCollection($principalBackend),
   67             new CalDAV\CalendarRoot($principalBackend, $this->caldavBackend),
   68         ];
   69 
   70         $this->request = new HTTP\Request('GET', '/', [
   71             'Content-Type' => 'text/calendar',
   72         ]);
   73         $this->response = new HTTP\ResponseMock();
   74 
   75         $this->server = new DAV\Server($tree);
   76         $this->server->httpRequest = $this->request;
   77         $this->server->httpResponse = $this->response;
   78 
   79         $this->aclPlugin = new DAVACL\Plugin();
   80         $this->aclPlugin->allowUnauthenticatedAccess = false;
   81         $this->server->addPlugin($this->aclPlugin);
   82 
   83         $authBackend = new DAV\Auth\Backend\Mock();
   84         $authBackend->setPrincipal('principals/user1');
   85         $this->authPlugin = new DAV\Auth\Plugin($authBackend);
   86         // Forcing authentication to work.
   87         $this->authPlugin->beforeMethod($this->request, $this->response);
   88         $this->server->addPlugin($this->authPlugin);
   89 
   90         // CalDAV plugin
   91         $this->plugin = new CalDAV\Plugin();
   92         $this->server->addPlugin($this->plugin);
   93 
   94         // Scheduling plugin
   95         $this->plugin = new Plugin();
   96         $this->server->addPlugin($this->plugin);
   97     }
   98 
   99     public function testWrongContentType()
  100     {
  101         $this->server->httpRequest = new HTTP\Request(
  102             'POST',
  103             '/calendars/user1/outbox',
  104             ['Content-Type' => 'text/plain']
  105         );
  106 
  107         $this->assertNull(
  108             $this->plugin->httpPost($this->server->httpRequest, $this->server->httpResponse)
  109         );
  110     }
  111 
  112     public function testNotFound()
  113     {
  114         $this->server->httpRequest = new HTTP\Request(
  115             'POST',
  116             '/calendars/user1/blabla',
  117             ['Content-Type' => 'text/calendar']
  118         );
  119 
  120         $this->assertNull(
  121             $this->plugin->httpPost($this->server->httpRequest, $this->server->httpResponse)
  122         );
  123     }
  124 
  125     public function testNotOutbox()
  126     {
  127         $this->server->httpRequest = new HTTP\Request(
  128             'POST',
  129             '/calendars/user1/inbox',
  130             ['Content-Type' => 'text/calendar']
  131         );
  132 
  133         $this->assertNull(
  134             $this->plugin->httpPost($this->server->httpRequest, $this->server->httpResponse)
  135         );
  136     }
  137 
  138     /**
  139      * @expectedException \Sabre\DAV\Exception\BadRequest
  140      */
  141     public function testNoItipMethod()
  142     {
  143         $this->server->httpRequest = new HTTP\Request(
  144             'POST',
  145             '/calendars/user1/outbox',
  146             ['Content-Type' => 'text/calendar']
  147         );
  148 
  149         $body = <<<ICS
  150 BEGIN:VCALENDAR
  151 BEGIN:VFREEBUSY
  152 END:VFREEBUSY
  153 END:VCALENDAR
  154 ICS;
  155 
  156         $this->server->httpRequest->setBody($body);
  157         $this->plugin->httpPost($this->server->httpRequest, $this->server->httpResponse);
  158     }
  159 
  160     /**
  161      * @expectedException \Sabre\DAV\Exception\NotImplemented
  162      */
  163     public function testNoVFreeBusy()
  164     {
  165         $this->server->httpRequest = new HTTP\Request(
  166             'POST',
  167             '/calendars/user1/outbox',
  168             ['Content-Type' => 'text/calendar']
  169         );
  170 
  171         $body = <<<ICS
  172 BEGIN:VCALENDAR
  173 METHOD:REQUEST
  174 BEGIN:VEVENT
  175 END:VEVENT
  176 END:VCALENDAR
  177 ICS;
  178 
  179         $this->server->httpRequest->setBody($body);
  180         $this->plugin->httpPost($this->server->httpRequest, $this->server->httpResponse);
  181     }
  182 
  183     /**
  184      * @expectedException \Sabre\DAV\Exception\Forbidden
  185      */
  186     public function testIncorrectOrganizer()
  187     {
  188         $this->server->httpRequest = new HTTP\Request(
  189             'POST',
  190             '/calendars/user1/outbox',
  191             ['Content-Type' => 'text/calendar']
  192         );
  193 
  194         $body = <<<ICS
  195 BEGIN:VCALENDAR
  196 METHOD:REQUEST
  197 BEGIN:VFREEBUSY
  198 ORGANIZER:mailto:john@wayne.org
  199 END:VFREEBUSY
  200 END:VCALENDAR
  201 ICS;
  202 
  203         $this->server->httpRequest->setBody($body);
  204         $this->plugin->httpPost($this->server->httpRequest, $this->server->httpResponse);
  205     }
  206 
  207     /**
  208      * @expectedException \Sabre\DAV\Exception\BadRequest
  209      */
  210     public function testNoAttendees()
  211     {
  212         $this->server->httpRequest = new HTTP\Request(
  213             'POST',
  214             '/calendars/user1/outbox',
  215             ['Content-Type' => 'text/calendar']
  216         );
  217 
  218         $body = <<<ICS
  219 BEGIN:VCALENDAR
  220 METHOD:REQUEST
  221 BEGIN:VFREEBUSY
  222 ORGANIZER:mailto:user1.sabredav@sabredav.org
  223 END:VFREEBUSY
  224 END:VCALENDAR
  225 ICS;
  226 
  227         $this->server->httpRequest->setBody($body);
  228         $this->plugin->httpPost($this->server->httpRequest, $this->server->httpResponse);
  229     }
  230 
  231     /**
  232      * @expectedException \Sabre\DAV\Exception\BadRequest
  233      */
  234     public function testNoDTStart()
  235     {
  236         $this->server->httpRequest = new HTTP\Request(
  237             'POST',
  238             '/calendars/user1/outbox',
  239             ['Content-Type' => 'text/calendar']
  240         );
  241 
  242         $body = <<<ICS
  243 BEGIN:VCALENDAR
  244 METHOD:REQUEST
  245 BEGIN:VFREEBUSY
  246 ORGANIZER:mailto:user1.sabredav@sabredav.org
  247 ATTENDEE:mailto:user2.sabredav@sabredav.org
  248 END:VFREEBUSY
  249 END:VCALENDAR
  250 ICS;
  251 
  252         $this->server->httpRequest->setBody($body);
  253         $this->plugin->httpPost($this->server->httpRequest, $this->server->httpResponse);
  254     }
  255 
  256     public function testSucceed()
  257     {
  258         $this->server->httpRequest = new HTTP\Request(
  259             'POST',
  260             '/calendars/user1/outbox',
  261             ['Content-Type' => 'text/calendar']
  262         );
  263 
  264         $body = <<<ICS
  265 BEGIN:VCALENDAR
  266 METHOD:REQUEST
  267 BEGIN:VFREEBUSY
  268 ORGANIZER:mailto:user1.sabredav@sabredav.org
  269 ATTENDEE:mailto:user2.sabredav@sabredav.org
  270 ATTENDEE:mailto:user3.sabredav@sabredav.org
  271 DTSTART:20110101T080000Z
  272 DTEND:20110101T180000Z
  273 END:VFREEBUSY
  274 END:VCALENDAR
  275 ICS;
  276 
  277         $this->server->httpRequest->setBody($body);
  278 
  279         // Lazily making the current principal an admin.
  280         $this->aclPlugin->adminPrincipals[] = 'principals/user1';
  281 
  282         $this->assertFalse(
  283             $this->plugin->httpPost($this->server->httpRequest, $this->response)
  284         );
  285 
  286         $this->assertEquals(200, $this->response->status);
  287         $this->assertEquals([
  288             'Content-Type' => ['application/xml'],
  289         ], $this->response->getHeaders());
  290 
  291         $strings = [
  292             '<d:href>mailto:user2.sabredav@sabredav.org</d:href>',
  293             '<d:href>mailto:user3.sabredav@sabredav.org</d:href>',
  294             '<cal:request-status>2.0;Success</cal:request-status>',
  295             '<cal:request-status>3.7;Could not find principal</cal:request-status>',
  296             'FREEBUSY:20110101T120000Z/20110101T130000Z',
  297         ];
  298 
  299         foreach ($strings as $string) {
  300             $this->assertTrue(
  301                 false !== strpos($this->response->getBodyAsString(), $string),
  302                 'The response body did not contain: '.$string.'Full response: '.$this->response->getBodyAsString()
  303             );
  304         }
  305 
  306         $this->assertTrue(
  307             false == strpos($this->response->getBodyAsString(), 'FREEBUSY;FBTYPE=BUSY:20110101T080000Z/20110101T090000Z'),
  308             'The response body did contain free busy info from a transparent calendar.'
  309         );
  310     }
  311 
  312     /**
  313      * Testing if the freebusy request still works, even if there are no
  314      * calendars in the target users' account.
  315      */
  316     public function testSucceedNoCalendars()
  317     {
  318         // Deleting calendars
  319         $this->caldavBackend->deleteCalendar(1);
  320         $this->caldavBackend->deleteCalendar(2);
  321 
  322         $this->server->httpRequest = new HTTP\Request(
  323             'POST',
  324             '/calendars/user1/outbox',
  325             ['Content-Type' => 'text/calendar']
  326         );
  327 
  328         $body = <<<ICS
  329 BEGIN:VCALENDAR
  330 METHOD:REQUEST
  331 BEGIN:VFREEBUSY
  332 ORGANIZER:mailto:user1.sabredav@sabredav.org
  333 ATTENDEE:mailto:user2.sabredav@sabredav.org
  334 DTSTART:20110101T080000Z
  335 DTEND:20110101T180000Z
  336 END:VFREEBUSY
  337 END:VCALENDAR
  338 ICS;
  339 
  340         $this->server->httpRequest->setBody($body);
  341 
  342         // Lazily making the current principal an admin.
  343         $this->aclPlugin->adminPrincipals[] = 'principals/user1';
  344 
  345         $this->assertFalse(
  346             $this->plugin->httpPost($this->server->httpRequest, $this->response)
  347         );
  348 
  349         $this->assertEquals(200, $this->response->status);
  350         $this->assertEquals([
  351             'Content-Type' => ['application/xml'],
  352         ], $this->response->getHeaders());
  353 
  354         $strings = [
  355             '<d:href>mailto:user2.sabredav@sabredav.org</d:href>',
  356             '<cal:request-status>2.0;Success</cal:request-status>',
  357         ];
  358 
  359         foreach ($strings as $string) {
  360             $this->assertTrue(
  361                 false !== strpos($this->response->getBodyAsString(), $string),
  362                 'The response body did not contain: '.$string.'Full response: '.$this->response->getBodyAsString()
  363             );
  364         }
  365     }
  366 
  367     public function testNoCalendarHomeFound()
  368     {
  369         $this->server->httpRequest = new HTTP\Request(
  370             'POST',
  371             '/calendars/user1/outbox',
  372             ['Content-Type' => 'text/calendar']
  373         );
  374 
  375         $body = <<<ICS
  376 BEGIN:VCALENDAR
  377 METHOD:REQUEST
  378 BEGIN:VFREEBUSY
  379 ORGANIZER:mailto:user1.sabredav@sabredav.org
  380 ATTENDEE:mailto:user2.sabredav@sabredav.org
  381 DTSTART:20110101T080000Z
  382 DTEND:20110101T180000Z
  383 END:VFREEBUSY
  384 END:VCALENDAR
  385 ICS;
  386 
  387         $this->server->httpRequest->setBody($body);
  388 
  389         // Lazily making the current principal an admin.
  390         $this->aclPlugin->adminPrincipals[] = 'principals/user1';
  391 
  392         // Removing the calendar home
  393         $this->server->on('propFind', function (DAV\PropFind $propFind) {
  394             $propFind->set('{'.Plugin::NS_CALDAV.'}calendar-home-set', null, 403);
  395         });
  396 
  397         $this->assertFalse(
  398             $this->plugin->httpPost($this->server->httpRequest, $this->response)
  399         );
  400 
  401         $this->assertEquals(200, $this->response->status);
  402         $this->assertEquals([
  403             'Content-Type' => ['application/xml'],
  404         ], $this->response->getHeaders());
  405 
  406         $strings = [
  407             '<d:href>mailto:user2.sabredav@sabredav.org</d:href>',
  408             '<cal:request-status>3.7;No calendar-home-set property found</cal:request-status>',
  409         ];
  410 
  411         foreach ($strings as $string) {
  412             $this->assertTrue(
  413                 false !== strpos($this->response->getBodyAsString(), $string),
  414                 'The response body did not contain: '.$string.'Full response: '.$this->response->getBodyAsString()
  415             );
  416         }
  417     }
  418 
  419     public function testNoInboxFound()
  420     {
  421         $this->server->httpRequest = new HTTP\Request(
  422             'POST',
  423             '/calendars/user1/outbox',
  424             ['Content-Type' => 'text/calendar']
  425         );
  426 
  427         $body = <<<ICS
  428 BEGIN:VCALENDAR
  429 METHOD:REQUEST
  430 BEGIN:VFREEBUSY
  431 ORGANIZER:mailto:user1.sabredav@sabredav.org
  432 ATTENDEE:mailto:user2.sabredav@sabredav.org
  433 DTSTART:20110101T080000Z
  434 DTEND:20110101T180000Z
  435 END:VFREEBUSY
  436 END:VCALENDAR
  437 ICS;
  438 
  439         $this->server->httpRequest->setBody($body);
  440 
  441         // Lazily making the current principal an admin.
  442         $this->aclPlugin->adminPrincipals[] = 'principals/user1';
  443 
  444         // Removing the inbox
  445         $this->server->on('propFind', function (DAV\PropFind $propFind) {
  446             $propFind->set('{'.Plugin::NS_CALDAV.'}schedule-inbox-URL', null, 403);
  447         });
  448 
  449         $this->assertFalse(
  450             $this->plugin->httpPost($this->server->httpRequest, $this->response)
  451         );
  452 
  453         $this->assertEquals(200, $this->response->status);
  454         $this->assertEquals([
  455             'Content-Type' => ['application/xml'],
  456         ], $this->response->getHeaders());
  457 
  458         $strings = [
  459             '<d:href>mailto:user2.sabredav@sabredav.org</d:href>',
  460             '<cal:request-status>3.7;No schedule-inbox-URL property found</cal:request-status>',
  461         ];
  462 
  463         foreach ($strings as $string) {
  464             $this->assertTrue(
  465                 false !== strpos($this->response->getBodyAsString(), $string),
  466                 'The response body did not contain: '.$string.'Full response: '.$this->response->getBodyAsString()
  467             );
  468         }
  469     }
  470 
  471     public function testSucceedUseVAVAILABILITY()
  472     {
  473         $this->server->httpRequest = new HTTP\Request(
  474             'POST',
  475             '/calendars/user1/outbox',
  476             ['Content-Type' => 'text/calendar']
  477         );
  478 
  479         $body = <<<ICS
  480 BEGIN:VCALENDAR
  481 METHOD:REQUEST
  482 BEGIN:VFREEBUSY
  483 ORGANIZER:mailto:user1.sabredav@sabredav.org
  484 ATTENDEE:mailto:user2.sabredav@sabredav.org
  485 DTSTART:20110101T080000Z
  486 DTEND:20110101T180000Z
  487 END:VFREEBUSY
  488 END:VCALENDAR
  489 ICS;
  490 
  491         $this->server->httpRequest->setBody($body);
  492 
  493         // Lazily making the current principal an admin.
  494         $this->aclPlugin->adminPrincipals[] = 'principals/user1';
  495 
  496         // Adding VAVAILABILITY manually
  497         $this->server->on('propFind', function (DAV\PropFind $propFind) {
  498             $propFind->handle('{'.Plugin::NS_CALDAV.'}calendar-availability', function () {
  499                 $avail = <<<ICS
  500 BEGIN:VCALENDAR
  501 BEGIN:VAVAILABILITY
  502 DTSTART:20110101T000000Z
  503 DTEND:20110102T000000Z
  504 BEGIN:AVAILABLE
  505 DTSTART:20110101T090000Z
  506 DTEND:20110101T170000Z
  507 END:AVAILABLE
  508 END:VAVAILABILITY
  509 END:VCALENDAR
  510 ICS;
  511 
  512                 return $avail;
  513             });
  514         });
  515 
  516         $this->assertFalse(
  517             $this->plugin->httpPost($this->server->httpRequest, $this->response)
  518         );
  519 
  520         $this->assertEquals(200, $this->response->status);
  521         $this->assertEquals([
  522             'Content-Type' => ['application/xml'],
  523         ], $this->response->getHeaders());
  524 
  525         $strings = [
  526             '<d:href>mailto:user2.sabredav@sabredav.org</d:href>',
  527             '<cal:request-status>2.0;Success</cal:request-status>',
  528             'FREEBUSY;FBTYPE=BUSY-UNAVAILABLE:20110101T080000Z/20110101T090000Z',
  529             'FREEBUSY:20110101T120000Z/20110101T130000Z',
  530             'FREEBUSY;FBTYPE=BUSY-UNAVAILABLE:20110101T170000Z/20110101T180000Z',
  531         ];
  532 
  533         foreach ($strings as $string) {
  534             $this->assertTrue(
  535                 false !== strpos($this->response->getBodyAsString(), $string),
  536                 'The response body did not contain: '.$string.'Full response: '.$this->response->getBodyAsString()
  537             );
  538         }
  539     }
  540 }