OrganizationFactoryTest.php 71 KB

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