OrganizationFactoryTest.php 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Tests\Unit\Service\Organization;
  4. use App\ApiResources\Organization\OrganizationCreationRequest;
  5. use App\ApiResources\Organization\OrganizationMemberCreationRequest;
  6. use App\Entity\Access\Access;
  7. use App\Entity\Core\ContactPoint;
  8. use App\Entity\Core\Country;
  9. use App\Entity\Education\Cycle;
  10. use App\Entity\Network\Network;
  11. use App\Entity\Network\NetworkOrganization;
  12. use App\Entity\Organization\Organization;
  13. use App\Entity\Organization\OrganizationAddressPostal;
  14. use App\Entity\Organization\Parameters;
  15. use App\Entity\Organization\Settings;
  16. use App\Entity\Organization\Subdomain;
  17. use App\Entity\Person\Person;
  18. use App\Entity\Person\PersonAddressPostal;
  19. use App\Enum\Core\ContactPointTypeEnum;
  20. use App\Enum\Education\CycleEnum;
  21. use App\Enum\Network\NetworkEnum;
  22. use App\Enum\Organization\AddressPostalOrganizationTypeEnum;
  23. use App\Enum\Organization\LegalEnum;
  24. use App\Enum\Organization\PrincipalTypeEnum;
  25. use App\Enum\Organization\SettingsProductEnum;
  26. use App\Enum\Person\AddressPostalPersonTypeEnum;
  27. use App\Enum\Person\GenderEnum;
  28. use App\Repository\Core\CountryRepository;
  29. use App\Repository\Organization\OrganizationIdentificationRepository;
  30. use App\Repository\Organization\OrganizationRepository;
  31. use App\Repository\Person\PersonRepository;
  32. use App\Service\ApiLegacy\ApiLegacyRequestService;
  33. use App\Service\Dolibarr\DolibarrApiService;
  34. use App\Service\Organization\OrganizationFactory;
  35. use App\Service\Organization\Utils as OrganizationUtils;
  36. use App\Service\Typo3\BindFileService;
  37. use App\Service\Typo3\SubdomainService;
  38. use App\Service\Typo3\Typo3Service;
  39. use App\Service\Utils\DatesUtils;
  40. use Doctrine\ORM\EntityManagerInterface;
  41. use libphonenumber\PhoneNumber;
  42. use libphonenumber\PhoneNumberUtil;
  43. use PHPUnit\Framework\MockObject\MockObject;
  44. use PHPUnit\Framework\TestCase;
  45. use Psr\Log\LoggerInterface;
  46. use Symfony\Component\HttpFoundation\Response;
  47. use Symfony\Contracts\HttpClient\ResponseInterface;
  48. class TestableOrganizationFactory extends OrganizationFactory
  49. {
  50. public function setPhoneNumberUtil(PhoneNumberUtil $phoneNumberUtil): void
  51. {
  52. $this->phoneNumberUtil = $phoneNumberUtil;
  53. }
  54. public function interruptIfOrganizationExists(OrganizationCreationRequest $organizationCreationRequest): void
  55. {
  56. parent::interruptIfOrganizationExists($organizationCreationRequest);
  57. }
  58. public function validateSubdomain(string $subdomainValue): void
  59. {
  60. parent::validateSubdomain($subdomainValue);
  61. }
  62. public function makeOrganizationWithRelations(
  63. OrganizationCreationRequest $organizationCreationRequest,
  64. ): Organization {
  65. return parent::makeOrganizationWithRelations($organizationCreationRequest);
  66. }
  67. public function makeOrganization(OrganizationCreationRequest $organizationCreationRequest): Organization
  68. {
  69. return parent::makeOrganization($organizationCreationRequest);
  70. }
  71. public function makeParameters(OrganizationCreationRequest $organizationCreationRequest): Parameters
  72. {
  73. return parent::makeParameters($organizationCreationRequest);
  74. }
  75. public function makeSettings(OrganizationCreationRequest $organizationCreationRequest): Settings
  76. {
  77. return parent::makeSettings($organizationCreationRequest);
  78. }
  79. public function makePostalAddress(OrganizationCreationRequest $organizationCreationRequest): OrganizationAddressPostal
  80. {
  81. return parent::makePostalAddress($organizationCreationRequest);
  82. }
  83. public function makeContactPoint(OrganizationCreationRequest $organizationCreationRequest): ContactPoint
  84. {
  85. return parent::makeContactPoint($organizationCreationRequest);
  86. }
  87. public function makeNetworkOrganization(OrganizationCreationRequest $organizationCreationRequest): NetworkOrganization
  88. {
  89. return parent::makeNetworkOrganization($organizationCreationRequest);
  90. }
  91. public function makeAdminAccess(OrganizationCreationRequest $organizationCreationRequest): Access
  92. {
  93. return parent::makeAdminAccess($organizationCreationRequest);
  94. }
  95. public function makeCycles(): array
  96. {
  97. return parent::makeCycles();
  98. }
  99. public function makeAccess(int|OrganizationMemberCreationRequest $creationRequestData): Access
  100. {
  101. return parent::makeAccess($creationRequestData);
  102. }
  103. public function makePersonPostalAddress(OrganizationMemberCreationRequest $organizationMemberCreationRequest): PersonAddressPostal
  104. {
  105. return parent::makePersonPostalAddress($organizationMemberCreationRequest);
  106. }
  107. public function makePersonContactPoint(OrganizationMemberCreationRequest $organizationMemberCreationRequest): ContactPoint
  108. {
  109. return parent::makePersonContactPoint($organizationMemberCreationRequest);
  110. }
  111. public function makeSubdomain(OrganizationCreationRequest $organizationCreationRequest): Subdomain
  112. {
  113. return parent::makeSubdomain($organizationCreationRequest);
  114. }
  115. public function createTypo3Website(Organization $organization): ?int
  116. {
  117. return parent::createTypo3Website($organization);
  118. }
  119. public function updateAdminassosDb(Organization $organization): void
  120. {
  121. parent::updateAdminassosDb($organization);
  122. }
  123. public function normalizeIdentificationField(string $value): string
  124. {
  125. return parent::normalizeIdentificationField($value);
  126. }
  127. }
  128. class OrganizationFactoryTest extends TestCase
  129. {
  130. private readonly MockObject|SubdomainService $subdomainService;
  131. private readonly MockObject|OrganizationRepository $organizationRepository;
  132. private readonly MockObject|CountryRepository $countryRepository;
  133. private readonly MockObject|OrganizationUtils $organizationUtils;
  134. private readonly MockObject|Typo3Service $typo3Service;
  135. private readonly MockObject|DolibarrApiService $dolibarrApiService;
  136. private readonly MockObject|EntityManagerInterface $entityManager;
  137. private readonly MockObject|PersonRepository $personRepository;
  138. private readonly MockObject|BindFileService $bindFileService;
  139. private readonly MockObject|LoggerInterface $logger;
  140. private readonly MockObject|OrganizationIdentificationRepository $organizationIdentificationRepository;
  141. private readonly MockObject|ApiLegacyRequestService $apiLegacyRequestService;
  142. private readonly MockObject|PhoneNumberUtil $phoneNumberUtil;
  143. public function setUp(): void
  144. {
  145. $this->subdomainService = $this->getMockBuilder(SubdomainService::class)->disableOriginalConstructor()->getMock();
  146. $this->organizationRepository = $this->getMockBuilder(OrganizationRepository::class)->disableOriginalConstructor()->getMock();
  147. $this->countryRepository = $this->getMockBuilder(CountryRepository::class)->disableOriginalConstructor()->getMock();
  148. $this->organizationUtils = $this->getMockBuilder(OrganizationUtils::class)->disableOriginalConstructor()->getMock();
  149. $this->typo3Service = $this->getMockBuilder(Typo3Service::class)->disableOriginalConstructor()->getMock();
  150. $this->dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)->disableOriginalConstructor()->getMock();
  151. $this->entityManager = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock();
  152. $this->personRepository = $this->getMockBuilder(PersonRepository::class)->disableOriginalConstructor()->getMock();
  153. $this->bindFileService = $this->getMockBuilder(BindFileService::class)->disableOriginalConstructor()->getMock();
  154. $this->logger = $this->getMockBuilder(LoggerInterface::class)->disableOriginalConstructor()->getMock();
  155. $this->organizationIdentificationRepository = $this->getMockBuilder(OrganizationIdentificationRepository::class)->disableOriginalConstructor()->getMock();
  156. $this->apiLegacyRequestService = $this->getMockBuilder(ApiLegacyRequestService::class)->disableOriginalConstructor()->getMock();
  157. $this->phoneNumberUtil = $this->getMockBuilder(PhoneNumberUtil::class)->disableOriginalConstructor()->getMock();
  158. }
  159. public function tearDown(): void
  160. {
  161. DatesUtils::clearFakeDatetime();
  162. }
  163. private function getOrganizationFactoryMockFor(string $methodName): TestableOrganizationFactory|MockObject
  164. {
  165. $organizationFactory = $this
  166. ->getMockBuilder(TestableOrganizationFactory::class)
  167. ->setConstructorArgs(
  168. [
  169. $this->subdomainService,
  170. $this->organizationRepository,
  171. $this->countryRepository,
  172. $this->organizationUtils,
  173. $this->typo3Service,
  174. $this->dolibarrApiService,
  175. $this->entityManager,
  176. $this->personRepository,
  177. $this->bindFileService,
  178. $this->organizationIdentificationRepository,
  179. $this->apiLegacyRequestService,
  180. ])
  181. ->setMethodsExcept(['setLoggerInterface', 'setPhoneNumberUtil', $methodName])
  182. ->getMock();
  183. $organizationFactory->setLoggerInterface($this->logger);
  184. $organizationFactory->setPhoneNumberUtil($this->phoneNumberUtil);
  185. return $organizationFactory;
  186. }
  187. public function testCreate(): void
  188. {
  189. $organizationFactory = $this->getOrganizationFactoryMockFor('create');
  190. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  191. $organizationCreationRequest->method('getName')->willReturn('foo');
  192. $organizationCreationRequest->method('getSubdomain')->willReturn('subdomain');
  193. $organizationCreationRequest->method('isClient')->willReturn(false);
  194. $organizationCreationRequest->method('getCreateWebsite')->willReturn(true);
  195. $this->entityManager->expects(self::once())->method('beginTransaction');
  196. $organizationFactory->expects(self::once())->method('interruptIfOrganizationExists');
  197. $organizationFactory->expects(self::once())->method('validateSubdomain')->with('subdomain');
  198. $organization = $this->getMockBuilder(Organization::class)->getMock();
  199. $organizationFactory
  200. ->expects(self::once())
  201. ->method('makeOrganizationWithRelations')
  202. ->with($organizationCreationRequest)
  203. ->willReturn($organization);
  204. $this->entityManager->expects(self::once())->method('persist')->with($organization);
  205. $this->entityManager->expects(self::once())->method('flush');
  206. $this->entityManager->expects(self::once())->method('commit');
  207. $this->dolibarrApiService
  208. ->expects(self::once())
  209. ->method('createSociety')
  210. ->with($organization, false)
  211. ->willReturn(456);
  212. $this->bindFileService
  213. ->expects(self::once())
  214. ->method('registerSubdomain')
  215. ->with('subdomain');
  216. $organizationFactory
  217. ->expects(self::once())
  218. ->method('createTypo3Website')
  219. ->with($organization)
  220. ->willReturn(789);
  221. $this->logger
  222. ->method('info')
  223. ->withConsecutive(
  224. ["Start the creation of a new organization named 'foo'"],
  225. ["Subdomain is valid and available : 'subdomain'"],
  226. ['Organization created with all its relations'],
  227. ['Organization persisted in the DB'],
  228. ['New dolibarr structure created (uid : 456)'],
  229. ['Subdomain registered'],
  230. ['Typo3 website created (root uid: 789)'],
  231. ['Adminassos db updated']
  232. );
  233. $organizationCreationRequest
  234. ->expects(self::once())
  235. ->method('setStatus')
  236. ->with(OrganizationCreationRequest::STATUS_OK);
  237. $result = $organizationFactory->create($organizationCreationRequest);
  238. $this->assertEquals(
  239. $organization,
  240. $result
  241. );
  242. }
  243. public function testCreateWithRollback(): void
  244. {
  245. $organizationFactory = $this->getOrganizationFactoryMockFor('create');
  246. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  247. $organizationCreationRequest->method('getName')->willReturn('foo');
  248. $organizationCreationRequest->method('getSubdomain')->willReturn('subdomain');
  249. $organizationCreationRequest->method('isClient')->willReturn(false);
  250. $organizationCreationRequest->method('getCreateWebsite')->willReturn(true);
  251. $this->entityManager->expects(self::once())->method('beginTransaction');
  252. $organizationFactory->expects(self::once())->method('interruptIfOrganizationExists');
  253. $organizationFactory->expects(self::once())->method('validateSubdomain')->with('subdomain');
  254. $organization = $this->getMockBuilder(Organization::class)->getMock();
  255. $organizationFactory
  256. ->expects(self::once())
  257. ->method('makeOrganizationWithRelations')
  258. ->with($organizationCreationRequest)
  259. ->willReturn($organization);
  260. $this->entityManager->expects(self::once())->method('persist')->with($organization);
  261. $this->entityManager->expects(self::once())->method('flush')->willThrowException(new \RuntimeException('some error'));
  262. $this->entityManager->expects(self::once())->method('rollback');
  263. $this->dolibarrApiService
  264. ->expects(self::never())
  265. ->method('createSociety');
  266. $this->bindFileService
  267. ->expects(self::never())
  268. ->method('registerSubdomain');
  269. $organizationFactory
  270. ->expects(self::never())
  271. ->method('createTypo3Website');
  272. $this->logger
  273. ->method('info')
  274. ->withConsecutive(
  275. ["Start the creation of a new organization named 'foo'"],
  276. ["Subdomain is valid and available : 'subdomain'"],
  277. ['Organization created with all its relations']
  278. );
  279. $this->logger
  280. ->method('critical')
  281. ->with(
  282. $this->matchesRegularExpression('/^An error happened, operation cancelled\nRuntimeException: some error.*/')
  283. );
  284. $this->expectException(\RuntimeException::class);
  285. $result = $organizationFactory->create($organizationCreationRequest);
  286. }
  287. public function testCreateWithExistingOrganization(): void
  288. {
  289. $organizationFactory = $this->getOrganizationFactoryMockFor('create');
  290. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  291. $organizationCreationRequest->method('getName')->willReturn('foo');
  292. $this->entityManager->expects(self::once())->method('beginTransaction');
  293. $organizationFactory->expects(self::once())->method('interruptIfOrganizationExists')->willThrowException(new \RuntimeException('An organization named foo already exists'));
  294. $organizationFactory->expects(self::never())->method('validateSubdomain');
  295. $organizationFactory
  296. ->expects(self::never())
  297. ->method('makeOrganizationWithRelations');
  298. $this->entityManager->expects(self::never())->method('persist');
  299. $this->entityManager->expects(self::never())->method('flush');
  300. $this->entityManager->expects(self::once())->method('rollback');
  301. $this->dolibarrApiService
  302. ->expects(self::never())
  303. ->method('createSociety');
  304. $this->bindFileService
  305. ->expects(self::never())
  306. ->method('registerSubdomain');
  307. $organizationFactory
  308. ->expects(self::never())
  309. ->method('createTypo3Website');
  310. $this->logger
  311. ->method('info')
  312. ->withConsecutive(
  313. ["Start the creation of a new organization named 'foo'"],
  314. );
  315. $this->logger
  316. ->method('critical')
  317. ->with(
  318. $this->matchesRegularExpression("/^An error happened, operation cancelled\nRuntimeException: An organization named foo already exists.*/")
  319. );
  320. $this->expectException(\RuntimeException::class);
  321. $this->expectExceptionMessage('An organization named foo already exists');
  322. $result = $organizationFactory->create($organizationCreationRequest);
  323. }
  324. public function testCreateNoWebsiteAndIsClient(): void
  325. {
  326. $organizationFactory = $this->getOrganizationFactoryMockFor('create');
  327. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  328. $organizationCreationRequest->method('getName')->willReturn('foo');
  329. $organizationCreationRequest->method('getSubdomain')->willReturn('subdomain');
  330. $organizationCreationRequest->method('isClient')->willReturn(true);
  331. $organizationCreationRequest->method('getCreateWebsite')->willReturn(false);
  332. $organizationFactory->expects(self::once())->method('interruptIfOrganizationExists');
  333. $organization = $this->getMockBuilder(Organization::class)->getMock();
  334. $organizationFactory
  335. ->expects(self::once())
  336. ->method('makeOrganizationWithRelations')
  337. ->with($organizationCreationRequest)
  338. ->willReturn($organization);
  339. $this->dolibarrApiService
  340. ->method('createSociety')
  341. ->with($organization, true)
  342. ->willReturn(456);
  343. $organizationFactory
  344. ->expects(self::never())
  345. ->method('createTypo3Website');
  346. $this->logger
  347. ->expects(self::once())
  348. ->method('warning')
  349. ->with('Typo3 website creation was not required');
  350. $result = $organizationFactory->create($organizationCreationRequest);
  351. $this->assertEquals(
  352. $organization,
  353. $result
  354. );
  355. }
  356. public function testCreateWithErrors(): void
  357. {
  358. $organizationFactory = $this->getOrganizationFactoryMockFor('create');
  359. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  360. $organizationCreationRequest->method('getName')->willReturn('foo');
  361. $organizationCreationRequest->method('getSubdomain')->willReturn('subdomain');
  362. $organizationCreationRequest->method('isClient')->willReturn(true);
  363. $organizationCreationRequest->method('getCreateWebsite')->willReturn(true);
  364. $organizationFactory->expects(self::once())->method('interruptIfOrganizationExists');
  365. $organization = $this->getMockBuilder(Organization::class)->getMock();
  366. $organizationFactory
  367. ->expects(self::once())
  368. ->method('makeOrganizationWithRelations')
  369. ->with($organizationCreationRequest)
  370. ->willReturn($organization);
  371. $this->dolibarrApiService
  372. ->method('createSociety')
  373. ->with($organization, true)
  374. ->willThrowException(new \RuntimeException('An error happened'));
  375. $this->bindFileService
  376. ->method('registerSubdomain')
  377. ->with('subdomain')
  378. ->willThrowException(new \RuntimeException('An error happened'));
  379. $organizationFactory
  380. ->method('createTypo3Website')
  381. ->with($organization)
  382. ->willThrowException(new \RuntimeException('An error happened'));
  383. $organizationFactory
  384. ->method('updateAdminassosDb')
  385. ->with($organization)
  386. ->willThrowException(new \RuntimeException('An error happened'));
  387. $this->logger
  388. ->expects(self::exactly(4))
  389. ->method('critical')
  390. ->willReturnOnConsecutiveCalls([
  391. 'An error happened while creating the dolibarr society, please proceed manually.',
  392. 'An error happened while updating the bind file, please proceed manually.',
  393. 'An error happened while creating the typo3 website, please proceed manually.',
  394. 'An error happened while updating the adminassos db, please proceed manually.',
  395. ]);
  396. $this->logger
  397. ->expects(self::once())
  398. ->method('warning')
  399. ->with('-- Operation ended with errors, check the logs for more information --');
  400. $organizationCreationRequest
  401. ->expects(self::once())
  402. ->method('setStatus')
  403. ->with(OrganizationCreationRequest::STATUS_OK_WITH_ERRORS);
  404. $result = $organizationFactory->create($organizationCreationRequest);
  405. $this->assertEquals(
  406. $organization,
  407. $result
  408. );
  409. }
  410. public function testInterruptIfOrganizationExistsNotExisting(): void
  411. {
  412. $organizationFactory = $this->getOrganizationFactoryMockFor('interruptIfOrganizationExists');
  413. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  414. $organizationCreationRequest->method('getName')->willReturn('foo');
  415. $organizationCreationRequest->method('getSiretNumber')->willReturn('');
  416. $organizationCreationRequest->method('getWaldecNumber')->willReturn('');
  417. $organizationCreationRequest->method('getIdentifier')->willReturn('');
  418. $organizationCreationRequest->method('getStreetAddress1')->willReturn('part1');
  419. $organizationCreationRequest->method('getStreetAddress2')->willReturn('part2');
  420. $organizationCreationRequest->method('getStreetAddress3')->willReturn('part3');
  421. $organizationCreationRequest->method('getCity')->willReturn('Paris');
  422. $organizationCreationRequest->method('getPostalCode')->willReturn('75000');
  423. $this->organizationIdentificationRepository
  424. ->expects(self::exactly(2))
  425. ->method('findOneBy')
  426. ->willReturn(null);
  427. $organizationFactory
  428. ->method('normalizeIdentificationField')
  429. ->willReturnMap([
  430. ['foo', 'foo'],
  431. ['part1 part2 part3', 'part1 part2 part3'],
  432. ]);
  433. $organizationFactory->interruptIfOrganizationExists($organizationCreationRequest);
  434. }
  435. public function testInterruptIfOrganizationExistsNotExistingNonExistingWithIdentifiers(): void
  436. {
  437. $organizationFactory = $this->getOrganizationFactoryMockFor('interruptIfOrganizationExists');
  438. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  439. $organizationCreationRequest->method('getName')->willReturn('foo');
  440. $organizationCreationRequest->method('getSiretNumber')->willReturn('123456');
  441. $organizationCreationRequest->method('getWaldecNumber')->willReturn('W123456');
  442. $organizationCreationRequest->method('getIdentifier')->willReturn('FR000000000000');
  443. $organizationCreationRequest->method('getStreetAddress1')->willReturn('part1');
  444. $organizationCreationRequest->method('getStreetAddress2')->willReturn('part2');
  445. $organizationCreationRequest->method('getStreetAddress3')->willReturn('part3');
  446. $organizationCreationRequest->method('getCity')->willReturn('Paris');
  447. $organizationCreationRequest->method('getPostalCode')->willReturn('75000');
  448. $organizationFactory
  449. ->method('normalizeIdentificationField')
  450. ->willReturnMap([
  451. ['foo', 'foo'],
  452. ['part1 part2 part3', 'part1 part2 part3'],
  453. ]);
  454. $this->organizationIdentificationRepository
  455. ->expects(self::exactly(5))
  456. ->method('findOneBy')
  457. ->willReturnMap([
  458. [['siretNumber' => '123456'], null, null],
  459. [['waldecNumber' => 'W123456'], null, null],
  460. [['identifier' => 'FR000000000000'], null, null],
  461. [['normalizedName' => 'foo', 'addressCity' => 'Paris'], null, null],
  462. [['normalizedAddress' => 'part1 part2 part3', 'addressCity' => 'Paris', 'postalCode' => '75000'], null, null],
  463. ]);
  464. $organizationFactory->interruptIfOrganizationExists($organizationCreationRequest);
  465. }
  466. public function testInterruptIfOrganizationExistsExistingWithSiret(): void
  467. {
  468. $organizationFactory = $this->getOrganizationFactoryMockFor('interruptIfOrganizationExists');
  469. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  470. $organizationCreationRequest->method('getName')->willReturn('foo');
  471. $organizationCreationRequest->method('getSiretNumber')->willReturn('123456');
  472. $organizationCreationRequest->method('getWaldecNumber')->willReturn('W123456');
  473. $organizationCreationRequest->method('getIdentifier')->willReturn('FR000000000000');
  474. $organizationCreationRequest->method('getStreetAddress1')->willReturn('part1');
  475. $organizationCreationRequest->method('getStreetAddress2')->willReturn('part2');
  476. $organizationCreationRequest->method('getStreetAddress3')->willReturn('part3');
  477. $organizationCreationRequest->method('getCity')->willReturn('Paris');
  478. $organizationCreationRequest->method('getPostalCode')->willReturn('75000');
  479. $organizationFactory
  480. ->method('normalizeIdentificationField')
  481. ->willReturnMap([
  482. ['foo', 'foo'],
  483. ['part1 part2 part3', 'part1 part2 part3'],
  484. ]);
  485. $organization = $this->getMockBuilder(Organization::class)->getMock();
  486. $this->organizationIdentificationRepository
  487. ->method('findOneBy')
  488. ->willReturnMap([
  489. [['siretNumber' => '123456'], null, $organization],
  490. [['waldecNumber' => 'W123456'], null, null],
  491. [['identifier' => 'FR000000000000'], null, null],
  492. [['normalizedName' => 'foo', 'addressCity' => 'Paris'], null, null],
  493. [['normalizedAddress' => 'part1 part2 part3', 'addressCity' => 'Paris', 'postalCode' => '75000'], null, null],
  494. ]);
  495. $this->expectException(\RuntimeException::class);
  496. $this->expectExceptionMessage("This siret number is already registered : '123456'");
  497. $organizationFactory->interruptIfOrganizationExists($organizationCreationRequest);
  498. }
  499. public function testInterruptIfOrganizationExistsExistingWithWaldec(): void
  500. {
  501. $organizationFactory = $this->getOrganizationFactoryMockFor('interruptIfOrganizationExists');
  502. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  503. $organizationCreationRequest->method('getName')->willReturn('foo');
  504. $organizationCreationRequest->method('getSiretNumber')->willReturn('123456');
  505. $organizationCreationRequest->method('getWaldecNumber')->willReturn('W123456');
  506. $organizationCreationRequest->method('getIdentifier')->willReturn('FR000000000000');
  507. $organizationCreationRequest->method('getStreetAddress1')->willReturn('part1');
  508. $organizationCreationRequest->method('getStreetAddress2')->willReturn('part2');
  509. $organizationCreationRequest->method('getStreetAddress3')->willReturn('part3');
  510. $organizationCreationRequest->method('getCity')->willReturn('Paris');
  511. $organizationCreationRequest->method('getPostalCode')->willReturn('75000');
  512. $organizationFactory
  513. ->method('normalizeIdentificationField')
  514. ->willReturnMap([
  515. ['foo', 'foo'],
  516. ['part1 part2 part3', 'part1 part2 part3'],
  517. ]);
  518. $organization = $this->getMockBuilder(Organization::class)->getMock();
  519. $this->organizationIdentificationRepository
  520. ->method('findOneBy')
  521. ->willReturnMap([
  522. [['siretNumber' => '123456'], null, null],
  523. [['waldecNumber' => 'W123456'], null, $organization],
  524. [['identifier' => 'FR000000000000'], null, null],
  525. [['normalizedName' => 'foo', 'addressCity' => 'Paris'], null, null],
  526. [['normalizedAddress' => 'part1 part2 part3', 'addressCity' => 'Paris', 'postalCode' => '75000'], null, null],
  527. ]);
  528. $this->expectException(\RuntimeException::class);
  529. $this->expectExceptionMessage("This RNA identifier (waldec number) is already registered : 'W123456'");
  530. $organizationFactory->interruptIfOrganizationExists($organizationCreationRequest);
  531. }
  532. public function testInterruptIfOrganizationExistsExistingWithCMFIdentifier(): void
  533. {
  534. $organizationFactory = $this->getOrganizationFactoryMockFor('interruptIfOrganizationExists');
  535. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  536. $organizationCreationRequest->method('getName')->willReturn('foo');
  537. $organizationCreationRequest->method('getSiretNumber')->willReturn('123456');
  538. $organizationCreationRequest->method('getWaldecNumber')->willReturn('W123456');
  539. $organizationCreationRequest->method('getIdentifier')->willReturn('FR000000000000');
  540. $organizationCreationRequest->method('getStreetAddress1')->willReturn('part1');
  541. $organizationCreationRequest->method('getStreetAddress2')->willReturn('part2');
  542. $organizationCreationRequest->method('getStreetAddress3')->willReturn('part3');
  543. $organizationCreationRequest->method('getCity')->willReturn('Paris');
  544. $organizationCreationRequest->method('getPostalCode')->willReturn('75000');
  545. $organizationFactory
  546. ->method('normalizeIdentificationField')
  547. ->willReturnMap([
  548. ['foo', 'foo'],
  549. ['part1 part2 part3', 'part1 part2 part3'],
  550. ]);
  551. $organization = $this->getMockBuilder(Organization::class)->getMock();
  552. $this->organizationIdentificationRepository
  553. ->method('findOneBy')
  554. ->willReturnMap([
  555. [['siretNumber' => '123456'], null, null],
  556. [['waldecNumber' => 'W123456'], null, null],
  557. [['identifier' => 'FR000000000000'], null, $organization],
  558. [['normalizedName' => 'foo', 'addressCity' => 'Paris'], null, null],
  559. [['normalizedAddress' => 'part1 part2 part3', 'addressCity' => 'Paris', 'postalCode' => '75000'], null, null],
  560. ]);
  561. $this->expectException(\RuntimeException::class);
  562. $this->expectExceptionMessage("This CMF identifier is already registered : 'FR000000000000'");
  563. $organizationFactory->interruptIfOrganizationExists($organizationCreationRequest);
  564. }
  565. public function testInterruptIfOrganizationExistsExistingWithSameNameAndCity(): void
  566. {
  567. $organizationFactory = $this->getOrganizationFactoryMockFor('interruptIfOrganizationExists');
  568. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  569. $organizationCreationRequest->method('getName')->willReturn('foo');
  570. $organizationCreationRequest->method('getSiretNumber')->willReturn('123456');
  571. $organizationCreationRequest->method('getWaldecNumber')->willReturn('W123456');
  572. $organizationCreationRequest->method('getIdentifier')->willReturn('FR000000000000');
  573. $organizationCreationRequest->method('getStreetAddress1')->willReturn('part1');
  574. $organizationCreationRequest->method('getStreetAddress2')->willReturn('part2');
  575. $organizationCreationRequest->method('getStreetAddress3')->willReturn('part3');
  576. $organizationCreationRequest->method('getCity')->willReturn('Paris');
  577. $organizationCreationRequest->method('getPostalCode')->willReturn('75000');
  578. $organizationFactory
  579. ->method('normalizeIdentificationField')
  580. ->willReturnMap([
  581. ['foo', 'foo'],
  582. ['part1 part2 part3', 'part1 part2 part3'],
  583. ]);
  584. $organization = $this->getMockBuilder(Organization::class)->getMock();
  585. $this->organizationIdentificationRepository
  586. ->method('findOneBy')
  587. ->willReturnMap([
  588. [['siretNumber' => '123456'], null, null],
  589. [['waldecNumber' => 'W123456'], null, null],
  590. [['identifier' => 'FR000000000000'], null, null],
  591. [['normalizedName' => 'foo', 'addressCity' => 'Paris'], null, $organization],
  592. [['normalizedAddress' => 'part1 part2 part3', 'addressCity' => 'Paris', 'postalCode' => '75000'], null, null],
  593. ]);
  594. $this->expectException(\RuntimeException::class);
  595. $this->expectExceptionMessage("An organization named 'foo' already exists in Paris");
  596. $organizationFactory->interruptIfOrganizationExists($organizationCreationRequest);
  597. }
  598. public function testInterruptIfOrganizationExistsExistingWithSameAddress(): void
  599. {
  600. $organizationFactory = $this->getOrganizationFactoryMockFor('interruptIfOrganizationExists');
  601. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  602. $organizationCreationRequest->method('getName')->willReturn('foo');
  603. $organizationCreationRequest->method('getSiretNumber')->willReturn('123456');
  604. $organizationCreationRequest->method('getWaldecNumber')->willReturn('W123456');
  605. $organizationCreationRequest->method('getIdentifier')->willReturn('FR000000000000');
  606. $organizationCreationRequest->method('getStreetAddress1')->willReturn('part1');
  607. $organizationCreationRequest->method('getStreetAddress2')->willReturn('part2');
  608. $organizationCreationRequest->method('getStreetAddress3')->willReturn('part3');
  609. $organizationCreationRequest->method('getCity')->willReturn('Paris');
  610. $organizationCreationRequest->method('getPostalCode')->willReturn('75000');
  611. $organizationFactory
  612. ->method('normalizeIdentificationField')
  613. ->willReturnMap([
  614. ['foo', 'foo'],
  615. ['part1 part2 part3', 'part1 part2 part3'],
  616. ]);
  617. $organization = $this->getMockBuilder(Organization::class)->getMock();
  618. $this->organizationIdentificationRepository
  619. ->method('findOneBy')
  620. ->willReturnMap([
  621. [['siretNumber' => '123456'], null, null],
  622. [['waldecNumber' => 'W123456'], null, null],
  623. [['identifier' => 'FR000000000000'], null, null],
  624. [['normalizedName' => 'foo', 'addressCity' => 'Paris'], null, null],
  625. [['normalizedAddress' => 'part1 part2 part3', 'addressCity' => 'Paris', 'postalCode' => '75000'], null, $organization],
  626. ]);
  627. $this->expectException(\RuntimeException::class);
  628. $this->expectExceptionMessage('An organization already exists at this address.');
  629. $organizationFactory->interruptIfOrganizationExists($organizationCreationRequest);
  630. }
  631. public function testValidateSubdomain(): void
  632. {
  633. $organizationFactory = $this->getOrganizationFactoryMockFor('validateSubdomain');
  634. $this->subdomainService->expects(self::once())->method('isValidSubdomain')->willReturn(true);
  635. $this->subdomainService->expects(self::once())->method('isReservedSubdomain')->willReturn(false);
  636. $this->subdomainService->expects(self::once())->method('isRegistered')->willReturn(false);
  637. $organizationFactory->validateSubdomain('foo');
  638. }
  639. public function testValidateSubdomainIsNotValid(): void
  640. {
  641. $organizationFactory = $this->getOrganizationFactoryMockFor('validateSubdomain');
  642. $this->subdomainService->method('isValidSubdomain')->willReturn(false);
  643. $this->subdomainService->method('isReservedSubdomain')->willReturn(false);
  644. $this->subdomainService->method('isRegistered')->willReturn(false);
  645. $this->expectException(\RuntimeException::class);
  646. $this->expectExceptionMessage('Not a valid subdomain : foo');
  647. $organizationFactory->validateSubdomain('foo');
  648. }
  649. public function testValidateSubdomainIsReserved(): void
  650. {
  651. $organizationFactory = $this->getOrganizationFactoryMockFor('validateSubdomain');
  652. $this->subdomainService->method('isValidSubdomain')->willReturn(true);
  653. $this->subdomainService->method('isReservedSubdomain')->willReturn(true);
  654. $this->subdomainService->method('isRegistered')->willReturn(false);
  655. $this->expectException(\RuntimeException::class);
  656. $this->expectExceptionMessage('This subdomain is not available : foo');
  657. $organizationFactory->validateSubdomain('foo');
  658. }
  659. public function testValidateSubdomainIsRegistered(): void
  660. {
  661. $organizationFactory = $this->getOrganizationFactoryMockFor('validateSubdomain');
  662. $this->subdomainService->method('isValidSubdomain')->willReturn(true);
  663. $this->subdomainService->method('isReservedSubdomain')->willReturn(false);
  664. $this->subdomainService->method('isRegistered')->willReturn(true);
  665. $this->expectException(\RuntimeException::class);
  666. $this->expectExceptionMessage('This subdomain is already registered : foo');
  667. $organizationFactory->validateSubdomain('foo');
  668. }
  669. public function testMakeOrganizationWithRelations()
  670. {
  671. $organizationFactory = $this->getOrganizationFactoryMockFor('makeOrganizationWithRelations');
  672. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  673. $organization = $this->getMockBuilder(Organization::class)->getMock();
  674. // Création de l'organisation
  675. $organizationFactory
  676. ->expects(self::once())
  677. ->method('makeOrganization')
  678. ->with($organizationCreationRequest)
  679. ->willReturn($organization);
  680. // Création des Parameters
  681. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  682. $organizationFactory
  683. ->expects(self::once())
  684. ->method('makeParameters')
  685. ->with($organizationCreationRequest)
  686. ->willReturn($parameters);
  687. $organization->expects(self::once())->method('setParameters')->with($parameters);
  688. // Création des Settings
  689. $settings = $this->getMockBuilder(Settings::class)->getMock();
  690. $organizationFactory
  691. ->expects(self::once())
  692. ->method('makeSettings')
  693. ->with($organizationCreationRequest)
  694. ->willReturn($settings);
  695. $organization->expects(self::once())->method('setSettings')->with($settings);
  696. // Création de l'adresse postale
  697. $organizationAddressPostal = $this->getMockBuilder(OrganizationAddressPostal::class)->getMock();
  698. $organizationFactory
  699. ->expects(self::once())
  700. ->method('makePostalAddress')
  701. ->with($organizationCreationRequest)
  702. ->willReturn($organizationAddressPostal);
  703. $organization->expects(self::once())->method('addOrganizationAddressPostal')->with($organizationAddressPostal);
  704. // Création du point de contact
  705. $contactPoint = $this->getMockBuilder(ContactPoint::class)->getMock();
  706. $organizationFactory
  707. ->expects(self::once())
  708. ->method('makeContactPoint')
  709. ->with($organizationCreationRequest)
  710. ->willReturn($contactPoint);
  711. $organization->expects(self::once())->method('addContactPoint')->with($contactPoint);
  712. // Rattachement au réseau
  713. $networkOrganization = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  714. $organizationFactory
  715. ->expects(self::once())
  716. ->method('makeNetworkOrganization')
  717. ->with($organizationCreationRequest)
  718. ->willReturn($networkOrganization);
  719. $organization->expects(self::once())->method('addNetworkOrganization')->with($networkOrganization);
  720. // Créé l'admin
  721. $adminAccess = $this->getMockBuilder(Access::class)->getMock();
  722. $organizationFactory
  723. ->expects(self::once())
  724. ->method('makeAdminAccess')
  725. ->with($organizationCreationRequest)
  726. ->willReturn($adminAccess);
  727. // Le `$organization->expects(...)->method('addAccess')` est implémenté plus loin,
  728. // après la création du président et du directeur
  729. // Création des cycles
  730. $cycle1 = $this->getMockBuilder(Cycle::class)->getMock();
  731. $cycle2 = $this->getMockBuilder(Cycle::class)->getMock();
  732. $cycle3 = $this->getMockBuilder(Cycle::class)->getMock();
  733. $organizationFactory
  734. ->expects(self::once())
  735. ->method('makeCycles')
  736. ->willReturn([$cycle1, $cycle2, $cycle3]);
  737. $organization->expects(self::exactly(3))->method('addCycle')->withConsecutive([$cycle1], [$cycle2], [$cycle3]);
  738. // Création du président et du directeur
  739. $organizationMemberCreationRequest1 = $this->getMockBuilder(OrganizationMemberCreationRequest::class)->getMock();
  740. $organizationCreationRequest->method('getPresident')->willReturn($organizationMemberCreationRequest1);
  741. $organizationMemberCreationRequest2 = $this->getMockBuilder(OrganizationMemberCreationRequest::class)->getMock();
  742. $organizationCreationRequest->method('getDirector')->willReturn($organizationMemberCreationRequest2);
  743. $access1 = $this->getMockBuilder(Access::class)->getMock();
  744. $access2 = $this->getMockBuilder(Access::class)->getMock();
  745. $organizationFactory
  746. ->expects(self::exactly(2))
  747. ->method('makeAccess')
  748. ->willReturnMap([
  749. [$organizationMemberCreationRequest1, $access1],
  750. [$organizationMemberCreationRequest2, $access2],
  751. ]);
  752. $organization
  753. ->expects(self::exactly(3))
  754. ->method('addAccess')
  755. ->withConsecutive([$adminAccess], [$access1], [$access2]);
  756. // Création du sous-domaine
  757. $subdomain = $this->getMockBuilder(Subdomain::class)->getMock();
  758. $organizationFactory
  759. ->expects(self::once())
  760. ->method('makeSubdomain')
  761. ->with($organizationCreationRequest)
  762. ->willReturn($subdomain);
  763. $organization->expects(self::once())->method('addSubdomain')->with($subdomain);
  764. // Enregistrement du sous domaine dans Parameters (retrocompatibilité v1)
  765. $organization->method('getParameters')->willReturn($parameters);
  766. $organizationCreationRequest->method('getSubdomain')->willReturn('foo');
  767. $parameters->expects(self::once())->method('setSubDomain')->with('foo');
  768. $parameters->expects(self::once())->method('setOtherWebsite')->with('https://foo.opentalent.fr');
  769. $result = $organizationFactory->makeOrganizationWithRelations($organizationCreationRequest);
  770. $this->assertEquals(
  771. $result,
  772. $organization
  773. );
  774. }
  775. public function testMakeOrganizationWithRelationsNoPresidentNoDirector()
  776. {
  777. $organizationFactory = $this->getOrganizationFactoryMockFor('makeOrganizationWithRelations');
  778. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  779. $organization = $this->getMockBuilder(Organization::class)->getMock();
  780. // Création de l'organisation
  781. $organizationFactory
  782. ->expects(self::once())
  783. ->method('makeOrganization')
  784. ->with($organizationCreationRequest)
  785. ->willReturn($organization);
  786. $organizationCreationRequest->method('getPresident')->willReturn(null);
  787. $organizationCreationRequest->method('getDirector')->willReturn(null);
  788. // Un seul appel, pour l'adminAccess
  789. $organization
  790. ->expects(self::once())
  791. ->method('addAccess');
  792. $organizationFactory->makeOrganizationWithRelations($organizationCreationRequest);
  793. }
  794. public function testMakeOrganization()
  795. {
  796. $organizationFactory = $this->getOrganizationFactoryMockFor('makeOrganization');
  797. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  798. $organizationCreationRequest->method('getName')->willReturn('My Organization');
  799. $organizationCreationRequest->method('getLegalStatus')->willReturn(LegalEnum::ASSOCIATION_LAW_1901);
  800. $organizationCreationRequest->method('getPrincipalType')->willReturn(PrincipalTypeEnum::ARTISTIC_EDUCATION_ONLY);
  801. $organization = $organizationFactory->makeOrganization($organizationCreationRequest);
  802. $this->assertEquals(
  803. 'My Organization',
  804. $organization->getName()
  805. );
  806. $this->assertEquals(
  807. LegalEnum::ASSOCIATION_LAW_1901,
  808. $organization->getLegalStatus()
  809. );
  810. $this->assertEquals(
  811. PrincipalTypeEnum::ARTISTIC_EDUCATION_ONLY,
  812. $organization->getPrincipalType()
  813. );
  814. }
  815. public function testMakeParameters(): void
  816. {
  817. $organizationFactory = $this->getOrganizationFactoryMockFor('makeParameters');
  818. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  819. $parameters = $organizationFactory->makeParameters($organizationCreationRequest);
  820. $this->assertInstanceOf(Parameters::class, $parameters);
  821. }
  822. public function testMakeSettings(): void
  823. {
  824. $organizationFactory = $this->getOrganizationFactoryMockFor('makeSettings');
  825. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  826. $organizationCreationRequest->method('getProduct')->willReturn(SettingsProductEnum::ARTIST_PREMIUM);
  827. $settings = $organizationFactory->makeSettings($organizationCreationRequest);
  828. $this->assertEquals(
  829. SettingsProductEnum::ARTIST_PREMIUM,
  830. $settings->getProduct()
  831. );
  832. }
  833. public function testMakePostalAddress(): void
  834. {
  835. $organizationFactory = $this->getOrganizationFactoryMockFor('makePostalAddress');
  836. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  837. $organizationCreationRequest->method('getStreetAddress1')->willReturn('address1');
  838. $organizationCreationRequest->method('getStreetAddress2')->willReturn('address2');
  839. $organizationCreationRequest->method('getStreetAddress3')->willReturn('address3');
  840. $organizationCreationRequest->method('getPostalCode')->willReturn('00000');
  841. $organizationCreationRequest->method('getCity')->willReturn('city');
  842. $organizationCreationRequest->method('getCountryId')->willReturn(1);
  843. $country = $this->getMockBuilder(Country::class)->getMock();
  844. $this->countryRepository->expects(self::once())->method('find')->with(1)->willReturn($country);
  845. $organizationAddressPostal = $organizationFactory->makePostalAddress($organizationCreationRequest);
  846. $this->assertEquals(
  847. AddressPostalOrganizationTypeEnum::ADDRESS_HEAD_OFFICE,
  848. $organizationAddressPostal->getType()
  849. );
  850. $addressPostal = $organizationAddressPostal->getAddressPostal();
  851. $this->assertEquals(
  852. 'address1',
  853. $addressPostal->getStreetAddress()
  854. );
  855. $this->assertEquals(
  856. 'address2',
  857. $addressPostal->getStreetAddressSecond()
  858. );
  859. $this->assertEquals(
  860. 'address3',
  861. $addressPostal->getStreetAddressThird()
  862. );
  863. $this->assertEquals(
  864. '00000',
  865. $addressPostal->getPostalCode()
  866. );
  867. $this->assertEquals(
  868. 'city',
  869. $addressPostal->getAddressCity()
  870. );
  871. $this->assertEquals(
  872. $country,
  873. $addressPostal->getAddressCountry()
  874. );
  875. }
  876. public function testMakePostalAddressUnexistingCountry(): void
  877. {
  878. $organizationFactory = $this->getOrganizationFactoryMockFor('makePostalAddress');
  879. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  880. $organizationCreationRequest->method('getCountryId')->willReturn(1);
  881. $this->countryRepository->expects(self::once())->method('find')->with(1)->willReturn(null);
  882. $this->expectException(\RuntimeException::class);
  883. $this->expectExceptionMessage('No country found for id 1');
  884. $organizationFactory->makePostalAddress($organizationCreationRequest);
  885. }
  886. public function testMakeContactPoint(): void
  887. {
  888. $organizationFactory = $this->getOrganizationFactoryMockFor('makeContactPoint');
  889. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  890. $organizationCreationRequest->method('getPhoneNumber')->willReturn('+33102030405');
  891. $organizationCreationRequest->method('getEmail')->willReturn('contact@domain.net');
  892. $this->phoneNumberUtil
  893. ->method('isPossibleNumber')
  894. ->with('+33102030405')
  895. ->willReturn(true);
  896. $phoneNumber = $this->getMockBuilder(PhoneNumber::class)->getMock();
  897. $this->phoneNumberUtil
  898. ->expects(self::once())
  899. ->method('parse')
  900. ->with('+33102030405')
  901. ->willReturn($phoneNumber);
  902. $contactPoint = $organizationFactory->makeContactPoint($organizationCreationRequest);
  903. $this->assertEquals(
  904. 'contact@domain.net',
  905. $contactPoint->getEmail()
  906. );
  907. $this->assertEquals(
  908. $phoneNumber,
  909. $contactPoint->getTelphone()
  910. );
  911. }
  912. public function testMakeContactPointInvalidPhoneNumber(): void
  913. {
  914. $organizationFactory = $this->getOrganizationFactoryMockFor('makeContactPoint');
  915. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  916. $organizationCreationRequest->method('getPhoneNumber')->willReturn('invalid');
  917. $organizationCreationRequest->method('getEmail')->willReturn('contact@domain.net');
  918. $this->phoneNumberUtil
  919. ->method('isPossibleNumber')
  920. ->with('invalid')
  921. ->willReturn(false);
  922. $phoneNumber = $this->getMockBuilder(PhoneNumber::class)->getMock();
  923. $this->phoneNumberUtil
  924. ->expects(self::never())
  925. ->method('parse');
  926. $this->expectException(\RuntimeException::class);
  927. $this->expectExceptionMessage('Phone number is invalid or missing');
  928. $organizationFactory->makeContactPoint($organizationCreationRequest);
  929. }
  930. public function testMakeNetworkOrganization(): void
  931. {
  932. $organizationFactory = $this->getOrganizationFactoryMockFor('makeNetworkOrganization');
  933. DatesUtils::setFakeDatetime('2024-01-01');
  934. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  935. $organizationCreationRequest->method('getParentId')->willReturn(123);
  936. $parent = $this->getMockBuilder(Organization::class)->getMock();
  937. $settings = $this->getMockBuilder(Settings::class)->getMock();
  938. $settings->method('getProduct')->willReturn(SettingsProductEnum::MANAGER);
  939. $parent->method('getSettings')->willReturn($settings);
  940. $this->organizationRepository->expects(self::once())->method('find')->with(123)->willReturn($parent);
  941. $network = $this->getMockBuilder(Network::class)->getMock();
  942. $networkOrganization = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  943. $networkOrganization->method('getNetwork')->willReturn($network);
  944. $this->organizationUtils
  945. ->expects(self::once())
  946. ->method('getActiveNetworkOrganization')
  947. ->with($parent)
  948. ->willReturn($networkOrganization);
  949. $networkOrganization = $organizationFactory->makeNetworkOrganization($organizationCreationRequest);
  950. $this->assertEquals(
  951. $parent,
  952. $networkOrganization->getParent()
  953. );
  954. $this->assertEquals(
  955. $network,
  956. $networkOrganization->getNetwork()
  957. );
  958. $this->assertEquals(
  959. '2024-01-01',
  960. $networkOrganization->getStartDate()->format('Y-m-d')
  961. );
  962. }
  963. public function testMakeNetworkOrganizationMissingParent(): void
  964. {
  965. $organizationFactory = $this->getOrganizationFactoryMockFor('makeNetworkOrganization');
  966. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  967. $organizationCreationRequest->method('getParentId')->willReturn(123);
  968. $this->expectException(\RuntimeException::class);
  969. $this->expectExceptionMessage('No parent organization found for id 123');
  970. $this->organizationRepository->expects(self::once())->method('find')->with(123)->willReturn(null);
  971. $organizationFactory->makeNetworkOrganization($organizationCreationRequest);
  972. }
  973. public function testMakeNetworkOrganizationParentIsNotManager(): void
  974. {
  975. $organizationFactory = $this->getOrganizationFactoryMockFor('makeNetworkOrganization');
  976. DatesUtils::setFakeDatetime('2024-01-01');
  977. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  978. $organizationCreationRequest->method('getParentId')->willReturn(123);
  979. $parent = $this->getMockBuilder(Organization::class)->getMock();
  980. $settings = $this->getMockBuilder(Settings::class)->getMock();
  981. $settings->method('getProduct')->willReturn(SettingsProductEnum::SCHOOL);
  982. $parent->method('getSettings')->willReturn($settings);
  983. $this->organizationRepository->expects(self::once())->method('find')->with(123)->willReturn($parent);
  984. $this->expectException(\RuntimeException::class);
  985. $this->expectExceptionMessage("Parent organization must have the product 'manager' (actual product: 'school')");
  986. $organizationFactory->makeNetworkOrganization($organizationCreationRequest);
  987. }
  988. public function testMakeNetworkOrganizationMissingNetwork(): void
  989. {
  990. $organizationFactory = $this->getOrganizationFactoryMockFor('makeNetworkOrganization');
  991. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  992. $organizationCreationRequest->method('getParentId')->willReturn(123);
  993. $parent = $this->getMockBuilder(Organization::class)->getMock();
  994. $settings = $this->getMockBuilder(Settings::class)->getMock();
  995. $settings->method('getProduct')->willReturn(SettingsProductEnum::MANAGER);
  996. $parent->method('getSettings')->willReturn($settings);
  997. $this->organizationRepository->expects(self::once())->method('find')->with(123)->willReturn($parent);
  998. $this->expectException(\RuntimeException::class);
  999. $this->expectExceptionMessage('No network found for parent 123');
  1000. $this->organizationRepository->expects(self::once())->method('find')->with(123)->willReturn($parent);
  1001. $organizationFactory->makeNetworkOrganization($organizationCreationRequest);
  1002. }
  1003. public function testMakeNetworkOrganizationIsCMFInvalidIdentifier(): void
  1004. {
  1005. $organizationFactory = $this->getOrganizationFactoryMockFor('makeNetworkOrganization');
  1006. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  1007. $organizationCreationRequest->method('getParentId')->willReturn(123);
  1008. $organizationCreationRequest->method('getIdentifier')->willReturn('invalid');
  1009. $parent = $this->getMockBuilder(Organization::class)->getMock();
  1010. $settings = $this->getMockBuilder(Settings::class)->getMock();
  1011. $settings->method('getProduct')->willReturn(SettingsProductEnum::MANAGER);
  1012. $parent->method('getSettings')->willReturn($settings);
  1013. $this->organizationRepository->method('find')->with(123)->willReturn($parent);
  1014. $network = $this->getMockBuilder(Network::class)->getMock();
  1015. $network->method('getId')->willReturn(NetworkEnum::CMF->value);
  1016. $networkOrganization = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  1017. $networkOrganization->method('getNetwork')->willReturn($network);
  1018. $this->organizationUtils
  1019. ->method('getActiveNetworkOrganization')
  1020. ->with($parent)
  1021. ->willReturn($networkOrganization);
  1022. $this->expectException(\RuntimeException::class);
  1023. $this->expectExceptionMessage('CMF identifier is missing or invalid.');
  1024. $organizationFactory->makeNetworkOrganization($organizationCreationRequest);
  1025. }
  1026. public function testMakeNetworkOrganizationIsNotCMFInvalidIdentifier(): void
  1027. {
  1028. $organizationFactory = $this->getOrganizationFactoryMockFor('makeNetworkOrganization');
  1029. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  1030. $organizationCreationRequest->method('getParentId')->willReturn(123);
  1031. $organizationCreationRequest->method('getIdentifier')->willReturn('invalid');
  1032. $parent = $this->getMockBuilder(Organization::class)->getMock();
  1033. $settings = $this->getMockBuilder(Settings::class)->getMock();
  1034. $settings->method('getProduct')->willReturn(SettingsProductEnum::MANAGER);
  1035. $parent->method('getSettings')->willReturn($settings);
  1036. $this->organizationRepository->method('find')->with(123)->willReturn($parent);
  1037. $network = $this->getMockBuilder(Network::class)->getMock();
  1038. $network->method('getId')->willReturn(NetworkEnum::OUTOFNET->value);
  1039. $networkOrganization = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  1040. $networkOrganization->method('getNetwork')->willReturn($network);
  1041. $this->organizationUtils
  1042. ->method('getActiveNetworkOrganization')
  1043. ->with($parent)
  1044. ->willReturn($networkOrganization);
  1045. $result = $organizationFactory->makeNetworkOrganization($organizationCreationRequest);
  1046. $this->assertEquals(
  1047. $network,
  1048. $result->getNetwork()
  1049. );
  1050. }
  1051. public function testMakeAdminAccess(): void
  1052. {
  1053. $organizationFactory = $this->getOrganizationFactoryMockFor('makeAdminAccess');
  1054. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  1055. $organizationCreationRequest->method('getSubdomain')->willReturn('foo');
  1056. $adminAccess = $organizationFactory->makeAdminAccess($organizationCreationRequest);
  1057. $this->assertTrue(
  1058. $adminAccess->getAdminAccess()
  1059. );
  1060. $this->assertEquals(
  1061. 'adminfoo',
  1062. $adminAccess->getPerson()->getUsername()
  1063. );
  1064. $this->assertEquals(
  1065. 32,
  1066. strlen($adminAccess->getPerson()->getPassword())
  1067. );
  1068. }
  1069. public function testMakeCycles(): void
  1070. {
  1071. $organizationFactory = $this->getOrganizationFactoryMockFor('makeCycles');
  1072. $cycles = $organizationFactory->makeCycles();
  1073. $cyclesExpectedData = [
  1074. ['Cycle initiation', 10, CycleEnum::INITIATION_CYCLE],
  1075. ['Cycle 1', 20, CycleEnum::CYCLE_1],
  1076. ['Cycle 2', 30, CycleEnum::CYCLE_2],
  1077. ['Cycle 3', 40, CycleEnum::CYCLE_3],
  1078. ['Cycle 4', 50, CycleEnum::CYCLE_4],
  1079. ['Hors cycle', 60, CycleEnum::OUT_CYCLE],
  1080. ];
  1081. $i = 0;
  1082. foreach ($cycles as $cycle) {
  1083. $this->assertEquals(
  1084. $cyclesExpectedData[$i][0],
  1085. $cycle->getLabel()
  1086. );
  1087. $this->assertEquals(
  1088. $cyclesExpectedData[$i][1],
  1089. $cycle->getOrder()
  1090. );
  1091. $this->assertEquals(
  1092. $cyclesExpectedData[$i][2],
  1093. $cycle->getCycleEnum()
  1094. );
  1095. $this->assertFalse(
  1096. $cycle->getIsSystem()
  1097. );
  1098. ++$i;
  1099. }
  1100. }
  1101. public function testMakeAccessNewPerson(): void
  1102. {
  1103. $organizationFactory = $this->getOrganizationFactoryMockFor('makeAccess');
  1104. $organizationMemberCreationRequest = $this->getMockBuilder(OrganizationMemberCreationRequest::class)->getMock();
  1105. $organizationMemberCreationRequest->method('getUsername')->willReturn('bob');
  1106. $organizationMemberCreationRequest->method('getName')->willReturn('Bob');
  1107. $organizationMemberCreationRequest->method('getGivenName')->willReturn('bOBBy');
  1108. $organizationMemberCreationRequest->method('getGender')->willReturn(GenderEnum::MISTER);
  1109. $personAddressPostal = $this->getMockBuilder(PersonAddressPostal::class)->getMock();
  1110. $organizationFactory
  1111. ->expects(self::once())
  1112. ->method('makePersonPostalAddress')
  1113. ->with($organizationMemberCreationRequest)
  1114. ->willReturn($personAddressPostal);
  1115. $contactPoint = $this->getMockBuilder(ContactPoint::class)->getMock();
  1116. $organizationFactory
  1117. ->expects(self::once())
  1118. ->method('makePersonContactPoint')
  1119. ->with($organizationMemberCreationRequest)
  1120. ->willReturn($contactPoint);
  1121. $access = $organizationFactory->makeAccess($organizationMemberCreationRequest);
  1122. $this->assertInstanceOf(Access::class, $access);
  1123. $this->assertEquals(
  1124. 'bob',
  1125. $access->getPerson()->getUsername()
  1126. );
  1127. $this->assertTrue(
  1128. strlen($access->getPerson()->getPassword()) === 32
  1129. );
  1130. $this->assertEquals(
  1131. 'Bob',
  1132. $access->getPerson()->getName()
  1133. );
  1134. $this->assertEquals(
  1135. 'Bobby',
  1136. $access->getPerson()->getGivenName()
  1137. );
  1138. $this->assertEquals(
  1139. [$personAddressPostal],
  1140. $access->getPerson()->getPersonAddressPostal()->toArray()
  1141. );
  1142. $this->assertEquals(
  1143. [$contactPoint],
  1144. $access->getPerson()->getContactPoints()->toArray()
  1145. );
  1146. }
  1147. public function testMakeAccessExistingPerson(): void
  1148. {
  1149. $organizationFactory = $this->getOrganizationFactoryMockFor('makeAccess');
  1150. $person = $this->getMockBuilder(Person::class)->getMock();
  1151. $this->personRepository
  1152. ->expects(self::once())
  1153. ->method('find')
  1154. ->with(123)
  1155. ->willReturn($person);
  1156. $access = $organizationFactory->makeAccess(123);
  1157. $this->assertInstanceOf(Access::class, $access);
  1158. $this->assertEquals(
  1159. $person,
  1160. $access->getPerson()
  1161. );
  1162. }
  1163. public function testMakeAccessPostalAddress()
  1164. {
  1165. $organizationFactory = $this->getOrganizationFactoryMockFor('makePersonPostalAddress');
  1166. $organizationMemberCreationRequest = $this->getMockBuilder(OrganizationMemberCreationRequest::class)->getMock();
  1167. $organizationMemberCreationRequest->method('getStreetAddress1')->willReturn('Aaa');
  1168. $organizationMemberCreationRequest->method('getStreetAddress2')->willReturn('Bbb');
  1169. $organizationMemberCreationRequest->method('getStreetAddress3')->willReturn(null);
  1170. $organizationMemberCreationRequest->method('getPostalCode')->willReturn('00000');
  1171. $organizationMemberCreationRequest->method('getCity')->willReturn('city');
  1172. $organizationMemberCreationRequest->method('getCountryId')->willReturn(123);
  1173. $country = $this->getMockBuilder(Country::class)->getMock();
  1174. $this->countryRepository
  1175. ->expects(self::once())
  1176. ->method('find')
  1177. ->with(123)
  1178. ->willReturn($country);
  1179. $personPostalAddress = $organizationFactory->makePersonPostalAddress($organizationMemberCreationRequest);
  1180. $this->assertEquals(
  1181. AddressPostalPersonTypeEnum::ADDRESS_PRINCIPAL,
  1182. $personPostalAddress->getType()
  1183. );
  1184. $postalAddress = $personPostalAddress->getAddressPostal();
  1185. $this->assertEquals('Aaa', $postalAddress->getStreetAddress());
  1186. $this->assertEquals('Bbb', $postalAddress->getStreetAddressSecond());
  1187. $this->assertEquals(null, $postalAddress->getStreetAddressThird());
  1188. $this->assertEquals('00000', $postalAddress->getPostalCode());
  1189. $this->assertEquals('city', $postalAddress->getAddressCity());
  1190. $this->assertEquals($country, $postalAddress->getAddressCountry());
  1191. }
  1192. public function testMakeAccessContactPoint(): void
  1193. {
  1194. $organizationFactory = $this->getOrganizationFactoryMockFor('makePersonContactPoint');
  1195. $organizationMemberCreationRequest = $this->getMockBuilder(OrganizationMemberCreationRequest::class)->getMock();
  1196. $organizationMemberCreationRequest->method('getPhone')->willReturn('+33102030405');
  1197. $organizationMemberCreationRequest->method('getEmail')->willReturn('email@domain.com');
  1198. $organizationMemberCreationRequest->method('getMobile')->willReturn('+33607080910');
  1199. $contactPoint = $organizationFactory->makePersonContactPoint($organizationMemberCreationRequest);
  1200. $this->assertEquals(
  1201. ContactPointTypeEnum::PRINCIPAL,
  1202. $contactPoint->getContactType()
  1203. );
  1204. $this->assertEquals(
  1205. '33',
  1206. $contactPoint->getTelphone()->getCountryCode()
  1207. );
  1208. $this->assertEquals(
  1209. '102030405',
  1210. $contactPoint->getTelphone()->getNationalNumber()
  1211. );
  1212. $this->assertEquals(
  1213. '33',
  1214. $contactPoint->getMobilPhone()->getCountryCode()
  1215. );
  1216. $this->assertEquals(
  1217. '607080910',
  1218. $contactPoint->getMobilPhone()->getNationalNumber()
  1219. );
  1220. $this->assertEquals(
  1221. 'email@domain.com',
  1222. $contactPoint->getEmail()
  1223. );
  1224. }
  1225. public function testMakePersonContactPointNoMobile(): void
  1226. {
  1227. $organizationFactory = $this->getOrganizationFactoryMockFor('makePersonContactPoint');
  1228. $organizationMemberCreationRequest = $this->getMockBuilder(OrganizationMemberCreationRequest::class)->getMock();
  1229. $organizationMemberCreationRequest->method('getPhone')->willReturn('+33102030405');
  1230. $organizationMemberCreationRequest->method('getMobile')->willReturn(null);
  1231. $contactPoint = $organizationFactory->makePersonContactPoint($organizationMemberCreationRequest);
  1232. $this->assertEquals(
  1233. null,
  1234. $contactPoint->getMobilPhone()
  1235. );
  1236. }
  1237. public function testMakeSubdomain(): void
  1238. {
  1239. $organizationFactory = $this->getOrganizationFactoryMockFor('makeSubdomain');
  1240. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  1241. $organizationCreationRequest->method('getSubdomain')->willReturn('subdomain');
  1242. $subdomain = $organizationFactory->makeSubdomain($organizationCreationRequest);
  1243. $this->assertEquals(
  1244. 'subdomain',
  1245. $subdomain->getSubdomain()
  1246. );
  1247. $this->assertTrue(
  1248. $subdomain->isActive()
  1249. );
  1250. }
  1251. public function testCreateTypo3Website(): void
  1252. {
  1253. $organizationFactory = $this->getOrganizationFactoryMockFor('createTypo3Website');
  1254. $organization = $this->getMockBuilder(Organization::class)->getMock();
  1255. $organization->method('getId')->willReturn(123);
  1256. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  1257. $response->method('getStatusCode')->willReturn(Response::HTTP_OK);
  1258. $response->method('getContent')->willReturn('{"root_uid": 456}');
  1259. $this->typo3Service->expects(self::once())->method('createSite')->with(123)->willReturn($response);
  1260. $organization->expects(self::once())->method('setCmsId')->with(456);
  1261. $this->entityManager->expects(self::once())->method('persist')->with($organization);
  1262. $this->entityManager->expects(self::once())->method('flush');
  1263. $result = $organizationFactory->createTypo3Website($organization);
  1264. $this->assertEquals(
  1265. 456,
  1266. $result
  1267. );
  1268. }
  1269. public function testCreateTypo3WebsiteWithError(): void
  1270. {
  1271. $organizationFactory = $this->getOrganizationFactoryMockFor('createTypo3Website');
  1272. $organization = $this->getMockBuilder(Organization::class)->getMock();
  1273. $organization->method('getId')->willReturn(123);
  1274. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  1275. $response->method('getStatusCode')->willReturn(Response::HTTP_FORBIDDEN);
  1276. $response->method('getContent')->willReturn('');
  1277. $this->typo3Service->expects(self::once())->method('createSite')->with(123)->willReturn($response);
  1278. $this->logger
  1279. ->expects(self::once())
  1280. ->method('critical')
  1281. ->with('/!\ A critical error happened while creating the Typo3 website');
  1282. $result = $organizationFactory->createTypo3Website($organization);
  1283. $this->assertNull($result);
  1284. }
  1285. public function testCreateTypo3WebsiteWithInvalidResponse(): void
  1286. {
  1287. $organizationFactory = $this->getOrganizationFactoryMockFor('createTypo3Website');
  1288. $organization = $this->getMockBuilder(Organization::class)->getMock();
  1289. $organization->method('getId')->willReturn(123);
  1290. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  1291. $response->method('getStatusCode')->willReturn(Response::HTTP_OK);
  1292. $response->method('getContent')->willReturn('<html lang="fr">Login page</html>');
  1293. $this->typo3Service->expects(self::once())->method('createSite')->with(123)->willReturn($response);
  1294. $this->logger
  1295. ->expects(self::once())
  1296. ->method('critical')
  1297. ->with('/!\ A critical error happened while creating the Typo3 website');
  1298. $result = $organizationFactory->createTypo3Website($organization);
  1299. $this->assertNull($result);
  1300. }
  1301. public function testUpdateAdminassosDb(): void
  1302. {
  1303. $organizationFactory = $this->getOrganizationFactoryMockFor('updateAdminassosDb');
  1304. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  1305. $response->method('getStatusCode')->willReturn(Response::HTTP_OK);
  1306. $this->apiLegacyRequestService
  1307. ->expects(self::once())
  1308. ->method('post')
  1309. ->with('/_internal/secure/organization/creation-event', ['organizationId' => 123])
  1310. ->willReturn($response);
  1311. $organization = $this->getMockBuilder(Organization::class)->getMock();
  1312. $organization->method('getId')->willReturn(123);
  1313. $organizationFactory->updateAdminassosDb($organization);
  1314. }
  1315. public function testUpdateAdminassosDbError(): void
  1316. {
  1317. $organizationFactory = $this->getOrganizationFactoryMockFor('updateAdminassosDb');
  1318. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  1319. $response->method('getStatusCode')->willReturn(Response::HTTP_BAD_REQUEST);
  1320. $response->method('getContent')->willReturn('some error');
  1321. $this->apiLegacyRequestService
  1322. ->expects(self::once())
  1323. ->method('post')
  1324. ->with('/_internal/secure/organization/creation-event', ['organizationId' => 123])
  1325. ->willReturn($response);
  1326. $organization = $this->getMockBuilder(Organization::class)->getMock();
  1327. $organization->method('getId')->willReturn(123);
  1328. $this->expectException(\RuntimeException::class);
  1329. $this->expectExceptionMessage('An error happened while updating the adminassos database: some error');
  1330. $organizationFactory->updateAdminassosDb($organization);
  1331. }
  1332. public function testNormalizeIdentificationField(): void
  1333. {
  1334. $organizationFactory = $this->getOrganizationFactoryMockFor('normalizeIdentificationField');
  1335. $this->assertEquals(
  1336. 'c+est+une+phrase+normalisee+',
  1337. $organizationFactory->normalizeIdentificationField("C'est une phrase normalisée.")
  1338. );
  1339. }
  1340. }