From c75c69890651d7a02c6a26c16040caa9747f1872 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Wed, 11 Mar 2026 12:15:53 +0100 Subject: [PATCH] fix(tests): Adapt Middleware tests to API change MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Removed a few tests rendered obsolete by the refactoring. Signed-off-by: Côme Chilliet --- .../ProvisioningApiMiddlewareTest.php | 6 +- .../Middleware/SubadminMiddlewareTest.php | 19 +++++-- .../Utility/ControllerMethodReflector.php | 4 ++ .../lib/AppFramework/Http/DispatcherTest.php | 2 +- .../Security/BruteForceMiddlewareTest.php | 2 +- .../Security/CORSMiddlewareTest.php | 6 +- .../PasswordConfirmationMiddlewareTest.php | 2 +- .../Security/RateLimitingMiddlewareTest.php | 2 +- .../Security/SecurityMiddlewareTest.php | 3 +- .../Middleware/SessionMiddlewareTest.php | 56 +++++-------------- .../Utility/ControllerMethodReflectorTest.php | 31 +++++----- 11 files changed, 64 insertions(+), 69 deletions(-) diff --git a/apps/provisioning_api/tests/Middleware/ProvisioningApiMiddlewareTest.php b/apps/provisioning_api/tests/Middleware/ProvisioningApiMiddlewareTest.php index 0e84adca09f..0b55424a255 100644 --- a/apps/provisioning_api/tests/Middleware/ProvisioningApiMiddlewareTest.php +++ b/apps/provisioning_api/tests/Middleware/ProvisioningApiMiddlewareTest.php @@ -53,10 +53,14 @@ class ProvisioningApiMiddlewareTest extends TestCase { ); $this->reflector->method('hasAnnotation') - ->willReturnCallback(function ($annotation) use ($subadminRequired, $hasSettingAuthorizationAnnotation) { + ->willReturnCallback(function ($annotation) use ($subadminRequired) { if ($annotation === 'NoSubAdminRequired') { return !$subadminRequired; } + return false; + }); + $this->reflector->method('hasAnnotationOrAttribute') + ->willReturnCallback(function ($annotation, $attribute) use ($hasSettingAuthorizationAnnotation) { if ($annotation === 'AuthorizedAdminSetting') { return $hasSettingAuthorizationAnnotation; } diff --git a/apps/settings/tests/Middleware/SubadminMiddlewareTest.php b/apps/settings/tests/Middleware/SubadminMiddlewareTest.php index 37cfb5ccc59..489359c3469 100644 --- a/apps/settings/tests/Middleware/SubadminMiddlewareTest.php +++ b/apps/settings/tests/Middleware/SubadminMiddlewareTest.php @@ -14,6 +14,7 @@ use OC\AppFramework\Middleware\Security\Exceptions\NotAdminException; use OC\AppFramework\Utility\ControllerMethodReflector; use OCA\Settings\Middleware\SubadminMiddleware; use OCP\AppFramework\Controller; +use OCP\AppFramework\Http\Attribute\AuthorizedAdminSetting; use OCP\AppFramework\Http\TemplateResponse; use OCP\Group\ISubAdmin; use OCP\IL10N; @@ -62,11 +63,16 @@ class SubadminMiddlewareTest extends \Test\TestCase { $this->expectException(NotAdminException::class); $this->reflector - ->expects($this->exactly(2)) + ->expects($this->exactly(1)) ->method('hasAnnotation') ->willReturnMap([ ['NoSubAdminRequired', false], - ['AuthorizedAdminSetting', false], + ]); + $this->reflector + ->expects($this->exactly(1)) + ->method('hasAnnotationOrAttribute') + ->willReturnMap([ + ['AuthorizedAdminSetting', AuthorizedAdminSetting::class, false], ]); $this->subAdminManager @@ -94,11 +100,16 @@ class SubadminMiddlewareTest extends \Test\TestCase { public function testBeforeControllerAsSubAdminWithoutAnnotation(): void { $this->reflector - ->expects($this->exactly(2)) + ->expects($this->exactly(1)) ->method('hasAnnotation') ->willReturnMap([ ['NoSubAdminRequired', false], - ['AuthorizedAdminSetting', false], + ]); + $this->reflector + ->expects($this->exactly(1)) + ->method('hasAnnotationOrAttribute') + ->willReturnMap([ + ['AuthorizedAdminSetting', AuthorizedAdminSetting::class, false], ]); $this->subAdminManager diff --git a/lib/private/AppFramework/Utility/ControllerMethodReflector.php b/lib/private/AppFramework/Utility/ControllerMethodReflector.php index 1360ca2737c..1d30ff2e137 100644 --- a/lib/private/AppFramework/Utility/ControllerMethodReflector.php +++ b/lib/private/AppFramework/Utility/ControllerMethodReflector.php @@ -33,6 +33,10 @@ class ControllerMethodReflector implements IControllerMethodReflector { * @param string $method the method which we want to inspect */ public function reflect($object, string $method) { + $this->annotations = []; + $this->types = []; + $this->parameters = []; + $this->ranges = []; $this->reflectionMethod = new \ReflectionMethod($object, $method); $this->startLine = $this->reflectionMethod->getStartLine(); $this->file = $this->reflectionMethod->getFileName(); diff --git a/tests/lib/AppFramework/Http/DispatcherTest.php b/tests/lib/AppFramework/Http/DispatcherTest.php index 1743474358b..03a1242dd1f 100644 --- a/tests/lib/AppFramework/Http/DispatcherTest.php +++ b/tests/lib/AppFramework/Http/DispatcherTest.php @@ -123,7 +123,7 @@ class DispatcherTest extends \Test\TestCase { $this->request = $this->createMock(Request::class); - $this->reflector = new ControllerMethodReflector(); + $this->reflector = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class)); $this->dispatcher = new Dispatcher( $this->http, diff --git a/tests/lib/AppFramework/Middleware/Security/BruteForceMiddlewareTest.php b/tests/lib/AppFramework/Middleware/Security/BruteForceMiddlewareTest.php index 0bf99390c22..e386cb82d2c 100644 --- a/tests/lib/AppFramework/Middleware/Security/BruteForceMiddlewareTest.php +++ b/tests/lib/AppFramework/Middleware/Security/BruteForceMiddlewareTest.php @@ -30,7 +30,7 @@ class BruteForceMiddlewareTest extends TestCase { protected function setUp(): void { parent::setUp(); - $this->reflector = new ControllerMethodReflector(); + $this->reflector = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class)); $this->throttler = $this->createMock(IThrottler::class); $this->request = $this->createMock(IRequest::class); $this->logger = $this->createMock(LoggerInterface::class); diff --git a/tests/lib/AppFramework/Middleware/Security/CORSMiddlewareTest.php b/tests/lib/AppFramework/Middleware/Security/CORSMiddlewareTest.php index c325ae638fb..8ffe343effc 100644 --- a/tests/lib/AppFramework/Middleware/Security/CORSMiddlewareTest.php +++ b/tests/lib/AppFramework/Middleware/Security/CORSMiddlewareTest.php @@ -36,7 +36,7 @@ class CORSMiddlewareTest extends \Test\TestCase { protected function setUp(): void { parent::setUp(); - $this->reflector = new ControllerMethodReflector(); + $this->reflector = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class)); $this->session = $this->createMock(Session::class); $this->throttler = $this->createMock(IThrottler::class); $this->logger = $this->createMock(LoggerInterface::class); @@ -82,6 +82,7 @@ class CORSMiddlewareTest extends \Test\TestCase { $this->createMock(IRequestId::class), $this->createMock(IConfig::class) ); + $this->reflector->reflect($this->controller, __FUNCTION__); $middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler, $this->logger); $response = $middleware->afterController($this->controller, __FUNCTION__, new Response()); @@ -303,6 +304,7 @@ class CORSMiddlewareTest extends \Test\TestCase { $this->createMock(IRequestId::class), $this->createMock(IConfig::class) ); + $this->reflector->reflect($this->controller, __FUNCTION__); $middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler, $this->logger); $response = $middleware->afterException($this->controller, __FUNCTION__, new SecurityException('A security exception')); @@ -319,6 +321,7 @@ class CORSMiddlewareTest extends \Test\TestCase { $this->createMock(IRequestId::class), $this->createMock(IConfig::class) ); + $this->reflector->reflect($this->controller, __FUNCTION__); $middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler, $this->logger); $response = $middleware->afterException($this->controller, __FUNCTION__, new SecurityException('A security exception', 501)); @@ -338,6 +341,7 @@ class CORSMiddlewareTest extends \Test\TestCase { $this->createMock(IRequestId::class), $this->createMock(IConfig::class) ); + $this->reflector->reflect($this->controller, __FUNCTION__); $middleware = new CORSMiddleware($request, $this->reflector, $this->session, $this->throttler, $this->logger); $middleware->afterException($this->controller, __FUNCTION__, new \Exception('A regular exception')); } diff --git a/tests/lib/AppFramework/Middleware/Security/PasswordConfirmationMiddlewareTest.php b/tests/lib/AppFramework/Middleware/Security/PasswordConfirmationMiddlewareTest.php index 90e801ca471..e0d1c254c9d 100644 --- a/tests/lib/AppFramework/Middleware/Security/PasswordConfirmationMiddlewareTest.php +++ b/tests/lib/AppFramework/Middleware/Security/PasswordConfirmationMiddlewareTest.php @@ -45,7 +45,7 @@ class PasswordConfirmationMiddlewareTest extends TestCase { private Manager $userManager; protected function setUp(): void { - $this->reflector = new ControllerMethodReflector(); + $this->reflector = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class)); $this->session = $this->createMock(ISession::class); $this->userSession = $this->createMock(IUserSession::class); $this->user = $this->createMock(IUser::class); diff --git a/tests/lib/AppFramework/Middleware/Security/RateLimitingMiddlewareTest.php b/tests/lib/AppFramework/Middleware/Security/RateLimitingMiddlewareTest.php index cf2a569f911..beb9424424f 100644 --- a/tests/lib/AppFramework/Middleware/Security/RateLimitingMiddlewareTest.php +++ b/tests/lib/AppFramework/Middleware/Security/RateLimitingMiddlewareTest.php @@ -45,7 +45,7 @@ class RateLimitingMiddlewareTest extends TestCase { $this->request = $this->createMock(IRequest::class); $this->userSession = $this->createMock(IUserSession::class); - $this->reflector = new ControllerMethodReflector(); + $this->reflector = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class)); $this->limiter = $this->createMock(Limiter::class); $this->session = $this->createMock(ISession::class); $this->appConfig = $this->createMock(IAppConfig::class); diff --git a/tests/lib/AppFramework/Middleware/Security/SecurityMiddlewareTest.php b/tests/lib/AppFramework/Middleware/Security/SecurityMiddlewareTest.php index 0c6fc21357d..b7d5f17cf05 100644 --- a/tests/lib/AppFramework/Middleware/Security/SecurityMiddlewareTest.php +++ b/tests/lib/AppFramework/Middleware/Security/SecurityMiddlewareTest.php @@ -83,7 +83,7 @@ class SecurityMiddlewareTest extends \Test\TestCase { 'test', $this->request ); - $this->reader = new ControllerMethodReflector(); + $this->reader = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class)); $this->logger = $this->createMock(LoggerInterface::class); $this->navigationManager = $this->createMock(INavigationManager::class); $this->urlGenerator = $this->createMock(IURLGenerator::class); @@ -444,6 +444,7 @@ class SecurityMiddlewareTest extends \Test\TestCase { ->willReturn(true); $controller = new $controllerClass('test', $this->request); + $this->reader->reflect($controller, 'foo'); try { $this->middleware->beforeController($controller, 'foo'); diff --git a/tests/lib/AppFramework/Middleware/SessionMiddlewareTest.php b/tests/lib/AppFramework/Middleware/SessionMiddlewareTest.php index 234696ddcf1..bc612c30316 100644 --- a/tests/lib/AppFramework/Middleware/SessionMiddlewareTest.php +++ b/tests/lib/AppFramework/Middleware/SessionMiddlewareTest.php @@ -11,6 +11,7 @@ namespace Test\AppFramework\Middleware; use OC\AppFramework\Middleware\SessionMiddleware; use OC\AppFramework\Utility\ControllerMethodReflector; use OCP\AppFramework\Controller; +use OCP\AppFramework\Http\Attribute\UseSession; use OCP\AppFramework\Http\Response; use OCP\IRequest; use OCP\ISession; @@ -19,8 +20,8 @@ use Test\AppFramework\Middleware\Mock\UseSessionController; use Test\TestCase; class SessionMiddlewareTest extends TestCase { - private ControllerMethodReflector|MockObject $reflector; - private ISession|MockObject $session; + private ControllerMethodReflector&MockObject $reflector; + private ISession&MockObject $session; private Controller $controller; private SessionMiddleware $middleware; @@ -39,70 +40,39 @@ class SessionMiddlewareTest extends TestCase { public function testSessionNotClosedOnBeforeController(): void { $this->configureSessionMock(0, 1); $this->reflector->expects(self::once()) - ->method('hasAnnotation') - ->with('UseSession') + ->method('hasAnnotationOrAttribute') + ->with('UseSession', UseSession::class) ->willReturn(true); $this->middleware->beforeController($this->controller, 'withAnnotation'); } - public function testSessionNotClosedOnBeforeControllerWithAttribute(): void { - $this->configureSessionMock(0, 1); - $this->reflector->expects(self::once()) - ->method('hasAnnotation') - ->with('UseSession') - ->willReturn(false); - - $this->middleware->beforeController($this->controller, 'withAttribute'); - } - public function testSessionClosedOnAfterController(): void { $this->configureSessionMock(1); $this->reflector->expects(self::once()) - ->method('hasAnnotation') - ->with('UseSession') + ->method('hasAnnotationOrAttribute') + ->with('UseSession', UseSession::class) ->willReturn(true); $this->middleware->afterController($this->controller, 'withAnnotation', new Response()); } - public function testSessionClosedOnAfterControllerWithAttribute(): void { - $this->configureSessionMock(1); - $this->reflector->expects(self::once()) - ->method('hasAnnotation') - ->with('UseSession') - ->willReturn(true); - - $this->middleware->afterController($this->controller, 'withAttribute', new Response()); - } - public function testSessionReopenedAndClosedOnBeforeController(): void { $this->configureSessionMock(1, 1); $this->reflector->expects(self::exactly(2)) - ->method('hasAnnotation') - ->with('UseSession') + ->method('hasAnnotationOrAttribute') + ->with('UseSession', UseSession::class) ->willReturn(true); $this->middleware->beforeController($this->controller, 'withAnnotation'); $this->middleware->afterController($this->controller, 'withAnnotation', new Response()); } - public function testSessionReopenedAndClosedOnBeforeControllerWithAttribute(): void { - $this->configureSessionMock(1, 1); - $this->reflector->expects(self::exactly(2)) - ->method('hasAnnotation') - ->with('UseSession') - ->willReturn(false); - - $this->middleware->beforeController($this->controller, 'withAttribute'); - $this->middleware->afterController($this->controller, 'withAttribute', new Response()); - } - public function testSessionClosedOnBeforeController(): void { $this->configureSessionMock(0); $this->reflector->expects(self::once()) - ->method('hasAnnotation') - ->with('UseSession') + ->method('hasAnnotationOrAttribute') + ->with('UseSession', UseSession::class) ->willReturn(false); $this->middleware->beforeController($this->controller, 'without'); @@ -111,8 +81,8 @@ class SessionMiddlewareTest extends TestCase { public function testSessionNotClosedOnAfterController(): void { $this->configureSessionMock(0); $this->reflector->expects(self::once()) - ->method('hasAnnotation') - ->with('UseSession') + ->method('hasAnnotationOrAttribute') + ->with('UseSession', UseSession::class) ->willReturn(false); $this->middleware->afterController($this->controller, 'without', new Response()); diff --git a/tests/lib/AppFramework/Utility/ControllerMethodReflectorTest.php b/tests/lib/AppFramework/Utility/ControllerMethodReflectorTest.php index 0d95dcac45f..56e3070f5dc 100644 --- a/tests/lib/AppFramework/Utility/ControllerMethodReflectorTest.php +++ b/tests/lib/AppFramework/Utility/ControllerMethodReflectorTest.php @@ -9,6 +9,7 @@ namespace Test\AppFramework\Utility; use OC\AppFramework\Utility\ControllerMethodReflector; +use Psr\Log\LoggerInterface; class BaseController { /** @@ -69,7 +70,7 @@ class ControllerMethodReflectorTest extends \Test\TestCase { * @Annotation */ public function testReadAnnotation(): void { - $reader = new ControllerMethodReflector(); + $reader = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class)); $reader->reflect( '\Test\AppFramework\Utility\ControllerMethodReflectorTest', 'testReadAnnotation' @@ -82,7 +83,7 @@ class ControllerMethodReflectorTest extends \Test\TestCase { * @Annotation(parameter=value) */ public function testGetAnnotationParameterSingle(): void { - $reader = new ControllerMethodReflector(); + $reader = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class)); $reader->reflect( self::class, __FUNCTION__ @@ -95,7 +96,7 @@ class ControllerMethodReflectorTest extends \Test\TestCase { * @Annotation(parameter1=value1, parameter2=value2,parameter3=value3) */ public function testGetAnnotationParameterMultiple(): void { - $reader = new ControllerMethodReflector(); + $reader = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class)); $reader->reflect( self::class, __FUNCTION__ @@ -111,7 +112,7 @@ class ControllerMethodReflectorTest extends \Test\TestCase { * @param test */ public function testReadAnnotationNoLowercase(): void { - $reader = new ControllerMethodReflector(); + $reader = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class)); $reader->reflect( '\Test\AppFramework\Utility\ControllerMethodReflectorTest', 'testReadAnnotationNoLowercase' @@ -127,7 +128,7 @@ class ControllerMethodReflectorTest extends \Test\TestCase { * @param int $test */ public function testReadTypeIntAnnotations(): void { - $reader = new ControllerMethodReflector(); + $reader = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class)); $reader->reflect( '\Test\AppFramework\Utility\ControllerMethodReflectorTest', 'testReadTypeIntAnnotations' @@ -145,7 +146,7 @@ class ControllerMethodReflectorTest extends \Test\TestCase { } public function testReadTypeIntAnnotationsScalarTypes(): void { - $reader = new ControllerMethodReflector(); + $reader = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class)); $reader->reflect( '\Test\AppFramework\Utility\ControllerMethodReflectorTest', 'arguments3' @@ -163,7 +164,7 @@ class ControllerMethodReflectorTest extends \Test\TestCase { * @param double $test something special */ public function testReadTypeDoubleAnnotations(): void { - $reader = new ControllerMethodReflector(); + $reader = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class)); $reader->reflect( '\Test\AppFramework\Utility\ControllerMethodReflectorTest', 'testReadTypeDoubleAnnotations' @@ -177,7 +178,7 @@ class ControllerMethodReflectorTest extends \Test\TestCase { * @param string $foo */ public function testReadTypeWhitespaceAnnotations(): void { - $reader = new ControllerMethodReflector(); + $reader = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class)); $reader->reflect( '\Test\AppFramework\Utility\ControllerMethodReflectorTest', 'testReadTypeWhitespaceAnnotations' @@ -190,7 +191,7 @@ class ControllerMethodReflectorTest extends \Test\TestCase { public function arguments($arg, $arg2 = 'hi') { } public function testReflectParameters(): void { - $reader = new ControllerMethodReflector(); + $reader = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class)); $reader->reflect( '\Test\AppFramework\Utility\ControllerMethodReflectorTest', 'arguments' @@ -203,7 +204,7 @@ class ControllerMethodReflectorTest extends \Test\TestCase { public function arguments2($arg) { } public function testReflectParameters2(): void { - $reader = new ControllerMethodReflector(); + $reader = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class)); $reader->reflect( '\Test\AppFramework\Utility\ControllerMethodReflectorTest', 'arguments2' @@ -214,7 +215,7 @@ class ControllerMethodReflectorTest extends \Test\TestCase { public function testInheritance(): void { - $reader = new ControllerMethodReflector(); + $reader = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class)); $reader->reflect('Test\AppFramework\Utility\EndController', 'test'); $this->assertTrue($reader->hasAnnotation('Annotation')); @@ -222,7 +223,7 @@ class ControllerMethodReflectorTest extends \Test\TestCase { public function testInheritanceOverride(): void { - $reader = new ControllerMethodReflector(); + $reader = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class)); $reader->reflect('Test\AppFramework\Utility\EndController', 'test2'); $this->assertTrue($reader->hasAnnotation('NoAnnotation')); @@ -231,14 +232,14 @@ class ControllerMethodReflectorTest extends \Test\TestCase { public function testInheritanceOverrideNoDocblock(): void { - $reader = new ControllerMethodReflector(); + $reader = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class)); $reader->reflect('Test\AppFramework\Utility\EndController', 'test3'); $this->assertFalse($reader->hasAnnotation('Annotation')); } public function testRangeDetectionPsalm(): void { - $reader = new ControllerMethodReflector(); + $reader = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class)); $reader->reflect('Test\AppFramework\Utility\EndController', 'test4'); $rangeInfo1 = $reader->getRange('rangedOne'); @@ -259,7 +260,7 @@ class ControllerMethodReflectorTest extends \Test\TestCase { } public function testRangeDetectionNative(): void { - $reader = new ControllerMethodReflector(); + $reader = new ControllerMethodReflector(\OCP\Server::get(LoggerInterface::class)); $reader->reflect('Test\AppFramework\Utility\EndController', 'test5'); $rangeInfo1 = $reader->getRange('rangedOne');