|
|
@@ -0,0 +1,227 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Test\Service\OnChange\Organization;
|
|
|
+
|
|
|
+use App\Entity\Access\Access;
|
|
|
+use App\Entity\Organization\Organization;
|
|
|
+use App\Entity\Organization\Subdomain;
|
|
|
+use App\Message\Command\Typo3\Typo3UpdateCommand;
|
|
|
+use App\Service\MailHub;
|
|
|
+use App\Service\OnChange\OnChangeContext;
|
|
|
+use App\Service\OnChange\Organization\OnSubdomainChange;
|
|
|
+use App\Service\Typo3\BindFileService;
|
|
|
+use Doctrine\ORM\EntityManagerInterface;
|
|
|
+use PHPUnit\Framework\TestCase;
|
|
|
+use Symfony\Component\Messenger\Envelope;
|
|
|
+use Symfony\Component\Messenger\MessageBusInterface;
|
|
|
+
|
|
|
+class OnSubdomainChangeTest extends TestCase
|
|
|
+{
|
|
|
+ private \App\Service\Organization\Utils $organizationUtils;
|
|
|
+ private \App\Service\Access\Utils $accessUtils;
|
|
|
+ private MailHub $mailHub;
|
|
|
+ private BindFileService $bindFileService;
|
|
|
+ private MessageBusInterface $messageBus;
|
|
|
+ private EntityManagerInterface $entityManager;
|
|
|
+ private OnSubdomainChange $onSubdomainChange;
|
|
|
+
|
|
|
+ public function setUp():void
|
|
|
+ {
|
|
|
+ $this->organizationUtils = $this->getMockBuilder(\App\Service\Organization\Utils::class)->disableOriginalConstructor()->getMock();
|
|
|
+ $this->accessUtils = $this->getMockBuilder(\App\Service\Access\Utils::class)->disableOriginalConstructor()->getMock();
|
|
|
+ $this->mailHub = $this->getMockBuilder(MailHub::class)->disableOriginalConstructor()->getMock();
|
|
|
+ $this->bindFileService = $this->getMockBuilder(BindFileService::class)->disableOriginalConstructor()->getMock();
|
|
|
+ $this->messageBus = $this->getMockBuilder(MessageBusInterface::class)->disableOriginalConstructor()->getMock();
|
|
|
+ $this->entityManager = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock();
|
|
|
+
|
|
|
+ $this->onSubdomainChange = new OnSubdomainChange(
|
|
|
+ $this->organizationUtils,
|
|
|
+ $this->accessUtils,
|
|
|
+ $this->mailHub,
|
|
|
+ $this->bindFileService,
|
|
|
+ $this->messageBus,
|
|
|
+ $this->entityManager
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testValidateIsOk(): void
|
|
|
+ {
|
|
|
+ $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
|
|
|
+ $context->method('isPostRequest')->willReturn(true);
|
|
|
+
|
|
|
+ $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
|
|
|
+ $organization->expects(self::once())->method('getSubdomains')->willReturn(new \Doctrine\Common\Collections\ArrayCollection([1,2]));
|
|
|
+
|
|
|
+ $subdomain = $this->getMockBuilder(Subdomain::class)->disableOriginalConstructor()->getMock();
|
|
|
+ $subdomain->expects(self::once())->method('getOrganization')->willReturn($organization);
|
|
|
+
|
|
|
+ $this->onSubdomainChange->validate($subdomain, $context);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testValidateIsPutRequest(): void
|
|
|
+ {
|
|
|
+ $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
|
|
|
+ $context->method('isPostRequest')->willReturn(false);
|
|
|
+
|
|
|
+ $subdomain = $this->getMockBuilder(Subdomain::class)->disableOriginalConstructor()->getMock();
|
|
|
+ $subdomain->expects(self::never())->method('getOrganization');
|
|
|
+
|
|
|
+ $this->onSubdomainChange->validate($subdomain, $context);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testValidateMaxReached(): void
|
|
|
+ {
|
|
|
+ $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
|
|
|
+ $context->method('isPostRequest')->willReturn(true);
|
|
|
+
|
|
|
+ $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
|
|
|
+ $organization->expects(self::once())->method('getSubdomains')->willReturn(new \Doctrine\Common\Collections\ArrayCollection([1,2,3]));
|
|
|
+
|
|
|
+ $subdomain = $this->getMockBuilder(Subdomain::class)->disableOriginalConstructor()->getMock();
|
|
|
+ $subdomain->expects(self::once())->method('getOrganization')->willReturn($organization);
|
|
|
+
|
|
|
+ try {
|
|
|
+ $this->onSubdomainChange->validate($subdomain, $context);
|
|
|
+ throw new \AssertionError('A validation error should have been thrown');
|
|
|
+ } catch (\RuntimeException) {}
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testOnChangeNoChange(): void
|
|
|
+ {
|
|
|
+ $onChange = $this
|
|
|
+ ->getMockBuilder(OnSubdomainChange::class)
|
|
|
+ ->onlyMethods(['sendEmailAfterSubdomainChange'])
|
|
|
+ ->disableOriginalConstructor()
|
|
|
+ ->getMock();
|
|
|
+
|
|
|
+ $this->bindFileService->expects(self::never())->method('registerSubdomain');
|
|
|
+ $this->entityManager->expects(self::never())->method('flush');
|
|
|
+ $this->messageBus->expects(self::never())->method('dispatch');
|
|
|
+ $onChange->expects(self::never())->method('sendEmailAfterSubdomainChange');
|
|
|
+
|
|
|
+ $subdomain = $this->getMockBuilder(Subdomain::class)->disableOriginalConstructor()->getMock();
|
|
|
+ $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
|
|
|
+
|
|
|
+ $onChange->onChange($subdomain, $context);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testOnChangeActivated(): void {
|
|
|
+ $this->bindFileService->expects(self::never())->method('registerSubdomain');
|
|
|
+ $this->entityManager->expects(self::once())->method('flush');
|
|
|
+ $this->messageBus
|
|
|
+ ->expects(self::once())
|
|
|
+ ->method('dispatch')
|
|
|
+ ->with(self::isInstanceOf(Typo3UpdateCommand::class))
|
|
|
+ ->willReturn(new Envelope(new Typo3UpdateCommand(1)));
|
|
|
+
|
|
|
+ $onChange = $this
|
|
|
+ ->getMockBuilder(OnSubdomainChange::class)
|
|
|
+ ->onlyMethods(['sendEmailAfterSubdomainChange'])
|
|
|
+ ->setConstructorArgs(
|
|
|
+ [$this->organizationUtils, $this->accessUtils, $this->mailHub, $this->bindFileService, $this->messageBus, $this->entityManager]
|
|
|
+ )
|
|
|
+ ->getMock();
|
|
|
+ $onChange->expects(self::once())->method('sendEmailAfterSubdomainChange');
|
|
|
+
|
|
|
+ // Le sous-domaine qu'on vient d'activer
|
|
|
+ $subdomain = $this->getMockBuilder(Subdomain::class)->disableOriginalConstructor()->getMock();
|
|
|
+ $subdomain->method('isActive')->willReturn(true);
|
|
|
+
|
|
|
+ // Son état précédent
|
|
|
+ $previousData = $this->getMockBuilder(Subdomain::class)->disableOriginalConstructor()->getMock();
|
|
|
+ $previousData->method('isActive')->willReturn(false);
|
|
|
+
|
|
|
+ // Le sous domaine qui était actif jusqu'ici, et que le OnChange devrait désactiver
|
|
|
+ $otherSubdomain = $this->getMockBuilder(Subdomain::class)->disableOriginalConstructor()->getMock();
|
|
|
+ $otherSubdomain->method('isActive')->willReturn(true);
|
|
|
+ $otherSubdomain->expects(self::once())->method('setActive')->with(false);
|
|
|
+
|
|
|
+ $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
|
|
|
+ $organization->method('getId')->willReturn(1);
|
|
|
+ $organization->expects(self::once())->method('getSubdomains')->willReturn(new \Doctrine\Common\Collections\ArrayCollection([$subdomain, $otherSubdomain]));
|
|
|
+
|
|
|
+ $subdomain->expects(self::exactly(2))->method('getOrganization')->willReturn($organization);
|
|
|
+
|
|
|
+ $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
|
|
|
+ $context->method('previousData')->willReturn($previousData);
|
|
|
+ $context->method('isPutRequest')->willReturn(true);
|
|
|
+ $context->method('isPostRequest')->willReturn(false);
|
|
|
+
|
|
|
+ $onChange->onChange($subdomain, $context);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testOnChangeCreated(): void {
|
|
|
+ $this->bindFileService->expects(self::once())->method('registerSubdomain');
|
|
|
+ $this->entityManager->expects(self::once())->method('flush');
|
|
|
+ $this->messageBus
|
|
|
+ ->expects(self::once())
|
|
|
+ ->method('dispatch')
|
|
|
+ ->with(self::isInstanceOf(Typo3UpdateCommand::class))
|
|
|
+ ->willReturn(new Envelope(new Typo3UpdateCommand(1)));
|
|
|
+
|
|
|
+ $onChange = $this
|
|
|
+ ->getMockBuilder(OnSubdomainChange::class)
|
|
|
+ ->onlyMethods(['sendEmailAfterSubdomainChange'])
|
|
|
+ ->setConstructorArgs(
|
|
|
+ [$this->organizationUtils, $this->accessUtils, $this->mailHub, $this->bindFileService, $this->messageBus, $this->entityManager]
|
|
|
+ )
|
|
|
+ ->getMock();
|
|
|
+ $onChange->expects(self::once())->method('sendEmailAfterSubdomainChange');
|
|
|
+
|
|
|
+ // Le sous-domaine qu'on vient d'activer
|
|
|
+ $subdomain = $this->getMockBuilder(Subdomain::class)->disableOriginalConstructor()->getMock();
|
|
|
+ $subdomain->method('isActive')->willReturn(true);
|
|
|
+
|
|
|
+ // Le sous domaine qui était actif jusqu'ici, et que le OnChange devrait désactiver
|
|
|
+ $otherSubdomain = $this->getMockBuilder(Subdomain::class)->disableOriginalConstructor()->getMock();
|
|
|
+ $otherSubdomain->method('isActive')->willReturn(true);
|
|
|
+ $otherSubdomain->expects(self::once())->method('setActive')->with(false);
|
|
|
+
|
|
|
+ $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
|
|
|
+ $organization->method('getId')->willReturn(1);
|
|
|
+ $organization->expects(self::once())->method('getSubdomains')->willReturn(new \Doctrine\Common\Collections\ArrayCollection([$subdomain, $otherSubdomain]));
|
|
|
+
|
|
|
+ $subdomain->expects(self::exactly(2))->method('getOrganization')->willReturn($organization);
|
|
|
+
|
|
|
+ $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
|
|
|
+ $context->method('previousData')->willReturn(null);
|
|
|
+ $context->method('isPutRequest')->willReturn(false);
|
|
|
+ $context->method('isPostRequest')->willReturn(true);
|
|
|
+
|
|
|
+ $onChange->onChange($subdomain, $context);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testSendEmailAfterSubdomainChange(): void {
|
|
|
+ $admin = $this->getMockBuilder(Access::class)->disableOriginalConstructor()->getMock();
|
|
|
+
|
|
|
+ $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
|
|
|
+
|
|
|
+ $subdomain = $this->getMockBuilder(Subdomain::class)->disableOriginalConstructor()->getMock();
|
|
|
+ $subdomain->expects(self::once())->method('getSubdomain')->willReturn('mysubdomain');
|
|
|
+ $subdomain->expects(self::exactly(3))->method('getOrganization')->willReturn($organization);
|
|
|
+
|
|
|
+ $this->accessUtils->expects(self::once())->method('findAdminFor')->with($organization)->willReturn($admin);
|
|
|
+
|
|
|
+ $this->organizationUtils
|
|
|
+ ->expects(self::once())
|
|
|
+ ->method('getOrganizationWebsite')
|
|
|
+ ->with($organization)
|
|
|
+ ->willReturn('mysubdomain.opentalent.fr');
|
|
|
+
|
|
|
+ $this->mailHub
|
|
|
+ ->expects(self::once())
|
|
|
+ ->method('sendAutomaticEmailToAdmin')
|
|
|
+ ->with(
|
|
|
+ $organization,
|
|
|
+ 'Nouveau sous domaine: mysubdomain',
|
|
|
+ 'subdomain',
|
|
|
+ [
|
|
|
+ 'access' => $admin,
|
|
|
+ 'subdomain' => $subdomain,
|
|
|
+ 'url' => 'mysubdomain.opentalent.fr'
|
|
|
+ ]
|
|
|
+ );
|
|
|
+
|
|
|
+ $this->onSubdomainChange->sendEmailAfterSubdomainChange($subdomain);
|
|
|
+ }
|
|
|
+}
|