From 292108e240568677affbc0b20e3b0cb806fc83e9 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Sun, 26 Aug 2018 10:37:28 +0200 Subject: [PATCH] Add event to allow inspecting and changing multipart responses --- lib/DAV/Server.php | 2 ++ tests/Sabre/DAV/ServerEventsTest.php | 38 ++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/lib/DAV/Server.php b/lib/DAV/Server.php index 09760e9d11..8b3b580c51 100644 --- a/lib/DAV/Server.php +++ b/lib/DAV/Server.php @@ -1639,6 +1639,8 @@ public function getResourceTypeForNode(INode $node) */ public function generateMultiStatus($fileProperties, $strip404s = false) { + $this->emit('beforeMultiStatus', [&$fileProperties]); + $w = $this->xml->getWriter(); $w->openMemory(); $w->contextUri = $this->baseUri; diff --git a/tests/Sabre/DAV/ServerEventsTest.php b/tests/Sabre/DAV/ServerEventsTest.php index 7d55ea02ed..23a693c5c2 100644 --- a/tests/Sabre/DAV/ServerEventsTest.php +++ b/tests/Sabre/DAV/ServerEventsTest.php @@ -14,6 +14,8 @@ class ServerEventsTest extends AbstractServer private $exception; + private $fileProperties; + public function testAfterBind() { $this->server->on('afterBind', [$this, 'afterBindHandler']); @@ -113,4 +115,40 @@ public function testMethod() // Fun fact, PHP 7.1 changes the order when sorting-by-callback. $this->assertTrue($k >= 2 && $k <= 3); } + + function multiStatusHandler(&$fileProperties) + { + $this->fileProperties = $fileProperties; + $fileProperties = array_slice($fileProperties, 0, 1); + } + + function testBeforeMultiStatus() + { + + $this->server->on('beforeMultiStatus', [$this, 'multiStatusHandler']); + + $response = $this->server->generateMultiStatus([ + [ + 'href' => 'foo', + '200' => [ + 'd:getcontentlength' => 10 + ], + ], + [ + 'href' => 'bar', + '200' => [ + 'd:getcontentlength' => 11 + ], + ] + ]); + + $body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/", "xmlns\\1=\"urn:DAV\"", $response); + $xml = simplexml_load_string($body); + $xml->registerXPathNamespace('d', 'urn:DAV'); + + $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:getcontentlength'); + $this->assertCount(1, $data); + + $this->assertCount(2, $this->fileProperties); + } }