From 8d9af3e26214f07e833a6b8d8b60329fc71916a7 Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Mon, 23 Jan 2023 17:11:39 +0100 Subject: feat(app-framework): Add support for global middlewares This allows apps to register middlewares that always register, not just for the app's own requests Signed-off-by: Christoph Wurst --- .../Bootstrap/RegistrationContextTest.php | 2 +- .../DependencyInjection/DIContainerTest.php | 38 ++++++++++++++++++++-- 2 files changed, 36 insertions(+), 4 deletions(-) (limited to 'tests/lib') diff --git a/tests/lib/AppFramework/Bootstrap/RegistrationContextTest.php b/tests/lib/AppFramework/Bootstrap/RegistrationContextTest.php index cee30b665fb..55f30a895f5 100644 --- a/tests/lib/AppFramework/Bootstrap/RegistrationContextTest.php +++ b/tests/lib/AppFramework/Bootstrap/RegistrationContextTest.php @@ -180,7 +180,7 @@ class RegistrationContextTest extends TestCase { } public function testGetMiddlewareRegistrations(): void { - $this->context->registerMiddleware('core', TwoFactorMiddleware::class); + $this->context->registerMiddleware('core', TwoFactorMiddleware::class, false); $registrations = $this->context->getMiddlewareRegistrations(); diff --git a/tests/lib/AppFramework/DependencyInjection/DIContainerTest.php b/tests/lib/AppFramework/DependencyInjection/DIContainerTest.php index bcf4e0a2771..04422643121 100644 --- a/tests/lib/AppFramework/DependencyInjection/DIContainerTest.php +++ b/tests/lib/AppFramework/DependencyInjection/DIContainerTest.php @@ -26,8 +26,8 @@ namespace Test\AppFramework\DependencyInjection; use OC\AppFramework\Bootstrap\Coordinator; +use OC\AppFramework\Bootstrap\MiddlewareRegistration; use OC\AppFramework\Bootstrap\RegistrationContext; -use OC\AppFramework\Bootstrap\ServiceRegistration; use OC\AppFramework\DependencyInjection\DIContainer; use OC\AppFramework\Http\Request; use OC\AppFramework\Middleware\Security\SecurityMiddleware; @@ -95,8 +95,40 @@ class DIContainerTest extends \Test\TestCase { $registrationContext = $this->createMock(RegistrationContext::class); $registrationContext->method('getMiddlewareRegistrations') ->willReturn([ - new ServiceRegistration($this->container['appName'], 'foo'), - new ServiceRegistration('otherapp', 'bar'), + new MiddlewareRegistration($this->container['appName'], 'foo', false), + new MiddlewareRegistration('otherapp', 'bar', false), + ]); + $this->container['foo'] = new class extends Middleware { + }; + $this->container['bar'] = new class extends Middleware { + }; + $coordinator->method('getRegistrationContext')->willReturn($registrationContext); + + $dispatcher = $this->container['MiddlewareDispatcher']; + + $middlewares = $dispatcher->getMiddlewares(); + self::assertNotEmpty($middlewares); + foreach ($middlewares as $middleware) { + if ($middleware === $this->container['bar']) { + $this->fail('Container must not register this middleware'); + } + if ($middleware === $this->container['foo']) { + // It is done + return; + } + } + $this->fail('Bootstrap registered middleware not found'); + } + + public function testMiddlewareDispatcherIncludesGlobalBootstrapMiddlewares(): void { + $coordinator = $this->createMock(Coordinator::class); + $this->container[Coordinator::class] = $coordinator; + $this->container['Request'] = $this->createMock(Request::class); + $registrationContext = $this->createMock(RegistrationContext::class); + $registrationContext->method('getMiddlewareRegistrations') + ->willReturn([ + new MiddlewareRegistration('otherapp', 'foo', true), + new MiddlewareRegistration('otherapp', 'bar', false), ]); $this->container['foo'] = new class extends Middleware { }; -- cgit v1.2.3