|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2026 SURFnet B.V. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +namespace OpenConext\EngineBlockBundle\Tests; |
| 20 | + |
| 21 | +use Mockery; |
| 22 | +use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; |
| 23 | +use OpenConext\EngineBlock\Request\CurrentCorrelationId; |
| 24 | +use OpenConext\EngineBlockBundle\Controller\FeedbackController; |
| 25 | +use PHPUnit\Framework\Attributes\Test; |
| 26 | +use PHPUnit\Framework\TestCase; |
| 27 | +use Psr\Log\LoggerInterface; |
| 28 | +use Symfony\Component\HttpFoundation\Request; |
| 29 | +use Symfony\Component\HttpFoundation\Session\Session; |
| 30 | +use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; |
| 31 | +use Symfony\Contracts\Translation\TranslatorInterface; |
| 32 | +use Twig\Environment; |
| 33 | + |
| 34 | +class FeedbackControllerTest extends TestCase |
| 35 | +{ |
| 36 | + use MockeryPHPUnitIntegration; |
| 37 | + |
| 38 | + #[Test] |
| 39 | + public function correlation_id_is_added_to_feedback_session_when_set(): void |
| 40 | + { |
| 41 | + $currentCorrelationId = new CurrentCorrelationId(); |
| 42 | + $currentCorrelationId->correlationId = 'test-correlation-id-abc123'; |
| 43 | + |
| 44 | + $controller = $this->buildController($currentCorrelationId); |
| 45 | + $request = $this->buildRequestWithSession(); |
| 46 | + |
| 47 | + $controller->unknownServiceProviderAction($request); |
| 48 | + |
| 49 | + $feedbackInfo = $request->getSession()->get('feedbackInfo'); |
| 50 | + $this->assertArrayHasKey('correlationId', $feedbackInfo); |
| 51 | + $this->assertSame('test-correlation-id-abc123', $feedbackInfo['correlationId']); |
| 52 | + } |
| 53 | + |
| 54 | + #[Test] |
| 55 | + public function correlation_id_is_not_added_to_feedback_session_when_null(): void |
| 56 | + { |
| 57 | + $currentCorrelationId = new CurrentCorrelationId(); |
| 58 | + $currentCorrelationId->correlationId = null; |
| 59 | + |
| 60 | + $controller = $this->buildController($currentCorrelationId); |
| 61 | + $request = $this->buildRequestWithSession(); |
| 62 | + |
| 63 | + $controller->unknownServiceProviderAction($request); |
| 64 | + |
| 65 | + $feedbackInfo = $request->getSession()->get('feedbackInfo'); |
| 66 | + $this->assertArrayNotHasKey('correlationId', $feedbackInfo); |
| 67 | + } |
| 68 | + |
| 69 | + private function buildController(CurrentCorrelationId $currentCorrelationId): FeedbackController |
| 70 | + { |
| 71 | + $translator = Mockery::mock(TranslatorInterface::class); |
| 72 | + $twig = Mockery::mock(Environment::class); |
| 73 | + $twig->allows('render')->andReturn('<html></html>'); |
| 74 | + $logger = Mockery::mock(LoggerInterface::class); |
| 75 | + |
| 76 | + return new FeedbackController($translator, $twig, $logger, $currentCorrelationId); |
| 77 | + } |
| 78 | + |
| 79 | + private function buildRequestWithSession(): Request |
| 80 | + { |
| 81 | + $request = Request::create('/authentication/feedback/unknown-service-provider?entity-id=https://sp.example.com'); |
| 82 | + $session = new Session(new MockArraySessionStorage()); |
| 83 | + $request->setSession($session); |
| 84 | + |
| 85 | + return $request; |
| 86 | + } |
| 87 | +} |
0 commit comments