From 46d37410dc320f91d55bc4d7a20d38186ce1438c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Lochm=C3=BCller?= Date: Mon, 18 Nov 2024 22:12:33 +0100 Subject: [PATCH] More UnitTests and cleanups --- Resources/Private/Build/PhpCsFixer.php | 1 + Tests/Unit/Cache/IdentifierBuilderTest.php | 3 +- .../Cache/Listener/AbstractListenerTest.php | 1 - .../Listener/CachingAllowedListenerTest.php | 1 - .../Cache/Listener/EnableListenerTest.php | 1 - .../Listener/ForceStaticCacheListenerTest.php | 31 ++++++++++++ .../Listener/NoAuthorizationListenerTest.php | 42 ++++++++++++++++ .../Listener/NoBackendUserListenerTest.php | 50 +++++++++++++++++++ .../NoWorkspacePreviewListenerTest.php | 41 +++++++++++++++ 9 files changed, 167 insertions(+), 4 deletions(-) create mode 100644 Tests/Unit/Cache/Listener/ForceStaticCacheListenerTest.php create mode 100644 Tests/Unit/Cache/Listener/NoAuthorizationListenerTest.php create mode 100644 Tests/Unit/Cache/Listener/NoBackendUserListenerTest.php create mode 100644 Tests/Unit/Cache/Listener/NoWorkspacePreviewListenerTest.php diff --git a/Resources/Private/Build/PhpCsFixer.php b/Resources/Private/Build/PhpCsFixer.php index 4f67684a357..2f222018e53 100755 --- a/Resources/Private/Build/PhpCsFixer.php +++ b/Resources/Private/Build/PhpCsFixer.php @@ -12,6 +12,7 @@ $finder = Finder::create() ->in($baseDir.'/Classes') ->in($baseDir.'/Configuration') + ->in($baseDir.'/Tests/Unit') ; return (new Config()) diff --git a/Tests/Unit/Cache/IdentifierBuilderTest.php b/Tests/Unit/Cache/IdentifierBuilderTest.php index af6f65746dd..09c03cfc04e 100755 --- a/Tests/Unit/Cache/IdentifierBuilderTest.php +++ b/Tests/Unit/Cache/IdentifierBuilderTest.php @@ -47,7 +47,8 @@ public function testCheckInValidPath(): void } } - protected function getIdentifierBuilder(): IdentifierBuilder { + protected function getIdentifierBuilder(): IdentifierBuilder + { return new IdentifierBuilder(new NoopEventDispatcher()); } } diff --git a/Tests/Unit/Cache/Listener/AbstractListenerTest.php b/Tests/Unit/Cache/Listener/AbstractListenerTest.php index ff8048c03cd..e5a4a7d821a 100644 --- a/Tests/Unit/Cache/Listener/AbstractListenerTest.php +++ b/Tests/Unit/Cache/Listener/AbstractListenerTest.php @@ -10,7 +10,6 @@ abstract class AbstractListenerTest extends AbstractTest { - protected function emptyCacheRuleEvent(): CacheRuleEvent { return new CacheRuleEvent( diff --git a/Tests/Unit/Cache/Listener/CachingAllowedListenerTest.php b/Tests/Unit/Cache/Listener/CachingAllowedListenerTest.php index 9bd88faa02f..2631d280d59 100644 --- a/Tests/Unit/Cache/Listener/CachingAllowedListenerTest.php +++ b/Tests/Unit/Cache/Listener/CachingAllowedListenerTest.php @@ -11,7 +11,6 @@ class CachingAllowedListenerTest extends AbstractListenerTest { - public function testNoExplanation(): void { $version = $this->getMockBuilder(Typo3Version::class)->disableOriginalConstructor()->getMock(); diff --git a/Tests/Unit/Cache/Listener/EnableListenerTest.php b/Tests/Unit/Cache/Listener/EnableListenerTest.php index 53aaafb4e34..ad4af27ab9e 100644 --- a/Tests/Unit/Cache/Listener/EnableListenerTest.php +++ b/Tests/Unit/Cache/Listener/EnableListenerTest.php @@ -9,7 +9,6 @@ class EnableListenerTest extends AbstractListenerTest { - public function testNoExplanation(): void { $configurationService = $this->getMockBuilder(ConfigurationService::class)->disableOriginalConstructor()->getMock(); diff --git a/Tests/Unit/Cache/Listener/ForceStaticCacheListenerTest.php b/Tests/Unit/Cache/Listener/ForceStaticCacheListenerTest.php new file mode 100644 index 00000000000..aed63b1bce0 --- /dev/null +++ b/Tests/Unit/Cache/Listener/ForceStaticCacheListenerTest.php @@ -0,0 +1,31 @@ +getMockBuilder(ServerRequestInterface::class)->getMock(), + ['dummy'], + false + ); + + $listener($cacheRuleEvent); + + self::assertEquals(['dummy'], $cacheRuleEvent->getExplanation()); + self::assertEquals(false, $cacheRuleEvent->isSkipProcessing()); + + } + +} diff --git a/Tests/Unit/Cache/Listener/NoAuthorizationListenerTest.php b/Tests/Unit/Cache/Listener/NoAuthorizationListenerTest.php new file mode 100644 index 00000000000..0cd17766329 --- /dev/null +++ b/Tests/Unit/Cache/Listener/NoAuthorizationListenerTest.php @@ -0,0 +1,42 @@ +emptyCacheRuleEvent(); + $listener($event); + + self::assertEquals([], $event->getExplanation()); + self::assertEquals(false, $event->isSkipProcessing()); + } + + + public function testAddExplanation(): void + { + $listener = new NoAuthorizationListener(); + + $request = $this->getMockBuilder(ServerRequestInterface::class)->getMock(); + $request->method('getHeaderLine')->willReturn('Content'); + + $event = new CacheRuleEvent( + $request, + [], + false + ); + $listener($event); + + self::assertNotEquals([], $event->getExplanation()); + self::assertEquals(true, $event->isSkipProcessing()); + } +} diff --git a/Tests/Unit/Cache/Listener/NoBackendUserListenerTest.php b/Tests/Unit/Cache/Listener/NoBackendUserListenerTest.php new file mode 100644 index 00000000000..7ceed1da4b5 --- /dev/null +++ b/Tests/Unit/Cache/Listener/NoBackendUserListenerTest.php @@ -0,0 +1,50 @@ +setAspect('backend.user', $userAspect); + + $listener = new NoBackendUserListener($context); + + $event = $this->emptyCacheRuleEvent(); + $listener($event); + + self::assertEquals([], $event->getExplanation()); + self::assertEquals(false, $event->isSkipProcessing()); + } + + public function testExplanationAndSkip(): void + { + $userAuth = new BackendUserAuthentication(); + $userAuth->user = ['uid' => 5]; + + $userAspect = new UserAspect($userAuth); + + $context = new Context(); + $context->setAspect('backend.user', $userAspect); + + $listener = new NoBackendUserListener($context); + + $event = $this->emptyCacheRuleEvent(); + $listener($event); + + self::assertNotEquals([], $event->getExplanation()); + self::assertEquals(true, $event->isSkipProcessing()); + } +} diff --git a/Tests/Unit/Cache/Listener/NoWorkspacePreviewListenerTest.php b/Tests/Unit/Cache/Listener/NoWorkspacePreviewListenerTest.php new file mode 100644 index 00000000000..daaba73d929 --- /dev/null +++ b/Tests/Unit/Cache/Listener/NoWorkspacePreviewListenerTest.php @@ -0,0 +1,41 @@ +setAspect('workspace', new WorkspaceAspect(0)); + + $listener = new NoWorkspacePreviewListener($context); + + $event = $this->emptyCacheRuleEvent(); + $listener($event); + + self::assertEquals([], $event->getExplanation()); + self::assertEquals(false, $event->isSkipProcessing()); + } + + public function testExplanationAndSkip(): void + { + $context = new Context(); + $context->setAspect('workspace', new WorkspaceAspect(1)); + + $listener = new NoWorkspacePreviewListener($context); + + $event = $this->emptyCacheRuleEvent(); + $listener($event); + + self::assertNotEquals([], $event->getExplanation()); + } + + +}