OrganizationFactoryTest.php 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472
  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 deleteDolibarrSociety(Organization $organization): void
  110. {
  111. parent::deleteDolibarrSociety($organization);
  112. }
  113. public function deleteOrganizationFiles(Organization $organization): void
  114. {
  115. parent::deleteOrganizationFiles($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('getParameters')->willReturn($parameters);
  1004. $this->organizationRepository
  1005. ->expects(self::once())
  1006. ->method('find')
  1007. ->with(123)
  1008. ->willReturn($organization);
  1009. $this->entityManager->expects(self::once())->method('beginTransaction');
  1010. $this->entityManager->expects(self::once())->method('flush');
  1011. $this->entityManager->expects(self::once())->method('commit');
  1012. $this->entityManager->expects(self::never())->method('rollback');
  1013. $organizationFactory->expects(self::once())->method('deleteOrganizationAccesses')->with($organization);
  1014. $this->entityManager->expects(self::exactly(2))->method('remove')->withConsecutive(
  1015. [$parameters],
  1016. [$organization]
  1017. );
  1018. $organizationFactory->expects(self::once())->method('deleteTypo3Website')->with($organization);
  1019. $organizationFactory->expects(self::once())->method('deleteDolibarrSociety')->with($organization);
  1020. $organizationFactory->expects(self::once())->method('deleteOrganizationFiles')->with($organization);
  1021. $organizationFactory->expects(self::once())->method('deleteDirectoriesV1')->with($organization);
  1022. $organizationFactory->expects(self::once())->method('deleteDirectories59')->with($organization);
  1023. $organizationDeletionRequest
  1024. ->expects(self::once())
  1025. ->method('setStatus')
  1026. ->with(OrganizationDeletionRequest::STATUS_OK);
  1027. $result = $organizationFactory->delete($organizationDeletionRequest);
  1028. $this->assertEquals(
  1029. $organizationDeletionRequest,
  1030. $result
  1031. );
  1032. }
  1033. public function testDeleteWithRollback(): void
  1034. {
  1035. $organizationFactory = $this->getOrganizationFactoryMockFor('delete');
  1036. $organizationDeletionRequest = $this->getMockBuilder(OrganizationDeletionRequest::class)->getMock();
  1037. $organizationDeletionRequest->method('getOrganizationId')->willReturn(123);
  1038. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  1039. $organization = $this->getMockBuilder(Organization::class)->getMock();
  1040. $organization->method('getParameters')->willReturn($parameters);
  1041. $this->organizationRepository
  1042. ->expects(self::once())
  1043. ->method('find')
  1044. ->with(123)
  1045. ->willReturn($organization);
  1046. $this->entityManager->expects(self::once())->method('beginTransaction');
  1047. $this->entityManager->expects(self::never())->method('flush');
  1048. $this->entityManager->expects(self::never())->method('commit');
  1049. $this->entityManager->expects(self::once())->method('rollback');
  1050. $organizationFactory->expects(self::once())->method('deleteOrganizationAccesses')->with($organization);
  1051. $this->entityManager->method('remove')->willThrowException(new \Exception('some error'));
  1052. $organizationFactory->expects(self::never())->method('deleteTypo3Website');
  1053. $organizationFactory->expects(self::never())->method('deleteDolibarrSociety');
  1054. $organizationFactory->expects(self::never())->method('deleteOrganizationFiles');
  1055. $organizationFactory->expects(self::never())->method('deleteDirectoriesV1');
  1056. $organizationFactory->expects(self::never())->method('deleteDirectories59');
  1057. $organizationDeletionRequest
  1058. ->expects(self::never())
  1059. ->method('setStatus');
  1060. $this->logger
  1061. ->expects(self::once())
  1062. ->method('critical')
  1063. ->with($this->callback(function($arg) {
  1064. return is_string($arg) && str_contains($arg, 'An error happened, operation cancelled') && str_contains($arg, 'some error');
  1065. }));
  1066. $this->expectException(\Exception::class);
  1067. $organizationFactory->delete($organizationDeletionRequest);
  1068. }
  1069. public function testDeleteWithNonBlockingErrors(): void
  1070. {
  1071. $organizationFactory = $this->getOrganizationFactoryMockFor('delete');
  1072. $organizationDeletionRequest = $this->getMockBuilder(OrganizationDeletionRequest::class)->getMock();
  1073. $organizationDeletionRequest->method('getOrganizationId')->willReturn(123);
  1074. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  1075. $organization = $this->getMockBuilder(Organization::class)->getMock();
  1076. $organization->method('getParameters')->willReturn($parameters);
  1077. $this->organizationRepository
  1078. ->expects(self::once())
  1079. ->method('find')
  1080. ->with(123)
  1081. ->willReturn($organization);
  1082. $this->entityManager->expects(self::once())->method('beginTransaction');
  1083. $this->entityManager->expects(self::once())->method('flush');
  1084. $this->entityManager->expects(self::once())->method('commit');
  1085. $this->entityManager->expects(self::never())->method('rollback');
  1086. $organizationFactory->expects(self::once())->method('deleteOrganizationAccesses')->with($organization);
  1087. $this->entityManager->expects(self::exactly(2))->method('remove')->withConsecutive(
  1088. [$parameters],
  1089. [$organization]
  1090. );
  1091. $organizationFactory->expects(self::once())->method('deleteTypo3Website')->willThrowException(new \Exception('some error'));
  1092. $organizationFactory->expects(self::once())->method('deleteDolibarrSociety')->willThrowException(new \Exception('some error'));
  1093. $organizationFactory->expects(self::once())->method('deleteOrganizationFiles')->willThrowException(new \Exception('some error'));
  1094. $organizationFactory->expects(self::once())->method('deleteDirectoriesV1')->willThrowException(new \Exception('some error'));
  1095. $organizationFactory->expects(self::once())->method('deleteDirectories59')->willThrowException(new \Exception('some error'));
  1096. $organizationDeletionRequest
  1097. ->expects(self::once())
  1098. ->method('setStatus')
  1099. ->with(OrganizationDeletionRequest::STATUS_OK_WITH_ERRORS);
  1100. $this->logger
  1101. ->expects(self::exactly(5))
  1102. ->method('critical')
  1103. ->withConsecutive(
  1104. ["An error happened while deleting the Typo3 website, please proceed manually."],
  1105. ["An error happened while deleting the Dolibarr society, please proceed manually."],
  1106. ["An error happened while deleting the local directories, please proceed manually."],
  1107. ["An error happened while deleting the V1 directories, please proceed manually."],
  1108. ["An error happened while deleting the 5.9 directories, please proceed manually."],
  1109. );
  1110. $result = $organizationFactory->delete($organizationDeletionRequest);
  1111. $this->assertEquals(
  1112. $organizationDeletionRequest,
  1113. $result
  1114. );
  1115. }
  1116. public function testDeleteOrganizationAccesses(): void {
  1117. $organizationFactory = $this->getOrganizationFactoryMockFor('deleteOrganizationAccesses');
  1118. $organization = $this->getMockBuilder(Organization::class)->getMock();
  1119. $access1 = $this->getMockBuilder(Access::class)->getMock();
  1120. $access2 = $this->getMockBuilder(Access::class)->getMock();
  1121. $access_other = $this->getMockBuilder(Access::class)->getMock();
  1122. $person1 = $this->getMockBuilder(Person::class)->getMock();
  1123. $person1->method('getAccesses')->willReturn(new ArrayCollection([$access1]));
  1124. $access1->method('getPerson')->willReturn($person1);
  1125. $person2 = $this->getMockBuilder(Person::class)->getMock();
  1126. $person2->method('getAccesses')->willReturn(new ArrayCollection([$access2, $access_other]));
  1127. $access2->method('getPerson')->willReturn($person2);
  1128. $organization->method('getAccesses')->willReturn(new ArrayCollection([$access1, $access2]));
  1129. $this->entityManager
  1130. ->expects(self::exactly(3))
  1131. ->method('remove')
  1132. ->withConsecutive(
  1133. [$person1],
  1134. [$access1],
  1135. [$access2],
  1136. );
  1137. $organizationFactory->deleteOrganizationAccesses($organization);
  1138. }
  1139. }