DolibarrSyncServiceTest.php 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455
  1. <?php
  2. /** @noinspection ALL */
  3. namespace App\Tests\Unit\Service\Dolibarr;
  4. use App\Entity\Access\Access;
  5. use App\Entity\Access\FunctionType;
  6. use App\Entity\Core\AddressPostal;
  7. use App\Entity\Core\ContactPoint;
  8. use App\Entity\Network\Network;
  9. use App\Entity\Network\NetworkOrganization;
  10. use App\Entity\Organization\Organization;
  11. use App\Entity\Organization\OrganizationAddressPostal;
  12. use App\Entity\Organization\Settings;
  13. use App\Entity\Person\Person;
  14. use App\Enum\Access\FunctionEnum;
  15. use App\Enum\Core\ContactPointTypeEnum;
  16. use App\Enum\Network\NetworkEnum;
  17. use App\Enum\Organization\AddressPostalOrganizationTypeEnum;
  18. use App\Enum\Organization\LegalEnum;
  19. use App\Enum\Organization\SettingsProductEnum;
  20. use App\Enum\Person\GenderEnum;
  21. use App\Repository\Access\AccessRepository;
  22. use App\Repository\Organization\OrganizationRepository;
  23. use App\Service\Core\AddressPostalUtils;
  24. use App\Service\Dolibarr\DolibarrApiService;
  25. use App\Service\Dolibarr\DolibarrSyncService;
  26. use App\Service\Organization\Utils;
  27. use App\Service\Rest\Operation\BaseRestOperation;
  28. use App\Service\Rest\Operation\CreateOperation;
  29. use App\Service\Rest\Operation\DeleteOperation;
  30. use App\Service\Rest\Operation\UpdateOperation;
  31. use App\Service\Utils\ArrayUtils;
  32. use App\Service\Utils\DatesUtils;
  33. use Doctrine\Common\Collections\ArrayCollection;
  34. use libphonenumber\PhoneNumber;
  35. use libphonenumber\PhoneNumberUtil;
  36. use PHPUnit\Framework\MockObject\MockObject;
  37. use PHPUnit\Framework\TestCase;
  38. use Psr\Log\LoggerInterface;
  39. use Symfony\Contracts\HttpClient\ResponseInterface;
  40. use Symfony\Contracts\Translation\TranslatorInterface;
  41. class TestableDolibarrSyncService extends DolibarrSyncService
  42. {
  43. public function getDolibarrSocietiesIndex(): array
  44. {
  45. return parent::getDolibarrSocietiesIndex();
  46. }
  47. public function findDolibarrContactFor(array $dolibarrContacts, Person $person): ?array
  48. {
  49. return parent::findDolibarrContactFor($dolibarrContacts, $person);
  50. }
  51. public function getActiveMembersIndex(): array
  52. {
  53. return parent::getActiveMembersIndex();
  54. }
  55. public function sanitizeDolibarrData(?array $data): ?array
  56. {
  57. return parent::sanitizeDolibarrData($data);
  58. }
  59. public function getOrganizationPostalAddress(Organization $organization): ?AddressPostal
  60. {
  61. return parent::getOrganizationPostalAddress($organization);
  62. }
  63. public function getOrganizationBillingPostalAddress(Organization $organization): ?AddressPostal
  64. {
  65. return parent::getOrganizationBillingPostalAddress($organization);
  66. }
  67. public function getOrganizationPhone(Organization $organization): ?string
  68. {
  69. return parent::getOrganizationPhone($organization);
  70. }
  71. public function getOrganizationEmail(Organization $organization): ?string
  72. {
  73. return parent::getOrganizationEmail($organization);
  74. }
  75. public function getOrganizationNetworkId(Organization $organization): ?int
  76. {
  77. return parent::getOrganizationNetworkId($organization);
  78. }
  79. public function countWithMission(array $missions, array $members): int
  80. {
  81. return parent::countWithMission($missions, $members);
  82. }
  83. public function getPersonContact(Person $person): ?ContactPoint
  84. {
  85. return parent::getPersonContact($person);
  86. }
  87. public function formatContactPosition(array $missions, ?string $gender = 'X'): string
  88. {
  89. return parent::formatContactPosition($missions, $gender);
  90. }
  91. public function formatPhoneNumber(PhoneNumber $phoneNumber): string
  92. {
  93. return parent::formatPhoneNumber($phoneNumber);
  94. }
  95. public function getExpectedTagsFor(Organization $organization): array
  96. {
  97. return parent::getExpectedTagsFor($organization);
  98. }
  99. public function validateResponse(ResponseInterface $response, BaseRestOperation $operation): void
  100. {
  101. parent::validateResponse($response, $operation);
  102. }
  103. }
  104. class DolibarrSyncServiceTest extends TestCase
  105. {
  106. private MockObject|OrganizationRepository $organizationRepository;
  107. private MockObject|AccessRepository $accessRepository;
  108. private MockObject|DolibarrApiService $dolibarrApiService;
  109. private MockObject|AddressPostalUtils $addressPostalUtils;
  110. private MockObject|ArrayUtils $arrayUtils;
  111. private MockObject|TranslatorInterface $translator;
  112. private MockObject|LoggerInterface $logger;
  113. private MockObject|Utils $organizationUtils;
  114. public function setUp(): void
  115. {
  116. $this->organizationRepository = $this->getMockBuilder(OrganizationRepository::class)
  117. ->disableOriginalConstructor()
  118. ->getMock();
  119. $this->accessRepository = $this->getMockBuilder(AccessRepository::class)
  120. ->disableOriginalConstructor()
  121. ->getMock();
  122. $this->dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  123. ->disableOriginalConstructor()
  124. ->getMock();
  125. $this->addressPostalUtils = $this->getMockBuilder(AddressPostalUtils::class)
  126. ->disableOriginalConstructor()
  127. ->getMock();
  128. $this->arrayUtils = $this->getMockBuilder(ArrayUtils::class)
  129. ->disableOriginalConstructor()
  130. ->getMock();
  131. $this->translator = $this->getMockBuilder(TranslatorInterface::class)
  132. ->disableOriginalConstructor()
  133. ->getMock();
  134. $this->logger = $this->getMockBuilder(LoggerInterface::class)
  135. ->disableOriginalConstructor()
  136. ->getMock();
  137. $this->organizationUtils = $this->getMockBuilder(Utils::class)
  138. ->disableOriginalConstructor()
  139. ->getMock();
  140. $this->logger->method('info')->willReturnSelf();
  141. $this->logger->method('debug')->willReturnSelf();
  142. $this->logger->method('warning')->willReturnSelf();
  143. $this->logger->method('error')->willReturnSelf();
  144. }
  145. public function tearDown(): void
  146. {
  147. DatesUtils::clearFakeDatetime();
  148. }
  149. private function getMockForMethod(string $method)
  150. {
  151. $dolibarrSyncService = $this->getMockBuilder(TestableDolibarrSyncService::class)
  152. ->setConstructorArgs([$this->organizationRepository, $this->accessRepository,
  153. $this->dolibarrApiService, $this->addressPostalUtils, $this->arrayUtils, $this->translator, $this->organizationUtils])
  154. ->setMethodsExcept([$method, 'setLoggerInterface'])
  155. ->getMock();
  156. $dolibarrSyncService->setLoggerInterface($this->logger);
  157. return $dolibarrSyncService;
  158. }
  159. /**
  160. * Full test of the scan method.
  161. *
  162. * @see DolibarrSyncService::scan()
  163. */
  164. public function testScan(): void
  165. {
  166. $dolibarrSyncService = $this->getMockForMethod('scan');
  167. // ----- Opentalent Organizations -----
  168. $orgId1 = 10;
  169. $organization1 = $this->getMockBuilder(Organization::class)->getMock();
  170. $organization1->method('getId')->willReturn($orgId1);
  171. $organization1->method('getName')->willReturn('Organization 10');
  172. $organizationData1 = [
  173. 'fullAddress' => '1 Rue Azerty',
  174. 'fullBillingAddress' => '2 Rue Ascii',
  175. 'addressOwner' => 'Mr Keyboard',
  176. 'postalCode' => '01110',
  177. 'city' => 'ByteCity',
  178. 'email' => 'foo@bar.net',
  179. 'phone' => '0102030405',
  180. 'networkId' => NetworkEnum::CMF->value,
  181. 'product' => SettingsProductEnum::SCHOOL,
  182. 'networkId' => NetworkEnum::CMF->value,
  183. 'legalStatus' => LegalEnum::LOCAL_AUTHORITY,
  184. ];
  185. $orgId2 = 20;
  186. $organization2 = $this->getMockBuilder(Organization::class)->getMock();
  187. $organization2->method('getId')->willReturn($orgId2);
  188. $organization2->method('getName')->willReturn('Organization 20');
  189. $organizationData2 = [
  190. 'email' => null,
  191. 'phone' => null,
  192. 'networkId' => null,
  193. 'product' => SettingsProductEnum::ARTIST,
  194. 'legalStatus' => LegalEnum::ASSOCIATION_LAW_1901,
  195. ];
  196. $orgId3 = 30;
  197. $organization3 = null; // This organization does not exist
  198. // Persons
  199. $accessId1 = 11; // Person 1
  200. $accessId2 = 21; // Person 2
  201. $accessId3 = 12; // Shall be ignored, because not an office member
  202. // access 4 does not exist
  203. $accessId5 = 13; // Invalid Person
  204. $personId1 = 100;
  205. $personData1 = [
  206. 'name' => 'Dupont',
  207. 'givenName' => 'Hercules',
  208. 'gender' => GenderEnum::MISTER,
  209. 'email' => 'an@email.net',
  210. 'phone' => '0102030405',
  211. 'mobilePhone' => '0607080910',
  212. ];
  213. $personId2 = 200;
  214. $personData2 = [
  215. 'name' => 'Simpson',
  216. 'givenName' => 'Lisa',
  217. 'gender' => null,
  218. ];
  219. $personId3 = 300; // Obsolete contact, does not exist anymore in the Opentalent DB
  220. $personId4 = 400; // Obsolete contact, does not exist anymore in the Opentalent DB
  221. $personId5 = 900; // Invalid contact with no firstname and no lastname
  222. $personData5 = [
  223. 'name' => '',
  224. 'givenName' => '',
  225. 'gender' => null,
  226. ];
  227. $activeMembers1 = [
  228. $accessId1 => [FunctionEnum::PRESIDENT->value],
  229. $accessId3 => [FunctionEnum::STUDENT->value],
  230. $accessId5 => [FunctionEnum::TREASURER->value],
  231. ];
  232. $activeMembers2 = [
  233. $accessId2 => [FunctionEnum::PRESIDENT->value],
  234. ];
  235. // ----- Opentalent : other vars -----
  236. $cmfId = 12097;
  237. $cmfDolibarrId = 12098;
  238. $ffecId = 91295;
  239. $ffecDolibarrId = 91296;
  240. // ----- Dolibarr societies -----
  241. // Existing society about to be updated
  242. $socId1 = 1;
  243. $dolibarrSociety1 = [
  244. 'id' => $socId1,
  245. 'name' => 'Organization 10',
  246. 'address' => '1 Rue Qwerty',
  247. 'zip' => '01024',
  248. 'town' => 'Ram',
  249. 'email' => 'some@email.com',
  250. 'phone' => null,
  251. 'status' => 0,
  252. 'parent' => '0',
  253. 'array_options' => [
  254. 'options_2iopeninfoopentalent' => '',
  255. 'options_2iopen_software_opentalent' => 'Opentalent Artist',
  256. ],
  257. ];
  258. // Existing society with no data
  259. $socId2 = 2;
  260. $dolibarrSociety2 = [
  261. 'id' => $socId2,
  262. 'array_options' => [],
  263. ];
  264. // This organization does not exist in the opentalent DB
  265. $socId3 = 3;
  266. $dolibarrSociety3 = null;
  267. // Dolibarr contacts
  268. $contactData1 = [
  269. 'id' => '1',
  270. 'civility_code' => '',
  271. 'lastname' => 'Dupond',
  272. 'firstname' => 'Bob',
  273. 'email' => 'abcd@mail.com',
  274. 'phone_pro' => '+33478570000',
  275. 'phone_mobile' => '+33682980000',
  276. 'poste' => 'Secrétaire',
  277. 'statut' => '1',
  278. 'array_options' => [
  279. 'options_2iopen_person_id' => '',
  280. ],
  281. ];
  282. // An obsolete contact that should be disabled
  283. $obsoleteContactData = [
  284. 'id' => '4',
  285. 'lastname' => 'Doe',
  286. 'firstname' => 'John',
  287. 'statut' => '1',
  288. 'array_options' => [
  289. 'options_2iopen_person_id' => $personId3,
  290. ],
  291. ];
  292. // An obsolete contact that should is already disabled
  293. $obsoleteContactData2 = [
  294. 'id' => '5',
  295. 'lastname' => 'Foo',
  296. 'firstname' => 'John',
  297. 'statut' => '0',
  298. 'array_options' => [
  299. 'options_2iopen_person_id' => $personId4,
  300. ],
  301. ];
  302. $billingContactData = [
  303. 'id' => '5',
  304. 'lastname' => 'Service facturation',
  305. 'firstname' => null,
  306. 'address' => '',
  307. 'zip' => '',
  308. 'town' => '',
  309. 'statut' => '1',
  310. 'array_options' => [
  311. 'options_2iopen_person_id' => null,
  312. ],
  313. ];
  314. $dolibarrSocietyContacts1 = [$contactData1, $obsoleteContactData, $obsoleteContactData2, $billingContactData];
  315. $dolibarrSocietyContacts2 = [];
  316. // ----- Setup Mocks -----
  317. $this->translator->method('trans')->willReturnMap([
  318. ['STUDENTS_COUNT', [], null, null, "Nombre d'élèves"],
  319. ['ADHERENTS_COUNT', [], null, null, "Nombre d'adhérents"],
  320. ['ADMIN_ACCESS_COUNT', [], null, null, "Nombre d'accès admin"],
  321. [SettingsProductEnum::SCHOOL->value, [], null, null, 'Opentalent School'],
  322. [SettingsProductEnum::ARTIST->value, [], null, null, 'Opentalent Artist'],
  323. [GenderEnum::MISTER->value, [], null, null, 'MR'],
  324. ]);
  325. // Get societies
  326. $this->organizationRepository->method('find')
  327. ->willReturnMap([
  328. [$orgId1, null, null, $organization1],
  329. [$orgId2, null, null, $organization2],
  330. [$orgId3, null, null, $organization3],
  331. ]);
  332. $dolibarrSyncService
  333. ->expects(self::once())
  334. ->method('getDolibarrSocietiesIndex')
  335. ->willReturn([
  336. $orgId1 => $dolibarrSociety1,
  337. $orgId2 => $dolibarrSociety2,
  338. $orgId3 => $dolibarrSociety3,
  339. ]);
  340. $dolibarrSyncService
  341. ->expects(self::once())
  342. ->method('getActiveMembersIndex')
  343. ->willReturn([
  344. $orgId1 => $activeMembers1,
  345. $orgId2 => $activeMembers2,
  346. $orgId3 => [],
  347. ]);
  348. // Function types
  349. $functionType1 = $this->getMockBuilder(FunctionType::class)->getMock();
  350. $functionType1->method('getMission')->willReturn(FunctionEnum::DIRECTOR);
  351. $functionType2 = $this->getMockBuilder(FunctionType::class)->getMock();
  352. $functionType2->method('getMission')->willReturn(FunctionEnum::PRESIDENT);
  353. // Get CMF and FFEC ids
  354. $this->dolibarrApiService->method('getSociety')->willReturnMap(
  355. [
  356. [$cmfId, ['id' => $cmfDolibarrId]],
  357. [$ffecId, ['id' => $ffecDolibarrId]],
  358. ]
  359. );
  360. // -- Loop over organizations --
  361. $dolibarrSyncService
  362. ->expects(self::exactly(5)) // 3 organizations and 2 contacts
  363. ->method('sanitizeDolibarrData')
  364. ->willReturnCallback(function ($args) { return $args; });
  365. $mainPostalAddress = $this->getMockBuilder(AddressPostal::class)->getMock();
  366. $mainPostalAddress->method('getStreetAddress')->willReturn($organizationData1['fullAddress']);
  367. $mainPostalAddress->method('getAddressOwner')->willReturn($organizationData1['addressOwner']);
  368. $mainPostalAddress->method('getPostalCode')->willReturn($organizationData1['postalCode']);
  369. $mainPostalAddress->method('getAddressCity')->willReturn($organizationData1['city']);
  370. $billingPostalAddress = $this->getMockBuilder(AddressPostal::class)->getMock();
  371. $billingPostalAddress->method('getStreetAddress')->willReturn($organizationData1['fullBillingAddress']);
  372. $billingPostalAddress->method('getAddressOwner')->willReturn($organizationData1['addressOwner']);
  373. $billingPostalAddress->method('getPostalCode')->willReturn($organizationData1['postalCode']);
  374. $billingPostalAddress->method('getAddressCity')->willReturn($organizationData1['city']);
  375. $dolibarrSyncService
  376. ->expects(self::exactly(2))
  377. ->method('getOrganizationPostalAddress')
  378. ->willReturnMap([
  379. [$organization1, $mainPostalAddress],
  380. [$organization2, null],
  381. ]);
  382. $dolibarrSyncService
  383. ->expects(self::exactly(1))
  384. ->method('getOrganizationBillingPostalAddress')
  385. ->willReturnMap([
  386. [$organization1, $billingPostalAddress],
  387. [$organization2, null],
  388. ]);
  389. $this->addressPostalUtils
  390. ->method('getFullStreetAddress')
  391. ->willReturnCallback(function ($a) { return $a->getStreetAddress(); });
  392. $dolibarrSyncService
  393. ->expects(self::exactly(2))
  394. ->method('getOrganizationEmail')
  395. ->willReturnMap([
  396. [$organization1, $organizationData1['email']],
  397. [$organization2, $organizationData2['email']],
  398. ]);
  399. $dolibarrSyncService
  400. ->expects(self::exactly(2))
  401. ->method('getOrganizationPhone')
  402. ->willReturnMap([
  403. [$organization1, $organizationData1['phone']],
  404. [$organization2, $organizationData2['phone']],
  405. ]);
  406. $dolibarrSyncService
  407. ->expects(self::exactly(2))
  408. ->method('getOrganizationNetworkId')
  409. ->willReturnMap([
  410. [$organization1, $organizationData1['networkId']],
  411. [$organization2, $organizationData2['networkId']],
  412. ]);
  413. $settings1 = $this->getMockBuilder(Settings::class)->getMock();
  414. $settings1->method('getProduct')->willReturn($organizationData1['product']);
  415. $organization1->method('getSettings')->willReturn($settings1);
  416. $settings2 = $this->getMockBuilder(Settings::class)->getMock();
  417. $settings2->method('getProduct')->willReturn($organizationData2['product']);
  418. $organization2->method('getSettings')->willReturn($settings2);
  419. $this->organizationUtils->method('isSchool')->willReturnMap([
  420. [$organization1, true],
  421. [$organization2, false],
  422. ]);
  423. $this->organizationUtils->method('isArtist')->willReturnMap([
  424. [$organization1, false],
  425. [$organization2, true],
  426. ]);
  427. $adminFunctions = [
  428. FunctionEnum::NETWORK_ANIMATOR->value, FunctionEnum::ARCHIVIST->value, FunctionEnum::COM_STAFF->value, FunctionEnum::ACCOUNTANT->value,
  429. FunctionEnum::COORDINATOR->value, FunctionEnum::CORRESPONDING->value, FunctionEnum::ADMINISTRATIVE_DIRECTOR->value,
  430. FunctionEnum::ADMINISTRATIVE_DIRECTOR_ASSISTANT->value, FunctionEnum::ADMINISTRATIVE_STAFF->value, FunctionEnum::TECHNICAL_STAFF->value,
  431. FunctionEnum::PRESENTER->value, FunctionEnum::ADMINISTRATIVE_OFFICER->value, FunctionEnum::ADMINISTRATIVE_SECRETARY->value,
  432. FunctionEnum::ACTIVE_MEMBER_OF_THE_CA->value, FunctionEnum::ACTIVE_COOPTED_MEMBER_OF_THE_CA->value, FunctionEnum::ACTIVE_SUBSTITUTE_MEMBER_OF_THE_CA->value,
  433. FunctionEnum::MEMBER_OF_THE_BOARD->value, FunctionEnum::PRESIDENT->value, FunctionEnum::PRESIDENT_ASSISTANT->value, FunctionEnum::SECRETARY->value,
  434. FunctionEnum::ASSISTANT_SECRETARY->value, FunctionEnum::TREASURER->value, FunctionEnum::TREASURER_ASSISTANT->value,
  435. FunctionEnum::VICE_PRESIDENT->value, FunctionEnum::ARTISTIC_DIRECTOR->value, FunctionEnum::ARTISTIC_DIRECTOR_ASSISTANT->value,
  436. FunctionEnum::DIRECTOR->value, FunctionEnum::DIRECTOR_ASSISTANT->value,
  437. ];
  438. $dolibarrSyncService->method('countWithMission')->willReturnMap([
  439. [[FunctionEnum::STUDENT->value], $activeMembers1, 1],
  440. [[FunctionEnum::ADHERENT->value], $activeMembers1, 2],
  441. [[FunctionEnum::ADHERENT->value], $activeMembers2, 0],
  442. [$adminFunctions, $activeMembers1, 1],
  443. [$adminFunctions, $activeMembers2, 2],
  444. ]);
  445. $this->arrayUtils
  446. ->expects(self::exactly(4))
  447. ->method('getChanges')
  448. ->willReturnCallback(
  449. function (array $initialArray, array $newArray, bool $recursive = false, ?callable $callback = null) {
  450. if (in_array('name', $newArray, true)) {
  451. // Organization 1 name is already defined and has not been changed
  452. unset($newArray['name']);
  453. }
  454. if (in_array('statut', $newArray, true)) {
  455. // Contact 1 statut is already defined and has not been changed
  456. unset($newArray['statut']);
  457. }
  458. return $newArray;
  459. }
  460. );
  461. $this->dolibarrApiService->method('getContacts')->willReturnMap([
  462. [$socId1, $dolibarrSocietyContacts1],
  463. [$socId2, $dolibarrSocietyContacts2],
  464. ]);
  465. // Loop over contacts
  466. // NB: Student will be skipped since it has no office role
  467. $person1 = $this->getMockBuilder(Person::class)->getMock();
  468. $person1->method('getId')->willReturn($personId1);
  469. $person1->method('getName')->willReturn($personData1['name']);
  470. $person1->method('getGivenName')->willReturn($personData1['givenName']);
  471. $person1->method('getGender')->willReturn($personData1['gender']);
  472. $person2 = $this->getMockBuilder(Person::class)->getMock();
  473. $person2->method('getId')->willReturn($personId2);
  474. $person2->method('getName')->willReturn($personData2['name']);
  475. $person2->method('getGivenName')->willReturn($personData2['givenName']);
  476. $person2->method('getGender')->willReturn($personData2['gender']);
  477. // An invalid person that should be ignored
  478. $person5 = $this->getMockBuilder(Person::class)->getMock();
  479. $person5->method('getId')->willReturn($personId5);
  480. $person5->method('getName')->willReturn($personData5['name']);
  481. $person5->method('getGivenName')->willReturn($personData5['givenName']);
  482. $access1 = $this->getMockBuilder(Access::class)->getMock();
  483. $access1->method('getPerson')->willReturn($person1);
  484. $access2 = $this->getMockBuilder(Access::class)->getMock();
  485. $access2->method('getPerson')->willReturn($person2);
  486. $access5 = $this->getMockBuilder(Access::class)->getMock();
  487. $access5->method('getPerson')->willReturn($person5);
  488. $this->accessRepository->method('find')->willReturnMap([
  489. [$accessId1, null, null, $access1],
  490. [$accessId2, null, null, $access2],
  491. [$accessId5, null, null, $access5],
  492. ]);
  493. $dolibarrSyncService->method('findDolibarrContactFor')->willReturnMap([
  494. [$dolibarrSocietyContacts1, $person1, $contactData1],
  495. [$dolibarrSocietyContacts2, $person2, null],
  496. ]);
  497. $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
  498. $contactPoint1->method('getEmail')->willReturn($personData1['email']);
  499. $phone = $this->getMockBuilder(PhoneNumber::class)->getMock();
  500. $mobilePhone = $this->getMockBuilder(PhoneNumber::class)->getMock();
  501. $dolibarrSyncService->method('formatPhoneNumber')->willReturnMap([
  502. [$phone, $personData1['phone']],
  503. [$mobilePhone, $personData1['mobilePhone']],
  504. ]);
  505. $contactPoint1->method('getTelphone')->willReturn($phone);
  506. $contactPoint1->method('getMobilPhone')->willReturn($mobilePhone);
  507. $dolibarrSyncService->method('getPersonContact')->willReturnMap([
  508. [$person1, $contactPoint1],
  509. [$person2, null],
  510. ]);
  511. $dolibarrSyncService->method('formatContactPosition')->willReturnMap([
  512. [[FunctionEnum::PRESIDENT->value], GenderEnum::MISTER->value, 'Président'],
  513. [[FunctionEnum::PRESIDENT->value], null, 'Président(e)'],
  514. ]);
  515. // Expected logged error messages
  516. $this->logger->expects(self::exactly(2))->method('error')->withConsecutive(
  517. ['Person 900 miss a lastname and/or a firstname, ignored.'],
  518. ['Organization 30 not found in the Opentalent DB'],
  519. );
  520. // Tags
  521. $this->dolibarrApiService->method('getSocietyTagsIds')->willReturnMap([
  522. [$socId1, [1, 68]],
  523. [$socId2, [3, 67]],
  524. ]);
  525. $dolibarrSyncService->method('getExpectedTagsFor')->willReturnMap([
  526. [$organization1, [67]],
  527. [$organization2, []],
  528. ]);
  529. // Expected progression callback triggers
  530. $progressionCallbackExpectedCalls = [[1, 3], [2, 3], [3, 3]];
  531. $progressionCallback = static function ($i, $total) use (&$progressionCallbackExpectedCalls) {
  532. [$expectedI, $expectedTotal] = array_shift($progressionCallbackExpectedCalls);
  533. if ($i !== $expectedI || $total !== $expectedTotal) {
  534. throw new \AssertionError('Progression callback error, expected parameters are ('.$expectedI.','.$expectedTotal.'), got ('.$i.', '.$total.')');
  535. }
  536. };
  537. DatesUtils::setFakeDatetime('2024-01-01T00:00:00+00:00');
  538. $operations = $dolibarrSyncService->scan($progressionCallback);
  539. $this->assertCount(9, $operations);
  540. $this->assertEqualsCanonicalizing(
  541. [
  542. '[PUT thirdparties/1]',
  543. "address : `1 Rue Qwerty` => `Mr Keyboard\n1 Rue Azerty`",
  544. 'zip : `01024` => `01110`',
  545. 'town : `Ram` => `ByteCity`',
  546. 'email : `some@email.com` => `foo@bar.net`',
  547. 'phone : `` => `0102030405`',
  548. 'parent : `0` => `12098`',
  549. 'array_options.options_2iopen_software_opentalent : `Opentalent Artist` => `Opentalent School`',
  550. "array_options.options_2iopeninfoopentalent : `` => `Nombre d'élèves : 1\nNombre d'adhérents : 2\nNombre d'accès admin : 1`",
  551. 'array_options.options_2iopen_last_sync_date : (new) => `2024-01-01T00:00:00+00:00`',
  552. 'status : `0` => `1`',
  553. ],
  554. $operations[0]->getChangeLog()
  555. );
  556. $this->assertEqualsCanonicalizing(
  557. [
  558. '[PUT contacts/1]',
  559. 'civility_code : `` => `MR`',
  560. 'lastname : `Dupond` => `Dupont`',
  561. 'firstname : `Bob` => `Hercules`',
  562. 'email : `abcd@mail.com` => `an@email.net`',
  563. 'phone_pro : `+33478570000` => `0102030405`',
  564. 'phone_mobile : `+33682980000` => `0607080910`',
  565. 'poste : `Secrétaire` => `Président`',
  566. 'array_options.options_2iopen_person_id : `` => `100`',
  567. 'array_options.options_2iopen_last_sync_date : (new) => `2024-01-01T00:00:00+00:00`',
  568. ],
  569. $operations[1]->getChangeLog()
  570. );
  571. $this->assertEqualsCanonicalizing(
  572. [
  573. '[PUT contacts/4]',
  574. 'statut : `1` => `0`',
  575. 'array_options.options_2iopen_last_sync_date : (new) => `2024-01-01T00:00:00+00:00`',
  576. ],
  577. $operations[2]->getChangeLog()
  578. );
  579. $this->assertEqualsCanonicalizing(
  580. [
  581. '[PUT contacts/5]',
  582. 'address : `` => `2 Rue Ascii`',
  583. 'town : `` => `ByteCity`',
  584. 'zip : `` => `01110`',
  585. ],
  586. $operations[3]->getChangeLog()
  587. );
  588. $this->assertEqualsCanonicalizing(
  589. [
  590. '[DELETE /thirdparties/1/categories/68]',
  591. ],
  592. $operations[4]->getChangeLog()
  593. );
  594. $this->assertEqualsCanonicalizing(
  595. [
  596. '[POST /thirdparties/1/categories/67]',
  597. ],
  598. $operations[5]->getChangeLog()
  599. );
  600. $this->assertEqualsCanonicalizing(
  601. [
  602. '[PUT thirdparties/2]',
  603. 'address : (new) => ``',
  604. 'email : (new) => ``',
  605. 'name : (new) => `Organization 20`',
  606. 'parent : (new) => ``',
  607. 'phone : (new) => ``',
  608. 'status : (new) => `1`',
  609. 'town : (new) => ``',
  610. 'zip : (new) => ``',
  611. 'array_options.options_2iopen_software_opentalent : (new) => `Opentalent Artist`',
  612. "array_options.options_2iopeninfoopentalent : (new) => `Nombre d'adhérents : 0\nNombre d'accès admin : 2`",
  613. 'array_options.options_2iopen_last_sync_date : (new) => `2024-01-01T00:00:00+00:00`',
  614. ],
  615. $operations[6]->getChangeLog()
  616. );
  617. $this->assertEqualsCanonicalizing(
  618. [
  619. '[POST contacts]',
  620. 'civility_code : (new) => ``',
  621. 'lastname : (new) => `Simpson`',
  622. 'firstname : (new) => `Lisa`',
  623. 'email : (new) => ``',
  624. 'phone_pro : (new) => ``',
  625. 'phone_mobile : (new) => ``',
  626. 'poste : (new) => `Président(e)`',
  627. 'statut : (new) => `1`',
  628. 'array_options.options_2iopen_person_id : (new) => `200`',
  629. 'array_options.options_2iopen_last_sync_date : (new) => `2024-01-01T00:00:00+00:00`',
  630. 'socid : (new) => `2`',
  631. ],
  632. $operations[7]->getChangeLog()
  633. );
  634. $this->assertEqualsCanonicalizing(
  635. [
  636. '[DELETE /thirdparties/2/categories/67]',
  637. ],
  638. $operations[8]->getChangeLog()
  639. );
  640. $this->assertCount(0, $progressionCallbackExpectedCalls);
  641. }
  642. /**
  643. * All valid operations shall be executed
  644. * If an operation is not in status READY, a warning shall be logged, and the operation shall be skipped.
  645. *
  646. * @see DolibarrSyncService::execute()
  647. */
  648. public function testExecute(): void
  649. {
  650. $dolibarrSyncService = $this->getMockForMethod('execute');
  651. $progressionCallbackExpectedCalls = [[1, 3], [2, 3], [3, 3]];
  652. $progressionCallback = static function ($i, $total) use (&$progressionCallbackExpectedCalls) {
  653. [$expectedI, $expectedTotal] = array_shift($progressionCallbackExpectedCalls);
  654. if ($i !== $expectedI || $total !== $expectedTotal) {
  655. throw new \AssertionError('Progression callback error, expected parameters are ('.$expectedI.','.$expectedTotal.'), got ('.$i.', '.$total.')');
  656. }
  657. };
  658. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  659. $operation1 = $this->getMockBuilder(UpdateOperation::class)->disableOriginalConstructor()->getMock();
  660. $operation1->method('getStatus')->willReturnOnConsecutiveCalls(BaseRestOperation::STATUS_READY, BaseRestOperation::STATUS_DONE);
  661. $operation1->method('getChangeLog')->willReturn(['foo']);
  662. $operation1->expects(self::once())->method('execute')->willReturn($response);
  663. $operation2 = $this->getMockBuilder(CreateOperation::class)->disableOriginalConstructor()->getMock();
  664. $operation2->method('getStatus')->willReturn(
  665. BaseRestOperation::STATUS_READY, BaseRestOperation::STATUS_ERROR, BaseRestOperation::STATUS_ERROR // An error happened
  666. );
  667. $operation2->expects(self::once())->method('execute');
  668. $operation3 = $this->getMockBuilder(DeleteOperation::class)->disableOriginalConstructor()->getMock();
  669. $operation3->method('getStatus')->willReturn(BaseRestOperation::STATUS_DONE); // Invalid status, should log a warning and not execute
  670. $operation3->expects(self::never())->method('execute');
  671. $this->logger->expects(self::exactly(2))->method('warning'); // 2 warnings because of the bad status of the Create Op
  672. $this->logger->expects(self::exactly(3))->method('error'); // The exception thrown during the execution of the Delete op will log 3 errors
  673. $dolibarrSyncService->execute([$operation1, $operation2, $operation3], $progressionCallback);
  674. }
  675. /**
  676. * @see DolibarrSyncService::run()
  677. */
  678. public function testRun()
  679. {
  680. $dolibarrSyncService = $this->getMockForMethod('run');
  681. $operations = ['operation1', 'operation2'];
  682. $dolibarrSyncService->expects(self::once())->method('scan')->willReturn($operations);
  683. $dolibarrSyncService->expects(self::once())->method('execute')->with($operations);
  684. $result = $dolibarrSyncService->run();
  685. $this->assertEquals($operations, $result);
  686. }
  687. /**
  688. * @see DolibarrSyncService::getDolibarrSocietiesIndex()
  689. */
  690. public function testGetDolibarrSocietiesIndex(): void
  691. {
  692. $dolibarrSyncService = $this->getMockForMethod('getDolibarrSocietiesIndex');
  693. $this->dolibarrApiService
  694. ->expects($this->once())
  695. ->method('getAllClients')
  696. ->willReturn(
  697. [
  698. ['id' => 1, 'array_options' => ['options_2iopen_organization_id' => 101]],
  699. ['id' => 2, 'array_options' => ['options_2iopen_organization_id' => 102]],
  700. ['id' => 3, 'array_options' => ['options_2iopen_organization_id' => null]], // No org id but also no contract, so it's ok
  701. ['id' => 4, 'name' => 'foo', 'array_options' => ['options_2iopen_organization_id' => null]], // No org id but has a contract, a warning should be logged
  702. ]
  703. );
  704. $this->dolibarrApiService->expects(self::exactly(2))->method('getActiveContract')->willReturnMap([
  705. [3, null],
  706. [4, ['dummy non-empty data']],
  707. ]);
  708. $this->logger->expects(self::once())->method('warning')->with('Dolibarr client has no organization id: foo (4)');
  709. $index = $dolibarrSyncService->getDolibarrSocietiesIndex();
  710. $this->assertEqualsCanonicalizing(
  711. [
  712. 1 => ['id' => 1, 'array_options' => ['options_2iopen_organization_id' => 101]],
  713. 2 => ['id' => 2, 'array_options' => ['options_2iopen_organization_id' => 102]],
  714. ],
  715. $index
  716. );
  717. }
  718. /**
  719. * @see DolibarrSyncService::getActiveMembersIndex()
  720. */
  721. public function testGetActiveMembersIndex(): void
  722. {
  723. $dolibarrSyncService = $this->getMockForMethod('getActiveMembersIndex');
  724. $this->accessRepository
  725. ->expects($this->once())
  726. ->method('getAllActiveMembersAndMissions')
  727. ->willReturn(
  728. [
  729. ['id' => 1, 'organization_id' => 1, 'mission' => FunctionEnum::PRESIDENT],
  730. ['id' => 2, 'organization_id' => 1, 'mission' => FunctionEnum::STUDENT],
  731. ['id' => 3, 'organization_id' => 2, 'mission' => FunctionEnum::PRESIDENT],
  732. ['id' => 3, 'organization_id' => 2, 'mission' => FunctionEnum::TEACHER],
  733. ]
  734. );
  735. $index = $dolibarrSyncService->getActiveMembersIndex();
  736. $this->assertEqualsCanonicalizing([
  737. 1 => [1 => [FunctionEnum::PRESIDENT->value], 2 => [FunctionEnum::STUDENT->value]],
  738. 2 => [3 => [FunctionEnum::PRESIDENT->value, FunctionEnum::TEACHER->value]],
  739. ], $index);
  740. }
  741. /**
  742. * @see DolibarrSyncService::findDolibarrContactFor()
  743. */
  744. public function testFindDolibarrContactForById(): void
  745. {
  746. $dolibarrSyncService = $this->getMockForMethod('findDolibarrContactFor');
  747. $contacts = [
  748. ['id' => 1, 'array_options' => ['options_2iopen_person_id' => 101]],
  749. ['id' => 2, 'array_options' => ['options_2iopen_person_id' => 102]],
  750. ];
  751. // Find by id
  752. $person = $this->getMockBuilder(Person::class)->getMock();
  753. $person->method('getId')->willReturn(102);
  754. $contact = $dolibarrSyncService->findDolibarrContactFor($contacts, $person);
  755. $this->assertEquals(2, $contact['id']);
  756. }
  757. /**
  758. * @see DolibarrSyncService::findDolibarrContactFor()
  759. */
  760. public function testFindDolibarrContactForByName(): void
  761. {
  762. $dolibarrSyncService = $this->getMockForMethod('findDolibarrContactFor');
  763. $contacts = [
  764. ['id' => 1, 'firstname' => 'mister', 'lastname' => 'X', 'array_options' => ['options_2iopen_person_id' => null]],
  765. ['id' => 2, 'firstname' => 'miss', 'lastname' => 'Y', 'array_options' => ['options_2iopen_person_id' => null]],
  766. ];
  767. // Find by full name (contact has no person id, it should be returned)
  768. $person = $this->getMockBuilder(Person::class)->getMock();
  769. $person->method('getId')->willReturn(101);
  770. $person->method('getName')->willReturn('X');
  771. $person->method('getGivenName')->willReturn('mister');
  772. $result = $dolibarrSyncService->findDolibarrContactFor($contacts, $person);
  773. $this->assertEquals(1, $result['id']);
  774. }
  775. /**
  776. * @see DolibarrSyncService::findDolibarrContactFor()
  777. */
  778. public function testFindDolibarrContactForByNameWithConflict(): void
  779. {
  780. $dolibarrSyncService = $this->getMockForMethod('findDolibarrContactFor');
  781. $contacts = [
  782. ['id' => 1, 'firstname' => 'mister', 'lastname' => 'X', 'array_options' => ['options_2iopen_person_id' => 1]],
  783. ['id' => 2, 'firstname' => 'miss', 'lastname' => 'Y', 'array_options' => ['options_2iopen_person_id' => 2]],
  784. ];
  785. // Find by full name (contact already has another person id, it should not be returned)
  786. $person = $this->getMockBuilder(Person::class)->getMock();
  787. $person->method('getId')->willReturn(101);
  788. $person->method('getName')->willReturn('X');
  789. $person->method('getGivenName')->willReturn('mister');
  790. $result = $dolibarrSyncService->findDolibarrContactFor($contacts, $person);
  791. $this->assertEquals(null, $result);
  792. }
  793. /**
  794. * @see DolibarrSyncService::findDolibarrContactFor()
  795. */
  796. public function testFindDolibarrContactNotFound(): void
  797. {
  798. $dolibarrSyncService = $this->getMockForMethod('findDolibarrContactFor');
  799. $contacts = [
  800. ['id' => 1, 'firstname' => 'mister', 'lastname' => 'X', 'array_options' => ['options_2iopen_person_id' => 1]],
  801. ['id' => 2, 'firstname' => 'miss', 'lastname' => 'Y', 'array_options' => ['options_2iopen_person_id' => 2]],
  802. ];
  803. // Do not find
  804. $person = $this->getMockBuilder(Person::class)->getMock();
  805. $person->method('getId')->willReturn(-1);
  806. $person->method('getName')->willReturn('Presley');
  807. $person->method('getGivenName')->willReturn('Elvis');
  808. $result = $dolibarrSyncService->findDolibarrContactFor($contacts, $person);
  809. $this->assertEquals(null, $result);
  810. }
  811. /**
  812. * @see DolibarrSyncService::sanitizeDolibarrData()
  813. */
  814. public function testSanitizeDolibarrData(): void
  815. {
  816. $dolibarrSyncService = $this->getMockForMethod('sanitizeDolibarrData');
  817. $result = $dolibarrSyncService->sanitizeDolibarrData(['a' => 'A', 'b' => '', 'c' => ['d' => 'D', 'e' => '']]);
  818. $this->assertEquals(
  819. ['a' => 'A', 'b' => null, 'c' => ['d' => 'D', 'e' => null]],
  820. $result
  821. );
  822. }
  823. /**
  824. * @see DolibarrSyncService::sanitizeDolibarrData()
  825. */
  826. public function testSanitizeDolibarrDataWithNull(): void
  827. {
  828. $dolibarrSyncService = $this->getMockForMethod('sanitizeDolibarrData');
  829. $result = $dolibarrSyncService->sanitizeDolibarrData(null);
  830. $this->assertEquals(null, $result);
  831. }
  832. /**
  833. * @see DolibarrSyncService::getOrganizationPostalAddress()
  834. */
  835. public function testGetOrganizationPostalAddress(): void
  836. {
  837. $dolibarrSyncService = $this->getMockForMethod('getOrganizationPostalAddress');
  838. $organization = $this->getMockBuilder(Organization::class)->getMock();
  839. $organizationAddressPostal1 = $this->getMockBuilder(OrganizationAddressPostal::class)->getMock();
  840. $organizationAddressPostal2 = $this->getMockBuilder(OrganizationAddressPostal::class)->getMock();
  841. $organizationAddressPostal3 = $this->getMockBuilder(OrganizationAddressPostal::class)->getMock();
  842. $addressPostal = $this->getMockBuilder(AddressPostal::class)->getMock();
  843. $organizationAddressPostal1->method('getType')->willReturn(AddressPostalOrganizationTypeEnum::ADDRESS_PRACTICE);
  844. $organizationAddressPostal2->method('getType')->willReturn(AddressPostalOrganizationTypeEnum::ADDRESS_BILL);
  845. $organizationAddressPostal3->method('getType')->willReturn(AddressPostalOrganizationTypeEnum::ADDRESS_OTHER);
  846. $organizationAddressPostal2->method('getAddressPostal')->willReturn($addressPostal);
  847. $organization->expects($this->once())
  848. ->method('getOrganizationAddressPostals')
  849. ->willReturn(
  850. new ArrayCollection([$organizationAddressPostal1, $organizationAddressPostal2, $organizationAddressPostal3])
  851. );
  852. $this->assertEquals(
  853. $addressPostal,
  854. $dolibarrSyncService->getOrganizationPostalAddress($organization)
  855. );
  856. }
  857. /**
  858. * @see DolibarrSyncService::getOrganizationPostalAddress()
  859. */
  860. public function testGetOrganizationPostalAddressNoResult(): void
  861. {
  862. $dolibarrSyncService = $this->getMockForMethod('getOrganizationPostalAddress');
  863. $organization = $this->getMockBuilder(Organization::class)->getMock();
  864. $organization->expects($this->once())
  865. ->method('getOrganizationAddressPostals')
  866. ->willReturn(new ArrayCollection([]));
  867. $this->assertEquals(
  868. null,
  869. $dolibarrSyncService->getOrganizationPostalAddress($organization)
  870. );
  871. }
  872. /**
  873. * @see DolibarrSyncService::getOrganizationPhone()
  874. */
  875. public function testGetOrganizationPhoneWithExistingPhone(): void
  876. {
  877. $dolibarrSyncService = $this->getMockForMethod('getOrganizationPhone');
  878. $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
  879. $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
  880. $contactPoint3 = $this->getMockBuilder(ContactPoint::class)->getMock();
  881. $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER);
  882. $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::BILL);
  883. $contactPoint3->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL);
  884. $phone = $this->getMockBuilder(PhoneNumber::class)->disableOriginalConstructor()->getMock();
  885. $contactPoint2->method('getTelphone')->willReturn($phone);
  886. $organization = $this->getMockBuilder(Organization::class)->getMock();
  887. $organization
  888. ->expects($this->once())
  889. ->method('getContactPoints')
  890. ->willReturn(
  891. new ArrayCollection([$contactPoint1, $contactPoint2, $contactPoint3])
  892. );
  893. $dolibarrSyncService->expects(self::once())->method('formatPhoneNumber')->with($phone)->willReturn('+33161626365');
  894. $this->assertEquals(
  895. '+33161626365',
  896. $dolibarrSyncService->getOrganizationPhone($organization)
  897. );
  898. }
  899. /**
  900. * @see DolibarrSyncService::getOrganizationPhone()
  901. */
  902. public function testGetOrganizationPhoneWithMobilePhone()
  903. {
  904. $dolibarrSyncService = $this->getMockForMethod('getOrganizationPhone');
  905. $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
  906. $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
  907. $contactPoint3 = $this->getMockBuilder(ContactPoint::class)->getMock();
  908. $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER);
  909. $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::BILL);
  910. $contactPoint3->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL);
  911. $contactPoint2->expects($this->once())->method('getTelphone')->willReturn(null);
  912. $mobilePhone = $this->getMockBuilder(PhoneNumber::class)->disableOriginalConstructor()->getMock();
  913. $contactPoint2->method('getMobilPhone')->willReturn($mobilePhone);
  914. $organization = $this->getMockBuilder(Organization::class)->getMock();
  915. $organization
  916. ->expects($this->once())
  917. ->method('getContactPoints')
  918. ->willReturn(
  919. new ArrayCollection([$contactPoint1, $contactPoint2, $contactPoint3])
  920. );
  921. $dolibarrSyncService->expects(self::once())->method('formatPhoneNumber')->with($mobilePhone)->willReturn('+33661626365');
  922. $this->assertEquals(
  923. '+33661626365',
  924. $dolibarrSyncService->getOrganizationPhone($organization)
  925. );
  926. }
  927. /**
  928. * @see DolibarrSyncService::getOrganizationPhone()
  929. */
  930. public function testGetOrganizationPhoneWithNoPhone()
  931. {
  932. $dolibarrSyncService = $this->getMockForMethod('getOrganizationPhone');
  933. $organization = $this->getMockBuilder(Organization::class)->getMock();
  934. $organization
  935. ->expects($this->once())
  936. ->method('getContactPoints')
  937. ->willReturn(new ArrayCollection([]));
  938. $dolibarrSyncService->expects(self::never())->method('formatPhoneNumber');
  939. $this->assertEquals(
  940. null,
  941. $dolibarrSyncService->getOrganizationPhone($organization)
  942. );
  943. }
  944. /**
  945. * @see DolibarrSyncService::getOrganizationEmail()
  946. */
  947. public function testGetOrganizationEmailWithExistingEmail()
  948. {
  949. $dolibarrSyncService = $this->getMockForMethod('getOrganizationEmail');
  950. $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
  951. $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
  952. $contactPoint3 = $this->getMockBuilder(ContactPoint::class)->getMock();
  953. $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER);
  954. $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::BILL);
  955. $contactPoint3->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL);
  956. $contactPoint2->method('getEmail')->willReturn('email@email.com');
  957. $organization = $this->getMockBuilder(Organization::class)->getMock();
  958. $organization
  959. ->expects($this->once())
  960. ->method('getContactPoints')
  961. ->willReturn(
  962. new ArrayCollection([$contactPoint1, $contactPoint2, $contactPoint3])
  963. );
  964. $this->assertEquals(
  965. 'email@email.com',
  966. $dolibarrSyncService->getOrganizationEmail($organization)
  967. );
  968. }
  969. /**
  970. * @see DolibarrSyncService::getOrganizationEmail()
  971. */
  972. public function testGetOrganizationEmailWithNoEmail()
  973. {
  974. $dolibarrSyncService = $this->getMockForMethod('getOrganizationEmail');
  975. $organization = $this->getMockBuilder(Organization::class)->getMock();
  976. $organization
  977. ->expects($this->once())
  978. ->method('getContactPoints')
  979. ->willReturn(new ArrayCollection([]));
  980. $this->assertEquals(
  981. null,
  982. $dolibarrSyncService->getOrganizationEmail($organization)
  983. );
  984. }
  985. /**
  986. * @see DolibarrSyncService::getOrganizationNetworkId()
  987. */
  988. public function testGetOrganizationNetworkId()
  989. {
  990. $dolibarrSyncService = $this->getMockForMethod('getOrganizationNetworkId');
  991. $organization = $this->getMockBuilder(Organization::class)->getMock();
  992. $network = $this->getMockBuilder(Network::class)->getMock();
  993. $network->method('getId')->willReturn(3);
  994. $networkOrganization = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  995. $networkOrganization->method('getNetwork')->willReturn($network);
  996. $organization->method('getNetworkOrganizations')->willReturn(new ArrayCollection([$networkOrganization]));
  997. $this->assertEquals(
  998. 3,
  999. $dolibarrSyncService->getOrganizationNetworkId($organization)
  1000. );
  1001. }
  1002. /**
  1003. * @see DolibarrSyncService::getOrganizationNetworkId()
  1004. */
  1005. public function testGetOrganizationNetworkIdWithMultipleResult()
  1006. {
  1007. $dolibarrSyncService = $this->getMockForMethod('getOrganizationNetworkId');
  1008. $network1 = $this->getMockBuilder(Network::class)->getMock();
  1009. $network1->method('getId')->willReturn(3);
  1010. $networkOrganization1 = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  1011. $networkOrganization1->method('getNetwork')->willReturn($network1);
  1012. $networkOrganization1->method('getEndDate')->willReturn(new \DateTime('2000-01-01'));
  1013. $network2 = $this->getMockBuilder(Network::class)->getMock();
  1014. $network2->method('getId')->willReturn(4);
  1015. $networkOrganization2 = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  1016. $networkOrganization2->method('getNetwork')->willReturn($network2);
  1017. $networkOrganization2->method('getEndDate')->willReturn(null);
  1018. $organization = $this->getMockBuilder(Organization::class)->getMock();
  1019. $organization->method('getNetworkOrganizations')->willReturn(
  1020. new ArrayCollection([$networkOrganization1, $networkOrganization2])
  1021. );
  1022. $this->assertEquals(
  1023. 4,
  1024. $dolibarrSyncService->getOrganizationNetworkId($organization)
  1025. );
  1026. }
  1027. /**
  1028. * @see DolibarrSyncService::getOrganizationNetworkId()
  1029. */
  1030. public function testGetOrganizationNetworkIdWithNoResult()
  1031. {
  1032. $dolibarrSyncService = $this->getMockForMethod('getOrganizationNetworkId');
  1033. $organization = $this->getMockBuilder(Organization::class)->getMock();
  1034. $network = $this->getMockBuilder(Network::class)->getMock();
  1035. $network->method('getId')->willReturn(3);
  1036. $networkOrganization = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  1037. $networkOrganization->method('getNetwork')->willReturn($network);
  1038. $networkOrganization->method('getEndDate')->willReturn(new \DateTime('2000-01-01'));
  1039. $organization->method('getNetworkOrganizations')->willReturn(new ArrayCollection([$networkOrganization]));
  1040. $this->assertEquals(
  1041. null,
  1042. $dolibarrSyncService->getOrganizationNetworkId($organization)
  1043. );
  1044. }
  1045. /**
  1046. * @see DolibarrSyncService::countWithMission()
  1047. */
  1048. public function testCountWithMission()
  1049. {
  1050. $dolibarrSyncService = $this->getMockForMethod('countWithMission');
  1051. $members = [
  1052. 123 => [FunctionEnum::PRESIDENT->value, FunctionEnum::TEACHER->value],
  1053. 124 => [FunctionEnum::TEACHER->value],
  1054. 125 => [FunctionEnum::STUDENT->value],
  1055. 126 => [FunctionEnum::TREASURER->value],
  1056. ];
  1057. $this->assertEquals(
  1058. 2,
  1059. $dolibarrSyncService->countWithMission([FunctionEnum::TEACHER->value], $members)
  1060. );
  1061. $this->assertEquals(
  1062. 3,
  1063. $dolibarrSyncService->countWithMission(
  1064. [FunctionEnum::TEACHER->value, FunctionEnum::TREASURER->value],
  1065. $members
  1066. )
  1067. );
  1068. $this->assertEquals(
  1069. 1,
  1070. $dolibarrSyncService->countWithMission([FunctionEnum::STUDENT->value], $members)
  1071. );
  1072. $this->assertEquals(
  1073. 0,
  1074. $dolibarrSyncService->countWithMission([FunctionEnum::ARCHIVIST->value], $members)
  1075. );
  1076. }
  1077. /**
  1078. * @see DolibarrSyncService::getPersonContact()
  1079. */
  1080. public function testGetPersonContact()
  1081. {
  1082. $dolibarrSyncService = $this->getMockForMethod('getPersonContact');
  1083. $person = $this->getMockBuilder(Person::class)->getMock();
  1084. $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
  1085. $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
  1086. $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER);
  1087. $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL);
  1088. $person->expects($this->once())->method('getContactPoints')->willReturn(new ArrayCollection([$contactPoint1, $contactPoint2]));
  1089. $this->assertEquals(
  1090. $contactPoint2,
  1091. $dolibarrSyncService->getPersonContact($person)
  1092. );
  1093. $person2 = $this->getMockBuilder(Person::class)->getMock();
  1094. $person2->expects($this->once())->method('getContactPoints')->willReturn(new ArrayCollection([]));
  1095. $this->assertEquals(
  1096. null,
  1097. $dolibarrSyncService->getPersonContact($person2)
  1098. );
  1099. }
  1100. /**
  1101. * @see DolibarrSyncService::formatContactPosition()
  1102. */
  1103. public function testFormatContactPosition()
  1104. {
  1105. $dolibarrSyncService = $this->getMockForMethod('formatContactPosition');
  1106. $this->translator->method('trans')->willReturnMap(
  1107. [
  1108. [FunctionEnum::PRESIDENT->value, ['gender' => 'X'], null, null, 'Président(e)'],
  1109. [FunctionEnum::PRESIDENT->value, ['gender' => 'M'], null, null, 'Président'],
  1110. [FunctionEnum::PRESIDENT->value, ['gender' => 'F'], null, null, 'Présidente'],
  1111. [FunctionEnum::DIRECTOR->value, ['gender' => 'X'], null, null, 'Directeur(ice)'],
  1112. [FunctionEnum::DIRECTOR->value, ['gender' => 'M'], null, null, 'Directeur'],
  1113. [FunctionEnum::DIRECTOR->value, ['gender' => 'F'], null, null, 'Directrice'],
  1114. [FunctionEnum::TEACHER->value, ['gender' => 'X'], null, null, 'Professeur(e)'],
  1115. [FunctionEnum::ARCHIVIST->value, ['gender' => 'X'], null, null, 'Archiviste'],
  1116. [FunctionEnum::TREASURER->value, ['gender' => 'X'], null, null, 'Trésorier(ère)'],
  1117. [FunctionEnum::ADMINISTRATIVE_STAFF->value, ['gender' => 'X'], null, null, 'Personnel administratif'],
  1118. ]
  1119. );
  1120. $this->assertEquals(
  1121. 'Président(e)',
  1122. $dolibarrSyncService->formatContactPosition([FunctionEnum::PRESIDENT->value])
  1123. );
  1124. $this->assertEquals(
  1125. 'Président',
  1126. $dolibarrSyncService->formatContactPosition([FunctionEnum::PRESIDENT->value], 'MISTER')
  1127. );
  1128. $this->assertEquals(
  1129. 'Présidente',
  1130. $dolibarrSyncService->formatContactPosition([FunctionEnum::PRESIDENT->value], 'MISS')
  1131. );
  1132. $this->assertEquals(
  1133. 'Présidente, Directrice',
  1134. $dolibarrSyncService->formatContactPosition(
  1135. [FunctionEnum::PRESIDENT->value, FunctionEnum::DIRECTOR->value],
  1136. 'MISS'
  1137. )
  1138. );
  1139. $this->assertEquals(
  1140. 'Président, Directeur',
  1141. $dolibarrSyncService->formatContactPosition(
  1142. [FunctionEnum::PRESIDENT->value, FunctionEnum::DIRECTOR->value, FunctionEnum::ADHERENT->value],
  1143. 'MISTER'
  1144. )
  1145. );
  1146. $this->assertEquals(
  1147. 'Président, Directeur',
  1148. $dolibarrSyncService->formatContactPosition(
  1149. [FunctionEnum::PRESIDENT->value, FunctionEnum::DIRECTOR->value, FunctionEnum::ADHERENT->value],
  1150. 'MISTER'
  1151. )
  1152. );
  1153. $this->assertEquals(
  1154. 'Président(e), Directeur(ice), Professeur(e), Archiviste, Trésorier(ère), Pers...',
  1155. $dolibarrSyncService->formatContactPosition(
  1156. [
  1157. FunctionEnum::PRESIDENT->value,
  1158. FunctionEnum::DIRECTOR->value,
  1159. FunctionEnum::TEACHER->value,
  1160. FunctionEnum::ARCHIVIST->value,
  1161. FunctionEnum::TREASURER->value,
  1162. FunctionEnum::ADMINISTRATIVE_STAFF->value,
  1163. ],
  1164. 'X'
  1165. )
  1166. );
  1167. }
  1168. /**
  1169. * @see DolibarrSyncService::formatPhoneNumber()
  1170. */
  1171. public function testFormatPhoneNumber()
  1172. {
  1173. $dolibarrSyncService = $this->getMockForMethod('formatPhoneNumber');
  1174. $phoneUtil = PhoneNumberUtil::getInstance();
  1175. $phoneNumber = $phoneUtil->parse('01 02 03 04 05', 'FR');
  1176. $this->assertEquals(
  1177. '+33102030405',
  1178. $dolibarrSyncService->formatPhoneNumber($phoneNumber)
  1179. );
  1180. }
  1181. public function testGetExpectedTagsFor()
  1182. {
  1183. $dolibarrSyncService = $this->getMockForMethod('getExpectedTagsFor');
  1184. $organization1 = $this->getMockBuilder(Organization::class)->getMock();
  1185. $organization1->method('getLegalStatus')->willReturn(LegalEnum::ASSOCIATION_LAW_1901);
  1186. $settings1 = $this->getMockBuilder(Settings::class)->getMock();
  1187. $settings1->method('getProduct')->willReturn(SettingsProductEnum::SCHOOL);
  1188. $organization1->method('getSettings')->willReturn($settings1);
  1189. $organization2 = $this->getMockBuilder(Organization::class)->getMock();
  1190. $organization2->method('getLegalStatus')->willReturn(LegalEnum::ASSOCIATION_LAW_1901);
  1191. $settings2 = $this->getMockBuilder(Settings::class)->getMock();
  1192. $settings2->method('getProduct')->willReturn(SettingsProductEnum::SCHOOL_PREMIUM);
  1193. $organization2->method('getSettings')->willReturn($settings2);
  1194. $organization3 = $this->getMockBuilder(Organization::class)->getMock();
  1195. $organization3->method('getLegalStatus')->willReturn(LegalEnum::LOCAL_AUTHORITY);
  1196. $settings3 = $this->getMockBuilder(Settings::class)->getMock();
  1197. $settings3->method('getProduct')->willReturn(SettingsProductEnum::SCHOOL);
  1198. $organization3->method('getSettings')->willReturn($settings3);
  1199. $organization4 = $this->getMockBuilder(Organization::class)->getMock();
  1200. $organization4->method('getLegalStatus')->willReturn(LegalEnum::LOCAL_AUTHORITY);
  1201. $settings4 = $this->getMockBuilder(Settings::class)->getMock();
  1202. $settings4->method('getProduct')->willReturn(SettingsProductEnum::SCHOOL_PREMIUM);
  1203. $organization4->method('getSettings')->willReturn($settings4);
  1204. $organization5 = $this->getMockBuilder(Organization::class)->getMock();
  1205. $organization5->method('getLegalStatus')->willReturn(LegalEnum::LOCAL_AUTHORITY);
  1206. $settings5 = $this->getMockBuilder(Settings::class)->getMock();
  1207. $settings5->method('getProduct')->willReturn(SettingsProductEnum::ARTIST);
  1208. $organization5->method('getSettings')->willReturn($settings5);
  1209. $this->assertEquals(
  1210. $dolibarrSyncService->getExpectedTagsFor($organization1),
  1211. [67]
  1212. );
  1213. $this->assertEquals(
  1214. $dolibarrSyncService->getExpectedTagsFor($organization2),
  1215. [69]
  1216. );
  1217. $this->assertEquals(
  1218. $dolibarrSyncService->getExpectedTagsFor($organization3),
  1219. [68]
  1220. );
  1221. $this->assertEquals(
  1222. $dolibarrSyncService->getExpectedTagsFor($organization4),
  1223. [70]
  1224. );
  1225. $this->assertEquals(
  1226. $dolibarrSyncService->getExpectedTagsFor($organization5),
  1227. []
  1228. );
  1229. }
  1230. }