"Fossies" - the Fresh Open Source Software Archive

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

    1 <?php
    2 
    3 declare(strict_types=1);
    4 
    5 namespace Sabre\DAV\FSExt;
    6 
    7 use Sabre\DAV;
    8 use Sabre\HTTP;
    9 
   10 require_once 'Sabre/DAV/AbstractServer.php';
   11 
   12 class ServerTest extends DAV\AbstractServer
   13 {
   14     protected function getRootNode()
   15     {
   16         return new Directory($this->tempDir);
   17     }
   18 
   19     public function testGet()
   20     {
   21         $request = new HTTP\Request('GET', '/test.txt');
   22         $filename = $this->tempDir.'/test.txt';
   23         $this->server->httpRequest = $request;
   24         $this->server->exec();
   25 
   26         $this->assertEquals(200, $this->response->getStatus(), 'Invalid status code received.');
   27         $this->assertEquals([
   28             'X-Sabre-Version' => [DAV\Version::VERSION],
   29             'Content-Type' => ['application/octet-stream'],
   30             'Content-Length' => [13],
   31             'Last-Modified' => [HTTP\toDate(new \DateTime('@'.filemtime($filename)))],
   32             'ETag' => ['"'.sha1(fileinode($filename).filesize($filename).filemtime($filename)).'"'],
   33             ],
   34             $this->response->getHeaders()
   35          );
   36 
   37         $this->assertEquals('Test contents', $this->response->getBodyAsString());
   38     }
   39 
   40     public function testHEAD()
   41     {
   42         $request = new HTTP\Request('HEAD', '/test.txt');
   43         $filename = $this->tempDir.'/test.txt';
   44         $this->server->httpRequest = ($request);
   45         $this->server->exec();
   46 
   47         $this->assertEquals([
   48             'X-Sabre-Version' => [DAV\Version::VERSION],
   49             'Content-Type' => ['application/octet-stream'],
   50             'Content-Length' => [13],
   51             'Last-Modified' => [HTTP\toDate(new \DateTime('@'.filemtime($this->tempDir.'/test.txt')))],
   52             'ETag' => ['"'.sha1(fileinode($filename).filesize($filename).filemtime($filename)).'"'],
   53             ],
   54             $this->response->getHeaders()
   55          );
   56 
   57         $this->assertEquals(200, $this->response->status);
   58         $this->assertEquals('', $this->response->getBodyAsString());
   59     }
   60 
   61     public function testPut()
   62     {
   63         $request = new HTTP\Request('PUT', '/testput.txt');
   64         $filename = $this->tempDir.'/testput.txt';
   65         $request->setBody('Testing new file');
   66         $this->server->httpRequest = ($request);
   67         $this->server->exec();
   68 
   69         $this->assertEquals([
   70             'X-Sabre-Version' => [DAV\Version::VERSION],
   71             'Content-Length' => ['0'],
   72             'ETag' => ['"'.sha1(fileinode($filename).filesize($filename).filemtime($filename)).'"'],
   73         ], $this->response->getHeaders());
   74 
   75         $this->assertEquals(201, $this->response->status);
   76         $this->assertEquals('', $this->response->getBodyAsString());
   77         $this->assertEquals('Testing new file', file_get_contents($filename));
   78     }
   79 
   80     public function testPutAlreadyExists()
   81     {
   82         $request = new HTTP\Request('PUT', '/test.txt', ['If-None-Match' => '*']);
   83         $request->setBody('Testing new file');
   84         $this->server->httpRequest = ($request);
   85         $this->server->exec();
   86 
   87         $this->assertEquals([
   88             'X-Sabre-Version' => [DAV\Version::VERSION],
   89             'Content-Type' => ['application/xml; charset=utf-8'],
   90         ], $this->response->getHeaders());
   91 
   92         $this->assertEquals(412, $this->response->status);
   93         $this->assertNotEquals('Testing new file', file_get_contents($this->tempDir.'/test.txt'));
   94     }
   95 
   96     public function testMkcol()
   97     {
   98         $request = new HTTP\Request('MKCOL', '/testcol');
   99         $this->server->httpRequest = ($request);
  100         $this->server->exec();
  101 
  102         $this->assertEquals([
  103             'X-Sabre-Version' => [DAV\Version::VERSION],
  104             'Content-Length' => ['0'],
  105         ], $this->response->getHeaders());
  106 
  107         $this->assertEquals(201, $this->response->status);
  108         $this->assertEquals('', $this->response->getBodyAsString());
  109         $this->assertTrue(is_dir($this->tempDir.'/testcol'));
  110     }
  111 
  112     public function testPutUpdate()
  113     {
  114         $request = new HTTP\Request('PUT', '/test.txt');
  115         $request->setBody('Testing updated file');
  116         $this->server->httpRequest = ($request);
  117         $this->server->exec();
  118 
  119         $this->assertEquals('0', $this->response->getHeader('Content-Length'));
  120 
  121         $this->assertEquals(204, $this->response->status);
  122         $this->assertEquals('', $this->response->getBodyAsString());
  123         $this->assertEquals('Testing updated file', file_get_contents($this->tempDir.'/test.txt'));
  124     }
  125 
  126     public function testDelete()
  127     {
  128         $request = new HTTP\Request('DELETE', '/test.txt');
  129         $this->server->httpRequest = ($request);
  130         $this->server->exec();
  131 
  132         $this->assertEquals([
  133             'X-Sabre-Version' => [DAV\Version::VERSION],
  134             'Content-Length' => ['0'],
  135         ], $this->response->getHeaders());
  136 
  137         $this->assertEquals(204, $this->response->status);
  138         $this->assertEquals('', $this->response->getBodyAsString());
  139         $this->assertFalse(file_exists($this->tempDir.'/test.txt'));
  140     }
  141 
  142     public function testDeleteDirectory()
  143     {
  144         mkdir($this->tempDir.'/testcol');
  145         file_put_contents($this->tempDir.'/testcol/test.txt', 'Hi! I\'m a file with a short lifespan');
  146 
  147         $request = new HTTP\Request('DELETE', '/testcol');
  148         $this->server->httpRequest = ($request);
  149         $this->server->exec();
  150 
  151         $this->assertEquals([
  152             'X-Sabre-Version' => [DAV\Version::VERSION],
  153             'Content-Length' => ['0'],
  154         ], $this->response->getHeaders());
  155         $this->assertEquals(204, $this->response->status);
  156         $this->assertEquals('', $this->response->getBodyAsString());
  157         $this->assertFalse(file_exists($this->tempDir.'/testcol'));
  158     }
  159 
  160     public function testOptions()
  161     {
  162         $request = new HTTP\Request('OPTIONS', '/');
  163         $this->server->httpRequest = ($request);
  164         $this->server->exec();
  165 
  166         $this->assertEquals([
  167             'DAV' => ['1, 3, extended-mkcol'],
  168             'MS-Author-Via' => ['DAV'],
  169             'Allow' => ['OPTIONS, GET, HEAD, DELETE, PROPFIND, PUT, PROPPATCH, COPY, MOVE, REPORT'],
  170             'Accept-Ranges' => ['bytes'],
  171             'Content-Length' => ['0'],
  172             'X-Sabre-Version' => [DAV\Version::VERSION],
  173         ], $this->response->getHeaders());
  174 
  175         $this->assertEquals(200, $this->response->status);
  176         $this->assertEquals('', $this->response->getBodyAsString());
  177     }
  178 
  179     public function testMove()
  180     {
  181         mkdir($this->tempDir.'/testcol');
  182 
  183         $request = new HTTP\Request('MOVE', '/test.txt', ['Destination' => '/testcol/test2.txt']);
  184         $this->server->httpRequest = ($request);
  185         $this->server->exec();
  186 
  187         $this->assertEquals(201, $this->response->status);
  188         $this->assertEquals('', $this->response->getBodyAsString());
  189 
  190         $this->assertEquals([
  191             'Content-Length' => ['0'],
  192             'X-Sabre-Version' => [DAV\Version::VERSION],
  193         ], $this->response->getHeaders());
  194 
  195         $this->assertTrue(
  196             is_file($this->tempDir.'/testcol/test2.txt')
  197         );
  198     }
  199 
  200     /**
  201      * This test checks if it's possible to move a non-FSExt collection into a
  202      * FSExt collection.
  203      *
  204      * The moveInto function *should* ignore the object and let sabredav itself
  205      * execute the slow move.
  206      */
  207     public function testMoveOtherObject()
  208     {
  209         mkdir($this->tempDir.'/tree1');
  210         mkdir($this->tempDir.'/tree2');
  211 
  212         $tree = new DAV\Tree(new DAV\SimpleCollection('root', [
  213             new DAV\FS\Directory($this->tempDir.'/tree1'),
  214             new DAV\FSExt\Directory($this->tempDir.'/tree2'),
  215         ]));
  216         $this->server->tree = $tree;
  217 
  218         $request = new HTTP\Request('MOVE', '/tree1', ['Destination' => '/tree2/tree1']);
  219         $this->server->httpRequest = ($request);
  220         $this->server->exec();
  221 
  222         $this->assertEquals(201, $this->response->status);
  223         $this->assertEquals('', $this->response->getBodyAsString());
  224 
  225         $this->assertEquals([
  226             'Content-Length' => ['0'],
  227             'X-Sabre-Version' => [DAV\Version::VERSION],
  228         ], $this->response->getHeaders());
  229 
  230         $this->assertTrue(
  231             is_dir($this->tempDir.'/tree2/tree1')
  232         );
  233     }
  234 
  235     public function testCopy()
  236     {
  237         mkdir($this->tempDir.'/testcol');
  238 
  239         $request = new HTTP\Request('COPY', '/test.txt', ['Destination' => '/testcol/test2.txt']);
  240         $this->server->httpRequest = ($request);
  241         $this->server->exec();
  242 
  243         $this->assertEquals(201, $this->response->status);
  244         $this->assertEquals('', $this->response->getBodyAsString());
  245 
  246         $this->assertEquals([
  247             'Content-Length' => ['0'],
  248             'X-Sabre-Version' => [DAV\Version::VERSION],
  249         ], $this->response->getHeaders());
  250 
  251         $this->assertTrue(is_file($this->tempDir.'/test.txt'));
  252         $this->assertTrue(is_file($this->tempDir.'/testcol/test2.txt'));
  253     }
  254 }