"Fossies" - the Fresh Open Source Software Archive

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

    1 <?php
    2 
    3 declare(strict_types=1);
    4 
    5 namespace Sabre\DAV\Sync;
    6 
    7 use Sabre\DAV;
    8 use Sabre\HTTP;
    9 
   10 require_once __DIR__.'/MockSyncCollection.php';
   11 
   12 class PluginTest extends \Sabre\DAVServerTest
   13 {
   14     protected $collection;
   15 
   16     public function setUp()
   17     {
   18         parent::setUp();
   19         $this->server->addPlugin(new Plugin());
   20     }
   21 
   22     public function testGetInfo()
   23     {
   24         $this->assertArrayHasKey(
   25             'name',
   26             (new Plugin())->getPluginInfo()
   27         );
   28     }
   29 
   30     public function setUpTree()
   31     {
   32         $this->collection =
   33             new MockSyncCollection('coll', [
   34                 new DAV\SimpleFile('file1.txt', 'foo'),
   35                 new DAV\SimpleFile('file2.txt', 'bar'),
   36             ]);
   37         $this->tree = [
   38             $this->collection,
   39             new DAV\SimpleCollection('normalcoll', []),
   40         ];
   41     }
   42 
   43     public function testSupportedReportSet()
   44     {
   45         $result = $this->server->getProperties('/coll', ['{DAV:}supported-report-set']);
   46         $this->assertFalse($result['{DAV:}supported-report-set']->has('{DAV:}sync-collection'));
   47 
   48         // Making a change
   49         $this->collection->addChange(['file1.txt'], [], []);
   50 
   51         $result = $this->server->getProperties('/coll', ['{DAV:}supported-report-set']);
   52         $this->assertTrue($result['{DAV:}supported-report-set']->has('{DAV:}sync-collection'));
   53     }
   54 
   55     public function testGetSyncToken()
   56     {
   57         $result = $this->server->getProperties('/coll', ['{DAV:}sync-token']);
   58         $this->assertFalse(isset($result['{DAV:}sync-token']));
   59 
   60         // Making a change
   61         $this->collection->addChange(['file1.txt'], [], []);
   62 
   63         $result = $this->server->getProperties('/coll', ['{DAV:}sync-token']);
   64         $this->assertTrue(isset($result['{DAV:}sync-token']));
   65 
   66         // non-sync-enabled collection
   67         $this->collection->addChange(['file1.txt'], [], []);
   68 
   69         $result = $this->server->getProperties('/normalcoll', ['{DAV:}sync-token']);
   70         $this->assertFalse(isset($result['{DAV:}sync-token']));
   71     }
   72 
   73     public function testSyncInitialSyncCollection()
   74     {
   75         // Making a change
   76         $this->collection->addChange(['file1.txt'], [], []);
   77 
   78         $request = new HTTP\Request('REPORT', '/coll/', ['Content-Type' => 'application/xml']);
   79 
   80         $body = <<<BLA
   81 <?xml version="1.0" encoding="utf-8" ?>
   82 <D:sync-collection xmlns:D="DAV:">
   83      <D:sync-token/>
   84      <D:sync-level>1</D:sync-level>
   85       <D:prop>
   86         <D:getcontentlength/>
   87       </D:prop>
   88 </D:sync-collection>
   89 BLA;
   90 
   91         $request->setBody($body);
   92 
   93         $response = $this->request($request);
   94 
   95         $this->assertEquals(207, $response->status, 'Full response body:'.$response->getBodyAsString());
   96 
   97         $multiStatus = $this->server->xml->parse($response->getBodyAsString());
   98 
   99         // Checking the sync-token
  100         $this->assertEquals(
  101             'http://sabre.io/ns/sync/1',
  102             $multiStatus->getSyncToken()
  103         );
  104 
  105         $responses = $multiStatus->getResponses();
  106         $this->assertEquals(2, count($responses), 'We expected exactly 2 {DAV:}response');
  107 
  108         $response = $responses[0];
  109 
  110         $this->assertNull($response->getHttpStatus());
  111         $this->assertEquals('/coll/file1.txt', $response->getHref());
  112         $this->assertEquals([
  113             200 => [
  114                 '{DAV:}getcontentlength' => 3,
  115             ],
  116         ], $response->getResponseProperties());
  117 
  118         $response = $responses[1];
  119 
  120         $this->assertNull($response->getHttpStatus());
  121         $this->assertEquals('/coll/file2.txt', $response->getHref());
  122         $this->assertEquals([
  123             200 => [
  124                 '{DAV:}getcontentlength' => 3,
  125             ],
  126         ], $response->getResponseProperties());
  127     }
  128 
  129     public function testSubsequentSyncSyncCollection()
  130     {
  131         // Making a change
  132         $this->collection->addChange(['file1.txt'], [], []);
  133         // Making another change
  134         $this->collection->addChange([], ['file2.txt'], ['file3.txt']);
  135 
  136         $request = HTTP\Sapi::createFromServerArray([
  137             'REQUEST_METHOD' => 'REPORT',
  138             'REQUEST_URI' => '/coll/',
  139             'CONTENT_TYPE' => 'application/xml',
  140         ]);
  141 
  142         $body = <<<BLA
  143 <?xml version="1.0" encoding="utf-8" ?>
  144 <D:sync-collection xmlns:D="DAV:">
  145      <D:sync-token>http://sabre.io/ns/sync/1</D:sync-token>
  146      <D:sync-level>infinite</D:sync-level>
  147       <D:prop>
  148         <D:getcontentlength/>
  149       </D:prop>
  150 </D:sync-collection>
  151 BLA;
  152 
  153         $request->setBody($body);
  154 
  155         $response = $this->request($request);
  156 
  157         $this->assertEquals(207, $response->status, 'Full response body:'.$response->getBodyAsString());
  158 
  159         $multiStatus = $this->server->xml->parse($response->getBodyAsString());
  160 
  161         // Checking the sync-token
  162         $this->assertEquals(
  163             'http://sabre.io/ns/sync/2',
  164             $multiStatus->getSyncToken()
  165         );
  166 
  167         $responses = $multiStatus->getResponses();
  168         $this->assertEquals(2, count($responses), 'We expected exactly 2 {DAV:}response');
  169 
  170         $response = $responses[0];
  171 
  172         $this->assertNull($response->getHttpStatus());
  173         $this->assertEquals('/coll/file2.txt', $response->getHref());
  174         $this->assertEquals([
  175             200 => [
  176                 '{DAV:}getcontentlength' => 3,
  177             ],
  178         ], $response->getResponseProperties());
  179 
  180         $response = $responses[1];
  181 
  182         $this->assertEquals('404', $response->getHttpStatus());
  183         $this->assertEquals('/coll/file3.txt', $response->getHref());
  184         $this->assertEquals([], $response->getResponseProperties());
  185     }
  186 
  187     public function testSubsequentSyncSyncCollectionLimit()
  188     {
  189         // Making a change
  190         $this->collection->addChange(['file1.txt'], [], []);
  191         // Making another change
  192         $this->collection->addChange([], ['file2.txt'], ['file3.txt']);
  193 
  194         $request = HTTP\Sapi::createFromServerArray([
  195             'REQUEST_METHOD' => 'REPORT',
  196             'REQUEST_URI' => '/coll/',
  197             'CONTENT_TYPE' => 'application/xml',
  198         ]);
  199 
  200         $body = <<<BLA
  201 <?xml version="1.0" encoding="utf-8" ?>
  202 <D:sync-collection xmlns:D="DAV:">
  203     <D:sync-token>http://sabre.io/ns/sync/1</D:sync-token>
  204     <D:sync-level>infinite</D:sync-level>
  205     <D:prop>
  206         <D:getcontentlength/>
  207     </D:prop>
  208     <D:limit><D:nresults>1</D:nresults></D:limit>
  209 </D:sync-collection>
  210 BLA;
  211 
  212         $request->setBody($body);
  213 
  214         $response = $this->request($request);
  215 
  216         $this->assertEquals(207, $response->status, 'Full response body:'.$response->getBodyAsString());
  217 
  218         $multiStatus = $this->server->xml->parse(
  219             $response->getBodyAsString()
  220         );
  221 
  222         // Checking the sync-token
  223         $this->assertEquals(
  224             'http://sabre.io/ns/sync/2',
  225             $multiStatus->getSyncToken()
  226         );
  227 
  228         $responses = $multiStatus->getResponses();
  229         $this->assertEquals(1, count($responses), 'We expected exactly 1 {DAV:}response');
  230 
  231         $response = $responses[0];
  232 
  233         $this->assertEquals('404', $response->getHttpStatus());
  234         $this->assertEquals('/coll/file3.txt', $response->getHref());
  235         $this->assertEquals([], $response->getResponseProperties());
  236     }
  237 
  238     public function testSubsequentSyncSyncCollectionDepthFallBack()
  239     {
  240         // Making a change
  241         $this->collection->addChange(['file1.txt'], [], []);
  242         // Making another change
  243         $this->collection->addChange([], ['file2.txt'], ['file3.txt']);
  244 
  245         $request = HTTP\Sapi::createFromServerArray([
  246             'REQUEST_METHOD' => 'REPORT',
  247             'REQUEST_URI' => '/coll/',
  248             'CONTENT_TYPE' => 'application/xml',
  249             'HTTP_DEPTH' => '1',
  250         ]);
  251 
  252         $body = <<<BLA
  253 <?xml version="1.0" encoding="utf-8" ?>
  254 <D:sync-collection xmlns:D="DAV:">
  255      <D:sync-token>http://sabre.io/ns/sync/1</D:sync-token>
  256       <D:prop>
  257         <D:getcontentlength/>
  258       </D:prop>
  259 </D:sync-collection>
  260 BLA;
  261 
  262         $request->setBody($body);
  263 
  264         $response = $this->request($request);
  265 
  266         $this->assertEquals(207, $response->status, 'Full response body:'.$response->getBodyAsString());
  267 
  268         $multiStatus = $this->server->xml->parse(
  269             $response->getBodyAsString()
  270         );
  271 
  272         // Checking the sync-token
  273         $this->assertEquals(
  274             'http://sabre.io/ns/sync/2',
  275             $multiStatus->getSyncToken()
  276         );
  277 
  278         $responses = $multiStatus->getResponses();
  279         $this->assertEquals(2, count($responses), 'We expected exactly 2 {DAV:}response');
  280 
  281         $response = $responses[0];
  282 
  283         $this->assertNull($response->getHttpStatus());
  284         $this->assertEquals('/coll/file2.txt', $response->getHref());
  285         $this->assertEquals([
  286             200 => [
  287                 '{DAV:}getcontentlength' => 3,
  288             ],
  289         ], $response->getResponseProperties());
  290 
  291         $response = $responses[1];
  292 
  293         $this->assertEquals('404', $response->getHttpStatus());
  294         $this->assertEquals('/coll/file3.txt', $response->getHref());
  295         $this->assertEquals([], $response->getResponseProperties());
  296     }
  297 
  298     public function testSyncNoSyncInfo()
  299     {
  300         $request = HTTP\Sapi::createFromServerArray([
  301             'REQUEST_METHOD' => 'REPORT',
  302             'REQUEST_URI' => '/coll/',
  303             'CONTENT_TYPE' => 'application/xml',
  304         ]);
  305 
  306         $body = <<<BLA
  307 <?xml version="1.0" encoding="utf-8" ?>
  308 <D:sync-collection xmlns:D="DAV:">
  309      <D:sync-token/>
  310      <D:sync-level>1</D:sync-level>
  311       <D:prop>
  312         <D:getcontentlength/>
  313       </D:prop>
  314 </D:sync-collection>
  315 BLA;
  316 
  317         $request->setBody($body);
  318 
  319         $response = $this->request($request);
  320 
  321         // The default state has no sync-token, so this report should not yet
  322         // be supported.
  323         $this->assertEquals(415, $response->status, 'Full response body:'.$response->getBodyAsString());
  324     }
  325 
  326     public function testSyncNoSyncCollection()
  327     {
  328         $request = HTTP\Sapi::createFromServerArray([
  329             'REQUEST_METHOD' => 'REPORT',
  330             'REQUEST_URI' => '/normalcoll/',
  331             'CONTENT_TYPE' => 'application/xml',
  332         ]);
  333 
  334         $body = <<<BLA
  335 <?xml version="1.0" encoding="utf-8" ?>
  336 <D:sync-collection xmlns:D="DAV:">
  337      <D:sync-token/>
  338      <D:sync-level>1</D:sync-level>
  339       <D:prop>
  340         <D:getcontentlength/>
  341       </D:prop>
  342 </D:sync-collection>
  343 BLA;
  344 
  345         $request->setBody($body);
  346 
  347         $response = $this->request($request);
  348 
  349         // The default state has no sync-token, so this report should not yet
  350         // be supported.
  351         $this->assertEquals(415, $response->status, 'Full response body:'.$response->getBodyAsString());
  352     }
  353 
  354     public function testSyncInvalidToken()
  355     {
  356         $this->collection->addChange(['file1.txt'], [], []);
  357         $request = HTTP\Sapi::createFromServerArray([
  358             'REQUEST_METHOD' => 'REPORT',
  359             'REQUEST_URI' => '/coll/',
  360             'CONTENT_TYPE' => 'application/xml',
  361         ]);
  362 
  363         $body = <<<BLA
  364 <?xml version="1.0" encoding="utf-8" ?>
  365 <D:sync-collection xmlns:D="DAV:">
  366      <D:sync-token>http://sabre.io/ns/sync/invalid</D:sync-token>
  367      <D:sync-level>1</D:sync-level>
  368       <D:prop>
  369         <D:getcontentlength/>
  370       </D:prop>
  371 </D:sync-collection>
  372 BLA;
  373 
  374         $request->setBody($body);
  375 
  376         $response = $this->request($request);
  377 
  378         // The default state has no sync-token, so this report should not yet
  379         // be supported.
  380         $this->assertEquals(403, $response->status, 'Full response body:'.$response->getBodyAsString());
  381     }
  382 
  383     public function testSyncInvalidTokenNoPrefix()
  384     {
  385         $this->collection->addChange(['file1.txt'], [], []);
  386         $request = HTTP\Sapi::createFromServerArray([
  387             'REQUEST_METHOD' => 'REPORT',
  388             'REQUEST_URI' => '/coll/',
  389             'CONTENT_TYPE' => 'application/xml',
  390         ]);
  391 
  392         $body = <<<BLA
  393 <?xml version="1.0" encoding="utf-8" ?>
  394 <D:sync-collection xmlns:D="DAV:">
  395      <D:sync-token>invalid</D:sync-token>
  396      <D:sync-level>1</D:sync-level>
  397       <D:prop>
  398         <D:getcontentlength/>
  399       </D:prop>
  400 </D:sync-collection>
  401 BLA;
  402 
  403         $request->setBody($body);
  404 
  405         $response = $this->request($request);
  406 
  407         // The default state has no sync-token, so this report should not yet
  408         // be supported.
  409         $this->assertEquals(403, $response->status, 'Full response body:'.$response->getBodyAsString());
  410     }
  411 
  412     public function testSyncNoSyncToken()
  413     {
  414         $request = HTTP\Sapi::createFromServerArray([
  415             'REQUEST_METHOD' => 'REPORT',
  416             'REQUEST_URI' => '/coll/',
  417             'CONTENT_TYPE' => 'application/xml',
  418         ]);
  419 
  420         $body = <<<BLA
  421 <?xml version="1.0" encoding="utf-8" ?>
  422 <D:sync-collection xmlns:D="DAV:">
  423      <D:sync-level>1</D:sync-level>
  424       <D:prop>
  425         <D:getcontentlength/>
  426       </D:prop>
  427 </D:sync-collection>
  428 BLA;
  429 
  430         $request->setBody($body);
  431 
  432         $response = $this->request($request);
  433 
  434         // The default state has no sync-token, so this report should not yet
  435         // be supported.
  436         $this->assertEquals(400, $response->status, 'Full response body:'.$response->getBodyAsString());
  437     }
  438 
  439     public function testSyncNoProp()
  440     {
  441         $this->collection->addChange(['file1.txt'], [], []);
  442         $request = HTTP\Sapi::createFromServerArray([
  443             'REQUEST_METHOD' => 'REPORT',
  444             'REQUEST_URI' => '/coll/',
  445             'CONTENT_TYPE' => 'application/xml',
  446         ]);
  447 
  448         $body = <<<BLA
  449 <?xml version="1.0" encoding="utf-8" ?>
  450 <D:sync-collection xmlns:D="DAV:">
  451      <D:sync-token />
  452      <D:sync-level>1</D:sync-level>
  453 </D:sync-collection>
  454 BLA;
  455 
  456         $request->setBody($body);
  457 
  458         $response = $this->request($request);
  459 
  460         // The default state has no sync-token, so this report should not yet
  461         // be supported.
  462         $this->assertEquals(400, $response->status, 'Full response body:'.$response->getBodyAsString());
  463     }
  464 
  465     public function testIfConditions()
  466     {
  467         $this->collection->addChange(['file1.txt'], [], []);
  468         $request = HTTP\Sapi::createFromServerArray([
  469             'REQUEST_METHOD' => 'DELETE',
  470             'REQUEST_URI' => '/coll/file1.txt',
  471             'HTTP_IF' => '</coll> (<http://sabre.io/ns/sync/1>)',
  472         ]);
  473         $response = $this->request($request);
  474 
  475         // If a 403 is thrown this works correctly. The file in questions
  476         // doesn't allow itself to be deleted.
  477         // If the If conditions failed, it would have been a 412 instead.
  478         $this->assertEquals(403, $response->status);
  479     }
  480 
  481     public function testIfConditionsNot()
  482     {
  483         $this->collection->addChange(['file1.txt'], [], []);
  484         $request = HTTP\Sapi::createFromServerArray([
  485             'REQUEST_METHOD' => 'DELETE',
  486             'REQUEST_URI' => '/coll/file1.txt',
  487             'HTTP_IF' => '</coll> (Not <http://sabre.io/ns/sync/2>)',
  488         ]);
  489         $response = $this->request($request);
  490 
  491         // If a 403 is thrown this works correctly. The file in questions
  492         // doesn't allow itself to be deleted.
  493         // If the If conditions failed, it would have been a 412 instead.
  494         $this->assertEquals(403, $response->status);
  495     }
  496 
  497     public function testIfConditionsNoSyncToken()
  498     {
  499         $this->collection->addChange(['file1.txt'], [], []);
  500         $request = HTTP\Sapi::createFromServerArray([
  501             'REQUEST_METHOD' => 'DELETE',
  502             'REQUEST_URI' => '/coll/file1.txt',
  503             'HTTP_IF' => '</coll> (<opaquelocktoken:foo>)',
  504         ]);
  505         $response = $this->request($request);
  506 
  507         $this->assertEquals(412, $response->status);
  508     }
  509 }