"Fossies" - the Fresh Open Source Software Archive

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

    1 <?php
    2 
    3 declare(strict_types=1);
    4 
    5 namespace Sabre\DAV;
    6 
    7 use Sabre\HTTP;
    8 
    9 class ServerMKCOLTest extends AbstractServer
   10 {
   11     public function testMkcol()
   12     {
   13         $serverVars = [
   14             'REQUEST_URI' => '/testcol',
   15             'REQUEST_METHOD' => 'MKCOL',
   16         ];
   17 
   18         $request = HTTP\Sapi::createFromServerArray($serverVars);
   19         $request->setBody('');
   20         $this->server->httpRequest = ($request);
   21         $this->server->exec();
   22 
   23         $this->assertEquals([
   24             'X-Sabre-Version' => [Version::VERSION],
   25             'Content-Length' => ['0'],
   26         ], $this->response->getHeaders());
   27 
   28         $this->assertEquals(201, $this->response->status);
   29         $this->assertEquals('', $this->response->getBodyAsString());
   30         $this->assertTrue(is_dir($this->tempDir.'/testcol'));
   31     }
   32 
   33     /**
   34      * @depends testMkcol
   35      */
   36     public function testMKCOLUnknownBody()
   37     {
   38         $serverVars = [
   39             'REQUEST_URI' => '/testcol',
   40             'REQUEST_METHOD' => 'MKCOL',
   41         ];
   42 
   43         $request = HTTP\Sapi::createFromServerArray($serverVars);
   44         $request->setBody('Hello');
   45         $this->server->httpRequest = ($request);
   46         $this->server->exec();
   47 
   48         $this->assertEquals([
   49             'X-Sabre-Version' => [Version::VERSION],
   50             'Content-Type' => ['application/xml; charset=utf-8'],
   51         ], $this->response->getHeaders());
   52 
   53         $this->assertEquals(415, $this->response->status);
   54     }
   55 
   56     /**
   57      * @depends testMkcol
   58      */
   59     public function testMKCOLBrokenXML()
   60     {
   61         $serverVars = [
   62             'REQUEST_URI' => '/testcol',
   63             'REQUEST_METHOD' => 'MKCOL',
   64             'HTTP_CONTENT_TYPE' => 'application/xml',
   65         ];
   66 
   67         $request = HTTP\Sapi::createFromServerArray($serverVars);
   68         $request->setBody('Hello');
   69         $this->server->httpRequest = ($request);
   70         $this->server->exec();
   71 
   72         $this->assertEquals([
   73             'X-Sabre-Version' => [Version::VERSION],
   74             'Content-Type' => ['application/xml; charset=utf-8'],
   75         ], $this->response->getHeaders());
   76 
   77         $this->assertEquals(400, $this->response->getStatus(), $this->response->getBodyAsString());
   78     }
   79 
   80     /**
   81      * @depends testMkcol
   82      */
   83     public function testMKCOLUnknownXML()
   84     {
   85         $serverVars = [
   86             'REQUEST_URI' => '/testcol',
   87             'REQUEST_METHOD' => 'MKCOL',
   88             'HTTP_CONTENT_TYPE' => 'application/xml',
   89         ];
   90 
   91         $request = HTTP\Sapi::createFromServerArray($serverVars);
   92         $request->setBody('<?xml version="1.0"?><html></html>');
   93         $this->server->httpRequest = ($request);
   94         $this->server->exec();
   95 
   96         $this->assertEquals([
   97             'X-Sabre-Version' => [Version::VERSION],
   98             'Content-Type' => ['application/xml; charset=utf-8'],
   99         ], $this->response->getHeaders());
  100 
  101         $this->assertEquals(400, $this->response->getStatus());
  102     }
  103 
  104     /**
  105      * @depends testMkcol
  106      */
  107     public function testMKCOLNoResourceType()
  108     {
  109         $serverVars = [
  110             'REQUEST_URI' => '/testcol',
  111             'REQUEST_METHOD' => 'MKCOL',
  112             'HTTP_CONTENT_TYPE' => 'application/xml',
  113         ];
  114 
  115         $request = HTTP\Sapi::createFromServerArray($serverVars);
  116         $request->setBody('<?xml version="1.0"?>
  117 <mkcol xmlns="DAV:">
  118   <set>
  119     <prop>
  120         <displayname>Evert</displayname>
  121     </prop>
  122   </set>
  123 </mkcol>');
  124         $this->server->httpRequest = ($request);
  125         $this->server->exec();
  126 
  127         $this->assertEquals([
  128             'X-Sabre-Version' => [Version::VERSION],
  129             'Content-Type' => ['application/xml; charset=utf-8'],
  130         ], $this->response->getHeaders());
  131 
  132         $this->assertEquals(400, $this->response->status, 'Wrong statuscode received. Full response body: '.$this->response->getBodyAsString());
  133     }
  134 
  135     /**
  136      * @depends testMkcol
  137      */
  138     public function testMKCOLIncorrectResourceType()
  139     {
  140         $serverVars = [
  141             'REQUEST_URI' => '/testcol',
  142             'REQUEST_METHOD' => 'MKCOL',
  143             'HTTP_CONTENT_TYPE' => 'application/xml',
  144         ];
  145 
  146         $request = HTTP\Sapi::createFromServerArray($serverVars);
  147         $request->setBody('<?xml version="1.0"?>
  148 <mkcol xmlns="DAV:">
  149   <set>
  150     <prop>
  151         <resourcetype><collection /><blabla /></resourcetype>
  152     </prop>
  153   </set>
  154 </mkcol>');
  155         $this->server->httpRequest = ($request);
  156         $this->server->exec();
  157 
  158         $this->assertEquals([
  159             'X-Sabre-Version' => [Version::VERSION],
  160             'Content-Type' => ['application/xml; charset=utf-8'],
  161         ], $this->response->getHeaders());
  162 
  163         $this->assertEquals(403, $this->response->status, 'Wrong statuscode received. Full response body: '.$this->response->getBodyAsString());
  164     }
  165 
  166     /**
  167      * @depends testMKCOLIncorrectResourceType
  168      */
  169     public function testMKCOLSuccess()
  170     {
  171         $serverVars = [
  172             'REQUEST_URI' => '/testcol',
  173             'REQUEST_METHOD' => 'MKCOL',
  174             'HTTP_CONTENT_TYPE' => 'application/xml',
  175         ];
  176 
  177         $request = HTTP\Sapi::createFromServerArray($serverVars);
  178         $request->setBody('<?xml version="1.0"?>
  179 <mkcol xmlns="DAV:">
  180   <set>
  181     <prop>
  182         <resourcetype><collection /></resourcetype>
  183     </prop>
  184   </set>
  185 </mkcol>');
  186         $this->server->httpRequest = ($request);
  187         $this->server->exec();
  188 
  189         $this->assertEquals([
  190             'X-Sabre-Version' => [Version::VERSION],
  191             'Content-Length' => ['0'],
  192         ], $this->response->getHeaders());
  193 
  194         $this->assertEquals(201, $this->response->status, 'Wrong statuscode received. Full response body: '.$this->response->getBodyAsString());
  195     }
  196 
  197     /**
  198      * @depends testMKCOLIncorrectResourceType
  199      */
  200     public function testMKCOLWhiteSpaceResourceType()
  201     {
  202         $serverVars = [
  203             'REQUEST_URI' => '/testcol',
  204             'REQUEST_METHOD' => 'MKCOL',
  205             'HTTP_CONTENT_TYPE' => 'application/xml',
  206         ];
  207 
  208         $request = HTTP\Sapi::createFromServerArray($serverVars);
  209         $request->setBody('<?xml version="1.0"?>
  210 <mkcol xmlns="DAV:">
  211   <set>
  212     <prop>
  213         <resourcetype>
  214             <collection />
  215         </resourcetype>
  216     </prop>
  217   </set>
  218 </mkcol>');
  219         $this->server->httpRequest = ($request);
  220         $this->server->exec();
  221 
  222         $this->assertEquals([
  223             'X-Sabre-Version' => [Version::VERSION],
  224             'Content-Length' => ['0'],
  225         ], $this->response->getHeaders());
  226 
  227         $this->assertEquals(201, $this->response->status, 'Wrong statuscode received. Full response body: '.$this->response->getBodyAsString());
  228     }
  229 
  230     /**
  231      * @depends testMKCOLIncorrectResourceType
  232      */
  233     public function testMKCOLNoParent()
  234     {
  235         $serverVars = [
  236             'REQUEST_URI' => '/testnoparent/409me',
  237             'REQUEST_METHOD' => 'MKCOL',
  238         ];
  239 
  240         $request = HTTP\Sapi::createFromServerArray($serverVars);
  241         $request->setBody('');
  242 
  243         $this->server->httpRequest = ($request);
  244         $this->server->exec();
  245 
  246         $this->assertEquals([
  247             'X-Sabre-Version' => [Version::VERSION],
  248             'Content-Type' => ['application/xml; charset=utf-8'],
  249         ], $this->response->getHeaders());
  250 
  251         $this->assertEquals(409, $this->response->status, 'Wrong statuscode received. Full response body: '.$this->response->getBodyAsString());
  252     }
  253 
  254     /**
  255      * @depends testMKCOLIncorrectResourceType
  256      */
  257     public function testMKCOLParentIsNoCollection()
  258     {
  259         $serverVars = [
  260             'REQUEST_URI' => '/test.txt/409me',
  261             'REQUEST_METHOD' => 'MKCOL',
  262         ];
  263 
  264         $request = HTTP\Sapi::createFromServerArray($serverVars);
  265         $request->setBody('');
  266 
  267         $this->server->httpRequest = ($request);
  268         $this->server->exec();
  269 
  270         $this->assertEquals([
  271             'X-Sabre-Version' => [Version::VERSION],
  272             'Content-Type' => ['application/xml; charset=utf-8'],
  273         ], $this->response->getHeaders());
  274 
  275         $this->assertEquals(409, $this->response->status, 'Wrong statuscode received. Full response body: '.$this->response->getBodyAsString());
  276     }
  277 
  278     /**
  279      * @depends testMKCOLIncorrectResourceType
  280      */
  281     public function testMKCOLAlreadyExists()
  282     {
  283         $serverVars = [
  284             'REQUEST_URI' => '/test.txt',
  285             'REQUEST_METHOD' => 'MKCOL',
  286         ];
  287 
  288         $request = HTTP\Sapi::createFromServerArray($serverVars);
  289         $request->setBody('');
  290 
  291         $this->server->httpRequest = ($request);
  292         $this->server->exec();
  293 
  294         $this->assertEquals([
  295             'X-Sabre-Version' => [Version::VERSION],
  296             'Content-Type' => ['application/xml; charset=utf-8'],
  297             'Allow' => ['OPTIONS, GET, HEAD, DELETE, PROPFIND, PUT, PROPPATCH, COPY, MOVE, REPORT'],
  298         ], $this->response->getHeaders());
  299 
  300         $this->assertEquals(405, $this->response->status, 'Wrong statuscode received. Full response body: '.$this->response->getBodyAsString());
  301     }
  302 
  303     /**
  304      * @depends testMKCOLSuccess
  305      * @depends testMKCOLAlreadyExists
  306      */
  307     public function testMKCOLAndProps()
  308     {
  309         $request = new HTTP\Request(
  310             'MKCOL',
  311             '/testcol',
  312             ['Content-Type' => 'application/xml']
  313         );
  314         $request->setBody('<?xml version="1.0"?>
  315 <mkcol xmlns="DAV:">
  316   <set>
  317     <prop>
  318         <resourcetype><collection /></resourcetype>
  319         <displayname>my new collection</displayname>
  320     </prop>
  321   </set>
  322 </mkcol>');
  323         $this->server->httpRequest = ($request);
  324         $this->server->exec();
  325 
  326         $bodyAsString = $this->response->getBodyAsString();
  327         $this->assertEquals(207, $this->response->status, 'Wrong statuscode received. Full response body: '.$bodyAsString);
  328 
  329         $this->assertEquals([
  330             'X-Sabre-Version' => [Version::VERSION],
  331             'Content-Type' => ['application/xml; charset=utf-8'],
  332         ], $this->response->getHeaders());
  333 
  334         $expected = <<<XML
  335 <?xml version="1.0"?>
  336 <d:multistatus xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns">
  337  <d:response>
  338   <d:href>/testcol</d:href>
  339   <d:propstat>
  340    <d:prop>
  341     <d:displayname />
  342    </d:prop>
  343    <d:status>HTTP/1.1 403 Forbidden</d:status>
  344   </d:propstat>
  345  </d:response>
  346 </d:multistatus>
  347 XML;
  348 
  349         $this->assertXmlStringEqualsXmlString(
  350             $expected,
  351             $bodyAsString
  352         );
  353     }
  354 }