"Fossies" - the Fresh Open Source Software Archive

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

    1 <?php
    2 
    3 declare(strict_types=1);
    4 
    5 namespace Sabre\DAVACL;
    6 
    7 use Sabre\DAV;
    8 use Sabre\HTTP;
    9 
   10 require_once 'Sabre/HTTP/ResponseMock.php';
   11 
   12 class PrincipalPropertySearchTest extends \PHPUnit\Framework\TestCase
   13 {
   14     public function getServer()
   15     {
   16         $backend = new PrincipalBackend\Mock();
   17 
   18         $dir = new DAV\SimpleCollection('root');
   19         $principals = new PrincipalCollection($backend);
   20         $dir->addChild($principals);
   21 
   22         $fakeServer = new DAV\Server($dir);
   23         $fakeServer->sapi = new HTTP\SapiMock();
   24         $fakeServer->httpResponse = new HTTP\ResponseMock();
   25         $fakeServer->debugExceptions = true;
   26         $plugin = new MockPlugin();
   27         $plugin->allowAccessToNodesWithoutACL = true;
   28         $plugin->allowUnauthenticatedAccess = false;
   29 
   30         $this->assertTrue($plugin instanceof Plugin);
   31         $fakeServer->addPlugin($plugin);
   32         $this->assertEquals($plugin, $fakeServer->getPlugin('acl'));
   33 
   34         return $fakeServer;
   35     }
   36 
   37     public function testDepth1()
   38     {
   39         $xml = '<?xml version="1.0"?>
   40 <d:principal-property-search xmlns:d="DAV:">
   41   <d:property-search>
   42      <d:prop>
   43        <d:displayname />
   44      </d:prop>
   45      <d:match>user</d:match>
   46   </d:property-search>
   47   <d:prop>
   48     <d:displayname />
   49     <d:getcontentlength />
   50   </d:prop>
   51 </d:principal-property-search>';
   52 
   53         $serverVars = [
   54             'REQUEST_METHOD' => 'REPORT',
   55             'HTTP_DEPTH' => '1',
   56             'REQUEST_URI' => '/principals',
   57         ];
   58 
   59         $request = HTTP\Sapi::createFromServerArray($serverVars);
   60         $request->setBody($xml);
   61 
   62         $server = $this->getServer();
   63         $server->httpRequest = $request;
   64 
   65         $server->exec();
   66 
   67         $this->assertEquals(400, $server->httpResponse->getStatus(), $server->httpResponse->getBodyAsString());
   68         $this->assertEquals([
   69             'X-Sabre-Version' => [DAV\Version::VERSION],
   70             'Content-Type' => ['application/xml; charset=utf-8'],
   71         ], $server->httpResponse->getHeaders());
   72     }
   73 
   74     public function testUnknownSearchField()
   75     {
   76         $xml = '<?xml version="1.0"?>
   77 <d:principal-property-search xmlns:d="DAV:">
   78   <d:property-search>
   79      <d:prop>
   80        <d:yourmom />
   81      </d:prop>
   82      <d:match>user</d:match>
   83   </d:property-search>
   84   <d:prop>
   85     <d:displayname />
   86     <d:getcontentlength />
   87   </d:prop>
   88 </d:principal-property-search>';
   89 
   90         $serverVars = [
   91             'REQUEST_METHOD' => 'REPORT',
   92             'HTTP_DEPTH' => '0',
   93             'REQUEST_URI' => '/principals',
   94         ];
   95 
   96         $request = HTTP\Sapi::createFromServerArray($serverVars);
   97         $request->setBody($xml);
   98 
   99         $server = $this->getServer();
  100         $server->httpRequest = $request;
  101 
  102         $server->exec();
  103 
  104         $this->assertEquals(207, $server->httpResponse->getStatus(), 'Full body: '.$server->httpResponse->getBodyAsString());
  105         $this->assertEquals([
  106             'X-Sabre-Version' => [DAV\Version::VERSION],
  107             'Content-Type' => ['application/xml; charset=utf-8'],
  108             'Vary' => ['Brief,Prefer'],
  109         ], $server->httpResponse->getHeaders());
  110     }
  111 
  112     public function testCorrect()
  113     {
  114         $xml = '<?xml version="1.0"?>
  115 <d:principal-property-search xmlns:d="DAV:">
  116   <d:apply-to-principal-collection-set />
  117   <d:property-search>
  118      <d:prop>
  119        <d:displayname />
  120      </d:prop>
  121      <d:match>user</d:match>
  122   </d:property-search>
  123   <d:prop>
  124     <d:displayname />
  125     <d:getcontentlength />
  126   </d:prop>
  127 </d:principal-property-search>';
  128 
  129         $serverVars = [
  130             'REQUEST_METHOD' => 'REPORT',
  131             'HTTP_DEPTH' => '0',
  132             'REQUEST_URI' => '/',
  133         ];
  134 
  135         $request = HTTP\Sapi::createFromServerArray($serverVars);
  136         $request->setBody($xml);
  137 
  138         $server = $this->getServer();
  139         $server->httpRequest = $request;
  140 
  141         $server->exec();
  142 
  143         $bodyAsString = $server->httpResponse->getBodyAsString();
  144         $this->assertEquals(207, $server->httpResponse->status, $bodyAsString);
  145         $this->assertEquals([
  146             'X-Sabre-Version' => [DAV\Version::VERSION],
  147             'Content-Type' => ['application/xml; charset=utf-8'],
  148             'Vary' => ['Brief,Prefer'],
  149         ], $server->httpResponse->getHeaders());
  150 
  151         $check = [
  152             '/d:multistatus',
  153             '/d:multistatus/d:response' => 2,
  154             '/d:multistatus/d:response/d:href' => 2,
  155             '/d:multistatus/d:response/d:propstat' => 4,
  156             '/d:multistatus/d:response/d:propstat/d:prop' => 4,
  157             '/d:multistatus/d:response/d:propstat/d:prop/d:displayname' => 2,
  158             '/d:multistatus/d:response/d:propstat/d:prop/d:getcontentlength' => 2,
  159             '/d:multistatus/d:response/d:propstat/d:status' => 4,
  160         ];
  161 
  162         $xml = simplexml_load_string($bodyAsString);
  163         $xml->registerXPathNamespace('d', 'DAV:');
  164         foreach ($check as $v1 => $v2) {
  165             $xpath = is_int($v1) ? $v2 : $v1;
  166 
  167             $result = $xml->xpath($xpath);
  168 
  169             $count = 1;
  170             if (!is_int($v1)) {
  171                 $count = $v2;
  172             }
  173 
  174             $this->assertEquals($count, count($result), 'we expected '.$count.' appearances of '.$xpath.' . We found '.count($result).'. Full response body: '.$server->httpResponse->getBodyAsString());
  175         }
  176     }
  177 
  178     public function testAND()
  179     {
  180         $xml = '<?xml version="1.0"?>
  181 <d:principal-property-search xmlns:d="DAV:">
  182   <d:apply-to-principal-collection-set />
  183   <d:property-search>
  184      <d:prop>
  185        <d:displayname />
  186      </d:prop>
  187      <d:match>user</d:match>
  188   </d:property-search>
  189   <d:property-search>
  190      <d:prop>
  191        <d:foo />
  192      </d:prop>
  193      <d:match>bar</d:match>
  194   </d:property-search>
  195   <d:prop>
  196     <d:displayname />
  197     <d:getcontentlength />
  198   </d:prop>
  199 </d:principal-property-search>';
  200 
  201         $serverVars = [
  202             'REQUEST_METHOD' => 'REPORT',
  203             'HTTP_DEPTH' => '0',
  204             'REQUEST_URI' => '/',
  205         ];
  206 
  207         $request = HTTP\Sapi::createFromServerArray($serverVars);
  208         $request->setBody($xml);
  209 
  210         $server = $this->getServer();
  211         $server->httpRequest = $request;
  212 
  213         $server->exec();
  214 
  215         $bodyAsString = $server->httpResponse->getBodyAsString();
  216         $this->assertEquals(207, $server->httpResponse->status, $bodyAsString);
  217         $this->assertEquals([
  218             'X-Sabre-Version' => [DAV\Version::VERSION],
  219             'Content-Type' => ['application/xml; charset=utf-8'],
  220             'Vary' => ['Brief,Prefer'],
  221         ], $server->httpResponse->getHeaders());
  222 
  223         $check = [
  224             '/d:multistatus',
  225             '/d:multistatus/d:response' => 0,
  226             '/d:multistatus/d:response/d:href' => 0,
  227             '/d:multistatus/d:response/d:propstat' => 0,
  228             '/d:multistatus/d:response/d:propstat/d:prop' => 0,
  229             '/d:multistatus/d:response/d:propstat/d:prop/d:displayname' => 0,
  230             '/d:multistatus/d:response/d:propstat/d:prop/d:getcontentlength' => 0,
  231             '/d:multistatus/d:response/d:propstat/d:status' => 0,
  232         ];
  233 
  234         $xml = simplexml_load_string($bodyAsString);
  235         $xml->registerXPathNamespace('d', 'DAV:');
  236         foreach ($check as $v1 => $v2) {
  237             $xpath = is_int($v1) ? $v2 : $v1;
  238 
  239             $result = $xml->xpath($xpath);
  240 
  241             $count = 1;
  242             if (!is_int($v1)) {
  243                 $count = $v2;
  244             }
  245 
  246             $this->assertEquals($count, count($result), 'we expected '.$count.' appearances of '.$xpath.' . We found '.count($result).'. Full response body: '.$server->httpResponse->getBodyAsString());
  247         }
  248     }
  249 
  250     public function testOR()
  251     {
  252         $xml = '<?xml version="1.0"?>
  253 <d:principal-property-search xmlns:d="DAV:" test="anyof">
  254   <d:apply-to-principal-collection-set />
  255   <d:property-search>
  256      <d:prop>
  257        <d:displayname />
  258      </d:prop>
  259      <d:match>user</d:match>
  260   </d:property-search>
  261   <d:property-search>
  262      <d:prop>
  263        <d:foo />
  264      </d:prop>
  265      <d:match>bar</d:match>
  266   </d:property-search>
  267   <d:prop>
  268     <d:displayname />
  269     <d:getcontentlength />
  270   </d:prop>
  271 </d:principal-property-search>';
  272 
  273         $serverVars = [
  274             'REQUEST_METHOD' => 'REPORT',
  275             'HTTP_DEPTH' => '0',
  276             'REQUEST_URI' => '/',
  277         ];
  278 
  279         $request = HTTP\Sapi::createFromServerArray($serverVars);
  280         $request->setBody($xml);
  281 
  282         $server = $this->getServer();
  283         $server->httpRequest = $request;
  284 
  285         $server->exec();
  286 
  287         $bodyAsString = $server->httpResponse->getBodyAsString();
  288         $this->assertEquals(207, $server->httpResponse->status, $bodyAsString);
  289         $this->assertEquals([
  290             'X-Sabre-Version' => [DAV\Version::VERSION],
  291             'Content-Type' => ['application/xml; charset=utf-8'],
  292             'Vary' => ['Brief,Prefer'],
  293         ], $server->httpResponse->getHeaders());
  294 
  295         $check = [
  296             '/d:multistatus',
  297             '/d:multistatus/d:response' => 2,
  298             '/d:multistatus/d:response/d:href' => 2,
  299             '/d:multistatus/d:response/d:propstat' => 4,
  300             '/d:multistatus/d:response/d:propstat/d:prop' => 4,
  301             '/d:multistatus/d:response/d:propstat/d:prop/d:displayname' => 2,
  302             '/d:multistatus/d:response/d:propstat/d:prop/d:getcontentlength' => 2,
  303             '/d:multistatus/d:response/d:propstat/d:status' => 4,
  304         ];
  305 
  306         $xml = simplexml_load_string($bodyAsString);
  307         $xml->registerXPathNamespace('d', 'DAV:');
  308         foreach ($check as $v1 => $v2) {
  309             $xpath = is_int($v1) ? $v2 : $v1;
  310 
  311             $result = $xml->xpath($xpath);
  312 
  313             $count = 1;
  314             if (!is_int($v1)) {
  315                 $count = $v2;
  316             }
  317 
  318             $this->assertEquals($count, count($result), 'we expected '.$count.' appearances of '.$xpath.' . We found '.count($result).'. Full response body: '.$server->httpResponse->getBodyAsString());
  319         }
  320     }
  321 
  322     public function testWrongUri()
  323     {
  324         $xml = '<?xml version="1.0"?>
  325 <d:principal-property-search xmlns:d="DAV:">
  326   <d:property-search>
  327      <d:prop>
  328        <d:displayname />
  329      </d:prop>
  330      <d:match>user</d:match>
  331   </d:property-search>
  332   <d:prop>
  333     <d:displayname />
  334     <d:getcontentlength />
  335   </d:prop>
  336 </d:principal-property-search>';
  337 
  338         $serverVars = [
  339             'REQUEST_METHOD' => 'REPORT',
  340             'HTTP_DEPTH' => '0',
  341             'REQUEST_URI' => '/',
  342         ];
  343 
  344         $request = HTTP\Sapi::createFromServerArray($serverVars);
  345         $request->setBody($xml);
  346 
  347         $server = $this->getServer();
  348         $server->httpRequest = $request;
  349 
  350         $server->exec();
  351 
  352         $bodyAsString = $server->httpResponse->getBodyAsString();
  353         $this->assertEquals(207, $server->httpResponse->status, $bodyAsString);
  354         $this->assertEquals([
  355             'X-Sabre-Version' => [DAV\Version::VERSION],
  356             'Content-Type' => ['application/xml; charset=utf-8'],
  357             'Vary' => ['Brief,Prefer'],
  358         ], $server->httpResponse->getHeaders());
  359 
  360         $check = [
  361             '/d:multistatus',
  362             '/d:multistatus/d:response' => 0,
  363         ];
  364 
  365         $xml = simplexml_load_string($bodyAsString);
  366         $xml->registerXPathNamespace('d', 'DAV:');
  367         foreach ($check as $v1 => $v2) {
  368             $xpath = is_int($v1) ? $v2 : $v1;
  369 
  370             $result = $xml->xpath($xpath);
  371 
  372             $count = 1;
  373             if (!is_int($v1)) {
  374                 $count = $v2;
  375             }
  376 
  377             $this->assertEquals($count, count($result), 'we expected '.$count.' appearances of '.$xpath.' . We found '.count($result).'. Full response body: '.$server->httpResponse->getBodyAsString());
  378         }
  379     }
  380 }
  381 
  382 class MockPlugin extends Plugin
  383 {
  384     public function getCurrentUserPrivilegeSet($node)
  385     {
  386         return [
  387             '{DAV:}read',
  388             '{DAV:}write',
  389         ];
  390     }
  391 }