SubdomainServiceTest.php 22 KB

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