SubdomainServiceTest.php 24 KB

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