OrganizationFactoryTest.php 60 KB

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