"Fossies" - the Fresh Open Source Software Archive

Member "dav-4.0.3/tests/Sabre/DAV/ServerPropsTest.php" (10 Jan 2020, 7510 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 "ServerPropsTest.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 require_once 'Sabre/HTTP/ResponseMock.php';
   10 require_once 'Sabre/DAV/AbstractServer.php';
   11 
   12 class ServerPropsTest extends AbstractServer
   13 {
   14     protected function getRootNode()
   15     {
   16         return new FSExt\Directory(SABRE_TEMPDIR);
   17     }
   18 
   19     public function setUp()
   20     {
   21         if (file_exists(SABRE_TEMPDIR.'../.sabredav')) {
   22             unlink(SABRE_TEMPDIR.'../.sabredav');
   23         }
   24         parent::setUp();
   25         file_put_contents(SABRE_TEMPDIR.'/test2.txt', 'Test contents2');
   26         mkdir(SABRE_TEMPDIR.'/col');
   27         file_put_contents(SABRE_TEMPDIR.'col/test.txt', 'Test contents');
   28         $this->server->addPlugin(new Locks\Plugin(new Locks\Backend\File(SABRE_TEMPDIR.'/.locksdb')));
   29     }
   30 
   31     public function tearDown()
   32     {
   33         parent::tearDown();
   34         if (file_exists(SABRE_TEMPDIR.'../.locksdb')) {
   35             unlink(SABRE_TEMPDIR.'../.locksdb');
   36         }
   37     }
   38 
   39     private function sendRequest($body, $path = '/', $headers = ['Depth' => '0'])
   40     {
   41         $request = new HTTP\Request('PROPFIND', $path, $headers, $body);
   42 
   43         $this->server->httpRequest = $request;
   44         $this->server->exec();
   45     }
   46 
   47     public function testPropFindEmptyBody()
   48     {
   49         $this->sendRequest('');
   50         $this->assertEquals(207, $this->response->status);
   51 
   52         $this->assertEquals([
   53                 'X-Sabre-Version' => [Version::VERSION],
   54                 'Content-Type' => ['application/xml; charset=utf-8'],
   55                 'DAV' => ['1, 3, extended-mkcol, 2'],
   56                 'Vary' => ['Brief,Prefer'],
   57             ],
   58             $this->response->getHeaders()
   59          );
   60 
   61         $body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/", 'xmlns\\1="urn:DAV"', $this->response->getBodyAsString());
   62         $xml = simplexml_load_string($body);
   63         $xml->registerXPathNamespace('d', 'urn:DAV');
   64 
   65         list($data) = $xml->xpath('/d:multistatus/d:response/d:href');
   66         $this->assertEquals('/', (string) $data, 'href element should have been /');
   67 
   68         $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:resourcetype');
   69         $this->assertEquals(1, count($data));
   70     }
   71 
   72     public function testPropFindEmptyBodyFile()
   73     {
   74         $this->sendRequest('', '/test2.txt', []);
   75         $this->assertEquals(207, $this->response->status);
   76 
   77         $this->assertEquals([
   78                 'X-Sabre-Version' => [Version::VERSION],
   79                 'Content-Type' => ['application/xml; charset=utf-8'],
   80                 'DAV' => ['1, 3, extended-mkcol, 2'],
   81                 'Vary' => ['Brief,Prefer'],
   82             ],
   83             $this->response->getHeaders()
   84          );
   85 
   86         $body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/", 'xmlns\\1="urn:DAV"', $this->response->getBodyAsString());
   87         $xml = simplexml_load_string($body);
   88         $xml->registerXPathNamespace('d', 'urn:DAV');
   89 
   90         list($data) = $xml->xpath('/d:multistatus/d:response/d:href');
   91         $this->assertEquals('/test2.txt', (string) $data, 'href element should have been /test2.txt');
   92 
   93         $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:getcontentlength');
   94         $this->assertEquals(1, count($data));
   95     }
   96 
   97     public function testSupportedLocks()
   98     {
   99         $xml = '<?xml version="1.0"?>
  100 <d:propfind xmlns:d="DAV:">
  101   <d:prop>
  102     <d:supportedlock />
  103   </d:prop>
  104 </d:propfind>';
  105 
  106         $this->sendRequest($xml);
  107 
  108         $body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/", 'xmlns\\1="urn:DAV"', $this->response->getBodyAsString());
  109         $xml = simplexml_load_string($body);
  110         $xml->registerXPathNamespace('d', 'urn:DAV');
  111 
  112         $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supportedlock/d:lockentry');
  113         $this->assertEquals(2, count($data), 'We expected two \'d:lockentry\' tags');
  114 
  115         $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supportedlock/d:lockentry/d:lockscope');
  116         $this->assertEquals(2, count($data), 'We expected two \'d:lockscope\' tags');
  117 
  118         $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supportedlock/d:lockentry/d:locktype');
  119         $this->assertEquals(2, count($data), 'We expected two \'d:locktype\' tags');
  120 
  121         $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supportedlock/d:lockentry/d:lockscope/d:shared');
  122         $this->assertEquals(1, count($data), 'We expected a \'d:shared\' tag');
  123 
  124         $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supportedlock/d:lockentry/d:lockscope/d:exclusive');
  125         $this->assertEquals(1, count($data), 'We expected a \'d:exclusive\' tag');
  126 
  127         $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supportedlock/d:lockentry/d:locktype/d:write');
  128         $this->assertEquals(2, count($data), 'We expected two \'d:write\' tags');
  129     }
  130 
  131     public function testLockDiscovery()
  132     {
  133         $xml = '<?xml version="1.0"?>
  134 <d:propfind xmlns:d="DAV:">
  135   <d:prop>
  136     <d:lockdiscovery />
  137   </d:prop>
  138 </d:propfind>';
  139 
  140         $this->sendRequest($xml);
  141 
  142         $body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/", 'xmlns\\1="urn:DAV"', $this->response->getBodyAsString());
  143         $xml = simplexml_load_string($body);
  144         $xml->registerXPathNamespace('d', 'urn:DAV');
  145 
  146         $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:lockdiscovery');
  147         $this->assertEquals(1, count($data), 'We expected a \'d:lockdiscovery\' tag');
  148     }
  149 
  150     public function testUnknownProperty()
  151     {
  152         $xml = '<?xml version="1.0"?>
  153 <d:propfind xmlns:d="DAV:">
  154   <d:prop>
  155     <d:macaroni />
  156   </d:prop>
  157 </d:propfind>';
  158 
  159         $this->sendRequest($xml);
  160         $body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/", 'xmlns\\1="urn:DAV"', $this->response->getBodyAsString());
  161         $xml = simplexml_load_string($body);
  162         $xml->registerXPathNamespace('d', 'urn:DAV');
  163         $pathTests = [
  164             '/d:multistatus',
  165             '/d:multistatus/d:response',
  166             '/d:multistatus/d:response/d:propstat',
  167             '/d:multistatus/d:response/d:propstat/d:status',
  168             '/d:multistatus/d:response/d:propstat/d:prop',
  169             '/d:multistatus/d:response/d:propstat/d:prop/d:macaroni',
  170         ];
  171         foreach ($pathTests as $test) {
  172             $this->assertTrue(true == count($xml->xpath($test)), 'We expected the '.$test.' element to appear in the response, we got: '.$body);
  173         }
  174 
  175         $val = $xml->xpath('/d:multistatus/d:response/d:propstat/d:status');
  176         $this->assertEquals(1, count($val), $body);
  177         $this->assertEquals('HTTP/1.1 404 Not Found', (string) $val[0]);
  178     }
  179 
  180     public function testParsePropPatchRequest()
  181     {
  182         $body = '<?xml version="1.0"?>
  183 <d:propertyupdate xmlns:d="DAV:" xmlns:s="http://sabredav.org/NS/test">
  184   <d:set><d:prop><s:someprop>somevalue</s:someprop></d:prop></d:set>
  185   <d:remove><d:prop><s:someprop2 /></d:prop></d:remove>
  186   <d:set><d:prop><s:someprop3>removeme</s:someprop3></d:prop></d:set>
  187   <d:remove><d:prop><s:someprop3 /></d:prop></d:remove>
  188 </d:propertyupdate>';
  189 
  190         $result = $this->server->xml->parse($body);
  191         $this->assertEquals([
  192             '{http://sabredav.org/NS/test}someprop' => 'somevalue',
  193             '{http://sabredav.org/NS/test}someprop2' => null,
  194             '{http://sabredav.org/NS/test}someprop3' => null,
  195         ], $result->properties);
  196     }
  197 }