SubdomainServiceTest.php 19 KB

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