From 44e7425e363b8ca33ba33826ef27e87690727bab Mon Sep 17 00:00:00 2001 From: Johan Kromhout Date: Tue, 14 Jan 2025 12:20:59 +0100 Subject: [PATCH] Fix: Don't reset contractual base on production entities Prior to this change, when a change request was sent, the contractualBase would be set to null, because it is not present in the form. This change updates the contractual base if necessary on update. The expected behaviour is: When the field is not present, add it with the correct value When the field is present and the value is incorrect update it When the field is present and the value is correct, leave it untouched Resolves https://github.com/SURFnet/sp-dashboard/issues/1343 --- .github/workflows/test-integration.yml | 2 +- .../EntityChangeRequestCommandHandler.php | 3 + .../EntityChangeRequestCommandHandlerTest.php | 179 ++++++++++++++++++ 3 files changed, 183 insertions(+), 1 deletion(-) create mode 100644 tests/integration/Application/CommandHandler/Entity/EntityChangeRequestCommandHandlerTest.php diff --git a/.github/workflows/test-integration.yml b/.github/workflows/test-integration.yml index 614e58887..caef9f788 100644 --- a/.github/workflows/test-integration.yml +++ b/.github/workflows/test-integration.yml @@ -25,7 +25,7 @@ jobs: - name: Checkout openconext-devconf uses: actions/checkout@v4 - with: + with: path: OpenConext-devconf repository: OpenConext/OpenConext-devconf diff --git a/src/Surfnet/ServiceProviderDashboard/Application/CommandHandler/Entity/EntityChangeRequestCommandHandler.php b/src/Surfnet/ServiceProviderDashboard/Application/CommandHandler/Entity/EntityChangeRequestCommandHandler.php index ce1ffdeb1..a7bd36ad3 100644 --- a/src/Surfnet/ServiceProviderDashboard/Application/CommandHandler/Entity/EntityChangeRequestCommandHandler.php +++ b/src/Surfnet/ServiceProviderDashboard/Application/CommandHandler/Entity/EntityChangeRequestCommandHandler.php @@ -30,6 +30,7 @@ use Surfnet\ServiceProviderDashboard\Application\Service\TicketService; use Surfnet\ServiceProviderDashboard\Domain\Entity\Entity\JiraTicketNumber; use Surfnet\ServiceProviderDashboard\Domain\Repository\EntityChangeRequestRepository; +use Surfnet\ServiceProviderDashboard\Domain\Service\ContractualBaseService; use Surfnet\ServiceProviderDashboard\Infrastructure\HttpClient\Exceptions\RuntimeException\PublishMetadataException; use Symfony\Component\HttpFoundation\RequestStack; @@ -41,6 +42,7 @@ class EntityChangeRequestCommandHandler implements CommandHandler public function __construct( private readonly EntityChangeRequestRepository $repository, + private readonly ContractualBaseService $contractualBaseHelper, private readonly EntityServiceInterface $entityService, private readonly TicketService $ticketService, private readonly RequestStack $requestStack, @@ -72,6 +74,7 @@ public function handle(PublishProductionCommandInterface $command): void $entity->getMetaData()->getNameEn() ) ); + $this->contractualBaseHelper->writeContractualBase($entity); // Create the Jira ticket (we need the ticket id for the revision notes in manage later on) $ticket = $this->ticketService->createJiraTicket( $entity, diff --git a/tests/integration/Application/CommandHandler/Entity/EntityChangeRequestCommandHandlerTest.php b/tests/integration/Application/CommandHandler/Entity/EntityChangeRequestCommandHandlerTest.php new file mode 100644 index 000000000..c51e8d82c --- /dev/null +++ b/tests/integration/Application/CommandHandler/Entity/EntityChangeRequestCommandHandlerTest.php @@ -0,0 +1,179 @@ +entityChangeRequestRepository = m::mock(EntityChangeRequestRepository::class); + $this->entityService = m::mock(EntityServiceInterface::class); + $this->ticketService = m::mock(TicketService::class); + $this->requestStack = m::mock(RequestStack::class); + $this->logger = new NullLogger(); + + $this->mailService = m::mock(MailService::class); + + $this->commandHandler = new EntityChangeRequestCommandHandler( + $this->entityChangeRequestRepository, + new ContractualBaseService(), + $this->entityService, + $this->ticketService, + $this->requestStack, + $this->mailService, + $this->logger, + 'customIssueType' + ); + + } + + public static function contractualBaseProvider(){ + return [ + 'Pristine equals enitity, no change' => [Constants::CONTRACTUAL_BASE_IX, []], + 'Pristine is null, update to correct value' => [null, ["metaDataFields.coin:contractual_base" => "IX"]], + 'Pristine is wrong, update to correct value' => [Constants::CONTRACTUAL_BASE_AO, ["metaDataFields.coin:contractual_base" => "IX"]], + ]; + } + + /** + * @dataProvider contractualBaseProvider + */ + public function test_it_should_update_contractual_base_on_change_request(?string $pristineContractualBase, array $expectedDiff) + { + $manageEntity = $this->createEntity(); + + $applicant = new Contact('john:doe', 'john@example.com', 'John Doe'); + + $pristineEntity = unserialize(serialize($manageEntity)); + if($pristineContractualBase !== null) { + $pristineEntity->getMetaData()->getCoin()->setContractualBase($pristineContractualBase); + } + + $this->entityService->shouldReceive('getPristineManageEntityById') + ->once() + ->with($manageEntity->getId(), $manageEntity->getEnvironment()) + ->andReturn($pristineEntity); + + $ticket = new Issue('ISSUE-123', 'foo', 'bar'); + + $this->ticketService->shouldReceive('createJiraTicket') + ->once() + ->andReturn($ticket); + + $this->entityChangeRequestRepository->shouldReceive('openChangeRequest') + ->once() + ->withArgs(function($entity, $pristineEntity) use ($expectedDiff){ + if($pristineEntity->diff($entity)->getDiff() !== $expectedDiff){ + var_dump($pristineEntity->diff($entity)->getDiff(), 'is not equal to' , $expectedDiff); + } + return $pristineEntity->diff($entity)->getDiff() === $expectedDiff; + }) + ->andReturn(['id' => 1]); + + $command = new EntityChangeRequestCommand($manageEntity, $applicant); + $this->commandHandler->handle($command); + } + + /** + * @return ManageEntity + */ + private function createEntity(): ManageEntity + { + $coin = new Coin( + null, + null, + null, + null, + null, + new TypeOfServiceCollection(), + null, + null, + null, + null + ); + + $manageEntity = new ManageEntity( + 1, + new AttributeList(), + new MetaData( + 'https://test.entity.id', + 'https://test.metadata.url', + null, + null, + 'Test Description', + null, + 'Test Entity', + null, + new ContactList(), + new Organization('Test org', "Test organisation", null, null, null, null), + $coin, + new Logo(null, null, null) + ), + new AllowedIdentityProviders([], true), new Protocol(Constants::TYPE_SAML), + null, + new Service() + ); + $manageEntity->setId('f1e394b2-815b-4018-b780-898976322016'); + $manageEntity->setEnvironment('production'); + $manageEntity->setStatus('published'); + return $manageEntity; + } + +}