SubdomainServiceTest.php 19 KB

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