OrganizationFactoryTest.php 50 KB

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