SubdomainServiceTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <?php
  2. namespace App\Tests\Service\Typo3;
  3. use App\Entity\Access\Access;
  4. use App\Entity\Organization\Organization;
  5. use App\Entity\Organization\Subdomain;
  6. use App\Entity\Person\Person;
  7. use App\Message\Command\MailerCommand;
  8. use App\Message\Command\Typo3\Typo3UpdateCommand;
  9. use App\Repository\Access\AccessRepository;
  10. use App\Repository\Organization\SubdomainRepository;
  11. use App\Service\Mailer\Model\SubdomainChangeModel;
  12. use App\Service\Organization\Utils as OrganizationUtils;
  13. use App\Service\Typo3\BindFileService;
  14. use App\Service\Typo3\SubdomainService;
  15. use Doctrine\Common\Collections\ArrayCollection;
  16. use Doctrine\ORM\EntityManagerInterface;
  17. use PHPUnit\Framework\MockObject\MockObject;
  18. use PHPUnit\Framework\TestCase;
  19. use Symfony\Bundle\SecurityBundle\Security;
  20. use Symfony\Component\Messenger\Envelope;
  21. use Symfony\Component\Messenger\MessageBusInterface;
  22. class TestableSubdomainService extends SubdomainService {
  23. public function setOrganizationActiveSubdomain(Subdomain $subdomain): Subdomain {
  24. return parent::setOrganizationActiveSubdomain($subdomain);
  25. }
  26. public function renameAdminUserToMatchSubdomain(Subdomain $subdomain): void {
  27. parent::renameAdminUserToMatchSubdomain($subdomain);
  28. }
  29. public function updateTypo3Website($organization): void {
  30. parent::updateTypo3Website($organization);
  31. }
  32. public function getMailModel(Subdomain $subdomain): SubdomainChangeModel
  33. {
  34. return parent::getMailModel($subdomain);
  35. }
  36. public function sendConfirmationEmail(Subdomain $subdomain): void {
  37. parent::sendConfirmationEmail($subdomain);
  38. }
  39. }
  40. class SubdomainServiceTest extends TestCase
  41. {
  42. private SubdomainRepository $subdomainRepository;
  43. private OrganizationUtils $organizationUtils;
  44. private BindFileService $bindFileService;
  45. private MessageBusInterface $messageBus;
  46. private EntityManagerInterface $entityManager;
  47. private AccessRepository $accessRepository;
  48. public function setUp():void
  49. {
  50. $this->subdomainRepository = $this->getMockBuilder(SubdomainRepository::class)->disableOriginalConstructor()->getMock();
  51. $this->entityManager = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock();
  52. $this->messageBus = $this->getMockBuilder(MessageBusInterface::class)->disableOriginalConstructor()->getMock();
  53. $this->organizationUtils = $this->getMockBuilder(OrganizationUtils::class)->disableOriginalConstructor()->getMock();
  54. $this->bindFileService = $this->getMockBuilder(BindFileService::class)->disableOriginalConstructor()->getMock();
  55. $this->accessRepository = $this->getMockBuilder(AccessRepository::class)->disableOriginalConstructor()->getMock();
  56. }
  57. private function makeOnSubdomainChangeMock(string $methodName): MockObject | TestableSubdomainService {
  58. return $this->getMockBuilder(TestableSubdomainService::class)
  59. ->setConstructorArgs([
  60. $this->subdomainRepository,
  61. $this->entityManager,
  62. $this->messageBus,
  63. $this->organizationUtils,
  64. $this->bindFileService,
  65. $this->accessRepository
  66. ])
  67. ->setMethodsExcept([$methodName])
  68. ->getMock();
  69. }
  70. /**
  71. * @see SubdomainService::canRegisterNewSubdomain()
  72. */
  73. public function testCanRegisterNewSubdomainTrue(): void
  74. {
  75. $subdomainService = $this->makeOnSubdomainChangeMock('canRegisterNewSubdomain');
  76. $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
  77. $organization->expects(self::once())->method('getSubdomains')->willReturn(new ArrayCollection([1, 2]));
  78. $this->assertTrue($subdomainService->canRegisterNewSubdomain($organization));
  79. }
  80. /**
  81. * @see SubdomainService::canRegisterNewSubdomain()
  82. */
  83. public function testCanRegisterNewSubdomainFalse(): void
  84. {
  85. $subdomainService = $this->makeOnSubdomainChangeMock('canRegisterNewSubdomain');
  86. $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
  87. $organization->expects(self::once())->method('getSubdomains')->willReturn(new ArrayCollection([1, 2, 3]));
  88. $this->assertFalse($subdomainService->canRegisterNewSubdomain($organization));
  89. }
  90. /**
  91. * @see SubdomainService::isValidSubdomain()
  92. */
  93. public function testIsValidSubdomain(): void {
  94. $subdomainService = $this->makeOnSubdomainChangeMock('isValidSubdomain');
  95. $this->assertTrue($subdomainService->isValidSubdomain('abcd'));
  96. $this->assertTrue($subdomainService->isValidSubdomain('abcdefgh'));
  97. $this->assertTrue($subdomainService->isValidSubdomain('abcd-efgh'));
  98. $this->assertTrue($subdomainService->isValidSubdomain('123'));
  99. $this->assertTrue($subdomainService->isValidSubdomain('a'));
  100. $this->assertFalse($subdomainService->isValidSubdomain('_abc'));
  101. $this->assertFalse($subdomainService->isValidSubdomain('abc-'));
  102. $this->assertFalse($subdomainService->isValidSubdomain(str_repeat('abcdef', 20)));
  103. }
  104. public function testAddNewSubdomain(): void {
  105. $subdomainService = $this->makeOnSubdomainChangeMock('addNewSubdomain');
  106. $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
  107. $subdomainService->expects(self::once())->method('isValidSubdomain')->with('sub')->willReturn(True);
  108. $subdomainService->expects(self::once())->method('canRegisterNewSubdomain')->with($organization)->willReturn(True);
  109. $this->subdomainRepository->expects(self::once())->method('findBy')->with(['subdomain' => 'sub'])->willReturn(0);
  110. $this->entityManager->expects(self::once())->method('persist');
  111. $this->entityManager->expects(self::once())->method('flush');
  112. $this->bindFileService->expects(self::once())->method('registerSubdomain')->with('sub');
  113. // Subdomain is not activated by default
  114. $subdomainService->expects(self::never())->method('activateSubdomain');
  115. $subdomain = $subdomainService->addNewSubdomain($organization, 'sub');
  116. $this->assertEquals($subdomain->getOrganization(), $organization);
  117. $this->assertEquals($subdomain->getSubdomain(), 'sub');
  118. $this->assertFalse($subdomain->isActive());
  119. }
  120. public function testAddNewSubdomainInvalid(): void
  121. {
  122. $subdomainService = $this->makeOnSubdomainChangeMock('addNewSubdomain');
  123. $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
  124. $subdomainService->expects(self::once())->method('isValidSubdomain')->with('_sub')->willReturn(False);
  125. $subdomainService->expects(self::never())->method('canRegisterNewSubdomain')->with($organization)->willReturn(True);
  126. $this->subdomainRepository->expects(self::never())->method('findBy')->with(['subdomain' => '_sub'])->willReturn(0);
  127. $this->entityManager->expects(self::never())->method('persist');
  128. $this->entityManager->expects(self::never())->method('flush');
  129. $this->bindFileService->expects(self::never())->method('registerSubdomain')->with('_sub');
  130. $subdomainService->expects(self::never())->method('activateSubdomain');
  131. $this->expectException(\RuntimeException::class);
  132. $this->expectExceptionMessage('Not a valid subdomain');
  133. $subdomainService->addNewSubdomain($organization, '_sub');
  134. }
  135. public function testAddNewSubdomainMaxReached(): void
  136. {
  137. $subdomainService = $this->makeOnSubdomainChangeMock('addNewSubdomain');
  138. $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
  139. $subdomainService->expects(self::once())->method('isValidSubdomain')->with('_sub')->willReturn(true);
  140. $subdomainService->expects(self::once())->method('canRegisterNewSubdomain')->with($organization)->willReturn(false);
  141. $this->subdomainRepository->expects(self::never())->method('findBy')->with(['subdomain' => '_sub'])->willReturn(0);
  142. $this->entityManager->expects(self::never())->method('persist');
  143. $this->entityManager->expects(self::never())->method('flush');
  144. $this->bindFileService->expects(self::never())->method('registerSubdomain')->with('_sub');
  145. $subdomainService->expects(self::never())->method('activateSubdomain');
  146. $this->expectException(\RuntimeException::class);
  147. $this->expectExceptionMessage('This organization can not register new subdomains');
  148. $subdomainService->addNewSubdomain($organization, '_sub');
  149. }
  150. public function testAddNewSubdomainExisting(): void
  151. {
  152. $subdomainService = $this->makeOnSubdomainChangeMock('addNewSubdomain');
  153. $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
  154. $subdomainService->expects(self::once())->method('isValidSubdomain')->with('sub')->willReturn(true);
  155. $subdomainService->expects(self::once())->method('canRegisterNewSubdomain')->with($organization)->willReturn(true);
  156. $this->subdomainRepository->expects(self::once())->method('findBy')->with(['subdomain' => 'sub'])->willReturn(1);
  157. $this->entityManager->expects(self::never())->method('persist');
  158. $this->entityManager->expects(self::never())->method('flush');
  159. $this->bindFileService->expects(self::never())->method('registerSubdomain')->with('sub');
  160. $subdomainService->expects(self::never())->method('activateSubdomain');
  161. $this->expectException(\RuntimeException::class);
  162. $this->expectExceptionMessage('This subdomain is already registered');
  163. $subdomainService->addNewSubdomain($organization, 'sub');
  164. }
  165. public function testAddNewSubdomainAndActivate(): void
  166. {
  167. $subdomainService = $this->makeOnSubdomainChangeMock('addNewSubdomain');
  168. $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
  169. $subdomainService->expects(self::once())->method('isValidSubdomain')->with('sub')->willReturn(true);
  170. $subdomainService->expects(self::once())->method('canRegisterNewSubdomain')->with($organization)->willReturn(true);
  171. $this->subdomainRepository->expects(self::once())->method('findBy')->with(['subdomain' => 'sub'])->willReturn(0);
  172. $subdomainService->expects(self::once())->method('activateSubdomain');
  173. $subdomainService->addNewSubdomain($organization, 'sub', true);
  174. }
  175. public function testActivateSubdomain(): void {
  176. $subdomainService = $this->makeOnSubdomainChangeMock('activateSubdomain');
  177. $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
  178. $initialSubdomain = $this->getMockBuilder(Subdomain::class)->getMock();
  179. $initialSubdomain->method('getId')->willReturn(1);
  180. $initialSubdomain->method('isActive')->willReturn(false);
  181. $activatedSubdomain = $this->getMockBuilder(Subdomain::class)->getMock();
  182. $activatedSubdomain->method('getId')->willReturn(1);
  183. $activatedSubdomain->method('isActive')->willReturn(true);
  184. $activatedSubdomain->method('getOrganization')->willReturn($organization);
  185. $subdomainService
  186. ->expects(self::once())
  187. ->method('setOrganizationActiveSubdomain')
  188. ->with($initialSubdomain)
  189. ->willReturn($activatedSubdomain);
  190. $subdomainService
  191. ->expects(self::once())
  192. ->method('renameAdminUserToMatchSubdomain')
  193. ->with($activatedSubdomain);
  194. $subdomainService
  195. ->expects(self::once())
  196. ->method('updateTypo3Website')
  197. ->with($organization);
  198. $subdomainService
  199. ->expects(self::once())
  200. ->method('sendConfirmationEmail')
  201. ->with($activatedSubdomain);
  202. $result = $subdomainService->activateSubdomain($initialSubdomain);
  203. $this->assertEquals($result, $activatedSubdomain);
  204. }
  205. public function testActivateSubdomainAlreadyActive(): void
  206. {
  207. $subdomainService = $this->makeOnSubdomainChangeMock('activateSubdomain');
  208. $subdomain = $this->getMockBuilder(Subdomain::class)->getMock();
  209. $subdomain->method('getId')->willReturn(1);
  210. $subdomain->method('isActive')->willReturn(true);
  211. $subdomainService->expects(self::never())->method('setOrganizationActiveSubdomain');
  212. $subdomainService->expects(self::never())->method('renameAdminUserToMatchSubdomain');
  213. $subdomainService->expects(self::never())->method('updateTypo3Website');
  214. $subdomainService->expects(self::never())->method('sendConfirmationEmail');
  215. $this->expectException(\RuntimeException::class);
  216. $this->expectExceptionMessage('The subdomain is already active');
  217. $subdomainService->activateSubdomain($subdomain);
  218. }
  219. public function testActivateSubdomainNotPersisted(): void
  220. {
  221. $subdomainService = $this->makeOnSubdomainChangeMock('activateSubdomain');
  222. $subdomain = $this->getMockBuilder(Subdomain::class)->getMock();
  223. $subdomain->method('getId')->willReturn(null);
  224. $subdomain->method('isActive')->willReturn(false);
  225. $subdomainService->expects(self::never())->method('setOrganizationActiveSubdomain');
  226. $subdomainService->expects(self::never())->method('renameAdminUserToMatchSubdomain');
  227. $subdomainService->expects(self::never())->method('updateTypo3Website');
  228. $subdomainService->expects(self::never())->method('sendConfirmationEmail');
  229. $this->expectException(\RuntimeException::class);
  230. $this->expectExceptionMessage('Can not activate a non-persisted subdomain');
  231. $subdomainService->activateSubdomain($subdomain);
  232. }
  233. public function testSetOrganizationActiveSubdomain(): void {
  234. $subdomainService = $this->makeOnSubdomainChangeMock('setOrganizationActiveSubdomain');
  235. $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
  236. $subdomain = $this->getMockBuilder(Subdomain::class)->getMock();
  237. $subdomain->method('getId')->willReturn(1);
  238. $subdomain->method('isActive')->willReturn(false);
  239. $otherSubdomain1 = $this->getMockBuilder(Subdomain::class)->getMock();
  240. $otherSubdomain1->method('getId')->willReturn(2);
  241. $otherSubdomain1->method('isActive')->willReturn(true);
  242. $otherSubdomain2 = $this->getMockBuilder(Subdomain::class)->getMock();
  243. $otherSubdomain2->method('getId')->willReturn(3);
  244. $otherSubdomain2->method('isActive')->willReturn(false);
  245. $organization->method('getSubdomains')->willReturn(new ArrayCollection([$otherSubdomain1, $otherSubdomain2]));
  246. $subdomain->method('getOrganization')->willReturn($organization);
  247. // The active subdomain is deactivated
  248. $otherSubdomain1->expects(self::once())->method('setActive')->with(false);
  249. // The inactive subdomain is not modified
  250. $otherSubdomain2->expects(self::never())->method('setActive');
  251. // The new subdomain is activated
  252. $subdomain->expects(self::once())->method('setActive')->with(true);
  253. $this->entityManager->expects(self::once())->method('flush');
  254. $this->entityManager->expects(self::once())->method('refresh')->with($organization);
  255. $result = $subdomainService->setOrganizationActiveSubdomain($subdomain);
  256. $this->assertEquals($result, $subdomain);
  257. }
  258. public function testRenameAdminUserToMatchSubdomain(): void {
  259. $subdomainService = $this->makeOnSubdomainChangeMock('renameAdminUserToMatchSubdomain');
  260. $organization = $this->getMockBuilder(Organization::class)->getMock();
  261. $person = $this->getMockBuilder(Person::class)->getMock();
  262. $access = $this->getMockBuilder(Access::class)->getMock();
  263. $subdomain = $this->getMockBuilder(Subdomain::class)->getMock();
  264. $this->accessRepository
  265. ->method('findAdminAccess')
  266. ->with($organization)
  267. ->willReturn($access);
  268. $subdomain->method('getSubdomain')->willReturn('sub');
  269. $subdomain->method('getOrganization')->willReturn($organization);
  270. $access->method('getPerson')->willReturn($person);
  271. $person->expects(self::once())->method('setUsername')->with('adminsub');
  272. $this->entityManager->expects(self::once())->method('flush');
  273. $subdomainService->renameAdminUserToMatchSubdomain($subdomain);
  274. }
  275. public function testUpdateTypo3Website(): void {
  276. $subdomainService = $this->makeOnSubdomainChangeMock('updateTypo3Website');
  277. $organization = $this->getMockBuilder(Organization::class)->getMock();
  278. $organization->method('getId')->willReturn(1);
  279. $this->messageBus
  280. ->expects(self::once())
  281. ->method('dispatch')
  282. ->with(self::isInstanceOf(Typo3UpdateCommand::class))
  283. ->willReturn(new Envelope(new Typo3UpdateCommand(1)));
  284. $subdomainService->updateTypo3Website($organization);
  285. }
  286. public function testGetMailModel(): void {
  287. $subdomainService = $this->makeOnSubdomainChangeMock('getMailModel');
  288. $access = $this->getMockBuilder(Access::class)->getMock();
  289. $access->method('getId')->willReturn(1);
  290. $organization = $this->getMockBuilder(Organization::class)->getMock();
  291. $organization->method('getId')->willReturn(1);
  292. $subdomain = $this->getMockBuilder(Subdomain::class)->disableOriginalConstructor()->getMock();
  293. $subdomain->method('getOrganization')->willReturn($organization);
  294. $subdomain->method('getId')->willReturn(1);
  295. $this->accessRepository
  296. ->method('findAdminAccess')
  297. ->with($organization)
  298. ->willReturn($access);
  299. $this->organizationUtils
  300. ->expects(self::once())
  301. ->method('getOrganizationWebsite')
  302. ->with($organization)
  303. ->willReturn('mysubdomain.opentalent.fr');
  304. $mailerModel = $subdomainService->getMailModel($subdomain);
  305. $this->assertInstanceOf(SubdomainChangeModel::class, $mailerModel);
  306. $this->assertEquals(1, $mailerModel->getSenderId());
  307. $this->assertEquals(1, $mailerModel->getOrganizationId());
  308. $this->assertEquals(1, $mailerModel->getSubdomainId());
  309. $this->assertEquals('mysubdomain.opentalent.fr', $mailerModel->getUrl());
  310. }
  311. public function testSendConfirmationEmail(): void {
  312. $subdomainService = $this->makeOnSubdomainChangeMock('sendConfirmationEmail');
  313. $subdomain = $this->getMockBuilder(Subdomain::class)->getMock();
  314. $subdomainChangeModel = $this->getMockBuilder(SubdomainChangeModel::class)->getMock();
  315. $subdomainService->method('getMailModel')->willReturn($subdomainChangeModel);
  316. $this->messageBus
  317. ->expects(self::once())
  318. ->method('dispatch')
  319. ->with(self::isInstanceOf(MailerCommand::class))
  320. ->willReturn(new Envelope(new MailerCommand($subdomainChangeModel)));
  321. $subdomainService->sendConfirmationEmail($subdomain);
  322. }
  323. }