"Fossies" - the Fresh Open Source Software Archive

Member "dav-4.0.3/tests/Sabre/CalDAV/SharingPluginTest.php" (10 Jan 2020, 11501 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 "SharingPluginTest.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\DAV\Xml\Element\Sharee;
    9 use Sabre\HTTP;
   10 
   11 class SharingPluginTest extends \Sabre\DAVServerTest
   12 {
   13     protected $setupCalDAV = true;
   14     protected $setupCalDAVSharing = true;
   15     protected $setupACL = true;
   16     protected $autoLogin = 'user1';
   17 
   18     public function setUp()
   19     {
   20         $this->caldavCalendars = [
   21             [
   22                 'principaluri' => 'principals/user1',
   23                 'id' => 1,
   24                 'uri' => 'cal1',
   25             ],
   26             [
   27                 'principaluri' => 'principals/user1',
   28                 'id' => 2,
   29                 'uri' => 'cal2',
   30                 'share-access' => \Sabre\DAV\Sharing\Plugin::ACCESS_READWRITE,
   31             ],
   32             [
   33                 'principaluri' => 'principals/user1',
   34                 'id' => 3,
   35                 'uri' => 'cal3',
   36             ],
   37         ];
   38 
   39         parent::setUp();
   40 
   41         // Making the logged in user an admin, for full access:
   42         $this->aclPlugin->adminPrincipals[] = 'principals/user2';
   43     }
   44 
   45     public function testSimple()
   46     {
   47         $this->assertInstanceOf('Sabre\\CalDAV\\SharingPlugin', $this->server->getPlugin('caldav-sharing'));
   48         $this->assertEquals(
   49             'caldav-sharing',
   50             $this->caldavSharingPlugin->getPluginInfo()['name']
   51         );
   52     }
   53 
   54     /**
   55      * @expectedException \LogicException
   56      */
   57     public function testSetupWithoutCoreSharingPlugin()
   58     {
   59         $server = new DAV\Server();
   60         $server->addPlugin(
   61             new SharingPlugin()
   62         );
   63     }
   64 
   65     public function testGetFeatures()
   66     {
   67         $this->assertEquals(['calendarserver-sharing'], $this->caldavSharingPlugin->getFeatures());
   68     }
   69 
   70     public function testBeforeGetShareableCalendar()
   71     {
   72         // Forcing the server to authenticate:
   73         $this->authPlugin->beforeMethod(new HTTP\Request('GET', '/'), new HTTP\Response());
   74         $props = $this->server->getProperties('calendars/user1/cal1', [
   75             '{'.Plugin::NS_CALENDARSERVER.'}invite',
   76             '{'.Plugin::NS_CALENDARSERVER.'}allowed-sharing-modes',
   77         ]);
   78 
   79         $this->assertInstanceOf('Sabre\\CalDAV\\Xml\\Property\\Invite', $props['{'.Plugin::NS_CALENDARSERVER.'}invite']);
   80         $this->assertInstanceOf('Sabre\\CalDAV\\Xml\\Property\\AllowedSharingModes', $props['{'.Plugin::NS_CALENDARSERVER.'}allowed-sharing-modes']);
   81     }
   82 
   83     public function testBeforeGetSharedCalendar()
   84     {
   85         $props = $this->server->getProperties('calendars/user1/cal2', [
   86             '{'.Plugin::NS_CALENDARSERVER.'}shared-url',
   87             '{'.Plugin::NS_CALENDARSERVER.'}invite',
   88         ]);
   89 
   90         $this->assertInstanceOf('Sabre\\CalDAV\\Xml\\Property\\Invite', $props['{'.Plugin::NS_CALENDARSERVER.'}invite']);
   91         //$this->assertInstanceOf('Sabre\\DAV\\Xml\\Property\\Href', $props['{' . Plugin::NS_CALENDARSERVER . '}shared-url']);
   92     }
   93 
   94     public function testUpdateResourceType()
   95     {
   96         $this->caldavBackend->updateInvites(1,
   97             [
   98                 new Sharee([
   99                     'href' => 'mailto:joe@example.org',
  100                 ]),
  101             ]
  102         );
  103         $result = $this->server->updateProperties('calendars/user1/cal1', [
  104             '{DAV:}resourcetype' => new DAV\Xml\Property\ResourceType(['{DAV:}collection']),
  105         ]);
  106 
  107         $this->assertEquals([
  108             '{DAV:}resourcetype' => 200,
  109         ], $result);
  110 
  111         $this->assertEquals(0, count($this->caldavBackend->getInvites(1)));
  112     }
  113 
  114     public function testUpdatePropertiesPassThru()
  115     {
  116         $result = $this->server->updateProperties('calendars/user1/cal3', [
  117             '{DAV:}foo' => 'bar',
  118         ]);
  119 
  120         $this->assertEquals([
  121             '{DAV:}foo' => 200,
  122         ], $result);
  123     }
  124 
  125     public function testUnknownMethodNoPOST()
  126     {
  127         $request = HTTP\Sapi::createFromServerArray([
  128             'REQUEST_METHOD' => 'PATCH',
  129             'REQUEST_URI' => '/',
  130         ]);
  131 
  132         $response = $this->request($request);
  133 
  134         $this->assertEquals(501, $response->status, $response->getBodyAsString());
  135     }
  136 
  137     public function testUnknownMethodNoXML()
  138     {
  139         $request = HTTP\Sapi::createFromServerArray([
  140             'REQUEST_METHOD' => 'POST',
  141             'REQUEST_URI' => '/',
  142             'CONTENT_TYPE' => 'text/plain',
  143         ]);
  144 
  145         $response = $this->request($request);
  146 
  147         $this->assertEquals(501, $response->status, $response->getBodyAsString());
  148     }
  149 
  150     public function testUnknownMethodNoNode()
  151     {
  152         $request = HTTP\Sapi::createFromServerArray([
  153             'REQUEST_METHOD' => 'POST',
  154             'REQUEST_URI' => '/foo',
  155             'CONTENT_TYPE' => 'text/xml',
  156         ]);
  157 
  158         $response = $this->request($request);
  159 
  160         $this->assertEquals(501, $response->status, $response->getBodyAsString());
  161     }
  162 
  163     public function testShareRequest()
  164     {
  165         $request = new HTTP\Request('POST', '/calendars/user1/cal1', ['Content-Type' => 'text/xml']);
  166 
  167         $xml = <<<RRR
  168 <?xml version="1.0"?>
  169 <cs:share xmlns:cs="http://calendarserver.org/ns/" xmlns:d="DAV:">
  170     <cs:set>
  171         <d:href>mailto:joe@example.org</d:href>
  172         <cs:common-name>Joe Shmoe</cs:common-name>
  173         <cs:read-write />
  174     </cs:set>
  175     <cs:remove>
  176         <d:href>mailto:nancy@example.org</d:href>
  177     </cs:remove>
  178 </cs:share>
  179 RRR;
  180 
  181         $request->setBody($xml);
  182 
  183         $response = $this->request($request, 200);
  184 
  185         $this->assertEquals(
  186             [
  187                 new Sharee([
  188                     'href' => 'mailto:joe@example.org',
  189                     'properties' => [
  190                         '{DAV:}displayname' => 'Joe Shmoe',
  191                     ],
  192                     'access' => \Sabre\DAV\Sharing\Plugin::ACCESS_READWRITE,
  193                     'inviteStatus' => \Sabre\DAV\Sharing\Plugin::INVITE_NORESPONSE,
  194                     'comment' => '',
  195                 ]),
  196             ],
  197             $this->caldavBackend->getInvites(1)
  198         );
  199 
  200         // Wiping out tree cache
  201         $this->server->tree->markDirty('');
  202 
  203         // Verifying that the calendar is now marked shared.
  204         $props = $this->server->getProperties('calendars/user1/cal1', ['{DAV:}resourcetype']);
  205         $this->assertTrue(
  206             $props['{DAV:}resourcetype']->is('{http://calendarserver.org/ns/}shared-owner')
  207         );
  208     }
  209 
  210     public function testShareRequestNoShareableCalendar()
  211     {
  212         $request = new HTTP\Request(
  213             'POST',
  214             '/calendars/user1/cal2',
  215             ['Content-Type' => 'text/xml']
  216         );
  217 
  218         $xml = '<?xml version="1.0"?>
  219 <cs:share xmlns:cs="'.Plugin::NS_CALENDARSERVER.'" xmlns:d="DAV:">
  220     <cs:set>
  221         <d:href>mailto:joe@example.org</d:href>
  222         <cs:common-name>Joe Shmoe</cs:common-name>
  223         <cs:read-write />
  224     </cs:set>
  225     <cs:remove>
  226         <d:href>mailto:nancy@example.org</d:href>
  227     </cs:remove>
  228 </cs:share>
  229 ';
  230 
  231         $request->setBody($xml);
  232 
  233         $response = $this->request($request, 403);
  234     }
  235 
  236     public function testInviteReply()
  237     {
  238         $request = HTTP\Sapi::createFromServerArray([
  239             'REQUEST_METHOD' => 'POST',
  240             'REQUEST_URI' => '/calendars/user1',
  241             'CONTENT_TYPE' => 'text/xml',
  242         ]);
  243 
  244         $xml = '<?xml version="1.0"?>
  245 <cs:invite-reply xmlns:cs="'.Plugin::NS_CALENDARSERVER.'" xmlns:d="DAV:">
  246     <cs:hosturl><d:href>/principals/owner</d:href></cs:hosturl>
  247     <cs:invite-accepted />
  248 </cs:invite-reply>
  249 ';
  250 
  251         $request->setBody($xml);
  252         $response = $this->request($request);
  253         $this->assertEquals(200, $response->status, $response->getBodyAsString());
  254     }
  255 
  256     public function testInviteBadXML()
  257     {
  258         $request = HTTP\Sapi::createFromServerArray([
  259             'REQUEST_METHOD' => 'POST',
  260             'REQUEST_URI' => '/calendars/user1',
  261             'CONTENT_TYPE' => 'text/xml',
  262         ]);
  263 
  264         $xml = '<?xml version="1.0"?>
  265 <cs:invite-reply xmlns:cs="'.Plugin::NS_CALENDARSERVER.'" xmlns:d="DAV:">
  266 </cs:invite-reply>
  267 ';
  268         $request->setBody($xml);
  269         $response = $this->request($request);
  270         $this->assertEquals(400, $response->status, $response->getBodyAsString());
  271     }
  272 
  273     public function testInviteWrongUrl()
  274     {
  275         $request = HTTP\Sapi::createFromServerArray([
  276             'REQUEST_METHOD' => 'POST',
  277             'REQUEST_URI' => '/calendars/user1/cal1',
  278             'CONTENT_TYPE' => 'text/xml',
  279         ]);
  280 
  281         $xml = '<?xml version="1.0"?>
  282 <cs:invite-reply xmlns:cs="'.Plugin::NS_CALENDARSERVER.'" xmlns:d="DAV:">
  283     <cs:hosturl><d:href>/principals/owner</d:href></cs:hosturl>
  284 </cs:invite-reply>
  285 ';
  286         $request->setBody($xml);
  287         $response = $this->request($request);
  288         $this->assertEquals(501, $response->status, $response->getBodyAsString());
  289 
  290         // If the plugin did not handle this request, it must ensure that the
  291         // body is still accessible by other plugins.
  292         $this->assertEquals($xml, $request->getBody(true));
  293     }
  294 
  295     public function testPostWithoutContentType()
  296     {
  297         $request = new HTTP\Request('POST', '/');
  298         $response = new HTTP\ResponseMock();
  299 
  300         $this->caldavSharingPlugin->httpPost($request, $response);
  301         $this->assertTrue(true);
  302     }
  303 
  304     public function testPublish()
  305     {
  306         $request = new HTTP\Request('POST', '/calendars/user1/cal1', ['Content-Type' => 'text/xml']);
  307 
  308         $xml = '<?xml version="1.0"?>
  309 <cs:publish-calendar xmlns:cs="'.Plugin::NS_CALENDARSERVER.'" xmlns:d="DAV:" />
  310 ';
  311 
  312         $request->setBody($xml);
  313 
  314         $response = $this->request($request);
  315         $this->assertEquals(202, $response->status, $response->getBodyAsString());
  316     }
  317 
  318     public function testUnpublish()
  319     {
  320         $request = new HTTP\Request(
  321             'POST',
  322             '/calendars/user1/cal1',
  323             ['Content-Type' => 'text/xml']
  324         );
  325 
  326         $xml = '<?xml version="1.0"?>
  327 <cs:unpublish-calendar xmlns:cs="'.Plugin::NS_CALENDARSERVER.'" xmlns:d="DAV:" />
  328 ';
  329 
  330         $request->setBody($xml);
  331 
  332         $response = $this->request($request);
  333         $this->assertEquals(200, $response->status, $response->getBodyAsString());
  334     }
  335 
  336     public function testPublishWrongUrl()
  337     {
  338         $request = new HTTP\Request(
  339             'POST',
  340             '/calendars/user1',
  341             ['Content-Type' => 'text/xml']
  342         );
  343 
  344         $xml = '<?xml version="1.0"?>
  345 <cs:publish-calendar xmlns:cs="'.Plugin::NS_CALENDARSERVER.'" xmlns:d="DAV:" />
  346 ';
  347 
  348         $request->setBody($xml);
  349         $this->request($request, 501);
  350     }
  351 
  352     public function testUnpublishWrongUrl()
  353     {
  354         $request = new HTTP\Request(
  355             'POST',
  356             '/calendars/user1',
  357             ['Content-Type' => 'text/xml']
  358         );
  359         $xml = '<?xml version="1.0"?>
  360 <cs:unpublish-calendar xmlns:cs="'.Plugin::NS_CALENDARSERVER.'" xmlns:d="DAV:" />
  361 ';
  362 
  363         $request->setBody($xml);
  364 
  365         $this->request($request, 501);
  366     }
  367 
  368     public function testUnknownXmlDoc()
  369     {
  370         $request = new HTTP\Request(
  371             'POST',
  372             '/calendars/user1/cal2',
  373             ['Content-Type' => 'text/xml']
  374         );
  375 
  376         $xml = '<?xml version="1.0"?>
  377 <cs:foo-bar xmlns:cs="'.Plugin::NS_CALENDARSERVER.'" xmlns:d="DAV:" />';
  378 
  379         $request->setBody($xml);
  380 
  381         $response = $this->request($request);
  382         $this->assertEquals(501, $response->status, $response->getBodyAsString());
  383     }
  384 }