OrganizationFactoryTest.php 53 KB

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