SubdomainServiceTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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 testIsReservedSubdomain(): void {
  109. $subdomainService = $this->makeSubdomainServiceMockFor('isReservedSubdomain');
  110. $this->parameterBag
  111. ->method('get')
  112. ->with('opentalent.subdomains')
  113. ->willReturn([
  114. 'reserved' => [
  115. 'abcd',
  116. 'abc\d+'
  117. ]
  118. ]);
  119. $this->assertTrue($subdomainService->isReservedSubdomain('abcd'));
  120. $this->assertTrue($subdomainService->isReservedSubdomain('abc123'));
  121. $this->assertFalse($subdomainService->isReservedSubdomain('abcde'));
  122. $this->assertFalse($subdomainService->isReservedSubdomain('abc'));
  123. $this->assertFalse($subdomainService->isReservedSubdomain('foo'));
  124. }
  125. public function testAddNewSubdomain(): void {
  126. $subdomainService = $this->makeSubdomainServiceMockFor('addNewSubdomain');
  127. $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
  128. $subdomainService->expects(self::once())->method('isValidSubdomain')->with('sub')->willReturn(True);
  129. $subdomainService->expects(self::once())->method('canRegisterNewSubdomain')->with($organization)->willReturn(True);
  130. $subdomainService->expects(self::once())->method('isReservedSubdomain')->with('sub')->willReturn(false);
  131. $this->subdomainRepository->expects(self::once())->method('findBy')->with(['subdomain' => 'sub'])->willReturn(0);
  132. $this->entityManager->expects(self::once())->method('persist');
  133. $this->entityManager->expects(self::once())->method('flush');
  134. $this->bindFileService->expects(self::once())->method('registerSubdomain')->with('sub');
  135. // Subdomain is not activated by default
  136. $subdomainService->expects(self::never())->method('activateSubdomain');
  137. $subdomain = $subdomainService->addNewSubdomain($organization, 'sub');
  138. $this->assertEquals($subdomain->getOrganization(), $organization);
  139. $this->assertEquals($subdomain->getSubdomain(), 'sub');
  140. $this->assertFalse($subdomain->isActive());
  141. }
  142. public function testAddNewSubdomainInvalid(): void
  143. {
  144. $subdomainService = $this->makeSubdomainServiceMockFor('addNewSubdomain');
  145. $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
  146. $subdomainService->expects(self::once())->method('isValidSubdomain')->with('_sub')->willReturn(False);
  147. $subdomainService->expects(self::never())->method('canRegisterNewSubdomain')->with($organization)->willReturn(True);
  148. $subdomainService->expects(self::never())->method('isReservedSubdomain')->with($organization)->willReturn(false);
  149. $this->subdomainRepository->expects(self::never())->method('findBy')->with(['subdomain' => '_sub'])->willReturn(0);
  150. $this->entityManager->expects(self::never())->method('persist');
  151. $this->entityManager->expects(self::never())->method('flush');
  152. $this->bindFileService->expects(self::never())->method('registerSubdomain')->with('_sub');
  153. $subdomainService->expects(self::never())->method('activateSubdomain');
  154. $this->expectException(\RuntimeException::class);
  155. $this->expectExceptionMessage('Not a valid subdomain');
  156. $subdomainService->addNewSubdomain($organization, '_sub');
  157. }
  158. public function testAddNewSubdomainMaxReached(): void
  159. {
  160. $subdomainService = $this->makeSubdomainServiceMockFor('addNewSubdomain');
  161. $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
  162. $subdomainService->expects(self::once())->method('isValidSubdomain')->with('_sub')->willReturn(true);
  163. $subdomainService->expects(self::once())->method('canRegisterNewSubdomain')->with($organization)->willReturn(false);
  164. $subdomainService->expects(self::never())->method('isReservedSubdomain')->with($organization)->willReturn(false);
  165. $this->subdomainRepository->expects(self::never())->method('findBy')->with(['subdomain' => '_sub'])->willReturn(0);
  166. $this->entityManager->expects(self::never())->method('persist');
  167. $this->entityManager->expects(self::never())->method('flush');
  168. $this->bindFileService->expects(self::never())->method('registerSubdomain')->with('_sub');
  169. $subdomainService->expects(self::never())->method('activateSubdomain');
  170. $this->expectException(\RuntimeException::class);
  171. $this->expectExceptionMessage('This organization can not register new subdomains');
  172. $subdomainService->addNewSubdomain($organization, '_sub');
  173. }
  174. public function testAddNewSubdomainIsReserved(): void
  175. {
  176. $subdomainService = $this->makeSubdomainServiceMockFor('addNewSubdomain');
  177. $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
  178. $subdomainService->expects(self::once())->method('isValidSubdomain')->with('_sub')->willReturn(true);
  179. $subdomainService->expects(self::once())->method('canRegisterNewSubdomain')->with($organization)->willReturn(true);
  180. $subdomainService->expects(self::once())->method('isReservedSubdomain')->with('_sub')->willReturn(true);
  181. $this->subdomainRepository->expects(self::never())->method('findBy')->with(['subdomain' => '_sub'])->willReturn(0);
  182. $this->entityManager->expects(self::never())->method('persist');
  183. $this->entityManager->expects(self::never())->method('flush');
  184. $this->bindFileService->expects(self::never())->method('registerSubdomain')->with('_sub');
  185. $subdomainService->expects(self::never())->method('activateSubdomain');
  186. $this->expectException(\RuntimeException::class);
  187. $this->expectExceptionMessage('This subdomain is not available');
  188. $subdomainService->addNewSubdomain($organization, '_sub');
  189. }
  190. public function testAddNewSubdomainExisting(): void
  191. {
  192. $subdomainService = $this->makeSubdomainServiceMockFor('addNewSubdomain');
  193. $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
  194. $subdomainService->expects(self::once())->method('isValidSubdomain')->with('sub')->willReturn(true);
  195. $subdomainService->expects(self::once())->method('canRegisterNewSubdomain')->with($organization)->willReturn(true);
  196. $subdomainService->expects(self::once())->method('isReservedSubdomain')->with('sub')->willReturn(false);
  197. $this->subdomainRepository->expects(self::once())->method('findBy')->with(['subdomain' => 'sub'])->willReturn(1);
  198. $this->entityManager->expects(self::never())->method('persist');
  199. $this->entityManager->expects(self::never())->method('flush');
  200. $this->bindFileService->expects(self::never())->method('registerSubdomain')->with('sub');
  201. $subdomainService->expects(self::never())->method('activateSubdomain');
  202. $this->expectException(\RuntimeException::class);
  203. $this->expectExceptionMessage('This subdomain is already registered');
  204. $subdomainService->addNewSubdomain($organization, 'sub');
  205. }
  206. public function testAddNewSubdomainAndActivate(): void
  207. {
  208. $subdomainService = $this->makeSubdomainServiceMockFor('addNewSubdomain');
  209. $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
  210. $subdomainService->expects(self::once())->method('isValidSubdomain')->with('sub')->willReturn(true);
  211. $subdomainService->expects(self::once())->method('canRegisterNewSubdomain')->with($organization)->willReturn(true);
  212. $subdomainService->expects(self::once())->method('isReservedSubdomain')->with('sub')->willReturn(false);
  213. $this->subdomainRepository->expects(self::once())->method('findBy')->with(['subdomain' => 'sub'])->willReturn(0);
  214. $subdomainService->expects(self::once())->method('activateSubdomain');
  215. $subdomainService->addNewSubdomain($organization, 'sub', true);
  216. }
  217. public function testActivateSubdomain(): void {
  218. $subdomainService = $this->makeSubdomainServiceMockFor('activateSubdomain');
  219. $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
  220. $initialSubdomain = $this->getMockBuilder(Subdomain::class)->getMock();
  221. $initialSubdomain->method('getId')->willReturn(1);
  222. $initialSubdomain->method('isActive')->willReturn(false);
  223. $activatedSubdomain = $this->getMockBuilder(Subdomain::class)->getMock();
  224. $activatedSubdomain->method('getId')->willReturn(1);
  225. $activatedSubdomain->method('isActive')->willReturn(true);
  226. $activatedSubdomain->method('getOrganization')->willReturn($organization);
  227. $subdomainService
  228. ->expects(self::once())
  229. ->method('setOrganizationActiveSubdomain')
  230. ->with($initialSubdomain)
  231. ->willReturn($activatedSubdomain);
  232. $subdomainService
  233. ->expects(self::once())
  234. ->method('renameAdminUserToMatchSubdomain')
  235. ->with($activatedSubdomain);
  236. $subdomainService
  237. ->expects(self::once())
  238. ->method('updateTypo3Website')
  239. ->with($organization);
  240. $subdomainService
  241. ->expects(self::once())
  242. ->method('sendConfirmationEmail')
  243. ->with($activatedSubdomain);
  244. $result = $subdomainService->activateSubdomain($initialSubdomain);
  245. $this->assertEquals($result, $activatedSubdomain);
  246. }
  247. public function testActivateSubdomainAlreadyActive(): void
  248. {
  249. $subdomainService = $this->makeSubdomainServiceMockFor('activateSubdomain');
  250. $subdomain = $this->getMockBuilder(Subdomain::class)->getMock();
  251. $subdomain->method('getId')->willReturn(1);
  252. $subdomain->method('isActive')->willReturn(true);
  253. $subdomainService->expects(self::never())->method('setOrganizationActiveSubdomain');
  254. $subdomainService->expects(self::never())->method('renameAdminUserToMatchSubdomain');
  255. $subdomainService->expects(self::never())->method('updateTypo3Website');
  256. $subdomainService->expects(self::never())->method('sendConfirmationEmail');
  257. $this->expectException(\RuntimeException::class);
  258. $this->expectExceptionMessage('The subdomain is already active');
  259. $subdomainService->activateSubdomain($subdomain);
  260. }
  261. public function testActivateSubdomainNotPersisted(): void
  262. {
  263. $subdomainService = $this->makeSubdomainServiceMockFor('activateSubdomain');
  264. $subdomain = $this->getMockBuilder(Subdomain::class)->getMock();
  265. $subdomain->method('getId')->willReturn(null);
  266. $subdomain->method('isActive')->willReturn(false);
  267. $subdomainService->expects(self::never())->method('setOrganizationActiveSubdomain');
  268. $subdomainService->expects(self::never())->method('renameAdminUserToMatchSubdomain');
  269. $subdomainService->expects(self::never())->method('updateTypo3Website');
  270. $subdomainService->expects(self::never())->method('sendConfirmationEmail');
  271. $this->expectException(\RuntimeException::class);
  272. $this->expectExceptionMessage('Can not activate a non-persisted subdomain');
  273. $subdomainService->activateSubdomain($subdomain);
  274. }
  275. public function testSetOrganizationActiveSubdomain(): void {
  276. $subdomainService = $this->makeSubdomainServiceMockFor('setOrganizationActiveSubdomain');
  277. $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
  278. $subdomain = $this->getMockBuilder(Subdomain::class)->getMock();
  279. $subdomain->method('getId')->willReturn(1);
  280. $subdomain->method('isActive')->willReturn(false);
  281. $otherSubdomain1 = $this->getMockBuilder(Subdomain::class)->getMock();
  282. $otherSubdomain1->method('getId')->willReturn(2);
  283. $otherSubdomain1->method('isActive')->willReturn(true);
  284. $otherSubdomain2 = $this->getMockBuilder(Subdomain::class)->getMock();
  285. $otherSubdomain2->method('getId')->willReturn(3);
  286. $otherSubdomain2->method('isActive')->willReturn(false);
  287. $organization->method('getSubdomains')->willReturn(new ArrayCollection([$otherSubdomain1, $otherSubdomain2]));
  288. $subdomain->method('getOrganization')->willReturn($organization);
  289. // The active subdomain is deactivated
  290. $otherSubdomain1->expects(self::once())->method('setActive')->with(false);
  291. // The inactive subdomain is not modified
  292. $otherSubdomain2->expects(self::never())->method('setActive');
  293. // The new subdomain is activated
  294. $subdomain->expects(self::once())->method('setActive')->with(true);
  295. $this->entityManager->expects(self::once())->method('flush');
  296. $this->entityManager->expects(self::once())->method('refresh')->with($organization);
  297. $result = $subdomainService->setOrganizationActiveSubdomain($subdomain);
  298. $this->assertEquals($result, $subdomain);
  299. }
  300. public function testRenameAdminUserToMatchSubdomain(): void {
  301. $subdomainService = $this->makeSubdomainServiceMockFor('renameAdminUserToMatchSubdomain');
  302. $organization = $this->getMockBuilder(Organization::class)->getMock();
  303. $person = $this->getMockBuilder(Person::class)->getMock();
  304. $access = $this->getMockBuilder(Access::class)->getMock();
  305. $subdomain = $this->getMockBuilder(Subdomain::class)->getMock();
  306. $this->accessRepository
  307. ->method('findAdminAccess')
  308. ->with($organization)
  309. ->willReturn($access);
  310. $subdomain->method('getSubdomain')->willReturn('sub');
  311. $subdomain->method('getOrganization')->willReturn($organization);
  312. $access->method('getPerson')->willReturn($person);
  313. $person->expects(self::once())->method('setUsername')->with('adminsub');
  314. $this->entityManager->expects(self::once())->method('flush');
  315. $subdomainService->renameAdminUserToMatchSubdomain($subdomain);
  316. }
  317. public function testUpdateTypo3Website(): void {
  318. $subdomainService = $this->makeSubdomainServiceMockFor('updateTypo3Website');
  319. $organization = $this->getMockBuilder(Organization::class)->getMock();
  320. $organization->method('getId')->willReturn(1);
  321. $this->messageBus
  322. ->expects(self::once())
  323. ->method('dispatch')
  324. ->with(self::isInstanceOf(Typo3UpdateCommand::class))
  325. ->willReturn(new Envelope(new Typo3UpdateCommand(1)));
  326. $subdomainService->updateTypo3Website($organization);
  327. }
  328. public function testGetMailModel(): void {
  329. $subdomainService = $this->makeSubdomainServiceMockFor('getMailModel');
  330. $access = $this->getMockBuilder(Access::class)->getMock();
  331. $access->method('getId')->willReturn(1);
  332. $organization = $this->getMockBuilder(Organization::class)->getMock();
  333. $organization->method('getId')->willReturn(1);
  334. $subdomain = $this->getMockBuilder(Subdomain::class)->disableOriginalConstructor()->getMock();
  335. $subdomain->method('getOrganization')->willReturn($organization);
  336. $subdomain->method('getId')->willReturn(1);
  337. $this->accessRepository
  338. ->method('findAdminAccess')
  339. ->with($organization)
  340. ->willReturn($access);
  341. $this->organizationUtils
  342. ->expects(self::once())
  343. ->method('getOrganizationWebsite')
  344. ->with($organization)
  345. ->willReturn('mysubdomain.opentalent.fr');
  346. $mailerModel = $subdomainService->getMailModel($subdomain);
  347. $this->assertInstanceOf(SubdomainChangeModel::class, $mailerModel);
  348. $this->assertEquals(1, $mailerModel->getSenderId());
  349. $this->assertEquals(1, $mailerModel->getOrganizationId());
  350. $this->assertEquals(1, $mailerModel->getSubdomainId());
  351. $this->assertEquals('mysubdomain.opentalent.fr', $mailerModel->getUrl());
  352. }
  353. public function testSendConfirmationEmail(): void {
  354. $subdomainService = $this->makeSubdomainServiceMockFor('sendConfirmationEmail');
  355. $subdomain = $this->getMockBuilder(Subdomain::class)->getMock();
  356. $subdomainChangeModel = $this->getMockBuilder(SubdomainChangeModel::class)->getMock();
  357. $subdomainService->method('getMailModel')->willReturn($subdomainChangeModel);
  358. $this->messageBus
  359. ->expects(self::once())
  360. ->method('dispatch')
  361. ->with(self::isInstanceOf(MailerCommand::class))
  362. ->willReturn(new Envelope(new MailerCommand($subdomainChangeModel)));
  363. $subdomainService->sendConfirmationEmail($subdomain);
  364. }
  365. }