OrganizationFactoryTest.php 63 KB

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