SubdomainServiceTest.php 19 KB

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