Selaa lähdekoodia

add tests (ongoing)

Olivier Massot 2 vuotta sitten
vanhempi
commit
dad4b0cf64
2 muutettua tiedostoa jossa 154 lisäystä ja 11 poistoa
  1. 38 11
      src/Service/Typo3/SubdomainService.php
  2. 116 0
      tests/Service/Typo3/SubdomainServiceTest.php

+ 38 - 11
src/Service/Typo3/SubdomainService.php

@@ -48,8 +48,12 @@ class SubdomainService
     /**
      * Is the input a valid value for a subdomain
      *
+     * @see https://www.rfc-editor.org/rfc/rfc3986#section-3.2.2
+     * @see https://www.rfc-editor.org/rfc/rfc1034#section-3.5
+     * @see https://www.rfc-editor.org/rfc/rfc1123#section-2.1
+     *
      * @param string $subdomainValue
-     * @return false|int
+     * @return bool
      */
     public function isValidSubdomain(string $subdomainValue): bool
     {
@@ -116,6 +120,27 @@ class SubdomainService
             throw new \RuntimeException('Can not activate a non-persisted subdomain');
         }
 
+        $subdomain = $this->setOrganizationActiveSubdomain($subdomain);
+
+        $this->renameAdminUserToMatchSubdomain($subdomain);
+
+        // Update the typo3 website (asynchronously with messenger)
+        $this->updateTypo3Website($subdomain->getOrganization());
+
+        // Send confirmation email
+        $this->sendConfirmationEmail($subdomain);
+
+        return $subdomain;
+    }
+
+    /**
+     * The subdomain becomes the only active subdomain of its organization.
+     * New state is persisted is database.
+     *
+     * @param Subdomain $subdomain
+     * @return void
+     */
+    protected function setOrganizationActiveSubdomain(Subdomain $subdomain): Subdomain {
         foreach ($subdomain->getOrganization()->getSubdomains() as $other) {
             if ($other !== $subdomain && $other->isActive()) {
                 $other->setActive(false);
@@ -127,16 +152,6 @@ class SubdomainService
         $this->em->flush();
         $this->em->refresh($subdomain->getOrganization());
 
-        $this->renameAdminUserToMatchSubdomain($subdomain);
-
-        // Update the typo3 website (asynchronously with messenger)
-        $this->messageBus->dispatch(
-            new Typo3UpdateCommand($subdomain->getOrganization()->getId())
-        );
-
-        // Send confirmation email
-        $this->sendConfirmationEmail($subdomain);
-
         return $subdomain;
     }
 
@@ -156,6 +171,18 @@ class SubdomainService
         $this->em->flush();
     }
 
+    /**
+     * Trigger an update of the typo3 organization's website
+     *
+     * @param $organization
+     * @return void
+     */
+    protected function updateTypo3Website($organization) {
+        $this->messageBus->dispatch(
+            new Typo3UpdateCommand($organization->getId())
+        );
+    }
+
     /**
      * Send the confirmation email to the organization after a new subdomain has been activated
      *

+ 116 - 0
tests/Service/Typo3/SubdomainServiceTest.php

@@ -0,0 +1,116 @@
+<?php
+
+namespace App\Tests\Service\Typo3;
+
+
+use App\Entity\Organization\Organization;
+use App\Entity\Organization\Subdomain;
+use App\Repository\Access\AccessRepository;
+use App\Repository\Organization\SubdomainRepository;
+use App\Service\Organization\Utils as OrganizationUtils;
+use App\Service\Typo3\BindFileService;
+use App\Service\Typo3\SubdomainService;
+use Doctrine\Common\Collections\ArrayCollection;
+use Doctrine\ORM\EntityManagerInterface;
+use PHPUnit\Framework\MockObject\MockObject;
+use PHPUnit\Framework\TestCase;
+use Symfony\Bundle\SecurityBundle\Security;
+use Symfony\Component\Messenger\MessageBusInterface;
+
+class TestableSubdomainService extends SubdomainService {
+    public function setOrganizationActiveSubdomain(Subdomain $subdomain): Subdomain {
+        return parent::setOrganizationActiveSubdomain($subdomain);
+    }
+
+    public function renameAdminUserToMatchSubdomain(Subdomain $subdomain): void {
+        parent::renameAdminUserToMatchSubdomain($subdomain);
+    }
+
+    public function updateTypo3Website($organization) {
+        parent::updateTypo3Website($organization);
+    }
+
+    public function sendConfirmationEmail(Subdomain $subdomain): void {
+        parent::sendConfirmationEmail($subdomain);
+    }
+}
+class SubdomainServiceTest extends TestCase
+{
+    private SubdomainRepository $subdomainRepository;
+    private OrganizationUtils $organizationUtils;
+    private Security $security;
+    private BindFileService $bindFileService;
+    private MessageBusInterface $messageBus;
+    private EntityManagerInterface $entityManager;
+    private AccessRepository $accessRepository;
+
+    public function setUp():void
+    {
+        $this->subdomainRepository = $this->getMockBuilder(SubdomainRepository::class)->disableOriginalConstructor()->getMock();
+        $this->entityManager = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock();
+        $this->messageBus = $this->getMockBuilder(MessageBusInterface::class)->disableOriginalConstructor()->getMock();
+        $this->security = $this->getMockBuilder(Security::class)->disableOriginalConstructor()->getMock();
+        $this->organizationUtils = $this->getMockBuilder(OrganizationUtils::class)->disableOriginalConstructor()->getMock();
+        $this->bindFileService = $this->getMockBuilder(BindFileService::class)->disableOriginalConstructor()->getMock();
+        $this->accessRepository = $this->getMockBuilder(AccessRepository::class)->disableOriginalConstructor()->getMock();
+    }
+
+    private function makeOnSubdomainChangeMock(string $methodName): MockObject | SubdomainService {
+        return $this->getMockBuilder(SubdomainService::class)
+            ->setConstructorArgs([
+                $this->subdomainRepository,
+                $this->entityManager,
+                $this->messageBus,
+                $this->security,
+                $this->organizationUtils,
+                $this->bindFileService,
+                $this->accessRepository
+            ])
+            ->setMethodsExcept([$methodName])
+            ->getMock();
+    }
+
+    /**
+     * @see SubdomainService::canRegisterNewSubdomain()
+     */
+    public function testCanRegisterNewSubdomainTrue(): void
+    {
+        $subdomainService = $this->makeOnSubdomainChangeMock('canRegisterNewSubdomain');
+
+        $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
+        $organization->expects(self::once())->method('getSubdomains')->willReturn(new ArrayCollection([1, 2]));
+
+        $this->assertTrue($subdomainService->canRegisterNewSubdomain($organization));
+    }
+
+    /**
+     * @see SubdomainService::canRegisterNewSubdomain()
+     */
+    public function testCanRegisterNewSubdomainFalse(): void
+    {
+        $subdomainService = $this->makeOnSubdomainChangeMock('canRegisterNewSubdomain');
+
+        $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
+        $organization->expects(self::once())->method('getSubdomains')->willReturn(new ArrayCollection([1, 2, 3]));
+
+        $this->assertFalse($subdomainService->canRegisterNewSubdomain($organization));
+    }
+
+    /**
+     * @see SubdomainService::isValidSubdomain()
+     */
+    public function testIsValidSubdomain(): void {
+        $subdomainService = $this->makeOnSubdomainChangeMock('isValidSubdomain');
+
+        $this->assertTrue($subdomainService->isValidSubdomain('abcd'));
+        $this->assertTrue($subdomainService->isValidSubdomain('abcdefgh'));
+        $this->assertTrue($subdomainService->isValidSubdomain('abcd-efgh'));
+        $this->assertTrue($subdomainService->isValidSubdomain('123'));
+        $this->assertTrue($subdomainService->isValidSubdomain('a'));
+
+        $this->assertFalse($subdomainService->isValidSubdomain('_abc'));
+        $this->assertFalse($subdomainService->isValidSubdomain('abc-'));
+        $this->assertFalse($subdomainService->isValidSubdomain(str_repeat('abcdef', 20)));
+
+    }
+}