diff --git a/apps/dav/appinfo/v1/caldav.php b/apps/dav/appinfo/v1/caldav.php index 9547ff88c8dc9..e350b02392a55 100644 --- a/apps/dav/appinfo/v1/caldav.php +++ b/apps/dav/appinfo/v1/caldav.php @@ -22,6 +22,7 @@ use OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin; use OCA\DAV\Connector\Sabre\MaintenancePlugin; use OCA\DAV\Connector\Sabre\Principal; +use OCA\DAV\Events\SabrePluginAddEvent; use OCP\Accounts\IAccountManager; use OCP\App\IAppManager; use OCP\EventDispatcher\IEventDispatcher; @@ -128,5 +129,8 @@ $server->addPlugin(Server::get(RateLimitingPlugin::class)); $server->addPlugin(Server::get(CalDavValidatePlugin::class)); +// allow setup of additional plugins +$dispatcher->dispatchTyped(new SabrePluginAddEvent($server)); + // And off we go! $server->start(); diff --git a/apps/dav/appinfo/v1/carddav.php b/apps/dav/appinfo/v1/carddav.php index 4f5218855235e..c1a9bb04fcaf3 100644 --- a/apps/dav/appinfo/v1/carddav.php +++ b/apps/dav/appinfo/v1/carddav.php @@ -21,6 +21,7 @@ use OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin; use OCA\DAV\Connector\Sabre\MaintenancePlugin; use OCA\DAV\Connector\Sabre\Principal; +use OCA\DAV\Events\SabrePluginAddEvent; use OCP\Accounts\IAccountManager; use OCP\App\IAppManager; use OCP\EventDispatcher\IEventDispatcher; @@ -106,5 +107,8 @@ $server->addPlugin(Server::get(CardDavRateLimitingPlugin::class)); $server->addPlugin(Server::get(CardDavValidatePlugin::class)); +// allow setup of additional plugins +Server::get(IEventDispatcher::class)->dispatchTyped(new SabrePluginAddEvent($server)); + // And off we go! $server->start(); diff --git a/apps/dav/tests/unit/AppInfo/LegacyEndpointsTest.php b/apps/dav/tests/unit/AppInfo/LegacyEndpointsTest.php new file mode 100644 index 0000000000000..9f53983481b9e --- /dev/null +++ b/apps/dav/tests/unit/AppInfo/LegacyEndpointsTest.php @@ -0,0 +1,76 @@ + ['v1/webdav.php', '/remote.php/webdav', null], + 'caldav' => ['v1/caldav.php', '/remote.php/caldav', 'caldav'], + 'carddav' => ['v1/carddav.php', '/remote.php/carddav', 'carddav'], + ]; + } + + #[DataProvider(methodName: 'endpoints')] + public function testTheEndpointDispatchesSabrePluginAddEvent(string $file, string $baseuri, ?string $plugin): void { + $server = $this->serverOf(__DIR__ . '/../../../appinfo/' . $file, $baseuri); + + $this->assertInstanceOf(SabreServer::class, $server, 'The event was dispatched before the server started'); + if ($plugin !== null) { + $this->assertNotNull($server->getPlugin($plugin)); + } + } + + /** + * Runs an endpoint up to the event and returns the server it carries. + */ + private function serverOf(string $endpoint, string $baseuri): ?SabreServer { + $request = $this->createMock(IRequest::class); + $request->method('getRequestUri')->willReturn($baseuri . '/'); + $this->overwriteService(IRequest::class, $request); + $captured = null; + $listener = static function (SabrePluginAddEvent $event) use (&$captured): void { + $captured = $event->getServer(); + throw new \RuntimeException('stop before the server starts'); + }; + $dispatcher = Server::get(IEventDispatcher::class); + $dispatcher->addListener(SabrePluginAddEvent::class, $listener, -1000); + // The endpoints close every output buffer; PHPUnit expects its own back. + $level = ob_get_level(); + try { + (static function (string $endpoint, string $baseuri): void { + require $endpoint; + })($endpoint, $baseuri); + } catch (\RuntimeException $e) { + $this->assertSame('stop before the server starts', $e->getMessage()); + } finally { + while (ob_get_level() < $level) { + ob_start(); + } + $dispatcher->removeListener(SabrePluginAddEvent::class, $listener); + $this->restoreService(IRequest::class); + } + return $captured; + } +}