Skip to content

Commit 7cbc315

Browse files
Merge pull request #1396 from jvillafanez/explicit_copy_events
Include "before" and "after" copy events as with move events
2 parents 130abb7 + 347e775 commit 7cbc315

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

lib/DAV/CorePlugin.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,10 @@ public function httpCopy(RequestInterface $request, ResponseInterface $response)
645645
if (!$this->server->emit('beforeBind', [$copyInfo['destination']])) {
646646
return false;
647647
}
648+
if (!$this->server->emit('beforeCopy', [$path, $copyInfo['destination']])) {
649+
return false;
650+
}
651+
648652
if ($copyInfo['destinationExists']) {
649653
if (!$this->server->emit('beforeUnbind', [$copyInfo['destination']])) {
650654
return false;
@@ -653,6 +657,7 @@ public function httpCopy(RequestInterface $request, ResponseInterface $response)
653657
}
654658

655659
$this->server->tree->copy($path, $copyInfo['destination']);
660+
$this->server->emit('afterCopy', [$path, $copyInfo['destination']]);
656661
$this->server->emit('afterBind', [$copyInfo['destination']]);
657662

658663
// If a resource was overwritten we should send a 204, otherwise a 201

tests/Sabre/DAV/ServerEventsTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,30 @@ public function testAfterCreateCollection()
5252
$this->assertEquals($newPath, $this->tempPath);
5353
}
5454

55+
public function testAfterCopy()
56+
{
57+
$tmpPath1 = '';
58+
$tmpPath2 = '';
59+
$this->server->on('afterCopy', function ($source, $destination) use (&$tmpPath1, &$tmpPath2) {
60+
$tmpPath1 = $source;
61+
$tmpPath2 = $destination;
62+
});
63+
64+
$oldPath = '/oldCopy.txt';
65+
$newPath = '/newCopy.txt';
66+
67+
$this->server->createFile($oldPath, 'body');
68+
$request = new HTTP\Request('COPY', $oldPath, [
69+
'Destination' => $newPath,
70+
]);
71+
$this->server->httpRequest = $request;
72+
73+
$this->server->exec();
74+
$this->assertEquals(201, $this->server->httpResponse->getStatus());
75+
$this->assertEquals(trim($oldPath, '/'), $tmpPath1);
76+
$this->assertEquals(trim($newPath, '/'), $tmpPath2);
77+
}
78+
5579
public function afterHandler($path)
5680
{
5781
$this->tempPath = $path;
@@ -91,6 +115,32 @@ public function testBeforeBindCancel()
91115
$this->assertEquals(500, $this->server->httpResponse->getStatus());
92116
}
93117

118+
public function testBeforeCopyCancel()
119+
{
120+
$tmpPath1 = '';
121+
$tmpPath2 = '';
122+
$this->server->on('beforeCopy', function ($source, $destination) use (&$tmpPath1, &$tmpPath2) {
123+
$tmpPath1 = $source;
124+
$tmpPath2 = $destination;
125+
126+
return false;
127+
});
128+
129+
$oldPath = '/oldCopy.txt';
130+
$newPath = '/newCopy.txt';
131+
132+
$this->server->createFile($oldPath, 'body');
133+
$request = new HTTP\Request('COPY', $oldPath, [
134+
'Destination' => $newPath,
135+
]);
136+
$this->server->httpRequest = $request;
137+
138+
$this->server->exec();
139+
$this->assertEquals(500, $this->server->httpResponse->getStatus());
140+
$this->assertEquals(trim($oldPath, '/'), $tmpPath1);
141+
$this->assertEquals(trim($newPath, '/'), $tmpPath2);
142+
}
143+
94144
public function beforeBindCancelHandler($path)
95145
{
96146
return false;

0 commit comments

Comments
 (0)