OrganizationFactoryTest.php 79 KB

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