OrganizationFactoryTest.php 60 KB

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