diff options
author | Christoph Wurst <christoph@winzerhof-wurst.at> | 2023-01-23 11:27:28 +0100 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2023-01-25 09:27:24 +0100 |
commit | 907ff68bfc345a4ea99197494e44dfe05a56d562 (patch) | |
tree | 477c9d6031a2f6ff8052d84259267ae4c0301159 /tests | |
parent | d51429a47232bbf46a2be832ecfa711f102da802 (diff) | |
download | nextcloud-server-907ff68bfc345a4ea99197494e44dfe05a56d562.tar.gz nextcloud-server-907ff68bfc345a4ea99197494e44dfe05a56d562.zip |
perf(app-framework): Make the app middleware registration lazy
Before this patch, app middlewares were registered on the dispatcher for
every app loaded in a Nextcloud process. With the patch, only
middlewares belonging to the same app of a dispatcher instance are
loaded.
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lib/AppFramework/Bootstrap/RegistrationContextTest.php | 29 | ||||
-rw-r--r-- | tests/lib/AppFramework/DependencyInjection/DIContainerTest.php | 36 |
2 files changed, 47 insertions, 18 deletions
diff --git a/tests/lib/AppFramework/Bootstrap/RegistrationContextTest.php b/tests/lib/AppFramework/Bootstrap/RegistrationContextTest.php index 22111f5b68e..cee30b665fb 100644 --- a/tests/lib/AppFramework/Bootstrap/RegistrationContextTest.php +++ b/tests/lib/AppFramework/Bootstrap/RegistrationContextTest.php @@ -27,6 +27,7 @@ namespace lib\AppFramework\Bootstrap; use OC\AppFramework\Bootstrap\RegistrationContext; use OC\AppFramework\Bootstrap\ServiceRegistration; +use OC\Core\Middleware\TwoFactorMiddleware; use OCP\AppFramework\App; use OCP\AppFramework\IAppContainer; use OCP\EventDispatcher\IEventDispatcher; @@ -145,24 +146,6 @@ class RegistrationContextTest extends TestCase { ]); } - public function testRegisterMiddleware(): void { - $app = $this->createMock(App::class); - $name = 'abc'; - $container = $this->createMock(IAppContainer::class); - $app->method('getContainer') - ->willReturn($container); - $container->expects($this->once()) - ->method('registerMiddleware') - ->with($name); - $this->logger->expects($this->never()) - ->method('error'); - - $this->context->for('myapp')->registerMiddleware($name); - $this->context->delegateMiddlewareRegistrations([ - 'myapp' => $app, - ]); - } - public function testRegisterUserMigrator(): void { $appIdA = 'myapp'; $migratorClassA = 'OCA\App\UserMigration\AppMigrator'; @@ -195,4 +178,14 @@ class RegistrationContextTest extends TestCase { [false] ]; } + + public function testGetMiddlewareRegistrations(): void { + $this->context->registerMiddleware('core', TwoFactorMiddleware::class); + + $registrations = $this->context->getMiddlewareRegistrations(); + + self::assertNotEmpty($registrations); + self::assertSame('core', $registrations[0]->getAppId()); + self::assertSame(TwoFactorMiddleware::class, $registrations[0]->getService()); + } } diff --git a/tests/lib/AppFramework/DependencyInjection/DIContainerTest.php b/tests/lib/AppFramework/DependencyInjection/DIContainerTest.php index 69367ad6ce2..bcf4e0a2771 100644 --- a/tests/lib/AppFramework/DependencyInjection/DIContainerTest.php +++ b/tests/lib/AppFramework/DependencyInjection/DIContainerTest.php @@ -25,9 +25,13 @@ namespace Test\AppFramework\DependencyInjection; +use OC\AppFramework\Bootstrap\Coordinator; +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; +use OCP\AppFramework\Middleware; use OCP\AppFramework\QueryException; use OCP\IConfig; use OCP\IRequestId; @@ -84,6 +88,38 @@ class DIContainerTest extends \Test\TestCase { $this->assertTrue($found); } + public function testMiddlewareDispatcherIncludesBootstrapMiddlewares(): 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 ServiceRegistration($this->container['appName'], 'foo'), + new ServiceRegistration('otherapp', 'bar'), + ]); + $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 testInvalidAppClass() { $this->expectException(QueryException::class); $this->container->query('\OCA\Name\Foo'); |