"Fossies" - the Fresh Open Source Software Archive

Member "dav-4.0.3/tests/Sabre/DAV/TemporaryFileFilterTest.php" (10 Jan 2020, 7590 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 "TemporaryFileFilterTest.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 class TemporaryFileFilterTest extends AbstractServer
   10 {
   11     public function setUp()
   12     {
   13         parent::setUp();
   14         $plugin = new TemporaryFileFilterPlugin(SABRE_TEMPDIR.'/tff');
   15         $this->server->addPlugin($plugin);
   16     }
   17 
   18     public function testPutNormal()
   19     {
   20         $request = new HTTP\Request('PUT', '/testput.txt', [], 'Testing new file');
   21 
   22         $this->server->httpRequest = ($request);
   23         $this->server->exec();
   24 
   25         $this->assertEquals('', $this->response->getBodyAsString());
   26         $this->assertEquals(201, $this->response->status);
   27         $this->assertEquals('0', $this->response->getHeader('Content-Length'));
   28 
   29         $this->assertEquals('Testing new file', file_get_contents(SABRE_TEMPDIR.'/testput.txt'));
   30     }
   31 
   32     public function testPutTemp()
   33     {
   34         // mimicking an OS/X resource fork
   35         $request = new HTTP\Request('PUT', '/._testput.txt', [], 'Testing new file');
   36 
   37         $this->server->httpRequest = ($request);
   38         $this->server->exec();
   39 
   40         $this->assertEquals('', $this->response->getBodyAsString());
   41         $this->assertEquals(201, $this->response->status);
   42         $this->assertEquals([
   43             'X-Sabre-Temp' => ['true'],
   44         ], $this->response->getHeaders());
   45 
   46         $this->assertFalse(file_exists(SABRE_TEMPDIR.'/._testput.txt'), '._testput.txt should not exist in the regular file structure.');
   47     }
   48 
   49     public function testPutTempIfNoneMatch()
   50     {
   51         // mimicking an OS/X resource fork
   52         $request = new HTTP\Request('PUT', '/._testput.txt', ['If-None-Match' => '*'], 'Testing new file');
   53 
   54         $this->server->httpRequest = ($request);
   55         $this->server->exec();
   56 
   57         $this->assertEquals('', $this->response->getBodyAsString());
   58         $this->assertEquals(201, $this->response->status);
   59         $this->assertEquals([
   60             'X-Sabre-Temp' => ['true'],
   61         ], $this->response->getHeaders());
   62 
   63         $this->assertFalse(file_exists(SABRE_TEMPDIR.'/._testput.txt'), '._testput.txt should not exist in the regular file structure.');
   64 
   65         $this->server->exec();
   66 
   67         $this->assertEquals(412, $this->response->status);
   68         $this->assertEquals([
   69             'X-Sabre-Temp' => ['true'],
   70             'Content-Type' => ['application/xml; charset=utf-8'],
   71         ], $this->response->getHeaders());
   72     }
   73 
   74     public function testPutGet()
   75     {
   76         // mimicking an OS/X resource fork
   77         $request = new HTTP\Request('PUT', '/._testput.txt', [], 'Testing new file');
   78         $this->server->httpRequest = ($request);
   79         $this->server->exec();
   80 
   81         $this->assertEquals('', $this->response->getBodyAsString());
   82         $this->assertEquals(201, $this->response->status);
   83         $this->assertEquals([
   84             'X-Sabre-Temp' => ['true'],
   85         ], $this->response->getHeaders());
   86 
   87         $request = new HTTP\Request('GET', '/._testput.txt');
   88 
   89         $this->server->httpRequest = $request;
   90         $this->server->exec();
   91 
   92         $this->assertEquals(200, $this->response->status);
   93         $this->assertEquals([
   94             'X-Sabre-Temp' => ['true'],
   95             'Content-Length' => [16],
   96             'Content-Type' => ['application/octet-stream'],
   97         ], $this->response->getHeaders());
   98 
   99         $this->assertEquals('Testing new file', stream_get_contents($this->response->body));
  100     }
  101 
  102     public function testGetWithBrowserPlugin()
  103     {
  104         $this->server->addPlugin(new Browser\Plugin());
  105         $request = new HTTP\Request('GET', '/');
  106 
  107         $this->server->httpRequest = $request;
  108         $this->server->exec();
  109 
  110         $this->assertEquals(200, $this->response->status);
  111     }
  112 
  113     public function testLockNonExistant()
  114     {
  115         mkdir(SABRE_TEMPDIR.'/locksdir');
  116         $locksBackend = new Locks\Backend\File(SABRE_TEMPDIR.'/locks');
  117         $locksPlugin = new Locks\Plugin($locksBackend);
  118         $this->server->addPlugin($locksPlugin);
  119 
  120         // mimicking an OS/X resource fork
  121         $request = new HTTP\Request('LOCK', '/._testput.txt');
  122         $request->setBody('<?xml version="1.0"?>
  123 <D:lockinfo xmlns:D="DAV:">
  124     <D:lockscope><D:exclusive/></D:lockscope>
  125     <D:locktype><D:write/></D:locktype>
  126     <D:owner>
  127         <D:href>http://example.org/~ejw/contact.html</D:href>
  128     </D:owner>
  129 </D:lockinfo>');
  130 
  131         $this->server->httpRequest = ($request);
  132         $this->server->exec();
  133 
  134         $this->assertEquals(201, $this->response->status);
  135         $this->assertEquals('application/xml; charset=utf-8', $this->response->getHeader('Content-Type'));
  136         $this->assertTrue(1 === preg_match('/^<opaquelocktoken:(.*)>$/', $this->response->getHeader('Lock-Token')), 'We did not get a valid Locktoken back ('.$this->response->getHeader('Lock-Token').')');
  137         $this->assertEquals('true', $this->response->getHeader('X-Sabre-Temp'));
  138 
  139         $this->assertFalse(file_exists(SABRE_TEMPDIR.'/._testlock.txt'), '._testlock.txt should not exist in the regular file structure.');
  140     }
  141 
  142     public function testPutDelete()
  143     {
  144         // mimicking an OS/X resource fork
  145         $request = new HTTP\Request('PUT', '/._testput.txt', [], 'Testing new file');
  146 
  147         $this->server->httpRequest = $request;
  148         $this->server->exec();
  149 
  150         $this->assertEquals('', $this->response->getBodyAsString());
  151         $this->assertEquals(201, $this->response->status);
  152         $this->assertEquals([
  153             'X-Sabre-Temp' => ['true'],
  154         ], $this->response->getHeaders());
  155 
  156         $request = new HTTP\Request('DELETE', '/._testput.txt');
  157         $this->server->httpRequest = $request;
  158         $this->server->exec();
  159 
  160         $this->assertEquals(204, $this->response->status, "Incorrect status code received. Full body:\n".$this->response->getBodyAsString());
  161         $this->assertEquals([
  162             'X-Sabre-Temp' => ['true'],
  163         ], $this->response->getHeaders());
  164 
  165         $this->assertEquals('', $this->response->getBodyAsString());
  166     }
  167 
  168     public function testPutPropfind()
  169     {
  170         // mimicking an OS/X resource fork
  171         $request = new HTTP\Request('PUT', '/._testput.txt', [], 'Testing new file');
  172         $this->server->httpRequest = $request;
  173         $this->server->exec();
  174 
  175         $bodyAsString = $this->response->getBodyAsString();
  176         $this->assertEquals('', $bodyAsString);
  177         $this->assertEquals(201, $this->response->status);
  178         $this->assertEquals([
  179             'X-Sabre-Temp' => ['true'],
  180         ], $this->response->getHeaders());
  181 
  182         $request = new HTTP\Request('PROPFIND', '/._testput.txt');
  183 
  184         $this->server->httpRequest = ($request);
  185         $this->server->exec();
  186 
  187         $bodyAsString = $this->response->getBodyAsString();
  188         $this->assertEquals(207, $this->response->status, 'Incorrect status code returned. Body: '.$bodyAsString);
  189         $this->assertEquals([
  190             'X-Sabre-Temp' => ['true'],
  191             'Content-Type' => ['application/xml; charset=utf-8'],
  192         ], $this->response->getHeaders());
  193 
  194         $body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/", 'xmlns\\1="urn:DAV"', $bodyAsString);
  195         $xml = simplexml_load_string($body);
  196         $xml->registerXPathNamespace('d', 'urn:DAV');
  197 
  198         list($data) = $xml->xpath('/d:multistatus/d:response/d:href');
  199         $this->assertEquals('/._testput.txt', (string) $data, 'href element should have been /._testput.txt');
  200 
  201         $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:resourcetype');
  202         $this->assertEquals(1, count($data));
  203     }
  204 }