Skip to content

Commit

Permalink
Merge pull request #1396 from jvillafanez/explicit_copy_events
Browse files Browse the repository at this point in the history
Include "before" and "after" copy events as with move events
  • Loading branch information
DeepDiver1975 committed Jun 20, 2022
2 parents 130abb7 + 347e775 commit 7cbc315
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/DAV/CorePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
50 changes: 50 additions & 0 deletions tests/Sabre/DAV/ServerEventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 7cbc315

Please sign in to comment.