"Fossies" - the Fresh Open Source Software Archive

Member "dav-4.0.3/tests/Sabre/DAVACL/ExpandPropertiesTest.php" (10 Jan 2020, 12189 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 "ExpandPropertiesTest.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 ExpandPropertiesTest extends \PHPUnit\Framework\TestCase
   13 {
   14     public function getServer()
   15     {
   16         $tree = [
   17             new DAV\Mock\PropertiesCollection('node1', [], [
   18                 '{http://sabredav.org/ns}simple' => 'foo',
   19                 '{http://sabredav.org/ns}href' => new DAV\Xml\Property\Href('node2'),
   20                 '{DAV:}displayname' => 'Node 1',
   21             ]),
   22             new DAV\Mock\PropertiesCollection('node2', [], [
   23                 '{http://sabredav.org/ns}simple' => 'simple',
   24                 '{http://sabredav.org/ns}hreflist' => new DAV\Xml\Property\Href(['node1', 'node3']),
   25                 '{DAV:}displayname' => 'Node 2',
   26             ]),
   27             new DAV\Mock\PropertiesCollection('node3', [], [
   28                 '{http://sabredav.org/ns}simple' => 'simple',
   29                 '{DAV:}displayname' => 'Node 3',
   30             ]),
   31         ];
   32 
   33         $fakeServer = new DAV\Server($tree);
   34         $fakeServer->sapi = new HTTP\SapiMock();
   35         $fakeServer->debugExceptions = true;
   36         $fakeServer->httpResponse = new HTTP\ResponseMock();
   37         $plugin = new Plugin();
   38         $plugin->allowUnauthenticatedAccess = false;
   39         // Anyone can do anything
   40         $plugin->setDefaultACL([
   41             [
   42                 'principal' => '{DAV:}all',
   43                 'privilege' => '{DAV:}all',
   44             ],
   45         ]);
   46         $this->assertTrue($plugin instanceof Plugin);
   47 
   48         $fakeServer->addPlugin($plugin);
   49         $this->assertEquals($plugin, $fakeServer->getPlugin('acl'));
   50 
   51         return $fakeServer;
   52     }
   53 
   54     public function testSimple()
   55     {
   56         $xml = '<?xml version="1.0"?>
   57 <d:expand-property xmlns:d="DAV:">
   58   <d:property name="displayname" />
   59   <d:property name="foo" namespace="http://www.sabredav.org/NS/2010/nonexistant" />
   60   <d:property name="simple" namespace="http://sabredav.org/ns" />
   61   <d:property name="href" namespace="http://sabredav.org/ns" />
   62 </d:expand-property>';
   63 
   64         $serverVars = [
   65             'REQUEST_METHOD' => 'REPORT',
   66             'HTTP_DEPTH' => '0',
   67             'REQUEST_URI' => '/node1',
   68         ];
   69 
   70         $request = HTTP\Sapi::createFromServerArray($serverVars);
   71         $request->setBody($xml);
   72 
   73         $server = $this->getServer();
   74         $server->httpRequest = $request;
   75 
   76         $server->exec();
   77 
   78         $this->assertEquals(207, $server->httpResponse->status, 'Incorrect status code received. Full body: '.$server->httpResponse->getBodyAsString());
   79         $this->assertEquals([
   80             'X-Sabre-Version' => [DAV\Version::VERSION],
   81             'Content-Type' => ['application/xml; charset=utf-8'],
   82         ], $server->httpResponse->getHeaders());
   83 
   84         $check = [
   85             '/d:multistatus',
   86             '/d:multistatus/d:response' => 1,
   87             '/d:multistatus/d:response/d:href' => 1,
   88             '/d:multistatus/d:response/d:propstat' => 2,
   89             '/d:multistatus/d:response/d:propstat/d:prop' => 2,
   90             '/d:multistatus/d:response/d:propstat/d:prop/d:displayname' => 1,
   91             '/d:multistatus/d:response/d:propstat/d:prop/s:simple' => 1,
   92             '/d:multistatus/d:response/d:propstat/d:prop/s:href' => 1,
   93             '/d:multistatus/d:response/d:propstat/d:prop/s:href/d:href' => 1,
   94         ];
   95 
   96         $xml = simplexml_load_string($server->httpResponse->getBodyAsString());
   97         $xml->registerXPathNamespace('d', 'DAV:');
   98         $xml->registerXPathNamespace('s', 'http://sabredav.org/ns');
   99         foreach ($check as $v1 => $v2) {
  100             $xpath = is_int($v1) ? $v2 : $v1;
  101 
  102             $result = $xml->xpath($xpath);
  103 
  104             $count = 1;
  105             if (!is_int($v1)) {
  106                 $count = $v2;
  107             }
  108 
  109             $this->assertEquals($count, count($result), 'we expected '.$count.' appearances of '.$xpath.' . We found '.count($result).'. Full response: '.$server->httpResponse->getBodyAsString());
  110         }
  111     }
  112 
  113     /**
  114      * @depends testSimple
  115      */
  116     public function testExpand()
  117     {
  118         $xml = '<?xml version="1.0"?>
  119 <d:expand-property xmlns:d="DAV:">
  120   <d:property name="href" namespace="http://sabredav.org/ns">
  121       <d:property name="displayname" />
  122   </d:property>
  123 </d:expand-property>';
  124 
  125         $serverVars = [
  126             'REQUEST_METHOD' => 'REPORT',
  127             'HTTP_DEPTH' => '0',
  128             'REQUEST_URI' => '/node1',
  129         ];
  130 
  131         $request = HTTP\Sapi::createFromServerArray($serverVars);
  132         $request->setBody($xml);
  133 
  134         $server = $this->getServer();
  135         $server->httpRequest = $request;
  136 
  137         $server->exec();
  138 
  139         $this->assertEquals(207, $server->httpResponse->status, 'Incorrect response status received. Full response body: '.$server->httpResponse->getBodyAsString());
  140         $this->assertEquals([
  141             'X-Sabre-Version' => [DAV\Version::VERSION],
  142             'Content-Type' => ['application/xml; charset=utf-8'],
  143         ], $server->httpResponse->getHeaders());
  144 
  145         $check = [
  146             '/d:multistatus',
  147             '/d:multistatus/d:response' => 1,
  148             '/d:multistatus/d:response/d:href' => 1,
  149             '/d:multistatus/d:response/d:propstat' => 1,
  150             '/d:multistatus/d:response/d:propstat/d:prop' => 1,
  151             '/d:multistatus/d:response/d:propstat/d:prop/s:href' => 1,
  152             '/d:multistatus/d:response/d:propstat/d:prop/s:href/d:response' => 1,
  153             '/d:multistatus/d:response/d:propstat/d:prop/s:href/d:response/d:href' => 1,
  154             '/d:multistatus/d:response/d:propstat/d:prop/s:href/d:response/d:propstat' => 1,
  155             '/d:multistatus/d:response/d:propstat/d:prop/s:href/d:response/d:propstat/d:prop' => 1,
  156             '/d:multistatus/d:response/d:propstat/d:prop/s:href/d:response/d:propstat/d:prop/d:displayname' => 1,
  157         ];
  158 
  159         $xml = simplexml_load_string($server->httpResponse->getBodyAsString());
  160         $xml->registerXPathNamespace('d', 'DAV:');
  161         $xml->registerXPathNamespace('s', 'http://sabredav.org/ns');
  162         foreach ($check as $v1 => $v2) {
  163             $xpath = is_int($v1) ? $v2 : $v1;
  164 
  165             $result = $xml->xpath($xpath);
  166 
  167             $count = 1;
  168             if (!is_int($v1)) {
  169                 $count = $v2;
  170             }
  171 
  172             $this->assertEquals($count, count($result), 'we expected '.$count.' appearances of '.$xpath.' . We found '.count($result).' Full response body: '.$server->httpResponse->getBodyAsString());
  173         }
  174     }
  175 
  176     /**
  177      * @depends testSimple
  178      */
  179     public function testExpandHrefList()
  180     {
  181         $xml = '<?xml version="1.0"?>
  182 <d:expand-property xmlns:d="DAV:">
  183   <d:property name="hreflist" namespace="http://sabredav.org/ns">
  184       <d:property name="displayname" />
  185   </d:property>
  186 </d:expand-property>';
  187 
  188         $serverVars = [
  189             'REQUEST_METHOD' => 'REPORT',
  190             'HTTP_DEPTH' => '0',
  191             'REQUEST_URI' => '/node2',
  192         ];
  193 
  194         $request = HTTP\Sapi::createFromServerArray($serverVars);
  195         $request->setBody($xml);
  196 
  197         $server = $this->getServer();
  198         $server->httpRequest = $request;
  199 
  200         $server->exec();
  201 
  202         $this->assertEquals(207, $server->httpResponse->status);
  203         $this->assertEquals([
  204             'X-Sabre-Version' => [DAV\Version::VERSION],
  205             'Content-Type' => ['application/xml; charset=utf-8'],
  206         ], $server->httpResponse->getHeaders());
  207 
  208         $check = [
  209             '/d:multistatus',
  210             '/d:multistatus/d:response' => 1,
  211             '/d:multistatus/d:response/d:href' => 1,
  212             '/d:multistatus/d:response/d:propstat' => 1,
  213             '/d:multistatus/d:response/d:propstat/d:prop' => 1,
  214             '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist' => 1,
  215             '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response' => 2,
  216             '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:href' => 2,
  217             '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat' => 2,
  218             '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop' => 2,
  219             '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop/d:displayname' => 2,
  220         ];
  221 
  222         $xml = simplexml_load_string($server->httpResponse->getBodyAsString());
  223         $xml->registerXPathNamespace('d', 'DAV:');
  224         $xml->registerXPathNamespace('s', 'http://sabredav.org/ns');
  225         foreach ($check as $v1 => $v2) {
  226             $xpath = is_int($v1) ? $v2 : $v1;
  227 
  228             $result = $xml->xpath($xpath);
  229 
  230             $count = 1;
  231             if (!is_int($v1)) {
  232                 $count = $v2;
  233             }
  234 
  235             $this->assertEquals($count, count($result), 'we expected '.$count.' appearances of '.$xpath.' . We found '.count($result));
  236         }
  237     }
  238 
  239     /**
  240      * @depends testExpand
  241      */
  242     public function testExpandDeep()
  243     {
  244         $xml = '<?xml version="1.0"?>
  245 <d:expand-property xmlns:d="DAV:">
  246   <d:property name="hreflist" namespace="http://sabredav.org/ns">
  247       <d:property name="href" namespace="http://sabredav.org/ns">
  248           <d:property name="displayname" />
  249       </d:property>
  250       <d:property name="displayname" />
  251   </d:property>
  252 </d:expand-property>';
  253 
  254         $serverVars = [
  255             'REQUEST_METHOD' => 'REPORT',
  256             'HTTP_DEPTH' => '0',
  257             'REQUEST_URI' => '/node2',
  258         ];
  259 
  260         $request = HTTP\Sapi::createFromServerArray($serverVars);
  261         $request->setBody($xml);
  262 
  263         $server = $this->getServer();
  264         $server->httpRequest = $request;
  265 
  266         $server->exec();
  267 
  268         $this->assertEquals(207, $server->httpResponse->status);
  269         $this->assertEquals([
  270             'X-Sabre-Version' => [DAV\Version::VERSION],
  271             'Content-Type' => ['application/xml; charset=utf-8'],
  272         ], $server->httpResponse->getHeaders());
  273 
  274         $check = [
  275             '/d:multistatus',
  276             '/d:multistatus/d:response' => 1,
  277             '/d:multistatus/d:response/d:href' => 1,
  278             '/d:multistatus/d:response/d:propstat' => 1,
  279             '/d:multistatus/d:response/d:propstat/d:prop' => 1,
  280             '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist' => 1,
  281             '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response' => 2,
  282             '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:href' => 2,
  283             '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat' => 3,
  284             '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop' => 3,
  285             '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop/d:displayname' => 2,
  286             '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop/s:href' => 2,
  287             '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop/s:href/d:response' => 1,
  288             '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop/s:href/d:response/d:href' => 1,
  289             '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop/s:href/d:response/d:propstat' => 1,
  290             '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop/s:href/d:response/d:propstat/d:prop' => 1,
  291             '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop/s:href/d:response/d:propstat/d:prop/d:displayname' => 1,
  292         ];
  293 
  294         $xml = simplexml_load_string($server->httpResponse->getBodyAsString());
  295         $xml->registerXPathNamespace('d', 'DAV:');
  296         $xml->registerXPathNamespace('s', 'http://sabredav.org/ns');
  297         foreach ($check as $v1 => $v2) {
  298             $xpath = is_int($v1) ? $v2 : $v1;
  299 
  300             $result = $xml->xpath($xpath);
  301 
  302             $count = 1;
  303             if (!is_int($v1)) {
  304                 $count = $v2;
  305             }
  306 
  307             $this->assertEquals($count, count($result), 'we expected '.$count.' appearances of '.$xpath.' . We found '.count($result));
  308         }
  309     }
  310 }