"Fossies" - the Fresh Open Source Software Archive

Member "dav-4.0.3/tests/Sabre/CalDAV/ValidateICalTest.php" (10 Jan 2020, 11788 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 "ValidateICalTest.php": 4.0.2_vs_4.0.3.

    1 <?php
    2 
    3 declare(strict_types=1);
    4 
    5 namespace Sabre\CalDAV;
    6 
    7 use Sabre\DAV;
    8 use Sabre\DAVACL;
    9 use Sabre\HTTP;
   10 
   11 require_once 'Sabre/HTTP/ResponseMock.php';
   12 
   13 class ValidateICalTest extends \PHPUnit\Framework\TestCase
   14 {
   15     /**
   16      * @var Sabre\DAV\Server
   17      */
   18     protected $server;
   19     /**
   20      * @var Sabre\CalDAV\Backend\Mock
   21      */
   22     protected $calBackend;
   23 
   24     public function setUp()
   25     {
   26         $calendars = [
   27             [
   28                 'id' => 'calendar1',
   29                 'principaluri' => 'principals/admin',
   30                 'uri' => 'calendar1',
   31                 '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set' => new Xml\Property\SupportedCalendarComponentSet(['VEVENT', 'VTODO', 'VJOURNAL']),
   32             ],
   33             [
   34                 'id' => 'calendar2',
   35                 'principaluri' => 'principals/admin',
   36                 'uri' => 'calendar2',
   37                 '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set' => new Xml\Property\SupportedCalendarComponentSet(['VTODO', 'VJOURNAL']),
   38             ],
   39         ];
   40 
   41         $this->calBackend = new Backend\Mock($calendars, []);
   42         $principalBackend = new DAVACL\PrincipalBackend\Mock();
   43 
   44         $tree = [
   45             new CalendarRoot($principalBackend, $this->calBackend),
   46         ];
   47 
   48         $this->server = new DAV\Server($tree);
   49         $this->server->sapi = new HTTP\SapiMock();
   50         $this->server->debugExceptions = true;
   51 
   52         $plugin = new Plugin();
   53         $this->server->addPlugin($plugin);
   54 
   55         $response = new HTTP\ResponseMock();
   56         $this->server->httpResponse = $response;
   57     }
   58 
   59     public function request(HTTP\Request $request)
   60     {
   61         $this->server->httpRequest = $request;
   62         $this->server->exec();
   63 
   64         return $this->server->httpResponse;
   65     }
   66 
   67     public function testCreateFile()
   68     {
   69         $request = HTTP\Sapi::createFromServerArray([
   70             'REQUEST_METHOD' => 'PUT',
   71             'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
   72         ]);
   73 
   74         $response = $this->request($request);
   75 
   76         $this->assertEquals(415, $response->status);
   77     }
   78 
   79     public function testCreateFileValid()
   80     {
   81         $request = new HTTP\Request(
   82             'PUT',
   83             '/calendars/admin/calendar1/blabla.ics',
   84             ['Prefer' => 'handling=strict']
   85         );
   86 
   87         $ics = <<<ICS
   88 BEGIN:VCALENDAR
   89 VERSION:2.0
   90 PRODID:foo
   91 BEGIN:VEVENT
   92 UID:foo
   93 DTSTAMP:20160406T052348Z
   94 DTSTART:20160706T140000Z
   95 END:VEVENT
   96 END:VCALENDAR
   97 ICS;
   98 
   99         $request->setBody($ics);
  100 
  101         $response = $this->request($request);
  102 
  103         $this->assertEquals(201, $response->status, 'Incorrect status returned! Full response body: '.$response->getBodyAsString());
  104         $this->assertEquals([
  105             'X-Sabre-Version' => [DAV\Version::VERSION],
  106             'Content-Length' => ['0'],
  107             'ETag' => ['"'.md5($ics).'"'],
  108         ], $response->getHeaders());
  109 
  110         $expected = [
  111             'uri' => 'blabla.ics',
  112             'calendardata' => $ics,
  113             'calendarid' => 'calendar1',
  114             'lastmodified' => null,
  115         ];
  116 
  117         $this->assertEquals($expected, $this->calBackend->getCalendarObject('calendar1', 'blabla.ics'));
  118     }
  119 
  120     public function testCreateFileNoVersion()
  121     {
  122         $request = new HTTP\Request(
  123             'PUT',
  124             '/calendars/admin/calendar1/blabla.ics',
  125             ['Prefer' => 'handling=strict']
  126         );
  127 
  128         $ics = <<<ICS
  129 BEGIN:VCALENDAR
  130 PRODID:foo
  131 BEGIN:VEVENT
  132 UID:foo
  133 DTSTAMP:20160406T052348Z
  134 DTSTART:20160706T140000Z
  135 END:VEVENT
  136 END:VCALENDAR
  137 ICS;
  138 
  139         $request->setBody($ics);
  140 
  141         $response = $this->request($request);
  142 
  143         $this->assertEquals(415, $response->status, 'Incorrect status returned! Full response body: '.$response->getBodyAsString());
  144     }
  145 
  146     public function testCreateFileNoVersionFixed()
  147     {
  148         $request = new HTTP\Request(
  149             'PUT',
  150             '/calendars/admin/calendar1/blabla.ics',
  151             ['Prefer' => 'handling=lenient']
  152         );
  153 
  154         $ics = <<<ICS
  155 BEGIN:VCALENDAR
  156 PRODID:foo
  157 BEGIN:VEVENT
  158 UID:foo
  159 DTSTAMP:20160406T052348Z
  160 DTSTART:20160706T140000Z
  161 END:VEVENT
  162 END:VCALENDAR
  163 ICS;
  164 
  165         $request->setBody($ics);
  166 
  167         $response = $this->request($request);
  168 
  169         $this->assertEquals(201, $response->status, 'Incorrect status returned! Full response body: '.$response->getBodyAsString());
  170         $this->assertEquals([
  171             'X-Sabre-Version' => [DAV\Version::VERSION],
  172             'Content-Length' => ['0'],
  173             'X-Sabre-Ew-Gross' => ['iCalendar validation warning: VERSION MUST appear exactly once in a VCALENDAR component'],
  174         ], $response->getHeaders());
  175 
  176         $ics = <<<ICS
  177 BEGIN:VCALENDAR\r
  178 VERSION:2.0\r
  179 PRODID:foo\r
  180 BEGIN:VEVENT\r
  181 UID:foo\r
  182 DTSTAMP:20160406T052348Z\r
  183 DTSTART:20160706T140000Z\r
  184 END:VEVENT\r
  185 END:VCALENDAR\r
  186 
  187 ICS;
  188 
  189         $expected = [
  190             'uri' => 'blabla.ics',
  191             'calendardata' => $ics,
  192             'calendarid' => 'calendar1',
  193             'lastmodified' => null,
  194         ];
  195 
  196         $this->assertEquals($expected, $this->calBackend->getCalendarObject('calendar1', 'blabla.ics'));
  197     }
  198 
  199     public function testCreateFileNoComponents()
  200     {
  201         $request = new HTTP\Request(
  202             'PUT',
  203             '/calendars/admin/calendar1/blabla.ics',
  204             ['Prefer' => 'handling=strict']
  205         );
  206         $ics = <<<ICS
  207 BEGIN:VCALENDAR
  208 VERSION:2.0
  209 PRODID:foo
  210 END:VCALENDAR
  211 ICS;
  212 
  213         $request->setBody($ics);
  214 
  215         $response = $this->request($request);
  216         $this->assertEquals(403, $response->status, 'Incorrect status returned! Full response body: '.$response->getBodyAsString());
  217     }
  218 
  219     public function testCreateFileNoUID()
  220     {
  221         $request = HTTP\Sapi::createFromServerArray([
  222             'REQUEST_METHOD' => 'PUT',
  223             'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
  224         ]);
  225         $request->setBody("BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n");
  226 
  227         $response = $this->request($request);
  228 
  229         $this->assertEquals(415, $response->status, 'Incorrect status returned! Full response body: '.$response->getBodyAsString());
  230     }
  231 
  232     public function testCreateFileVCard()
  233     {
  234         $request = HTTP\Sapi::createFromServerArray([
  235             'REQUEST_METHOD' => 'PUT',
  236             'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
  237         ]);
  238         $request->setBody("BEGIN:VCARD\r\nEND:VCARD\r\n");
  239 
  240         $response = $this->request($request);
  241 
  242         $this->assertEquals(415, $response->status, 'Incorrect status returned! Full response body: '.$response->getBodyAsString());
  243     }
  244 
  245     public function testCreateFile2Components()
  246     {
  247         $request = HTTP\Sapi::createFromServerArray([
  248             'REQUEST_METHOD' => 'PUT',
  249             'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
  250         ]);
  251         $request->setBody("BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nUID:foo\r\nEND:VEVENT\r\nBEGIN:VJOURNAL\r\nUID:foo\r\nEND:VJOURNAL\r\nEND:VCALENDAR\r\n");
  252 
  253         $response = $this->request($request);
  254 
  255         $this->assertEquals(415, $response->status, 'Incorrect status returned! Full response body: '.$response->getBodyAsString());
  256     }
  257 
  258     public function testCreateFile2UIDS()
  259     {
  260         $request = HTTP\Sapi::createFromServerArray([
  261             'REQUEST_METHOD' => 'PUT',
  262             'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
  263         ]);
  264         $request->setBody("BEGIN:VCALENDAR\r\nBEGIN:VTIMEZONE\r\nEND:VTIMEZONE\r\nBEGIN:VEVENT\r\nUID:foo\r\nEND:VEVENT\r\nBEGIN:VEVENT\r\nUID:bar\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n");
  265 
  266         $response = $this->request($request);
  267 
  268         $this->assertEquals(415, $response->status, 'Incorrect status returned! Full response body: '.$response->getBodyAsString());
  269     }
  270 
  271     public function testCreateFileWrongComponent()
  272     {
  273         $request = HTTP\Sapi::createFromServerArray([
  274             'REQUEST_METHOD' => 'PUT',
  275             'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
  276         ]);
  277         $request->setBody("BEGIN:VCALENDAR\r\nBEGIN:VTIMEZONE\r\nEND:VTIMEZONE\r\nBEGIN:VFREEBUSY\r\nUID:foo\r\nEND:VFREEBUSY\r\nEND:VCALENDAR\r\n");
  278 
  279         $response = $this->request($request);
  280 
  281         $this->assertEquals(403, $response->status, 'Incorrect status returned! Full response body: '.$response->getBodyAsString());
  282     }
  283 
  284     public function testUpdateFile()
  285     {
  286         $this->calBackend->createCalendarObject('calendar1', 'blabla.ics', 'foo');
  287         $request = HTTP\Sapi::createFromServerArray([
  288             'REQUEST_METHOD' => 'PUT',
  289             'REQUEST_URI' => '/calendars/admin/calendar1/blabla.ics',
  290         ]);
  291 
  292         $response = $this->request($request);
  293 
  294         $this->assertEquals(415, $response->status);
  295     }
  296 
  297     public function testUpdateFileParsableBody()
  298     {
  299         $this->calBackend->createCalendarObject('calendar1', 'blabla.ics', 'foo');
  300         $request = new HTTP\Request(
  301             'PUT',
  302             '/calendars/admin/calendar1/blabla.ics'
  303         );
  304         $ics = <<<ICS
  305 BEGIN:VCALENDAR
  306 VERSION:2.0
  307 PRODID:foo
  308 BEGIN:VEVENT
  309 UID:foo
  310 DTSTAMP:20160406T052348Z
  311 DTSTART:20160706T140000Z
  312 END:VEVENT
  313 END:VCALENDAR
  314 ICS;
  315 
  316         $request->setBody($ics);
  317         $response = $this->request($request);
  318 
  319         $this->assertEquals(204, $response->status);
  320 
  321         $expected = [
  322             'uri' => 'blabla.ics',
  323             'calendardata' => $ics,
  324             'calendarid' => 'calendar1',
  325             'lastmodified' => null,
  326         ];
  327 
  328         $this->assertEquals($expected, $this->calBackend->getCalendarObject('calendar1', 'blabla.ics'));
  329     }
  330 
  331     public function testCreateFileInvalidComponent()
  332     {
  333         $request = HTTP\Sapi::createFromServerArray([
  334             'REQUEST_METHOD' => 'PUT',
  335             'REQUEST_URI' => '/calendars/admin/calendar2/blabla.ics',
  336         ]);
  337         $request->setBody("BEGIN:VCALENDAR\r\nBEGIN:VTIMEZONE\r\nEND:VTIMEZONE\r\nBEGIN:VEVENT\r\nUID:foo\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n");
  338 
  339         $response = $this->request($request);
  340 
  341         $this->assertEquals(403, $response->status, 'Incorrect status returned! Full response body: '.$response->getBodyAsString());
  342     }
  343 
  344     public function testUpdateFileInvalidComponent()
  345     {
  346         $this->calBackend->createCalendarObject('calendar2', 'blabla.ics', 'foo');
  347         $request = HTTP\Sapi::createFromServerArray([
  348             'REQUEST_METHOD' => 'PUT',
  349             'REQUEST_URI' => '/calendars/admin/calendar2/blabla.ics',
  350         ]);
  351         $request->setBody("BEGIN:VCALENDAR\r\nBEGIN:VTIMEZONE\r\nEND:VTIMEZONE\r\nBEGIN:VEVENT\r\nUID:foo\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n");
  352 
  353         $response = $this->request($request);
  354 
  355         $this->assertEquals(403, $response->status, 'Incorrect status returned! Full response body: '.$response->getBodyAsString());
  356     }
  357 
  358     /**
  359      * What we are testing here, is if we send in a latin1 character, the
  360      * server should automatically transform this into UTF-8.
  361      *
  362      * More importantly. If any transformation happens, the etag must no longer
  363      * be returned by the server.
  364      */
  365     public function testCreateFileModified()
  366     {
  367         $request = new HTTP\Request(
  368             'PUT',
  369             '/calendars/admin/calendar1/blabla.ics'
  370         );
  371         $ics = <<<ICS
  372 BEGIN:VCALENDAR
  373 VERSION:2.0
  374 PRODID:foo
  375 BEGIN:VEVENT
  376 UID:foo
  377 SUMMARY:Meeting in M\xfcnster
  378 DTSTAMP:20160406T052348Z
  379 DTSTART:20160706T140000Z
  380 END:VEVENT
  381 END:VCALENDAR
  382 ICS;
  383 
  384         $request->setBody($ics);
  385 
  386         $response = $this->request($request);
  387 
  388         $this->assertEquals(201, $response->status, 'Incorrect status returned! Full response body: '.$response->getBodyAsString());
  389         $this->assertNull($response->getHeader('ETag'));
  390     }
  391 }