SubdomainServiceTest.php 19 KB

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