Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include "before" and "after" copy events as with move events #1396

Merged
merged 2 commits into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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