diff --git a/lib/DAV/CorePlugin.php b/lib/DAV/CorePlugin.php index 74350c28d6..dbd8976b17 100644 --- a/lib/DAV/CorePlugin.php +++ b/lib/DAV/CorePlugin.php @@ -645,6 +645,10 @@ public function httpCopy(RequestInterface $request, ResponseInterface $response) if (!$this->server->emit('beforeBind', [$copyInfo['destination']])) { return false; } + if (!$this->server->emit('beforeCopy', [$path, $copyInfo['destination']])) { + return false; + } + if ($copyInfo['destinationExists']) { if (!$this->server->emit('beforeUnbind', [$copyInfo['destination']])) { return false; @@ -653,6 +657,7 @@ public function httpCopy(RequestInterface $request, ResponseInterface $response) } $this->server->tree->copy($path, $copyInfo['destination']); + $this->server->emit('afterCopy', [$path, $copyInfo['destination']]); $this->server->emit('afterBind', [$copyInfo['destination']]); // If a resource was overwritten we should send a 204, otherwise a 201 diff --git a/tests/Sabre/DAV/ServerEventsTest.php b/tests/Sabre/DAV/ServerEventsTest.php index e9490d4f97..733b78a1fd 100644 --- a/tests/Sabre/DAV/ServerEventsTest.php +++ b/tests/Sabre/DAV/ServerEventsTest.php @@ -52,6 +52,30 @@ public function testAfterCreateCollection() $this->assertEquals($newPath, $this->tempPath); } + public function testAfterCopy() + { + $tmpPath1 = ''; + $tmpPath2 = ''; + $this->server->on('afterCopy', function ($source, $destination) use (&$tmpPath1, &$tmpPath2) { + $tmpPath1 = $source; + $tmpPath2 = $destination; + }); + + $oldPath = '/oldCopy.txt'; + $newPath = '/newCopy.txt'; + + $this->server->createFile($oldPath, 'body'); + $request = new HTTP\Request('COPY', $oldPath, [ + 'Destination' => $newPath, + ]); + $this->server->httpRequest = $request; + + $this->server->exec(); + $this->assertEquals(201, $this->server->httpResponse->getStatus()); + $this->assertEquals(trim($oldPath, '/'), $tmpPath1); + $this->assertEquals(trim($newPath, '/'), $tmpPath2); + } + public function afterHandler($path) { $this->tempPath = $path; @@ -91,6 +115,32 @@ public function testBeforeBindCancel() $this->assertEquals(500, $this->server->httpResponse->getStatus()); } + public function testBeforeCopyCancel() + { + $tmpPath1 = ''; + $tmpPath2 = ''; + $this->server->on('beforeCopy', function ($source, $destination) use (&$tmpPath1, &$tmpPath2) { + $tmpPath1 = $source; + $tmpPath2 = $destination; + + return false; + }); + + $oldPath = '/oldCopy.txt'; + $newPath = '/newCopy.txt'; + + $this->server->createFile($oldPath, 'body'); + $request = new HTTP\Request('COPY', $oldPath, [ + 'Destination' => $newPath, + ]); + $this->server->httpRequest = $request; + + $this->server->exec(); + $this->assertEquals(500, $this->server->httpResponse->getStatus()); + $this->assertEquals(trim($oldPath, '/'), $tmpPath1); + $this->assertEquals(trim($newPath, '/'), $tmpPath2); + } + public function beforeBindCancelHandler($path) { return false;