OrganizationFactoryTest.php 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259
  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\Access\FunctionType;
  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\Access\FunctionEnum;
  22. use App\Enum\Core\ContactPointTypeEnum;
  23. use App\Enum\Education\CycleEnum;
  24. use App\Enum\Network\NetworkEnum;
  25. use App\Enum\Organization\AddressPostalOrganizationTypeEnum;
  26. use App\Enum\Organization\LegalEnum;
  27. use App\Enum\Organization\PrincipalTypeEnum;
  28. use App\Enum\Organization\SettingsProductEnum;
  29. use App\Enum\Person\AddressPostalPersonTypeEnum;
  30. use App\Enum\Person\GenderEnum;
  31. use App\Repository\Access\FunctionTypeRepository;
  32. use App\Repository\Core\CountryRepository;
  33. use App\Repository\Organization\OrganizationIdentificationRepository;
  34. use App\Repository\Organization\OrganizationRepository;
  35. use App\Repository\Person\PersonRepository;
  36. use App\Service\ApiLegacy\ApiLegacyRequestService;
  37. use App\Service\Dolibarr\DolibarrApiService;
  38. use App\Service\File\FileManager;
  39. use App\Service\Organization\OrganizationFactory;
  40. use App\Service\Organization\Utils as OrganizationUtils;
  41. use App\Service\Typo3\BindFileService;
  42. use App\Service\Typo3\SubdomainService;
  43. use App\Service\Typo3\Typo3Service;
  44. use App\Service\Utils\DatesUtils;
  45. use Doctrine\Common\Collections\ArrayCollection;
  46. use Doctrine\ORM\EntityManagerInterface;
  47. use libphonenumber\PhoneNumber;
  48. use libphonenumber\PhoneNumberUtil;
  49. use PHPUnit\Framework\MockObject\MockObject;
  50. use PHPUnit\Framework\TestCase;
  51. use Psr\Log\LoggerInterface;
  52. use Symfony\Component\HttpFoundation\Response;
  53. use Symfony\Contracts\HttpClient\ResponseInterface;
  54. class TestableOrganizationFactory extends OrganizationFactory
  55. {
  56. public function setPhoneNumberUtil(PhoneNumberUtil $phoneNumberUtil): void
  57. {
  58. $this->phoneNumberUtil = $phoneNumberUtil;
  59. }
  60. public function interruptIfOrganizationExists(OrganizationCreationRequest $organizationCreationRequest): void
  61. {
  62. parent::interruptIfOrganizationExists($organizationCreationRequest);
  63. }
  64. public function validateSubdomain(string $subdomainValue): void
  65. {
  66. parent::validateSubdomain($subdomainValue);
  67. }
  68. public function makeOrganizationWithRelations(
  69. OrganizationCreationRequest $organizationCreationRequest,
  70. ): Organization {
  71. return parent::makeOrganizationWithRelations($organizationCreationRequest);
  72. }
  73. public function makeOrganization(OrganizationCreationRequest $organizationCreationRequest): Organization
  74. {
  75. return parent::makeOrganization($organizationCreationRequest);
  76. }
  77. public function makeParameters(OrganizationCreationRequest $organizationCreationRequest): Parameters
  78. {
  79. return parent::makeParameters($organizationCreationRequest);
  80. }
  81. public function makeSettings(OrganizationCreationRequest $organizationCreationRequest): Settings
  82. {
  83. return parent::makeSettings($organizationCreationRequest);
  84. }
  85. public function makePostalAddress(OrganizationCreationRequest $organizationCreationRequest): OrganizationAddressPostal
  86. {
  87. return parent::makePostalAddress($organizationCreationRequest);
  88. }
  89. public function makeContactPoint(OrganizationCreationRequest $organizationCreationRequest): ContactPoint
  90. {
  91. return parent::makeContactPoint($organizationCreationRequest);
  92. }
  93. public function makeNetworkOrganization(OrganizationCreationRequest $organizationCreationRequest): NetworkOrganization
  94. {
  95. return parent::makeNetworkOrganization($organizationCreationRequest);
  96. }
  97. public function makeAdminAccess(OrganizationCreationRequest $organizationCreationRequest): Access
  98. {
  99. return parent::makeAdminAccess($organizationCreationRequest);
  100. }
  101. public function makeCycles(): array
  102. {
  103. return parent::makeCycles();
  104. }
  105. public function makeAccess(int|OrganizationMemberCreationRequest $creationRequestData, FunctionEnum $function, \DateTime $creationDate, ?int $authorId): Access
  106. {
  107. return parent::makeAccess($creationRequestData, $function, $creationDate, $authorId);
  108. }
  109. public function makePersonPostalAddress(OrganizationMemberCreationRequest $organizationMemberCreationRequest, \DateTime $creationDate, ?int $authorId): PersonAddressPostal
  110. {
  111. return parent::makePersonPostalAddress($organizationMemberCreationRequest, $creationDate, $authorId);
  112. }
  113. public function makePersonContactPoint(OrganizationMemberCreationRequest $organizationMemberCreationRequest, \DateTime $creationDate, ?int $authorId): ContactPoint
  114. {
  115. return parent::makePersonContactPoint($organizationMemberCreationRequest, $creationDate, $authorId);
  116. }
  117. public function makeSubdomain(OrganizationCreationRequest $organizationCreationRequest): Subdomain
  118. {
  119. return parent::makeSubdomain($organizationCreationRequest);
  120. }
  121. public function createTypo3Website(Organization $organization): ?int
  122. {
  123. return parent::createTypo3Website($organization);
  124. }
  125. public function updateAdminassosDb(Organization $organization): void
  126. {
  127. parent::updateAdminassosDb($organization);
  128. }
  129. public function normalizeIdentificationField(string $value): string
  130. {
  131. return parent::normalizeIdentificationField($value);
  132. }
  133. public function deleteTypo3Website(int $organizationId): void
  134. {
  135. parent::deleteTypo3Website($organizationId);
  136. }
  137. public function switchDolibarrSocietyToProspect(int $organizationId): void
  138. {
  139. parent::switchDolibarrSocietyToProspect($organizationId);
  140. }
  141. public function getFutureOrphanPersons(Organization $organization): array
  142. {
  143. return parent::getFutureOrphanPersons($organization);
  144. }
  145. }
  146. class OrganizationFactoryTest extends TestCase
  147. {
  148. private readonly MockObject|SubdomainService $subdomainService;
  149. private readonly MockObject|OrganizationRepository $organizationRepository;
  150. private readonly MockObject|CountryRepository $countryRepository;
  151. private readonly MockObject|OrganizationUtils $organizationUtils;
  152. private readonly MockObject|Typo3Service $typo3Service;
  153. private readonly MockObject|DolibarrApiService $dolibarrApiService;
  154. private readonly MockObject|EntityManagerInterface $entityManager;
  155. private readonly MockObject|PersonRepository $personRepository;
  156. private readonly MockObject|BindFileService $bindFileService;
  157. private readonly MockObject|LoggerInterface $logger;
  158. private readonly MockObject|OrganizationIdentificationRepository $organizationIdentificationRepository;
  159. private readonly MockObject|ApiLegacyRequestService $apiLegacyRequestService;
  160. private readonly MockObject|PhoneNumberUtil $phoneNumberUtil;
  161. private readonly MockObject|FunctionTypeRepository $functionTypeRepository;
  162. private readonly MockObject|\App\Repository\Access\AccessRepository $accessRepository;
  163. public function setUp(): void
  164. {
  165. $this->subdomainService = $this->getMockBuilder(SubdomainService::class)->disableOriginalConstructor()->getMock();
  166. $this->organizationRepository = $this->getMockBuilder(OrganizationRepository::class)->disableOriginalConstructor()->getMock();
  167. $this->countryRepository = $this->getMockBuilder(CountryRepository::class)->disableOriginalConstructor()->getMock();
  168. $this->organizationUtils = $this->getMockBuilder(OrganizationUtils::class)->disableOriginalConstructor()->getMock();
  169. $this->typo3Service = $this->getMockBuilder(Typo3Service::class)->disableOriginalConstructor()->getMock();
  170. $this->dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)->disableOriginalConstructor()->getMock();
  171. $this->entityManager = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock();
  172. $this->personRepository = $this->getMockBuilder(PersonRepository::class)->disableOriginalConstructor()->getMock();
  173. $this->bindFileService = $this->getMockBuilder(BindFileService::class)->disableOriginalConstructor()->getMock();
  174. $this->logger = $this->getMockBuilder(LoggerInterface::class)->disableOriginalConstructor()->getMock();
  175. $this->organizationIdentificationRepository = $this->getMockBuilder(OrganizationIdentificationRepository::class)->disableOriginalConstructor()->getMock();
  176. $this->apiLegacyRequestService = $this->getMockBuilder(ApiLegacyRequestService::class)->disableOriginalConstructor()->getMock();
  177. $this->phoneNumberUtil = $this->getMockBuilder(PhoneNumberUtil::class)->disableOriginalConstructor()->getMock();
  178. $this->functionTypeRepository = $this->getMockBuilder(FunctionTypeRepository::class)->disableOriginalConstructor()->getMock();
  179. $this->fileManager = $this->getMockBuilder(FileManager::class)->disableOriginalConstructor()->getMock();
  180. $this->accessRepository = $this->getMockBuilder(\App\Repository\Access\AccessRepository::class)->disableOriginalConstructor()->getMock();
  181. }
  182. public function tearDown(): void
  183. {
  184. DatesUtils::clearFakeDatetime();
  185. }
  186. private function getOrganizationFactoryMockFor(string $methodName): TestableOrganizationFactory|MockObject
  187. {
  188. $organizationFactory = $this
  189. ->getMockBuilder(TestableOrganizationFactory::class)
  190. ->setConstructorArgs(
  191. [
  192. $this->subdomainService,
  193. $this->organizationRepository,
  194. $this->countryRepository,
  195. $this->organizationUtils,
  196. $this->typo3Service,
  197. $this->dolibarrApiService,
  198. $this->entityManager,
  199. $this->personRepository,
  200. $this->bindFileService,
  201. $this->organizationIdentificationRepository,
  202. $this->apiLegacyRequestService,
  203. $this->functionTypeRepository,
  204. $this->fileManager,
  205. $this->accessRepository,
  206. ])
  207. ->setMethodsExcept(['setLoggerInterface', 'setPhoneNumberUtil', $methodName])
  208. ->getMock();
  209. $organizationFactory->setLoggerInterface($this->logger);
  210. $organizationFactory->setPhoneNumberUtil($this->phoneNumberUtil);
  211. return $organizationFactory;
  212. }
  213. public function testCreate(): void
  214. {
  215. $organizationFactory = $this->getOrganizationFactoryMockFor('create');
  216. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  217. $organizationCreationRequest->method('getName')->willReturn('foo');
  218. $organizationCreationRequest->method('getSubdomain')->willReturn('subdomain');
  219. $organizationCreationRequest->method('isClient')->willReturn(false);
  220. $organizationCreationRequest->method('getCreateWebsite')->willReturn(true);
  221. $this->entityManager->expects(self::once())->method('beginTransaction');
  222. $organizationFactory->expects(self::once())->method('interruptIfOrganizationExists');
  223. $organizationFactory->expects(self::once())->method('validateSubdomain')->with('subdomain');
  224. $organization = $this->getMockBuilder(Organization::class)->getMock();
  225. $organizationFactory
  226. ->expects(self::once())
  227. ->method('makeOrganizationWithRelations')
  228. ->with($organizationCreationRequest)
  229. ->willReturn($organization);
  230. $this->entityManager->expects(self::once())->method('persist')->with($organization);
  231. $this->entityManager->expects(self::once())->method('flush');
  232. $this->entityManager->expects(self::once())->method('commit');
  233. $this->dolibarrApiService
  234. ->expects(self::once())
  235. ->method('createSociety')
  236. ->with($organization, false)
  237. ->willReturn(456);
  238. $this->bindFileService
  239. ->expects(self::once())
  240. ->method('registerSubdomain')
  241. ->with('subdomain');
  242. $organizationFactory
  243. ->expects(self::once())
  244. ->method('createTypo3Website')
  245. ->with($organization)
  246. ->willReturn(789);
  247. $this->logger
  248. ->method('info')
  249. ->withConsecutive(
  250. ["Start the creation of a new organization named 'foo'"],
  251. ["Subdomain is valid and available : 'subdomain'"],
  252. ['Organization created with all its relations'],
  253. ['Organization persisted in the DB'],
  254. ['New dolibarr structure created (uid : 456)'],
  255. ['Subdomain registered'],
  256. ['Typo3 website created (root uid: 789)'],
  257. ['Adminassos db updated']
  258. );
  259. $organizationCreationRequest
  260. ->expects(self::once())
  261. ->method('setStatus')
  262. ->with(OrganizationCreationRequest::STATUS_OK);
  263. $result = $organizationFactory->create($organizationCreationRequest);
  264. $this->assertEquals(
  265. $organization,
  266. $result
  267. );
  268. }
  269. public function testCreateWithRollback(): void
  270. {
  271. $organizationFactory = $this->getOrganizationFactoryMockFor('create');
  272. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  273. $organizationCreationRequest->method('getName')->willReturn('foo');
  274. $organizationCreationRequest->method('getSubdomain')->willReturn('subdomain');
  275. $organizationCreationRequest->method('isClient')->willReturn(false);
  276. $organizationCreationRequest->method('getCreateWebsite')->willReturn(true);
  277. $this->entityManager->expects(self::once())->method('beginTransaction');
  278. $organizationFactory->expects(self::once())->method('interruptIfOrganizationExists');
  279. $organizationFactory->expects(self::once())->method('validateSubdomain')->with('subdomain');
  280. $organization = $this->getMockBuilder(Organization::class)->getMock();
  281. $organizationFactory
  282. ->expects(self::once())
  283. ->method('makeOrganizationWithRelations')
  284. ->with($organizationCreationRequest)
  285. ->willReturn($organization);
  286. $this->entityManager->expects(self::once())->method('persist')->with($organization);
  287. $this->entityManager->expects(self::once())->method('flush')->willThrowException(new \RuntimeException('some error'));
  288. $this->entityManager->expects(self::once())->method('rollback');
  289. $this->dolibarrApiService
  290. ->expects(self::never())
  291. ->method('createSociety');
  292. $this->bindFileService
  293. ->expects(self::never())
  294. ->method('registerSubdomain');
  295. $organizationFactory
  296. ->expects(self::never())
  297. ->method('createTypo3Website');
  298. $this->logger
  299. ->method('info')
  300. ->withConsecutive(
  301. ["Start the creation of a new organization named 'foo'"],
  302. ["Subdomain is valid and available : 'subdomain'"],
  303. ['Organization created with all its relations']
  304. );
  305. $this->logger
  306. ->method('critical')
  307. ->with(
  308. $this->matchesRegularExpression('/^An error happened, operation cancelled\nRuntimeException: some error.*/')
  309. );
  310. $this->expectException(\RuntimeException::class);
  311. $result = $organizationFactory->create($organizationCreationRequest);
  312. }
  313. public function testCreateWithExistingOrganization(): void
  314. {
  315. $organizationFactory = $this->getOrganizationFactoryMockFor('create');
  316. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  317. $organizationCreationRequest->method('getName')->willReturn('foo');
  318. $this->entityManager->expects(self::once())->method('beginTransaction');
  319. $organizationFactory->expects(self::once())->method('interruptIfOrganizationExists')->willThrowException(new \RuntimeException('An organization named foo already exists'));
  320. $organizationFactory->expects(self::never())->method('validateSubdomain');
  321. $organizationFactory
  322. ->expects(self::never())
  323. ->method('makeOrganizationWithRelations');
  324. $this->entityManager->expects(self::never())->method('persist');
  325. $this->entityManager->expects(self::never())->method('flush');
  326. $this->entityManager->expects(self::once())->method('rollback');
  327. $this->dolibarrApiService
  328. ->expects(self::never())
  329. ->method('createSociety');
  330. $this->bindFileService
  331. ->expects(self::never())
  332. ->method('registerSubdomain');
  333. $organizationFactory
  334. ->expects(self::never())
  335. ->method('createTypo3Website');
  336. $this->logger
  337. ->method('info')
  338. ->withConsecutive(
  339. ["Start the creation of a new organization named 'foo'"],
  340. );
  341. $this->logger
  342. ->method('critical')
  343. ->with(
  344. $this->matchesRegularExpression("/^An error happened, operation cancelled\nRuntimeException: An organization named foo already exists.*/")
  345. );
  346. $this->expectException(\RuntimeException::class);
  347. $this->expectExceptionMessage('An organization named foo already exists');
  348. $result = $organizationFactory->create($organizationCreationRequest);
  349. }
  350. public function testCreateNoWebsiteAndIsClient(): void
  351. {
  352. $organizationFactory = $this->getOrganizationFactoryMockFor('create');
  353. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  354. $organizationCreationRequest->method('getName')->willReturn('foo');
  355. $organizationCreationRequest->method('getSubdomain')->willReturn('subdomain');
  356. $organizationCreationRequest->method('isClient')->willReturn(true);
  357. $organizationCreationRequest->method('getCreateWebsite')->willReturn(false);
  358. $organizationFactory->expects(self::once())->method('interruptIfOrganizationExists');
  359. $organization = $this->getMockBuilder(Organization::class)->getMock();
  360. $organizationFactory
  361. ->expects(self::once())
  362. ->method('makeOrganizationWithRelations')
  363. ->with($organizationCreationRequest)
  364. ->willReturn($organization);
  365. $this->dolibarrApiService
  366. ->method('createSociety')
  367. ->with($organization, true)
  368. ->willReturn(456);
  369. $organizationFactory
  370. ->expects(self::never())
  371. ->method('createTypo3Website');
  372. $this->logger
  373. ->expects(self::once())
  374. ->method('warning')
  375. ->with('Typo3 website creation was not required');
  376. $result = $organizationFactory->create($organizationCreationRequest);
  377. $this->assertEquals(
  378. $organization,
  379. $result
  380. );
  381. }
  382. public function testCreateWithErrors(): void
  383. {
  384. $organizationFactory = $this->getOrganizationFactoryMockFor('create');
  385. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  386. $organizationCreationRequest->method('getName')->willReturn('foo');
  387. $organizationCreationRequest->method('getSubdomain')->willReturn('subdomain');
  388. $organizationCreationRequest->method('isClient')->willReturn(true);
  389. $organizationCreationRequest->method('getCreateWebsite')->willReturn(true);
  390. $organizationFactory->expects(self::once())->method('interruptIfOrganizationExists');
  391. $organization = $this->getMockBuilder(Organization::class)->getMock();
  392. $organizationFactory
  393. ->expects(self::once())
  394. ->method('makeOrganizationWithRelations')
  395. ->with($organizationCreationRequest)
  396. ->willReturn($organization);
  397. $this->dolibarrApiService
  398. ->method('createSociety')
  399. ->with($organization, true)
  400. ->willThrowException(new \RuntimeException('An error happened'));
  401. $this->bindFileService
  402. ->method('registerSubdomain')
  403. ->with('subdomain')
  404. ->willThrowException(new \RuntimeException('An error happened'));
  405. $organizationFactory
  406. ->method('createTypo3Website')
  407. ->with($organization)
  408. ->willThrowException(new \RuntimeException('An error happened'));
  409. $organizationFactory
  410. ->method('updateAdminassosDb')
  411. ->with($organization)
  412. ->willThrowException(new \RuntimeException('An error happened'));
  413. $this->logger
  414. ->expects(self::exactly(4))
  415. ->method('critical')
  416. ->willReturnOnConsecutiveCalls([
  417. 'An error happened while creating the dolibarr society, please proceed manually.',
  418. 'An error happened while updating the bind file, please proceed manually.',
  419. 'An error happened while creating the typo3 website, please proceed manually.',
  420. 'An error happened while updating the adminassos db, please proceed manually.',
  421. ]);
  422. $this->logger
  423. ->expects(self::once())
  424. ->method('warning')
  425. ->with('-- Operation ended with errors, check the logs for more information --');
  426. $organizationCreationRequest
  427. ->expects(self::once())
  428. ->method('setStatus')
  429. ->with(OrganizationCreationRequest::STATUS_OK_WITH_ERRORS);
  430. $result = $organizationFactory->create($organizationCreationRequest);
  431. $this->assertEquals(
  432. $organization,
  433. $result
  434. );
  435. }
  436. public function testInterruptIfOrganizationExistsNotExisting(): void
  437. {
  438. $organizationFactory = $this->getOrganizationFactoryMockFor('interruptIfOrganizationExists');
  439. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  440. $organizationCreationRequest->method('getName')->willReturn('foo');
  441. $organizationCreationRequest->method('getSiretNumber')->willReturn('');
  442. $organizationCreationRequest->method('getWaldecNumber')->willReturn('');
  443. $organizationCreationRequest->method('getIdentifier')->willReturn('');
  444. $organizationCreationRequest->method('getStreetAddress1')->willReturn('part1');
  445. $organizationCreationRequest->method('getStreetAddress2')->willReturn('part2');
  446. $organizationCreationRequest->method('getStreetAddress3')->willReturn('part3');
  447. $organizationCreationRequest->method('getCity')->willReturn('Paris');
  448. $organizationCreationRequest->method('getPostalCode')->willReturn('75000');
  449. $this->organizationIdentificationRepository
  450. ->expects(self::exactly(2))
  451. ->method('findOneBy')
  452. ->willReturn(null);
  453. $organizationFactory
  454. ->method('normalizeIdentificationField')
  455. ->willReturnMap([
  456. ['foo', 'foo'],
  457. ['part1 part2 part3', 'part1 part2 part3'],
  458. ]);
  459. $organizationFactory->interruptIfOrganizationExists($organizationCreationRequest);
  460. }
  461. public function testInterruptIfOrganizationExistsNotExistingNonExistingWithIdentifiers(): void
  462. {
  463. $organizationFactory = $this->getOrganizationFactoryMockFor('interruptIfOrganizationExists');
  464. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  465. $organizationCreationRequest->method('getName')->willReturn('foo');
  466. $organizationCreationRequest->method('getSiretNumber')->willReturn('123456');
  467. $organizationCreationRequest->method('getWaldecNumber')->willReturn('W123456');
  468. $organizationCreationRequest->method('getIdentifier')->willReturn('FR000000000000');
  469. $organizationCreationRequest->method('getStreetAddress1')->willReturn('part1');
  470. $organizationCreationRequest->method('getStreetAddress2')->willReturn('part2');
  471. $organizationCreationRequest->method('getStreetAddress3')->willReturn('part3');
  472. $organizationCreationRequest->method('getCity')->willReturn('Paris');
  473. $organizationCreationRequest->method('getPostalCode')->willReturn('75000');
  474. $organizationFactory
  475. ->method('normalizeIdentificationField')
  476. ->willReturnMap([
  477. ['foo', 'foo'],
  478. ['part1 part2 part3', 'part1 part2 part3'],
  479. ]);
  480. $this->organizationIdentificationRepository
  481. ->expects(self::exactly(5))
  482. ->method('findOneBy')
  483. ->willReturnMap([
  484. [['siretNumber' => '123456'], null, null],
  485. [['waldecNumber' => 'W123456'], null, null],
  486. [['identifier' => 'FR000000000000'], null, null],
  487. [['normalizedName' => 'foo', 'addressCity' => 'Paris'], null, null],
  488. [['normalizedAddress' => 'part1 part2 part3', 'addressCity' => 'Paris', 'postalCode' => '75000'], null, null],
  489. ]);
  490. $organizationFactory->interruptIfOrganizationExists($organizationCreationRequest);
  491. }
  492. public function testInterruptIfOrganizationExistsExistingWithSiret(): void
  493. {
  494. $organizationFactory = $this->getOrganizationFactoryMockFor('interruptIfOrganizationExists');
  495. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  496. $organizationCreationRequest->method('getName')->willReturn('foo');
  497. $organizationCreationRequest->method('getSiretNumber')->willReturn('123456');
  498. $organizationCreationRequest->method('getWaldecNumber')->willReturn('W123456');
  499. $organizationCreationRequest->method('getIdentifier')->willReturn('FR000000000000');
  500. $organizationCreationRequest->method('getStreetAddress1')->willReturn('part1');
  501. $organizationCreationRequest->method('getStreetAddress2')->willReturn('part2');
  502. $organizationCreationRequest->method('getStreetAddress3')->willReturn('part3');
  503. $organizationCreationRequest->method('getCity')->willReturn('Paris');
  504. $organizationCreationRequest->method('getPostalCode')->willReturn('75000');
  505. $organizationFactory
  506. ->method('normalizeIdentificationField')
  507. ->willReturnMap([
  508. ['foo', 'foo'],
  509. ['part1 part2 part3', 'part1 part2 part3'],
  510. ]);
  511. $organization = $this->getMockBuilder(Organization::class)->getMock();
  512. $this->organizationIdentificationRepository
  513. ->method('findOneBy')
  514. ->willReturnMap([
  515. [['siretNumber' => '123456'], null, $organization],
  516. [['waldecNumber' => 'W123456'], null, null],
  517. [['identifier' => 'FR000000000000'], null, null],
  518. [['normalizedName' => 'foo', 'addressCity' => 'Paris'], null, null],
  519. [['normalizedAddress' => 'part1 part2 part3', 'addressCity' => 'Paris', 'postalCode' => '75000'], null, null],
  520. ]);
  521. $this->expectException(\RuntimeException::class);
  522. $this->expectExceptionMessage("This siret number is already registered : '123456'");
  523. $organizationFactory->interruptIfOrganizationExists($organizationCreationRequest);
  524. }
  525. public function testInterruptIfOrganizationExistsExistingWithWaldec(): void
  526. {
  527. $organizationFactory = $this->getOrganizationFactoryMockFor('interruptIfOrganizationExists');
  528. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  529. $organizationCreationRequest->method('getName')->willReturn('foo');
  530. $organizationCreationRequest->method('getSiretNumber')->willReturn('123456');
  531. $organizationCreationRequest->method('getWaldecNumber')->willReturn('W123456');
  532. $organizationCreationRequest->method('getIdentifier')->willReturn('FR000000000000');
  533. $organizationCreationRequest->method('getStreetAddress1')->willReturn('part1');
  534. $organizationCreationRequest->method('getStreetAddress2')->willReturn('part2');
  535. $organizationCreationRequest->method('getStreetAddress3')->willReturn('part3');
  536. $organizationCreationRequest->method('getCity')->willReturn('Paris');
  537. $organizationCreationRequest->method('getPostalCode')->willReturn('75000');
  538. $organizationFactory
  539. ->method('normalizeIdentificationField')
  540. ->willReturnMap([
  541. ['foo', 'foo'],
  542. ['part1 part2 part3', 'part1 part2 part3'],
  543. ]);
  544. $organization = $this->getMockBuilder(Organization::class)->getMock();
  545. $this->organizationIdentificationRepository
  546. ->method('findOneBy')
  547. ->willReturnMap([
  548. [['siretNumber' => '123456'], null, null],
  549. [['waldecNumber' => 'W123456'], null, $organization],
  550. [['identifier' => 'FR000000000000'], null, null],
  551. [['normalizedName' => 'foo', 'addressCity' => 'Paris'], null, null],
  552. [['normalizedAddress' => 'part1 part2 part3', 'addressCity' => 'Paris', 'postalCode' => '75000'], null, null],
  553. ]);
  554. $this->expectException(\RuntimeException::class);
  555. $this->expectExceptionMessage("This RNA identifier (waldec number) is already registered : 'W123456'");
  556. $organizationFactory->interruptIfOrganizationExists($organizationCreationRequest);
  557. }
  558. public function testInterruptIfOrganizationExistsExistingWithCMFIdentifier(): void
  559. {
  560. $organizationFactory = $this->getOrganizationFactoryMockFor('interruptIfOrganizationExists');
  561. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  562. $organizationCreationRequest->method('getName')->willReturn('foo');
  563. $organizationCreationRequest->method('getSiretNumber')->willReturn('123456');
  564. $organizationCreationRequest->method('getWaldecNumber')->willReturn('W123456');
  565. $organizationCreationRequest->method('getIdentifier')->willReturn('FR000000000000');
  566. $organizationCreationRequest->method('getStreetAddress1')->willReturn('part1');
  567. $organizationCreationRequest->method('getStreetAddress2')->willReturn('part2');
  568. $organizationCreationRequest->method('getStreetAddress3')->willReturn('part3');
  569. $organizationCreationRequest->method('getCity')->willReturn('Paris');
  570. $organizationCreationRequest->method('getPostalCode')->willReturn('75000');
  571. $organizationFactory
  572. ->method('normalizeIdentificationField')
  573. ->willReturnMap([
  574. ['foo', 'foo'],
  575. ['part1 part2 part3', 'part1 part2 part3'],
  576. ]);
  577. $organization = $this->getMockBuilder(Organization::class)->getMock();
  578. $this->organizationIdentificationRepository
  579. ->method('findOneBy')
  580. ->willReturnMap([
  581. [['siretNumber' => '123456'], null, null],
  582. [['waldecNumber' => 'W123456'], null, null],
  583. [['identifier' => 'FR000000000000'], null, $organization],
  584. [['normalizedName' => 'foo', 'addressCity' => 'Paris'], null, null],
  585. [['normalizedAddress' => 'part1 part2 part3', 'addressCity' => 'Paris', 'postalCode' => '75000'], null, null],
  586. ]);
  587. $this->expectException(\RuntimeException::class);
  588. $this->expectExceptionMessage("This CMF identifier is already registered : 'FR000000000000'");
  589. $organizationFactory->interruptIfOrganizationExists($organizationCreationRequest);
  590. }
  591. public function testInterruptIfOrganizationExistsExistingWithSameNameAndCity(): void
  592. {
  593. $organizationFactory = $this->getOrganizationFactoryMockFor('interruptIfOrganizationExists');
  594. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  595. $organizationCreationRequest->method('getName')->willReturn('foo');
  596. $organizationCreationRequest->method('getSiretNumber')->willReturn('123456');
  597. $organizationCreationRequest->method('getWaldecNumber')->willReturn('W123456');
  598. $organizationCreationRequest->method('getIdentifier')->willReturn('FR000000000000');
  599. $organizationCreationRequest->method('getStreetAddress1')->willReturn('part1');
  600. $organizationCreationRequest->method('getStreetAddress2')->willReturn('part2');
  601. $organizationCreationRequest->method('getStreetAddress3')->willReturn('part3');
  602. $organizationCreationRequest->method('getCity')->willReturn('Paris');
  603. $organizationCreationRequest->method('getPostalCode')->willReturn('75000');
  604. $organizationFactory
  605. ->method('normalizeIdentificationField')
  606. ->willReturnMap([
  607. ['foo', 'foo'],
  608. ['part1 part2 part3', 'part1 part2 part3'],
  609. ]);
  610. $organization = $this->getMockBuilder(Organization::class)->getMock();
  611. $this->organizationIdentificationRepository
  612. ->method('findOneBy')
  613. ->willReturnMap([
  614. [['siretNumber' => '123456'], null, null],
  615. [['waldecNumber' => 'W123456'], null, null],
  616. [['identifier' => 'FR000000000000'], null, null],
  617. [['normalizedName' => 'foo', 'addressCity' => 'Paris'], null, $organization],
  618. [['normalizedAddress' => 'part1 part2 part3', 'addressCity' => 'Paris', 'postalCode' => '75000'], null, null],
  619. ]);
  620. $this->expectException(\RuntimeException::class);
  621. $this->expectExceptionMessage("An organization named 'foo' already exists in Paris");
  622. $organizationFactory->interruptIfOrganizationExists($organizationCreationRequest);
  623. }
  624. public function testInterruptIfOrganizationExistsExistingWithSameAddress(): void
  625. {
  626. $organizationFactory = $this->getOrganizationFactoryMockFor('interruptIfOrganizationExists');
  627. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  628. $organizationCreationRequest->method('getName')->willReturn('foo');
  629. $organizationCreationRequest->method('getSiretNumber')->willReturn('123456');
  630. $organizationCreationRequest->method('getWaldecNumber')->willReturn('W123456');
  631. $organizationCreationRequest->method('getIdentifier')->willReturn('FR000000000000');
  632. $organizationCreationRequest->method('getStreetAddress1')->willReturn('part1');
  633. $organizationCreationRequest->method('getStreetAddress2')->willReturn('part2');
  634. $organizationCreationRequest->method('getStreetAddress3')->willReturn('part3');
  635. $organizationCreationRequest->method('getCity')->willReturn('Paris');
  636. $organizationCreationRequest->method('getPostalCode')->willReturn('75000');
  637. $organizationFactory
  638. ->method('normalizeIdentificationField')
  639. ->willReturnMap([
  640. ['foo', 'foo'],
  641. ['part1 part2 part3', 'part1 part2 part3'],
  642. ]);
  643. $organization = $this->getMockBuilder(Organization::class)->getMock();
  644. $this->organizationIdentificationRepository
  645. ->method('findOneBy')
  646. ->willReturnMap([
  647. [['siretNumber' => '123456'], null, null],
  648. [['waldecNumber' => 'W123456'], null, null],
  649. [['identifier' => 'FR000000000000'], null, null],
  650. [['normalizedName' => 'foo', 'addressCity' => 'Paris'], null, null],
  651. [['normalizedAddress' => 'part1 part2 part3', 'addressCity' => 'Paris', 'postalCode' => '75000'], null, $organization],
  652. ]);
  653. $this->expectException(\RuntimeException::class);
  654. $this->expectExceptionMessage('An organization already exists at this address.');
  655. $organizationFactory->interruptIfOrganizationExists($organizationCreationRequest);
  656. }
  657. public function testValidateSubdomain(): void
  658. {
  659. $organizationFactory = $this->getOrganizationFactoryMockFor('validateSubdomain');
  660. $this->subdomainService->expects(self::once())->method('isValidSubdomain')->willReturn(true);
  661. $this->subdomainService->expects(self::once())->method('isReservedSubdomain')->willReturn(false);
  662. $this->subdomainService->expects(self::once())->method('isRegistered')->willReturn(false);
  663. $organizationFactory->validateSubdomain('foo');
  664. }
  665. public function testValidateSubdomainIsNotValid(): void
  666. {
  667. $organizationFactory = $this->getOrganizationFactoryMockFor('validateSubdomain');
  668. $this->subdomainService->method('isValidSubdomain')->willReturn(false);
  669. $this->subdomainService->method('isReservedSubdomain')->willReturn(false);
  670. $this->subdomainService->method('isRegistered')->willReturn(false);
  671. $this->expectException(\RuntimeException::class);
  672. $this->expectExceptionMessage('Not a valid subdomain : foo');
  673. $organizationFactory->validateSubdomain('foo');
  674. }
  675. public function testValidateSubdomainIsReserved(): void
  676. {
  677. $organizationFactory = $this->getOrganizationFactoryMockFor('validateSubdomain');
  678. $this->subdomainService->method('isValidSubdomain')->willReturn(true);
  679. $this->subdomainService->method('isReservedSubdomain')->willReturn(true);
  680. $this->subdomainService->method('isRegistered')->willReturn(false);
  681. $this->expectException(\RuntimeException::class);
  682. $this->expectExceptionMessage('This subdomain is not available : foo');
  683. $organizationFactory->validateSubdomain('foo');
  684. }
  685. public function testValidateSubdomainIsRegistered(): void
  686. {
  687. $organizationFactory = $this->getOrganizationFactoryMockFor('validateSubdomain');
  688. $this->subdomainService->method('isValidSubdomain')->willReturn(true);
  689. $this->subdomainService->method('isReservedSubdomain')->willReturn(false);
  690. $this->subdomainService->method('isRegistered')->willReturn(true);
  691. $this->expectException(\RuntimeException::class);
  692. $this->expectExceptionMessage('This subdomain is already registered : foo');
  693. $organizationFactory->validateSubdomain('foo');
  694. }
  695. public function testMakeOrganizationWithRelations(): void
  696. {
  697. $organizationFactory = $this->getOrganizationFactoryMockFor('makeOrganizationWithRelations');
  698. $creationDate = $this->getMockBuilder(\DateTime::class)->getMock();
  699. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  700. $organizationCreationRequest->method('getCreationDate')->willReturn($creationDate);
  701. $organizationCreationRequest->method('getAuthorId')->willReturn(1);
  702. $organization = $this->getMockBuilder(Organization::class)->getMock();
  703. // Création de l'organisation
  704. $organizationFactory
  705. ->expects(self::once())
  706. ->method('makeOrganization')
  707. ->with($organizationCreationRequest)
  708. ->willReturn($organization);
  709. // Création des Parameters
  710. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  711. $organizationFactory
  712. ->expects(self::once())
  713. ->method('makeParameters')
  714. ->with($organizationCreationRequest)
  715. ->willReturn($parameters);
  716. $organization->expects(self::once())->method('setParameters')->with($parameters);
  717. // Création des Settings
  718. $settings = $this->getMockBuilder(Settings::class)->getMock();
  719. $organizationFactory
  720. ->expects(self::once())
  721. ->method('makeSettings')
  722. ->with($organizationCreationRequest)
  723. ->willReturn($settings);
  724. $organization->expects(self::once())->method('setSettings')->with($settings);
  725. // Création de l'adresse postale
  726. $organizationAddressPostal = $this->getMockBuilder(OrganizationAddressPostal::class)->getMock();
  727. $organizationFactory
  728. ->expects(self::once())
  729. ->method('makePostalAddress')
  730. ->with($organizationCreationRequest)
  731. ->willReturn($organizationAddressPostal);
  732. $organization->expects(self::once())->method('addOrganizationAddressPostal')->with($organizationAddressPostal);
  733. // Création du point de contact
  734. $contactPoint = $this->getMockBuilder(ContactPoint::class)->getMock();
  735. $organizationFactory
  736. ->expects(self::once())
  737. ->method('makeContactPoint')
  738. ->with($organizationCreationRequest)
  739. ->willReturn($contactPoint);
  740. $organization->expects(self::once())->method('addContactPoint')->with($contactPoint);
  741. // Rattachement au réseau
  742. $networkOrganization = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  743. $organizationFactory
  744. ->expects(self::once())
  745. ->method('makeNetworkOrganization')
  746. ->with($organizationCreationRequest)
  747. ->willReturn($networkOrganization);
  748. $organization->expects(self::once())->method('addNetworkOrganization')->with($networkOrganization);
  749. // Créé l'admin
  750. $adminAccess = $this->getMockBuilder(Access::class)->getMock();
  751. $organizationFactory
  752. ->expects(self::once())
  753. ->method('makeAdminAccess')
  754. ->with($organizationCreationRequest)
  755. ->willReturn($adminAccess);
  756. // Le `$organization->expects(...)->method('addAccess')` est implémenté plus loin,
  757. // après la création du président et du directeur.
  758. // Création des cycles
  759. $cycle1 = $this->getMockBuilder(Cycle::class)->getMock();
  760. $cycle2 = $this->getMockBuilder(Cycle::class)->getMock();
  761. $cycle3 = $this->getMockBuilder(Cycle::class)->getMock();
  762. $organizationFactory
  763. ->expects(self::once())
  764. ->method('makeCycles')
  765. ->willReturn([$cycle1, $cycle2, $cycle3]);
  766. $organization->expects(self::exactly(3))->method('addCycle')->withConsecutive([$cycle1], [$cycle2], [$cycle3]);
  767. // Création du président et du directeur
  768. $organizationMemberCreationRequest1 = $this->getMockBuilder(OrganizationMemberCreationRequest::class)->getMock();
  769. $organizationCreationRequest->method('getPresident')->willReturn($organizationMemberCreationRequest1);
  770. $organizationMemberCreationRequest2 = $this->getMockBuilder(OrganizationMemberCreationRequest::class)->getMock();
  771. $organizationCreationRequest->method('getDirector')->willReturn($organizationMemberCreationRequest2);
  772. $access1 = $this->getMockBuilder(Access::class)->getMock();
  773. $access2 = $this->getMockBuilder(Access::class)->getMock();
  774. $organizationFactory
  775. ->expects(self::exactly(2))
  776. ->method('makeAccess')
  777. ->willReturnMap([
  778. [$organizationMemberCreationRequest1, FunctionEnum::PRESIDENT, $creationDate, 1, $access1],
  779. [$organizationMemberCreationRequest2, FunctionEnum::DIRECTOR, $creationDate, 1, $access2],
  780. ]);
  781. $organization
  782. ->expects(self::exactly(3))
  783. ->method('addAccess')
  784. ->withConsecutive([$adminAccess], [$access1], [$access2]);
  785. // Création du sous-domaine
  786. $subdomain = $this->getMockBuilder(Subdomain::class)->getMock();
  787. $organizationFactory
  788. ->expects(self::once())
  789. ->method('makeSubdomain')
  790. ->with($organizationCreationRequest)
  791. ->willReturn($subdomain);
  792. $organization->expects(self::once())->method('addSubdomain')->with($subdomain);
  793. // Enregistrement du sous domaine dans Parameters (retrocompatibilité v1)
  794. $organization->method('getParameters')->willReturn($parameters);
  795. $organizationCreationRequest->method('getSubdomain')->willReturn('foo');
  796. $parameters->expects(self::once())->method('setSubDomain')->with('foo');
  797. $parameters->expects(self::once())->method('setOtherWebsite')->with('https://foo.opentalent.fr');
  798. $result = $organizationFactory->makeOrganizationWithRelations($organizationCreationRequest);
  799. $this->assertEquals(
  800. $result,
  801. $organization
  802. );
  803. }
  804. public function testMakeOrganizationWithRelationsNoPresidentNoDirector(): void
  805. {
  806. $organizationFactory = $this->getOrganizationFactoryMockFor('makeOrganizationWithRelations');
  807. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  808. $organization = $this->getMockBuilder(Organization::class)->getMock();
  809. // Création de l'organisation
  810. $organizationFactory
  811. ->expects(self::once())
  812. ->method('makeOrganization')
  813. ->with($organizationCreationRequest)
  814. ->willReturn($organization);
  815. $organizationCreationRequest->method('getPresident')->willReturn(null);
  816. $organizationCreationRequest->method('getDirector')->willReturn(null);
  817. // Un seul appel, pour l'adminAccess
  818. $organization
  819. ->expects(self::once())
  820. ->method('addAccess');
  821. $organizationFactory->makeOrganizationWithRelations($organizationCreationRequest);
  822. }
  823. public function testMakeOrganization(): void
  824. {
  825. $organizationFactory = $this->getOrganizationFactoryMockFor('makeOrganization');
  826. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  827. $organizationCreationRequest->method('getName')->willReturn('My Organization');
  828. $organizationCreationRequest->method('getLegalStatus')->willReturn(LegalEnum::ASSOCIATION_LAW_1901);
  829. $organizationCreationRequest->method('getPrincipalType')->willReturn(PrincipalTypeEnum::ARTISTIC_EDUCATION_ONLY);
  830. $organization = $organizationFactory->makeOrganization($organizationCreationRequest);
  831. $this->assertEquals(
  832. 'My Organization',
  833. $organization->getName()
  834. );
  835. $this->assertEquals(
  836. LegalEnum::ASSOCIATION_LAW_1901,
  837. $organization->getLegalStatus()
  838. );
  839. $this->assertEquals(
  840. PrincipalTypeEnum::ARTISTIC_EDUCATION_ONLY,
  841. $organization->getPrincipalType()
  842. );
  843. }
  844. public function testMakeParameters(): void
  845. {
  846. $organizationFactory = $this->getOrganizationFactoryMockFor('makeParameters');
  847. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  848. $parameters = $organizationFactory->makeParameters($organizationCreationRequest);
  849. $this->assertInstanceOf(Parameters::class, $parameters);
  850. }
  851. public function testMakeSettings(): void
  852. {
  853. $organizationFactory = $this->getOrganizationFactoryMockFor('makeSettings');
  854. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  855. $organizationCreationRequest->method('getProduct')->willReturn(SettingsProductEnum::ARTIST_PREMIUM);
  856. $settings = $organizationFactory->makeSettings($organizationCreationRequest);
  857. $this->assertEquals(
  858. SettingsProductEnum::ARTIST_PREMIUM,
  859. $settings->getProduct()
  860. );
  861. }
  862. public function testMakePostalAddress(): void
  863. {
  864. $organizationFactory = $this->getOrganizationFactoryMockFor('makePostalAddress');
  865. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  866. $organizationCreationRequest->method('getStreetAddress1')->willReturn('address1');
  867. $organizationCreationRequest->method('getStreetAddress2')->willReturn('address2');
  868. $organizationCreationRequest->method('getStreetAddress3')->willReturn('address3');
  869. $organizationCreationRequest->method('getPostalCode')->willReturn('00000');
  870. $organizationCreationRequest->method('getCity')->willReturn('city');
  871. $organizationCreationRequest->method('getCountryId')->willReturn(1);
  872. $country = $this->getMockBuilder(Country::class)->getMock();
  873. $this->countryRepository->expects(self::once())->method('find')->with(1)->willReturn($country);
  874. $organizationAddressPostal = $organizationFactory->makePostalAddress($organizationCreationRequest);
  875. $this->assertEquals(
  876. AddressPostalOrganizationTypeEnum::ADDRESS_HEAD_OFFICE,
  877. $organizationAddressPostal->getType()
  878. );
  879. $addressPostal = $organizationAddressPostal->getAddressPostal();
  880. $this->assertEquals(
  881. 'address1',
  882. $addressPostal->getStreetAddress()
  883. );
  884. $this->assertEquals(
  885. 'address2',
  886. $addressPostal->getStreetAddressSecond()
  887. );
  888. $this->assertEquals(
  889. 'address3',
  890. $addressPostal->getStreetAddressThird()
  891. );
  892. $this->assertEquals(
  893. '00000',
  894. $addressPostal->getPostalCode()
  895. );
  896. $this->assertEquals(
  897. 'city',
  898. $addressPostal->getAddressCity()
  899. );
  900. $this->assertEquals(
  901. $country,
  902. $addressPostal->getAddressCountry()
  903. );
  904. }
  905. public function testMakePostalAddressNonExistingCountry(): void
  906. {
  907. $organizationFactory = $this->getOrganizationFactoryMockFor('makePostalAddress');
  908. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  909. $organizationCreationRequest->method('getCountryId')->willReturn(1);
  910. $this->countryRepository->expects(self::once())->method('find')->with(1)->willReturn(null);
  911. $this->expectException(\RuntimeException::class);
  912. $this->expectExceptionMessage('No country found for id 1');
  913. $organizationFactory->makePostalAddress($organizationCreationRequest);
  914. }
  915. public function testMakeContactPoint(): void
  916. {
  917. $organizationFactory = $this->getOrganizationFactoryMockFor('makeContactPoint');
  918. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  919. $organizationCreationRequest->method('getPhoneNumber')->willReturn('+33102030405');
  920. $organizationCreationRequest->method('getEmail')->willReturn('contact@domain.net');
  921. $this->phoneNumberUtil
  922. ->method('isPossibleNumber')
  923. ->with('+33102030405')
  924. ->willReturn(true);
  925. $phoneNumber = $this->getMockBuilder(PhoneNumber::class)->getMock();
  926. $this->phoneNumberUtil
  927. ->expects(self::once())
  928. ->method('parse')
  929. ->with('+33102030405')
  930. ->willReturn($phoneNumber);
  931. $contactPoint = $organizationFactory->makeContactPoint($organizationCreationRequest);
  932. $this->assertEquals(
  933. 'contact@domain.net',
  934. $contactPoint->getEmail()
  935. );
  936. $this->assertEquals(
  937. $phoneNumber,
  938. $contactPoint->getTelphone()
  939. );
  940. }
  941. public function testMakeContactPointInvalidPhoneNumber(): void
  942. {
  943. $organizationFactory = $this->getOrganizationFactoryMockFor('makeContactPoint');
  944. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  945. $organizationCreationRequest->method('getPhoneNumber')->willReturn('invalid');
  946. $organizationCreationRequest->method('getEmail')->willReturn('contact@domain.net');
  947. $this->phoneNumberUtil
  948. ->method('isPossibleNumber')
  949. ->with('invalid')
  950. ->willReturn(false);
  951. $phoneNumber = $this->getMockBuilder(PhoneNumber::class)->getMock();
  952. $this->phoneNumberUtil
  953. ->expects(self::never())
  954. ->method('parse');
  955. $this->expectException(\RuntimeException::class);
  956. $this->expectExceptionMessage('Phone number is invalid or missing');
  957. $organizationFactory->makeContactPoint($organizationCreationRequest);
  958. }
  959. public function testMakeNetworkOrganization(): void
  960. {
  961. $organizationFactory = $this->getOrganizationFactoryMockFor('makeNetworkOrganization');
  962. DatesUtils::setFakeDatetime('2024-01-01');
  963. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  964. $organizationCreationRequest->method('getParentId')->willReturn(123);
  965. $parent = $this->getMockBuilder(Organization::class)->getMock();
  966. $settings = $this->getMockBuilder(Settings::class)->getMock();
  967. $settings->method('getProduct')->willReturn(SettingsProductEnum::MANAGER);
  968. $parent->method('getSettings')->willReturn($settings);
  969. $this->organizationRepository->expects(self::once())->method('find')->with(123)->willReturn($parent);
  970. $network = $this->getMockBuilder(Network::class)->getMock();
  971. $networkOrganization = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  972. $networkOrganization->method('getNetwork')->willReturn($network);
  973. $this->organizationUtils
  974. ->expects(self::once())
  975. ->method('getActiveNetworkOrganization')
  976. ->with($parent)
  977. ->willReturn($networkOrganization);
  978. $networkOrganization = $organizationFactory->makeNetworkOrganization($organizationCreationRequest);
  979. $this->assertEquals(
  980. $parent,
  981. $networkOrganization->getParent()
  982. );
  983. $this->assertEquals(
  984. $network,
  985. $networkOrganization->getNetwork()
  986. );
  987. $this->assertEquals(
  988. '2024-01-01',
  989. $networkOrganization->getStartDate()->format('Y-m-d')
  990. );
  991. }
  992. public function testMakeNetworkOrganizationMissingParent(): void
  993. {
  994. $organizationFactory = $this->getOrganizationFactoryMockFor('makeNetworkOrganization');
  995. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  996. $organizationCreationRequest->method('getParentId')->willReturn(123);
  997. $this->expectException(\RuntimeException::class);
  998. $this->expectExceptionMessage('No parent organization found for id 123');
  999. $this->organizationRepository->expects(self::once())->method('find')->with(123)->willReturn(null);
  1000. $organizationFactory->makeNetworkOrganization($organizationCreationRequest);
  1001. }
  1002. public function testMakeNetworkOrganizationParentIsNotManager(): void
  1003. {
  1004. $organizationFactory = $this->getOrganizationFactoryMockFor('makeNetworkOrganization');
  1005. DatesUtils::setFakeDatetime('2024-01-01');
  1006. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  1007. $organizationCreationRequest->method('getParentId')->willReturn(123);
  1008. $parent = $this->getMockBuilder(Organization::class)->getMock();
  1009. $settings = $this->getMockBuilder(Settings::class)->getMock();
  1010. $settings->method('getProduct')->willReturn(SettingsProductEnum::SCHOOL);
  1011. $parent->method('getSettings')->willReturn($settings);
  1012. $this->organizationRepository->expects(self::once())->method('find')->with(123)->willReturn($parent);
  1013. $this->expectException(\RuntimeException::class);
  1014. $this->expectExceptionMessage("Parent organization must have the product 'manager' (actual product: 'school')");
  1015. $organizationFactory->makeNetworkOrganization($organizationCreationRequest);
  1016. }
  1017. public function testMakeNetworkOrganizationMissingNetwork(): void
  1018. {
  1019. $organizationFactory = $this->getOrganizationFactoryMockFor('makeNetworkOrganization');
  1020. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  1021. $organizationCreationRequest->method('getParentId')->willReturn(123);
  1022. $parent = $this->getMockBuilder(Organization::class)->getMock();
  1023. $settings = $this->getMockBuilder(Settings::class)->getMock();
  1024. $settings->method('getProduct')->willReturn(SettingsProductEnum::MANAGER);
  1025. $parent->method('getSettings')->willReturn($settings);
  1026. $this->organizationRepository->expects(self::once())->method('find')->with(123)->willReturn($parent);
  1027. $this->expectException(\RuntimeException::class);
  1028. $this->expectExceptionMessage('No network found for parent 123');
  1029. $this->organizationRepository->expects(self::once())->method('find')->with(123)->willReturn($parent);
  1030. $organizationFactory->makeNetworkOrganization($organizationCreationRequest);
  1031. }
  1032. public function testMakeNetworkOrganizationIsCMFInvalidIdentifier(): void
  1033. {
  1034. $organizationFactory = $this->getOrganizationFactoryMockFor('makeNetworkOrganization');
  1035. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  1036. $organizationCreationRequest->method('getParentId')->willReturn(123);
  1037. $organizationCreationRequest->method('getIdentifier')->willReturn('invalid');
  1038. $parent = $this->getMockBuilder(Organization::class)->getMock();
  1039. $settings = $this->getMockBuilder(Settings::class)->getMock();
  1040. $settings->method('getProduct')->willReturn(SettingsProductEnum::MANAGER);
  1041. $parent->method('getSettings')->willReturn($settings);
  1042. $this->organizationRepository->method('find')->with(123)->willReturn($parent);
  1043. $network = $this->getMockBuilder(Network::class)->getMock();
  1044. $network->method('getId')->willReturn(NetworkEnum::CMF->value);
  1045. $networkOrganization = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  1046. $networkOrganization->method('getNetwork')->willReturn($network);
  1047. $this->organizationUtils
  1048. ->method('getActiveNetworkOrganization')
  1049. ->with($parent)
  1050. ->willReturn($networkOrganization);
  1051. $this->expectException(\RuntimeException::class);
  1052. $this->expectExceptionMessage('CMF identifier is missing or invalid.');
  1053. $organizationFactory->makeNetworkOrganization($organizationCreationRequest);
  1054. }
  1055. public function testMakeNetworkOrganizationIsNotCMFInvalidIdentifier(): void
  1056. {
  1057. $organizationFactory = $this->getOrganizationFactoryMockFor('makeNetworkOrganization');
  1058. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  1059. $organizationCreationRequest->method('getParentId')->willReturn(123);
  1060. $organizationCreationRequest->method('getIdentifier')->willReturn('invalid');
  1061. $parent = $this->getMockBuilder(Organization::class)->getMock();
  1062. $settings = $this->getMockBuilder(Settings::class)->getMock();
  1063. $settings->method('getProduct')->willReturn(SettingsProductEnum::MANAGER);
  1064. $parent->method('getSettings')->willReturn($settings);
  1065. $this->organizationRepository->method('find')->with(123)->willReturn($parent);
  1066. $network = $this->getMockBuilder(Network::class)->getMock();
  1067. $network->method('getId')->willReturn(NetworkEnum::OUTOFNET->value);
  1068. $networkOrganization = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  1069. $networkOrganization->method('getNetwork')->willReturn($network);
  1070. $this->organizationUtils
  1071. ->method('getActiveNetworkOrganization')
  1072. ->with($parent)
  1073. ->willReturn($networkOrganization);
  1074. $result = $organizationFactory->makeNetworkOrganization($organizationCreationRequest);
  1075. $this->assertEquals(
  1076. $network,
  1077. $result->getNetwork()
  1078. );
  1079. }
  1080. public function testMakeAdminAccess(): void
  1081. {
  1082. $organizationFactory = $this->getOrganizationFactoryMockFor('makeAdminAccess');
  1083. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  1084. $organizationCreationRequest->method('getSubdomain')->willReturn('foo');
  1085. $adminAccess = $organizationFactory->makeAdminAccess($organizationCreationRequest);
  1086. $this->assertTrue(
  1087. $adminAccess->getAdminAccess()
  1088. );
  1089. $this->assertEquals(
  1090. 'adminfoo',
  1091. $adminAccess->getPerson()->getUsername()
  1092. );
  1093. $this->assertEquals(
  1094. 32,
  1095. strlen($adminAccess->getPerson()->getPassword())
  1096. );
  1097. }
  1098. public function testMakeCycles(): void
  1099. {
  1100. $organizationFactory = $this->getOrganizationFactoryMockFor('makeCycles');
  1101. $cycles = $organizationFactory->makeCycles();
  1102. $cyclesExpectedData = [
  1103. ['Cycle initiation', 10, CycleEnum::INITIATION_CYCLE],
  1104. ['Cycle 1', 20, CycleEnum::CYCLE_1],
  1105. ['Cycle 2', 30, CycleEnum::CYCLE_2],
  1106. ['Cycle 3', 40, CycleEnum::CYCLE_3],
  1107. ['Cycle 4', 50, CycleEnum::CYCLE_4],
  1108. ['Hors cycle', 60, CycleEnum::OUT_CYCLE],
  1109. ];
  1110. $i = 0;
  1111. foreach ($cycles as $cycle) {
  1112. $this->assertEquals(
  1113. $cyclesExpectedData[$i][0],
  1114. $cycle->getLabel()
  1115. );
  1116. $this->assertEquals(
  1117. $cyclesExpectedData[$i][1],
  1118. $cycle->getOrder()
  1119. );
  1120. $this->assertEquals(
  1121. $cyclesExpectedData[$i][2],
  1122. $cycle->getCycleEnum()
  1123. );
  1124. $this->assertFalse(
  1125. $cycle->getIsSystem()
  1126. );
  1127. ++$i;
  1128. }
  1129. }
  1130. public function testMakeAccessNewPerson(): void
  1131. {
  1132. $organizationFactory = $this->getOrganizationFactoryMockFor('makeAccess');
  1133. $organizationMemberCreationRequest = $this->getMockBuilder(OrganizationMemberCreationRequest::class)->getMock();
  1134. $organizationMemberCreationRequest->method('getUsername')->willReturn('bob');
  1135. $organizationMemberCreationRequest->method('getName')->willReturn('Bob');
  1136. $organizationMemberCreationRequest->method('getGivenName')->willReturn('bOBBy');
  1137. $organizationMemberCreationRequest->method('getGender')->willReturn(GenderEnum::MISTER);
  1138. $this->personRepository
  1139. ->method('findOneBy')
  1140. ->with(['username' => 'bob'], null)
  1141. ->willReturn(null);
  1142. $personAddressPostal = $this->getMockBuilder(PersonAddressPostal::class)->getMock();
  1143. $organizationFactory
  1144. ->expects(self::once())
  1145. ->method('makePersonPostalAddress')
  1146. ->with($organizationMemberCreationRequest)
  1147. ->willReturn($personAddressPostal);
  1148. $contactPoint = $this->getMockBuilder(ContactPoint::class)->getMock();
  1149. $organizationFactory
  1150. ->expects(self::once())
  1151. ->method('makePersonContactPoint')
  1152. ->with($organizationMemberCreationRequest)
  1153. ->willReturn($contactPoint);
  1154. $functionType = $this->getMockBuilder(FunctionType::class)->getMock();
  1155. $this->functionTypeRepository
  1156. ->expects(self::once())
  1157. ->method('findOneBy')
  1158. ->with(['mission' => FunctionEnum::ACCOUNTANT])
  1159. ->willReturn($functionType);
  1160. $creationDate = $this->getMockBuilder(\DateTime::class)->getMock();
  1161. $access = $organizationFactory->makeAccess(
  1162. $organizationMemberCreationRequest,
  1163. FunctionEnum::ACCOUNTANT,
  1164. $creationDate,
  1165. 1
  1166. );
  1167. $this->assertInstanceOf(Access::class, $access);
  1168. $this->assertEquals(
  1169. 'bob',
  1170. $access->getPerson()->getUsername()
  1171. );
  1172. $this->assertTrue(
  1173. strlen($access->getPerson()->getPassword()) === 32
  1174. );
  1175. $this->assertEquals(
  1176. 'Bob',
  1177. $access->getPerson()->getName()
  1178. );
  1179. $this->assertEquals(
  1180. 'Bobby',
  1181. $access->getPerson()->getGivenName()
  1182. );
  1183. $this->assertEquals(
  1184. [$personAddressPostal],
  1185. $access->getPerson()->getPersonAddressPostal()->toArray()
  1186. );
  1187. $this->assertEquals(
  1188. [$contactPoint],
  1189. $access->getPerson()->getContactPoints()->toArray()
  1190. );
  1191. $this->assertEquals(
  1192. $functionType,
  1193. $access->getOrganizationFunction()->first()->getFunctionType()
  1194. );
  1195. $this->assertEquals(
  1196. $creationDate,
  1197. $access->getOrganizationFunction()->first()->getStartDate()
  1198. );
  1199. }
  1200. public function testMakeAccessNewPersonUsernameAlreadyInUse(): void
  1201. {
  1202. $organizationFactory = $this->getOrganizationFactoryMockFor('makeAccess');
  1203. $organizationMemberCreationRequest = $this->getMockBuilder(OrganizationMemberCreationRequest::class)->getMock();
  1204. $organizationMemberCreationRequest->method('getUsername')->willReturn('bob');
  1205. $organizationMemberCreationRequest->method('getName')->willReturn('Bob');
  1206. $organizationMemberCreationRequest->method('getGivenName')->willReturn('bOBBy');
  1207. $organizationMemberCreationRequest->method('getGender')->willReturn(GenderEnum::MISTER);
  1208. $person = $this->getMockBuilder(Person::class)->getMock();
  1209. $this->personRepository
  1210. ->method('findOneBy')
  1211. ->with(['username' => 'bob'], null)
  1212. ->willReturn($person);
  1213. $organizationFactory
  1214. ->expects(self::never())
  1215. ->method('makePersonPostalAddress');
  1216. $organizationFactory
  1217. ->expects(self::never())
  1218. ->method('makePersonContactPoint');
  1219. $this->expectException(\RuntimeException::class);
  1220. $this->expectExceptionMessage('Username already in use : bob');
  1221. $creationDate = $this->getMockBuilder(\DateTime::class)->getMock();
  1222. $organizationFactory->makeAccess(
  1223. $organizationMemberCreationRequest,
  1224. FunctionEnum::ADHERENT,
  1225. $creationDate,
  1226. 1
  1227. );
  1228. }
  1229. public function testMakeAccessExistingPerson(): void
  1230. {
  1231. $organizationFactory = $this->getOrganizationFactoryMockFor('makeAccess');
  1232. $person = $this->getMockBuilder(Person::class)->getMock();
  1233. $this->personRepository
  1234. ->expects(self::once())
  1235. ->method('find')
  1236. ->with(123)
  1237. ->willReturn($person);
  1238. $creationDate = $this->getMockBuilder(\DateTime::class)->getMock();
  1239. $functionType = $this->getMockBuilder(FunctionType::class)->getMock();
  1240. $this->functionTypeRepository
  1241. ->expects(self::once())
  1242. ->method('findOneBy')
  1243. ->with(['mission' => FunctionEnum::ACCOUNTANT])
  1244. ->willReturn($functionType);
  1245. $creationDate = $this->getMockBuilder(\DateTime::class)->getMock();
  1246. $access = $organizationFactory->makeAccess(
  1247. 123,
  1248. FunctionEnum::ACCOUNTANT,
  1249. $creationDate,
  1250. 1
  1251. );
  1252. $this->assertInstanceOf(Access::class, $access);
  1253. $this->assertEquals(
  1254. $person,
  1255. $access->getPerson()
  1256. );
  1257. $this->assertEquals(
  1258. $functionType,
  1259. $access->getOrganizationFunction()->first()->getFunctionType()
  1260. );
  1261. $this->assertEquals(
  1262. $creationDate,
  1263. $access->getOrganizationFunction()->first()->getStartDate()
  1264. );
  1265. }
  1266. public function testMakeAccessPostalAddress(): void
  1267. {
  1268. $organizationFactory = $this->getOrganizationFactoryMockFor('makePersonPostalAddress');
  1269. $organizationMemberCreationRequest = $this->getMockBuilder(OrganizationMemberCreationRequest::class)->getMock();
  1270. $organizationMemberCreationRequest->method('getStreetAddress1')->willReturn('Aaa');
  1271. $organizationMemberCreationRequest->method('getStreetAddress2')->willReturn('Bbb');
  1272. $organizationMemberCreationRequest->method('getStreetAddress3')->willReturn(null);
  1273. $organizationMemberCreationRequest->method('getPostalCode')->willReturn('00000');
  1274. $organizationMemberCreationRequest->method('getCity')->willReturn('city');
  1275. $organizationMemberCreationRequest->method('getCountryId')->willReturn(123);
  1276. $country = $this->getMockBuilder(Country::class)->getMock();
  1277. $this->countryRepository
  1278. ->expects(self::once())
  1279. ->method('find')
  1280. ->with(123)
  1281. ->willReturn($country);
  1282. $creationDate = $this->getMockBuilder(\DateTime::class)->getMock();
  1283. $personPostalAddress = $organizationFactory->makePersonPostalAddress(
  1284. $organizationMemberCreationRequest,
  1285. $creationDate,
  1286. 1
  1287. );
  1288. $this->assertEquals(
  1289. AddressPostalPersonTypeEnum::ADDRESS_PRINCIPAL,
  1290. $personPostalAddress->getType()
  1291. );
  1292. $postalAddress = $personPostalAddress->getAddressPostal();
  1293. $this->assertEquals('Aaa', $postalAddress->getStreetAddress());
  1294. $this->assertEquals('Bbb', $postalAddress->getStreetAddressSecond());
  1295. $this->assertEquals(null, $postalAddress->getStreetAddressThird());
  1296. $this->assertEquals('00000', $postalAddress->getPostalCode());
  1297. $this->assertEquals('city', $postalAddress->getAddressCity());
  1298. $this->assertEquals($country, $postalAddress->getAddressCountry());
  1299. }
  1300. public function testMakeAccessContactPoint(): void
  1301. {
  1302. $organizationFactory = $this->getOrganizationFactoryMockFor('makePersonContactPoint');
  1303. $organizationMemberCreationRequest = $this->getMockBuilder(OrganizationMemberCreationRequest::class)->getMock();
  1304. $organizationMemberCreationRequest->method('getPhone')->willReturn('+33102030405');
  1305. $organizationMemberCreationRequest->method('getEmail')->willReturn('email@domain.com');
  1306. $organizationMemberCreationRequest->method('getMobile')->willReturn('+33607080910');
  1307. $this->phoneNumberUtil
  1308. ->expects(self::exactly(2))
  1309. ->method('isPossibleNumber')
  1310. ->willReturnMap([
  1311. ['+33102030405', null, true],
  1312. ['+33607080910', null, true],
  1313. ]);
  1314. $phoneNumber = $this->getMockBuilder(PhoneNumber::class)->getMock();
  1315. $mobilePhoneNumber = $this->getMockBuilder(PhoneNumber::class)->getMock();
  1316. $this->phoneNumberUtil
  1317. ->expects(self::exactly(2))
  1318. ->method('parse')
  1319. ->willReturnMap([
  1320. ['+33102030405', null, null, false, $phoneNumber],
  1321. ['+33607080910', null, null, false, $mobilePhoneNumber],
  1322. ]);
  1323. $creationDate = $this->getMockBuilder(\DateTime::class)->getMock();
  1324. $contactPoint = $organizationFactory->makePersonContactPoint(
  1325. $organizationMemberCreationRequest,
  1326. $creationDate,
  1327. 1
  1328. );
  1329. $this->assertEquals(
  1330. ContactPointTypeEnum::PRINCIPAL,
  1331. $contactPoint->getContactType()
  1332. );
  1333. $this->assertEquals(
  1334. 'email@domain.com',
  1335. $contactPoint->getEmail()
  1336. );
  1337. $this->assertEquals(
  1338. $phoneNumber,
  1339. $contactPoint->getTelphone()
  1340. );
  1341. $this->assertEquals(
  1342. $mobilePhoneNumber,
  1343. $contactPoint->getMobilPhone()
  1344. );
  1345. }
  1346. public function testMakeAccessContactPointInvalidPhone(): void
  1347. {
  1348. $organizationFactory = $this->getOrganizationFactoryMockFor('makePersonContactPoint');
  1349. $organizationMemberCreationRequest = $this->getMockBuilder(OrganizationMemberCreationRequest::class)->getMock();
  1350. $organizationMemberCreationRequest->method('getUsername')->willReturn('bob');
  1351. $organizationMemberCreationRequest->method('getPhone')->willReturn('invalid');
  1352. $organizationMemberCreationRequest->method('getEmail')->willReturn('email@domain.com');
  1353. $organizationMemberCreationRequest->method('getMobile')->willReturn('+33607080910');
  1354. $this->phoneNumberUtil
  1355. ->expects(self::once())
  1356. ->method('isPossibleNumber')
  1357. ->with('invalid')
  1358. ->willReturn(false);
  1359. $this->phoneNumberUtil
  1360. ->expects(self::never())
  1361. ->method('parse');
  1362. $this->expectException(\RuntimeException::class);
  1363. $this->expectExceptionMessage('Phone number is invalid or missing (person: bob)');
  1364. $creationDate = $this->getMockBuilder(\DateTime::class)->getMock();
  1365. $organizationFactory->makePersonContactPoint(
  1366. $organizationMemberCreationRequest,
  1367. $creationDate,
  1368. 1
  1369. );
  1370. }
  1371. public function testMakeAccessContactPointInvalidMobilePhone(): void
  1372. {
  1373. $organizationFactory = $this->getOrganizationFactoryMockFor('makePersonContactPoint');
  1374. $organizationMemberCreationRequest = $this->getMockBuilder(OrganizationMemberCreationRequest::class)->getMock();
  1375. $organizationMemberCreationRequest->method('getUsername')->willReturn('bob');
  1376. $organizationMemberCreationRequest->method('getPhone')->willReturn('+33102030405');
  1377. $organizationMemberCreationRequest->method('getEmail')->willReturn('email@domain.com');
  1378. $organizationMemberCreationRequest->method('getMobile')->willReturn('invalid');
  1379. $this->phoneNumberUtil
  1380. ->expects(self::exactly(2))
  1381. ->method('isPossibleNumber')
  1382. ->willReturnMap([
  1383. ['+33102030405', null, true],
  1384. ['invalid', null, false],
  1385. ]);
  1386. $this->phoneNumberUtil
  1387. ->expects(self::never())
  1388. ->method('parse');
  1389. $this->expectException(\RuntimeException::class);
  1390. $this->expectExceptionMessage('Mobile phone number is invalid (person: bob)');
  1391. $creationDate = $this->getMockBuilder(\DateTime::class)->getMock();
  1392. $organizationFactory->makePersonContactPoint(
  1393. $organizationMemberCreationRequest,
  1394. $creationDate,
  1395. 1
  1396. );
  1397. }
  1398. public function testMakePersonContactPointNoMobile(): void
  1399. {
  1400. $organizationFactory = $this->getOrganizationFactoryMockFor('makePersonContactPoint');
  1401. $organizationMemberCreationRequest = $this->getMockBuilder(OrganizationMemberCreationRequest::class)->getMock();
  1402. $organizationMemberCreationRequest->method('getPhone')->willReturn('+33102030405');
  1403. $organizationMemberCreationRequest->method('getMobile')->willReturn(null);
  1404. $this->phoneNumberUtil
  1405. ->expects(self::once())
  1406. ->method('isPossibleNumber')
  1407. ->with('+33102030405')
  1408. ->willReturn(true);
  1409. $creationDate = $this->getMockBuilder(\DateTime::class)->getMock();
  1410. $contactPoint = $organizationFactory->makePersonContactPoint(
  1411. $organizationMemberCreationRequest,
  1412. $creationDate,
  1413. 1
  1414. );
  1415. $this->assertEquals(
  1416. null,
  1417. $contactPoint->getMobilPhone()
  1418. );
  1419. }
  1420. public function testMakeSubdomain(): void
  1421. {
  1422. $organizationFactory = $this->getOrganizationFactoryMockFor('makeSubdomain');
  1423. $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
  1424. $organizationCreationRequest->method('getSubdomain')->willReturn('subdomain');
  1425. $subdomain = $organizationFactory->makeSubdomain($organizationCreationRequest);
  1426. $this->assertEquals(
  1427. 'subdomain',
  1428. $subdomain->getSubdomain()
  1429. );
  1430. $this->assertTrue(
  1431. $subdomain->isActive()
  1432. );
  1433. }
  1434. public function testCreateTypo3Website(): void
  1435. {
  1436. $organizationFactory = $this->getOrganizationFactoryMockFor('createTypo3Website');
  1437. $organization = $this->getMockBuilder(Organization::class)->getMock();
  1438. $organization->method('getId')->willReturn(123);
  1439. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  1440. $response->method('getStatusCode')->willReturn(Response::HTTP_OK);
  1441. $response->method('getContent')->willReturn('{"root_uid": 456}');
  1442. $this->typo3Service->expects(self::once())->method('createSite')->with(123)->willReturn($response);
  1443. $organization->expects(self::once())->method('setCmsId')->with(456);
  1444. $this->entityManager->expects(self::once())->method('persist')->with($organization);
  1445. $this->entityManager->expects(self::once())->method('flush');
  1446. $result = $organizationFactory->createTypo3Website($organization);
  1447. $this->assertEquals(
  1448. 456,
  1449. $result
  1450. );
  1451. }
  1452. public function testCreateTypo3WebsiteWithError(): void
  1453. {
  1454. $organizationFactory = $this->getOrganizationFactoryMockFor('createTypo3Website');
  1455. $organization = $this->getMockBuilder(Organization::class)->getMock();
  1456. $organization->method('getId')->willReturn(123);
  1457. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  1458. $response->method('getStatusCode')->willReturn(Response::HTTP_FORBIDDEN);
  1459. $response->method('getContent')->willReturn('');
  1460. $this->typo3Service->expects(self::once())->method('createSite')->with(123)->willReturn($response);
  1461. $this->logger
  1462. ->expects(self::once())
  1463. ->method('critical')
  1464. ->with('/!\ A critical error happened while creating the Typo3 website');
  1465. $result = $organizationFactory->createTypo3Website($organization);
  1466. $this->assertNull($result);
  1467. }
  1468. public function testCreateTypo3WebsiteWithInvalidResponse(): void
  1469. {
  1470. $organizationFactory = $this->getOrganizationFactoryMockFor('createTypo3Website');
  1471. $organization = $this->getMockBuilder(Organization::class)->getMock();
  1472. $organization->method('getId')->willReturn(123);
  1473. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  1474. $response->method('getStatusCode')->willReturn(Response::HTTP_OK);
  1475. $response->method('getContent')->willReturn('<html lang="fr">Login page</html>');
  1476. $this->typo3Service->expects(self::once())->method('createSite')->with(123)->willReturn($response);
  1477. $this->logger
  1478. ->expects(self::once())
  1479. ->method('critical')
  1480. ->with('/!\ A critical error happened while creating the Typo3 website');
  1481. $result = $organizationFactory->createTypo3Website($organization);
  1482. $this->assertNull($result);
  1483. }
  1484. public function testUpdateAdminassosDb(): void
  1485. {
  1486. $organizationFactory = $this->getOrganizationFactoryMockFor('updateAdminassosDb');
  1487. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  1488. $response->method('getStatusCode')->willReturn(Response::HTTP_OK);
  1489. $this->apiLegacyRequestService
  1490. ->expects(self::once())
  1491. ->method('get')
  1492. ->with('/_internal/request/adminassos/create/organization/123')
  1493. ->willReturn($response);
  1494. $organization = $this->getMockBuilder(Organization::class)->getMock();
  1495. $organization->method('getId')->willReturn(123);
  1496. $organizationFactory->updateAdminassosDb($organization);
  1497. }
  1498. public function testUpdateAdminassosDbError(): void
  1499. {
  1500. $organizationFactory = $this->getOrganizationFactoryMockFor('updateAdminassosDb');
  1501. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  1502. $response->method('getStatusCode')->willReturn(Response::HTTP_BAD_REQUEST);
  1503. $response->method('getContent')->willReturn('some error');
  1504. $this->apiLegacyRequestService
  1505. ->expects(self::once())
  1506. ->method('get')
  1507. ->with('/_internal/request/adminassos/create/organization/123')
  1508. ->willReturn($response);
  1509. $organization = $this->getMockBuilder(Organization::class)->getMock();
  1510. $organization->method('getId')->willReturn(123);
  1511. $this->expectException(\RuntimeException::class);
  1512. $this->expectExceptionMessage('An error happened while updating the adminassos database: some error');
  1513. $organizationFactory->updateAdminassosDb($organization);
  1514. }
  1515. public function testNormalizeIdentificationField(): void
  1516. {
  1517. $organizationFactory = $this->getOrganizationFactoryMockFor('normalizeIdentificationField');
  1518. $this->assertEquals(
  1519. 'c+est+une+phrase+normalisee+',
  1520. $organizationFactory->normalizeIdentificationField("C'est une phrase normalisée.")
  1521. );
  1522. }
  1523. public function testDelete(): void
  1524. {
  1525. $organizationFactory = $this->getOrganizationFactoryMockFor('delete');
  1526. $organizationDeletionRequest = $this->getMockBuilder(OrganizationDeletionRequest::class)->getMock();
  1527. $organizationDeletionRequest->method('getOrganizationId')->willReturn(123);
  1528. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  1529. $organization = $this->getMockBuilder(Organization::class)->getMock();
  1530. $organization->method('getId')->willReturn(123);
  1531. $organization->method('getParameters')->willReturn($parameters);
  1532. $this->organizationRepository
  1533. ->expects(self::once())
  1534. ->method('find')
  1535. ->with(123)
  1536. ->willReturn($organization);
  1537. $this->entityManager->expects(self::once())->method('beginTransaction');
  1538. $this->entityManager->expects(self::once())->method('flush');
  1539. $this->entityManager->expects(self::once())->method('commit');
  1540. $this->entityManager->expects(self::never())->method('rollback');
  1541. $person1 = $this->getMockBuilder(Person::class)->getMock();
  1542. $person1->method('getId')->willReturn(1);
  1543. $person2 = $this->getMockBuilder(Person::class)->getMock();
  1544. $person2->method('getId')->willReturn(2);
  1545. $person3 = $this->getMockBuilder(Person::class)->getMock();
  1546. $person3->method('getId')->willReturn(3);
  1547. $organizationFactory
  1548. ->method('getFutureOrphanPersons')
  1549. ->with($organization)
  1550. ->willReturn([$person1, $person2, $person3]);
  1551. $this->entityManager->expects(self::exactly(5))->method('remove')->withConsecutive(
  1552. [$parameters],
  1553. [$organization],
  1554. [$person1],
  1555. [$person2],
  1556. [$person3],
  1557. );
  1558. $organizationFactory->expects(self::once())->method('deleteTypo3Website')->with(123);
  1559. $organizationFactory->expects(self::once())->method('switchDolibarrSocietyToProspect')->with(123);
  1560. $this->fileManager->expects(self::once())->method('deleteOrganizationFiles')->with(123);
  1561. $this->fileManager
  1562. ->expects(self::exactly(3))
  1563. ->method('deletePersonFiles')
  1564. ->withConsecutive([1], [2], [3]);
  1565. $organizationDeletionRequest
  1566. ->expects(self::once())
  1567. ->method('setStatus')
  1568. ->with(OrganizationDeletionRequest::STATUS_OK);
  1569. $result = $organizationFactory->delete($organizationDeletionRequest);
  1570. $this->assertEquals(
  1571. $organizationDeletionRequest,
  1572. $result
  1573. );
  1574. }
  1575. public function testDeleteWithRollback(): void
  1576. {
  1577. $organizationFactory = $this->getOrganizationFactoryMockFor('delete');
  1578. $organizationDeletionRequest = $this->getMockBuilder(OrganizationDeletionRequest::class)->getMock();
  1579. $organizationDeletionRequest->method('getOrganizationId')->willReturn(123);
  1580. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  1581. $organization = $this->getMockBuilder(Organization::class)->getMock();
  1582. $organization->method('getParameters')->willReturn($parameters);
  1583. $person1 = $this->getMockBuilder(Person::class)->getMock();
  1584. $person1->method('getId')->willReturn(1);
  1585. $person2 = $this->getMockBuilder(Person::class)->getMock();
  1586. $person2->method('getId')->willReturn(2);
  1587. $person3 = $this->getMockBuilder(Person::class)->getMock();
  1588. $person3->method('getId')->willReturn(3);
  1589. $organizationFactory
  1590. ->method('getFutureOrphanPersons')
  1591. ->with($organization)
  1592. ->willReturn([$person1, $person2, $person3]);
  1593. $this->organizationRepository
  1594. ->expects(self::once())
  1595. ->method('find')
  1596. ->with(123)
  1597. ->willReturn($organization);
  1598. $this->entityManager->expects(self::once())->method('beginTransaction');
  1599. $this->entityManager->expects(self::never())->method('flush');
  1600. $this->entityManager->expects(self::never())->method('commit');
  1601. $this->entityManager->expects(self::once())->method('rollback');
  1602. $this->entityManager->method('remove')->willThrowException(new \Exception('some error'));
  1603. $organizationFactory->expects(self::never())->method('deleteTypo3Website');
  1604. $organizationFactory->expects(self::never())->method('switchDolibarrSocietyToProspect');
  1605. $organizationDeletionRequest
  1606. ->expects(self::never())
  1607. ->method('setStatus');
  1608. $this->logger
  1609. ->expects(self::once())
  1610. ->method('critical')
  1611. ->with($this->callback(function ($arg) {
  1612. return is_string($arg) && str_contains($arg, 'An error happened, operation cancelled') && str_contains($arg, 'some error');
  1613. }));
  1614. $this->expectException(\Exception::class);
  1615. $organizationFactory->delete($organizationDeletionRequest);
  1616. }
  1617. public function testDeleteWithNonBlockingErrors(): void
  1618. {
  1619. $organizationFactory = $this->getOrganizationFactoryMockFor('delete');
  1620. $organizationDeletionRequest = $this->getMockBuilder(OrganizationDeletionRequest::class)->getMock();
  1621. $organizationDeletionRequest->method('getOrganizationId')->willReturn(123);
  1622. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  1623. $organization = $this->getMockBuilder(Organization::class)->getMock();
  1624. $organization->method('getId')->willReturn(123);
  1625. $organization->method('getParameters')->willReturn($parameters);
  1626. $person1 = $this->getMockBuilder(Person::class)->getMock();
  1627. $person1->method('getId')->willReturn(1);
  1628. $person2 = $this->getMockBuilder(Person::class)->getMock();
  1629. $person2->method('getId')->willReturn(2);
  1630. $person3 = $this->getMockBuilder(Person::class)->getMock();
  1631. $person3->method('getId')->willReturn(3);
  1632. $organizationFactory
  1633. ->method('getFutureOrphanPersons')
  1634. ->with($organization)
  1635. ->willReturn([$person1, $person2, $person3]);
  1636. $this->organizationRepository
  1637. ->expects(self::once())
  1638. ->method('find')
  1639. ->with(123)
  1640. ->willReturn($organization);
  1641. $this->entityManager->expects(self::once())->method('beginTransaction');
  1642. $this->entityManager->expects(self::once())->method('flush');
  1643. $this->entityManager->expects(self::once())->method('commit');
  1644. $this->entityManager->expects(self::never())->method('rollback');
  1645. $this->entityManager->expects(self::exactly(5))->method('remove')->withConsecutive(
  1646. [$parameters],
  1647. [$organization],
  1648. [$person1],
  1649. [$person2],
  1650. [$person3],
  1651. );
  1652. $organizationFactory->expects(self::once())->method('deleteTypo3Website')->willThrowException(new \Exception('some error'));
  1653. $organizationFactory->expects(self::once())->method('switchDolibarrSocietyToProspect')->willThrowException(new \Exception('some error'));
  1654. $organizationDeletionRequest
  1655. ->expects(self::once())
  1656. ->method('setStatus')
  1657. ->with(OrganizationDeletionRequest::STATUS_OK_WITH_ERRORS);
  1658. $this->fileManager
  1659. ->expects(self::once())
  1660. ->method('deleteOrganizationFiles')
  1661. ->with(123)
  1662. ->willThrowException(new \Exception('some error'));
  1663. $this->fileManager
  1664. ->expects(self::exactly(3))
  1665. ->method('deletePersonFiles')
  1666. ->withConsecutive([1], [2], [3])
  1667. ->willThrowException(new \Exception('some error'));
  1668. $this->logger
  1669. ->expects(self::exactly(6))
  1670. ->method('critical')
  1671. ->withConsecutive(
  1672. ['An error happened while deleting the Typo3 website, please proceed manually.'],
  1673. ['An error happened while updating the Dolibarr society, please proceed manually.'],
  1674. ["An error happened while deleting the organization's files, please proceed manually."],
  1675. ["An error happened while deleting the person's files, please proceed manually (id=1)."],
  1676. ["An error happened while deleting the person's files, please proceed manually (id=2)."],
  1677. ["An error happened while deleting the person's files, please proceed manually (id=3)."],
  1678. );
  1679. $result = $organizationFactory->delete($organizationDeletionRequest);
  1680. $this->assertEquals(
  1681. $organizationDeletionRequest,
  1682. $result
  1683. );
  1684. }
  1685. public function testGetFutureOrphanPersons(): void
  1686. {
  1687. $organizationFactory = $this->getOrganizationFactoryMockFor('getFutureOrphanPersons');
  1688. $organization = $this->getMockBuilder(Organization::class)->getMock();
  1689. $access1 = $this->getMockBuilder(Access::class)->getMock();
  1690. $person1 = $this->getMockBuilder(Person::class)->getMock();
  1691. $access1->method('getPerson')->willReturn($person1);
  1692. $access2 = $this->getMockBuilder(Access::class)->getMock();
  1693. $person2 = $this->getMockBuilder(Person::class)->getMock();
  1694. $access2->method('getPerson')->willReturn($person2);
  1695. $access3 = $this->getMockBuilder(Access::class)->getMock();
  1696. $person3 = $this->getMockBuilder(Person::class)->getMock();
  1697. $access3->method('getPerson')->willReturn($person3);
  1698. $otherAccess1 = $this->getMockBuilder(Access::class)->getMock();
  1699. $otherAccess2 = $this->getMockBuilder(Access::class)->getMock();
  1700. $person1->method('getAccesses')->willReturn(new ArrayCollection([$access1, $otherAccess1]));
  1701. $person2->method('getAccesses')->willReturn(new ArrayCollection([$access2, $otherAccess2]));
  1702. $person3->method('getAccesses')->willReturn(new ArrayCollection([$access3]));
  1703. $organization->method('getAccesses')->willReturn(new ArrayCollection([$access1, $access2, $access3]));
  1704. $this->assertEquals(
  1705. [$person3],
  1706. $organizationFactory->getFutureOrphanPersons($organization)
  1707. );
  1708. }
  1709. public function testDeleteTypo3Website(): void
  1710. {
  1711. $organizationFactory = $this->getOrganizationFactoryMockFor('deleteTypo3Website');
  1712. $this->typo3Service->expects(self::once())->method('hardDeleteSite')->with(123);
  1713. $organizationFactory->deleteTypo3Website(123);
  1714. }
  1715. public function testSwitchDolibarrSocietyToProspect(): void
  1716. {
  1717. $organizationFactory = $this->getOrganizationFactoryMockFor('switchDolibarrSocietyToProspect');
  1718. $this->dolibarrApiService
  1719. ->expects(self::once())
  1720. ->method('switchSocietyToProspect')
  1721. ->with(123);
  1722. $organizationFactory->switchDolibarrSocietyToProspect(123);
  1723. }
  1724. public function testSetAdminAccountPassword(): void
  1725. {
  1726. $organizationFactory = $this->getOrganizationFactoryMockFor('setAdminAccountPassword');
  1727. // Create mock organization
  1728. $organization = $this->getMockBuilder(Organization::class)->getMock();
  1729. // Create mock access with adminAccess=true
  1730. $access = $this->getMockBuilder(Access::class)->getMock();
  1731. // Create mock person
  1732. $person = $this->getMockBuilder(Person::class)->getMock();
  1733. // Set up the access to return the person
  1734. $access->method('getPerson')->willReturn($person);
  1735. // Set up the AccessRepository to return the access when findAdminAccess is called
  1736. $this->accessRepository->method('findAdminAccess')->with($organization)->willReturn($access);
  1737. // Expect the person's setPassword method to be called with the password
  1738. $person->expects(self::once())->method('setPassword')->with('Password123!');
  1739. // Expect the EntityManager's persist and flush methods to be called
  1740. $this->entityManager->expects(self::once())->method('persist')->with($person);
  1741. $this->entityManager->expects(self::once())->method('flush');
  1742. // Call the method with a valid password
  1743. $organizationFactory->setAdminAccountPassword($organization, 'Password123!');
  1744. }
  1745. public function testSetAdminAccountPasswordInvalidPassword(): void
  1746. {
  1747. $organizationFactory = $this->getOrganizationFactoryMockFor('setAdminAccountPassword');
  1748. // Create mock organization
  1749. $organization = $this->getMockBuilder(Organization::class)->getMock();
  1750. // Expect an exception to be thrown for an invalid password
  1751. $this->expectException(\RuntimeException::class);
  1752. $this->expectExceptionMessage('Password must be at least 8 characters long and include at least one uppercase letter, one lowercase letter, one digit, and one special character.');
  1753. // Call the method with an invalid password
  1754. $organizationFactory->setAdminAccountPassword($organization, 'password');
  1755. }
  1756. public function testSetAdminAccountPasswordNoAdminAccount(): void
  1757. {
  1758. $organizationFactory = $this->getOrganizationFactoryMockFor('setAdminAccountPassword');
  1759. // Create mock organization
  1760. $organization = $this->getMockBuilder(Organization::class)->getMock();
  1761. // Set up the AccessRepository to return null when findAdminAccess is called
  1762. $this->accessRepository->method('findAdminAccess')->with($organization)->willReturn(null);
  1763. // Expect an exception to be thrown when no admin account is found
  1764. $this->expectException(\RuntimeException::class);
  1765. $this->expectExceptionMessage('No admin account found for this organization.');
  1766. // Call the method with a valid password
  1767. $organizationFactory->setAdminAccountPassword($organization, 'Password123!');
  1768. }
  1769. }