DolibarrSyncServiceTest.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. <?php
  2. namespace App\Tests\Service\Dolibarr;
  3. use App\Entity\Access\Access;
  4. use App\Entity\Access\FunctionType;
  5. use App\Entity\Core\AddressPostal;
  6. use App\Entity\Core\ContactPoint;
  7. use App\Entity\Network\Network;
  8. use App\Entity\Network\NetworkOrganization;
  9. use App\Entity\Organization\Organization;
  10. use App\Entity\Organization\OrganizationAddressPostal;
  11. use App\Entity\Organization\Settings;
  12. use App\Entity\Person\Person;
  13. use App\Enum\Access\FunctionEnum;
  14. use App\Enum\Access\RoleEnum;
  15. use App\Enum\Core\ContactPointTypeEnum;
  16. use App\Enum\Organization\AddressPostalOrganizationTypeEnum;
  17. use App\Enum\Organization\SettingsProductEnum;
  18. use App\Repository\Access\AccessRepository;
  19. use App\Repository\Access\FunctionTypeRepository;
  20. use App\Repository\Organization\OrganizationRepository;
  21. use App\Service\Dolibarr\DolibarrApiService;
  22. use App\Service\Dolibarr\DolibarrSyncService;
  23. use App\Service\Rest\Operation\CreateOperation;
  24. use App\Service\Rest\Operation\UpdateOperation;
  25. use Doctrine\Common\Collections\ArrayCollection;
  26. use JetBrains\PhpStorm\Pure;
  27. use libphonenumber\PhoneNumber;
  28. use libphonenumber\PhoneNumberUtil;
  29. use PHPUnit\Framework\TestCase;
  30. use Psr\Log\LoggerInterface;
  31. use Symfony\Contracts\HttpClient\ResponseInterface;
  32. use Symfony\Contracts\Translation\TranslatorInterface;
  33. class TestableDolibarrSyncService extends DolibarrSyncService {
  34. public function getDolibarrSocietiesIndex(): array { return parent::getDolibarrSocietiesIndex(); }
  35. public static function findDolibarrContactFor(array $dolibarrContacts, Person $person): ?array { return parent::findDolibarrContactFor($dolibarrContacts, $person); }
  36. public function getActiveMembersIndex(): array { return parent::getActiveMembersIndex(); }
  37. public static function sanitizeDolibarrData(?array $data): ?array { return parent::sanitizeDolibarrData($data); }
  38. public function getOrganizationPostalAddress(Organization $organization): ?AddressPostal { return parent::getOrganizationPostalAddress($organization); }
  39. public function getOrganizationPhone(Organization $organization): ?string { return parent::getOrganizationPhone($organization); }
  40. public function getOrganizationEmail(Organization $organization): ?string { return parent::getOrganizationEmail($organization); }
  41. public static function countWithMission(array $missions, array $members): int { return parent::countWithMission($missions, $members); }
  42. public function getPersonContact(Person $person): ?ContactPoint { return parent::getPersonContact($person); }
  43. public function formatContactPosition(array $missions, ?string $gender = 'X'): string { return parent::formatContactPosition($missions, $gender); }
  44. public static function formatPhoneNumber(PhoneNumber $phoneNumber): string { return parent::formatPhoneNumber($phoneNumber); }
  45. public static function getChanges(array $initialData, array $newData): array { return parent::getChanges($initialData, $newData); }
  46. }
  47. class DolibarrSyncServiceTest extends TestCase
  48. {
  49. private OrganizationRepository $organizationRepository;
  50. private AccessRepository $accessRepository;
  51. private FunctionTypeRepository $functionTypeRepository;
  52. private DolibarrApiService $dolibarrApiService;
  53. private TranslatorInterface $translator;
  54. private LoggerInterface $logger;
  55. public function setUp(): void {
  56. $this->organizationRepository = $this->getMockBuilder(OrganizationRepository::class)
  57. ->disableOriginalConstructor()
  58. ->getMock();
  59. $this->accessRepository = $this->getMockBuilder(AccessRepository::class)
  60. ->disableOriginalConstructor()
  61. ->getMock();
  62. $this->functionTypeRepository = $this->getMockBuilder(FunctionTypeRepository::class)
  63. ->disableOriginalConstructor()
  64. ->getMock();
  65. $this->dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  66. ->disableOriginalConstructor()
  67. ->getMock();
  68. $this->translator = $this->getMockBuilder(TranslatorInterface::class)
  69. ->disableOriginalConstructor()
  70. ->getMock();
  71. $this->logger = $this->getMockBuilder(LoggerInterface::class)
  72. ->disableOriginalConstructor()
  73. ->getMock();
  74. $this->logger->method('info')->willReturnSelf();
  75. $this->logger->method('debug')->willReturnSelf();
  76. $this->logger->method('warning')->willReturnSelf();
  77. $this->logger->method('error')->willReturnSelf();
  78. }
  79. #[Pure]
  80. private function newDolibarrSyncService(): TestableDolibarrSyncService
  81. {
  82. return new TestableDolibarrSyncService(
  83. $this->organizationRepository,
  84. $this->accessRepository,
  85. $this->functionTypeRepository,
  86. $this->dolibarrApiService,
  87. $this->translator,
  88. $this->logger
  89. );
  90. }
  91. private function getJsonContentFromFixture(string $filename): array {
  92. $filepath = dirname(__FILE__) . '/fixtures/' . $filename;
  93. return json_decode(file_get_contents($filepath), true);
  94. }
  95. public function testScan() {
  96. // mock services and special methods from repos
  97. $this->dolibarrApiService
  98. ->expects($this->once())
  99. ->method('getAllClients')
  100. ->willReturn(
  101. $this->getJsonContentFromFixture('thirdparty.json')
  102. );
  103. $this->dolibarrApiService->method('getSociety')->willReturnMap(
  104. [
  105. [12097, ['id' => 711]],
  106. [91295, ['id' => 5086]]
  107. ]
  108. );
  109. $this->accessRepository
  110. ->expects($this->once())
  111. ->method('getAllActiveMembersAndMissions')
  112. ->willReturn(
  113. [
  114. ['id' => 1, 'organization_id' => 37306, 'mission' => FunctionEnum::PRESIDENT()->getValue()],
  115. ['id' => 1, 'organization_id' => 37306, 'mission' => FunctionEnum::ADHERENT()->getValue()],
  116. ['id' => 2, 'organization_id' => 37306, 'mission' => FunctionEnum::TREASURER()->getValue()],
  117. ['id' => 2, 'organization_id' => 37306, 'mission' => FunctionEnum::ADHERENT()->getValue()],
  118. ['id' => 3, 'organization_id' => 37306, 'mission' => FunctionEnum::STUDENT()->getValue()],
  119. ['id' => 3, 'organization_id' => 37306, 'mission' => FunctionEnum::ADHERENT()->getValue()],
  120. ]
  121. );
  122. // Function types
  123. $functionType1 = $this->getMockBuilder(FunctionType::class)->getMock();
  124. $functionType1->method('getMission')->willReturn(FunctionEnum::DIRECTOR()->getValue());
  125. $functionType2 = $this->getMockBuilder(FunctionType::class)->getMock();
  126. $functionType2->method('getMission')->willReturn(FunctionEnum::PRESIDENT()->getValue());
  127. $this->functionTypeRepository
  128. ->expects($this->once())
  129. ->method('findBy')
  130. ->with(['roleByDefault' => RoleEnum::ROLE_ADMIN()->getValue()])
  131. ->willReturn([$functionType1, $functionType2]);
  132. // Organization's name
  133. $organization = $this->getMockBuilder(Organization::class)->getMock();
  134. $organization->method('getId')->willReturn(37306);
  135. $organization->method('getName')->willReturn("Etablissement d'Enseignement Artistique");
  136. // Postal address
  137. $organizationAddressPostal = $this->getMockBuilder(OrganizationAddressPostal::class)->getMock();
  138. $addressPostal = $this->getMockBuilder(AddressPostal::class)->getMock();
  139. $addressPostal->method('getStreetAddress')->willReturn('21b baker street');
  140. $addressPostal->method('getStreetAddressSecond')->willReturn('');
  141. $addressPostal->method('getStreetAddressThird')->willReturn('');
  142. $addressPostal->method('getAddressOwner')->willReturn('');
  143. $addressPostal->method('getPostalCode')->willReturn('250 329');
  144. $addressPostal->method('getAddressCity')->willReturn('Londres');
  145. $organizationAddressPostal->method('getType')->willReturn(AddressPostalOrganizationTypeEnum::ADDRESS_CONTACT()->getValue());
  146. $organizationAddressPostal->method('getAddressPostal')->willReturn($addressPostal);
  147. $organization->method('getOrganizationAddressPostals')->willReturn(
  148. new ArrayCollection([$organizationAddressPostal])
  149. );
  150. // Email and phone
  151. $phoneUtil = PhoneNumberUtil::getInstance();
  152. $contactPoint = $this->getMockBuilder(ContactPoint::class)->getMock();
  153. $contactPoint->method('getContactType')->willReturn(ContactPointTypeEnum::CONTACT()->getValue());
  154. $contactPoint->method('getEmail')->willReturn('email@email.com');
  155. $phoneNumber = $phoneUtil->parse('01 02 03 04 05', 'FR');
  156. $contactPoint->method('getTelphone')->willReturn($phoneNumber);
  157. $organization->method('getContactPoints')->willReturn(
  158. new ArrayCollection([$contactPoint])
  159. );
  160. // Network
  161. $network = $this->getMockBuilder(Network::class)->getMock();
  162. $network->method('getId')->willReturn(91295);
  163. $networkOrganization = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  164. $networkOrganization->method('getNetwork')->willReturn($network);
  165. $organization->method('getNetworkOrganizations')->willReturn(new ArrayCollection([$networkOrganization]));
  166. // Product
  167. $settings = $this->getMockBuilder(Settings::class)->getMock();
  168. $settings->method('getProduct')->willReturn(SettingsProductEnum::SCHOOL()->getValue());
  169. $organization->method('getSettings')->willReturn($settings);
  170. // Get dolibarr contacts
  171. $this->dolibarrApiService
  172. ->method('getContacts')
  173. ->with(1726)
  174. ->willReturn(
  175. array_filter(
  176. $this->getJsonContentFromFixture('contacts.json'),
  177. static function ($c) {
  178. return in_array(
  179. (int)$c["array_options"]["options_2iopen_person_id"],
  180. [
  181. 108939, // existing person, to be updated
  182. 156252 // no matching person, to be deleted
  183. // a new contact shall be created too
  184. ]
  185. ); }
  186. )
  187. );
  188. $this->organizationRepository->method('find')->willReturn($organization);
  189. $access1 = $this->getMockBuilder(Access::class)->getMock();
  190. $access1->method('getId')->willReturn(1);
  191. $person1 = $this->getMockBuilder(Person::class)->getMock();
  192. $person1->method('getId')->willReturn(108939);
  193. $person1->method('getName')->willReturn('Holmes');
  194. $person1->method('getGender')->willReturn('MISTER');
  195. $person1->method('getGivenName')->willReturn('Sherlock');
  196. $person1->method('getGivenName')->willReturn('Sherlock');
  197. $personContactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
  198. $personContactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::CONTACT()->getValue());
  199. $personContactPoint1->method('getEmail')->willReturn('sherlock@holmes.com');
  200. $phoneNumber1 = $phoneUtil->parse('02 98 76 54 32', 'FR');
  201. $personContactPoint1->method('getTelphone')->willReturn($phoneNumber1);
  202. $personContactPoint1->method('getMobilPhone')->willReturn(null);
  203. $person1->method('getContactPoints')->willReturn(
  204. new ArrayCollection([$personContactPoint1])
  205. );
  206. $access1->method('getPerson')->willReturn($person1);
  207. $access2 = $this->getMockBuilder(Access::class)->getMock();
  208. $access2->method('getId')->willReturn(1);
  209. $person2 = $this->getMockBuilder(Person::class)->getMock();
  210. $person2->method('getId')->willReturn(1000);
  211. $person2->method('getName')->willReturn('Watson');
  212. $person2->method('getGender')->willReturn('MISTER');
  213. $person2->method('getGivenName')->willReturn('John');
  214. $person2->method('getGivenName')->willReturn('John');
  215. $personContactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
  216. $personContactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::CONTACT()->getValue());
  217. $personContactPoint2->method('getEmail')->willReturn('docteur@watson.com');
  218. $phoneNumber2 = $phoneUtil->parse('02 10 11 12 13', 'FR');
  219. $personContactPoint2->method('getTelphone')->willReturn($phoneNumber2);
  220. $personContactPoint2->method('getMobilPhone')->willReturn(null);
  221. $person2->method('getContactPoints')->willReturn(
  222. new ArrayCollection([$personContactPoint2])
  223. );
  224. $access2->method('getPerson')->willReturn($person2);
  225. $this->accessRepository->method('find')->willReturnMap(
  226. [
  227. [1, null, null, $access1],
  228. [2, null, null, $access2],
  229. ]
  230. );
  231. $this->translator->method('trans')->willReturnMap(
  232. [
  233. ['STUDENTS_COUNT', [], null, null, "Nombre d'élèves"],
  234. ['ADHERENTS_COUNT', [], null, null, "Nombre d'adhérents"],
  235. ['ADMIN_ACCESS_COUNT', [], null, null, "Nombre d'accès admin"],
  236. ]
  237. );
  238. $syncService = $this->newDolibarrSyncService();
  239. $operations = $syncService->scan();
  240. $this->assertCount(4, $operations);
  241. $this->assertEquals(
  242. [
  243. '[PUT thirdparties/1726]',
  244. "address : `\n217, rue Raoul Follereau\n` => `21b baker street`",
  245. 'zip : `74300` => `250 329`',
  246. 'town : `CLUSES` => `Londres`',
  247. 'email : `` => `email@email.com`',
  248. 'phone : `+33678403010` => `+33102030405`',
  249. 'parent : `` => `5086`',
  250. "array_options.options_2iopeninfoopentalent : `` => `Nombre d'élèves : 1\nNombre d'adhérents : 3\nNombre d'accès admin : 1`"
  251. ],
  252. $operations[0]->getChangeLog()
  253. );
  254. $this->assertEquals(
  255. [
  256. '[PUT contacts/5868]',
  257. 'civility_code : `MME` => ``',
  258. 'lastname : `DUPONT` => `Holmes`',
  259. 'firstname : `Valerie` => `Sherlock`',
  260. 'email : `abcd@hotmail.com` => ``',
  261. 'phone_pro : `+33478570000` => ``',
  262. 'phone_mobile : `+33682980000` => ``',
  263. 'poste : `Secrétaire` => ``'
  264. ],
  265. $operations[1]->getChangeLog()
  266. );
  267. $this->assertEquals(
  268. [
  269. '[POST contacts]',
  270. 'civility_code : (new) => ``',
  271. 'lastname : (new) => `Watson`',
  272. 'firstname : (new) => `John`',
  273. 'email : (new) => ``',
  274. 'phone_pro : (new) => ``',
  275. 'phone_mobile : (new) => ``',
  276. 'poste : (new) => ``',
  277. 'statut : (new) => `1`',
  278. 'array_options.options_2iopen_person_id : (new) => `1000`',
  279. 'socid : (new) => `1726`'
  280. ],
  281. $operations[2]->getChangeLog()
  282. );
  283. $this->assertEquals(
  284. ['[PUT contacts/5869]', 'statut : `1` => `0`'],
  285. $operations[3]->getChangeLog()
  286. );
  287. }
  288. public function testExecuteError()
  289. {
  290. $operation = new CreateOperation('operation 1', 'thirdparty', ['data' => 1]);
  291. $this->assertEquals($operation->getStatus(), $operation::STATUS_READY);
  292. $responseError = $this->getMockBuilder(ResponseInterface::class)->getMock();
  293. $responseError->method('getStatusCode')->willReturn(500);
  294. $this->dolibarrApiService->method('request')->willReturn($responseError);
  295. // POST operation will returned a server error
  296. $syncService = $this->newDolibarrSyncService();
  297. $operation = $syncService->execute([$operation])[0];
  298. $this->assertEquals($operation::STATUS_ERROR, $operation->getStatus());
  299. }
  300. public function testExecuteInvalid()
  301. {
  302. $operation = new UpdateOperation('operation 1', 'thirdparty', 1, ['data' => 1]);
  303. $responseInvalid = $this->getMockBuilder(ResponseInterface::class)->getMock();
  304. $responseInvalid->method('getStatusCode')->willReturn(200);
  305. $responseInvalid->method('toArray')->willReturn(['data' => 0]);
  306. $this->dolibarrApiService->method('request')->willReturn($responseInvalid);
  307. // POST operation will return a different content that the one which were posted, this should log a warning
  308. $this->logger->expects($this->once())->method('warning');
  309. $syncService = $this->newDolibarrSyncService();
  310. $operation = $syncService->execute([$operation])[0];
  311. $this->assertEquals($operation::STATUS_DONE, $operation->getStatus());
  312. }
  313. public function testExecuteOk() {
  314. $operation = new CreateOperation('operation 1', 'thirdparty', ['data' => 1]);
  315. $responseOk = $this->getMockBuilder(ResponseInterface::class)->getMock();
  316. $responseOk->method('getStatusCode')->willReturn(200);
  317. $responseOk->method('toArray')->willReturn(['data' => 1]);
  318. $this->dolibarrApiService->method('request')->willReturn($responseOk);
  319. $syncService = $this->newDolibarrSyncService();
  320. $operation = $syncService->execute([$operation])[0];
  321. $this->assertEquals($operation::STATUS_DONE, $operation->getStatus());
  322. }
  323. public function testGetDolibarrSocietiesIndex() {
  324. $this->dolibarrApiService
  325. ->expects($this->once())
  326. ->method('getAllClients')
  327. ->willReturn(
  328. $this->getJsonContentFromFixture('thirdparties.json')
  329. );
  330. $syncService = $this->newDolibarrSyncService();
  331. $index = $syncService->getDolibarrSocietiesIndex();
  332. $this->assertEquals("13930", $index[13930]['array_options']['options_2iopen_organization_id']);
  333. }
  334. public function testFindDolibarrContactFor() {
  335. $contacts = $this->getJsonContentFromFixture('contacts.json');
  336. // Find by id
  337. $person1 = $this->getMockBuilder(Person::class)->getMock();
  338. $person1->method('getId')->willReturn(108939);
  339. $contact1 = TestableDolibarrSyncService::findDolibarrContactFor($contacts, $person1);
  340. $this->assertEquals("5868", $contact1['id']);
  341. // Find by full name (contact already has another person id, it should not be returned)
  342. $person2 = $this->getMockBuilder(Person::class)->getMock();
  343. $person2->method('getId')->willReturn(-1);
  344. $person2->method('getName')->willReturn('dupont');
  345. $person2->method('getGivenName')->willReturn('Valerie');
  346. $contact2 = TestableDolibarrSyncService::findDolibarrContactFor($contacts, $person2);
  347. $this->assertEquals(null, $contact2);
  348. // Find by full name (contact has no person id, it should be returned)
  349. $person3 = $this->getMockBuilder(Person::class)->getMock();
  350. $person3->method('getId')->willReturn(-1);
  351. $person3->method('getName')->willReturn('ZORRO');
  352. $person3->method('getGivenName')->willReturn('Fabrice');
  353. $contact3 = TestableDolibarrSyncService::findDolibarrContactFor($contacts, $person3);
  354. $this->assertEquals("5872", $contact3['id']);
  355. // Do not find
  356. $person4 = $this->getMockBuilder(Person::class)->getMock();
  357. $person4->method('getId')->willReturn(-1);
  358. $contact4 = TestableDolibarrSyncService::findDolibarrContactFor($contacts, $person4);
  359. $this->assertEquals(null, $contact4);
  360. }
  361. public function testActiveMembersIndex() {
  362. $this->accessRepository
  363. ->expects($this->once())
  364. ->method('getAllActiveMembersAndMissions')
  365. ->willReturn(
  366. [
  367. ['id' => 123, 'organization_id' => 1, 'mission' => FunctionEnum::PRESIDENT()->getValue()],
  368. ['id' => 123, 'organization_id' => 1, 'mission' => FunctionEnum::TEACHER()->getValue()],
  369. ['id' => 124, 'organization_id' => 1, 'mission' => FunctionEnum::ADMINISTRATIVE_STAFF()->getValue()],
  370. ['id' => 125, 'organization_id' => 2, 'mission' => FunctionEnum::ADHERENT()->getValue()],
  371. ]
  372. );
  373. $syncService = $this->newDolibarrSyncService();
  374. $this->assertEquals(
  375. [
  376. 1 => [
  377. 123 => [FunctionEnum::PRESIDENT()->getValue(), FunctionEnum::TEACHER()->getValue()],
  378. 124 => [FunctionEnum::ADMINISTRATIVE_STAFF()->getValue()]
  379. ],
  380. 2 => [
  381. 125 => [FunctionEnum::ADHERENT()->getValue()]
  382. ]
  383. ],
  384. $syncService->getActiveMembersIndex()
  385. );
  386. }
  387. public function testSanitizeDolibarrData() {
  388. $this->assertEquals(null, TestableDolibarrSyncService::sanitizeDolibarrData(null));
  389. $this->assertEquals(
  390. ['a' => 'A', 'b' => null, 'c' => ['d' => 'D', 'e' => null]],
  391. TestableDolibarrSyncService::sanitizeDolibarrData(
  392. ['a' => 'A', 'b' => '', 'c' => ['d' => 'D', 'e' => '']]
  393. )
  394. );
  395. }
  396. public function testGetOrganizationPostalAddress() {
  397. $organization = $this->getMockBuilder(Organization::class)->getMock();
  398. $organizationAddressPostal1 = $this->getMockBuilder(OrganizationAddressPostal::class)->getMock();
  399. $organizationAddressPostal2 = $this->getMockBuilder(OrganizationAddressPostal::class)->getMock();
  400. $organizationAddressPostal3 = $this->getMockBuilder(OrganizationAddressPostal::class)->getMock();
  401. $addressPostal = $this->getMockBuilder(AddressPostal::class)->getMock();
  402. $organizationAddressPostal1->method('getType')->willReturn(AddressPostalOrganizationTypeEnum::ADDRESS_PRACTICE()->getValue());
  403. $organizationAddressPostal2->method('getType')->willReturn(AddressPostalOrganizationTypeEnum::ADDRESS_BILL()->getValue());
  404. $organizationAddressPostal3->method('getType')->willReturn(AddressPostalOrganizationTypeEnum::ADDRESS_OTHER()->getValue());
  405. $organizationAddressPostal2->method('getAddressPostal')->willReturn($addressPostal);
  406. $organization->expects($this->once())
  407. ->method('getOrganizationAddressPostals')
  408. ->willReturn(
  409. new ArrayCollection([$organizationAddressPostal1, $organizationAddressPostal2, $organizationAddressPostal3])
  410. );
  411. $syncService = $this->newDolibarrSyncService($organization);
  412. $this->assertEquals(
  413. $addressPostal,
  414. $syncService->getOrganizationPostalAddress($organization)
  415. );
  416. $organization2 = $this->getMockBuilder(Organization::class)->getMock();
  417. $organization2->expects($this->once())
  418. ->method('getOrganizationAddressPostals')
  419. ->willReturn(new ArrayCollection([]));
  420. $this->assertEquals(
  421. null,
  422. $syncService->getOrganizationPostalAddress($organization2)
  423. );
  424. }
  425. public function testGetOrganizationPhoneWithExistingPhone()
  426. {
  427. $organization = $this->getMockBuilder(Organization::class)->getMock();
  428. $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
  429. $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
  430. $contactPoint3 = $this->getMockBuilder(ContactPoint::class)->getMock();
  431. $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER()->getValue());
  432. $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::BILL()->getValue());
  433. $contactPoint3->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL()->getValue());
  434. $phoneUtil = PhoneNumberUtil::getInstance();
  435. $phoneNumber = $phoneUtil->parse('0161626365', "FR");
  436. $contactPoint2->method('getTelphone')->willReturn($phoneNumber);
  437. $organization
  438. ->expects($this->once())
  439. ->method('getContactPoints')
  440. ->willReturn(
  441. new ArrayCollection([$contactPoint1, $contactPoint2, $contactPoint3])
  442. );
  443. $syncService = $this->newDolibarrSyncService();
  444. $this->assertEquals(
  445. '+33161626365',
  446. $syncService->getOrganizationPhone($organization)
  447. );
  448. }
  449. public function testGetOrganizationPhoneWithMobilePhone() {
  450. $organization = $this->getMockBuilder(Organization::class)->getMock();
  451. $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
  452. $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
  453. $contactPoint3 = $this->getMockBuilder(ContactPoint::class)->getMock();
  454. $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER()->getValue());
  455. $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::BILL()->getValue());
  456. $contactPoint3->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL()->getValue());
  457. $contactPoint2->expects($this->once())->method('getTelphone')->willReturn(null);
  458. $phoneUtil = PhoneNumberUtil::getInstance();
  459. $phoneNumber = $phoneUtil->parse('0661626365', "FR");
  460. $contactPoint2->method('getMobilPhone')->willReturn($phoneNumber);
  461. $organization
  462. ->expects($this->once())
  463. ->method('getContactPoints')
  464. ->willReturn(
  465. new ArrayCollection([$contactPoint1, $contactPoint2, $contactPoint3])
  466. );
  467. $syncService = $this->newDolibarrSyncService();
  468. $this->assertEquals(
  469. '+33661626365',
  470. $syncService->getOrganizationPhone($organization)
  471. );
  472. }
  473. public function testGetOrganizationPhoneWithNoPhone() {
  474. $organization = $this->getMockBuilder(Organization::class)->getMock();
  475. $organization
  476. ->expects($this->once())
  477. ->method('getContactPoints')
  478. ->willReturn(new ArrayCollection([]));
  479. $syncService = $this->newDolibarrSyncService();
  480. $this->assertEquals(
  481. null,
  482. $syncService->getOrganizationPhone($organization)
  483. );
  484. }
  485. public function testGetOrganizationEmailWithExistingEmail() {
  486. $organization = $this->getMockBuilder(Organization::class)->getMock();
  487. $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
  488. $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
  489. $contactPoint3 = $this->getMockBuilder(ContactPoint::class)->getMock();
  490. $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER()->getValue());
  491. $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::BILL()->getValue());
  492. $contactPoint3->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL()->getValue());
  493. $contactPoint2->method('getEmail')->willReturn('email@email.com');
  494. $organization
  495. ->expects($this->once())
  496. ->method('getContactPoints')
  497. ->willReturn(
  498. new ArrayCollection([$contactPoint1, $contactPoint2, $contactPoint3])
  499. );
  500. $syncService = $this->newDolibarrSyncService();
  501. $this->assertEquals(
  502. 'email@email.com',
  503. $syncService->getOrganizationEmail($organization)
  504. );
  505. }
  506. public function testGetOrganizationEmailWithNoEmail() {
  507. $organization = $this->getMockBuilder(Organization::class)->getMock();
  508. $organization
  509. ->expects($this->once())
  510. ->method('getContactPoints')
  511. ->willReturn(new ArrayCollection([]));
  512. $syncService = $this->newDolibarrSyncService();
  513. $this->assertEquals(
  514. null,
  515. $syncService->getOrganizationEmail($organization)
  516. );
  517. }
  518. public function testCountWithMission() {
  519. $members = [
  520. 123 => [FunctionEnum::PRESIDENT()->getValue(), FunctionEnum::TEACHER()->getValue()],
  521. 124 => [FunctionEnum::TEACHER()->getValue()],
  522. 125 => [FunctionEnum::STUDENT()->getValue()],
  523. 126 => [FunctionEnum::TREASURER()->getValue()],
  524. ];
  525. $this->assertEquals(
  526. 2,
  527. TestableDolibarrSyncService::countWithMission([FunctionEnum::TEACHER()->getValue()], $members)
  528. );
  529. $this->assertEquals(
  530. 3,
  531. TestableDolibarrSyncService::countWithMission(
  532. [FunctionEnum::TEACHER()->getValue(), FunctionEnum::TREASURER()->getValue()],
  533. $members
  534. )
  535. );
  536. $this->assertEquals(
  537. 1,
  538. TestableDolibarrSyncService::countWithMission([FunctionEnum::STUDENT()->getValue()], $members)
  539. );
  540. $this->assertEquals(
  541. 0,
  542. TestableDolibarrSyncService::countWithMission([FunctionEnum::ARCHIVIST()->getValue()], $members)
  543. );
  544. }
  545. public function testGetPersonContact() {
  546. $person = $this->getMockBuilder(Person::class)->getMock();
  547. $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
  548. $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
  549. $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER()->getValue());
  550. $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL()->getValue());
  551. $person->expects($this->once())->method('getContactPoints')->willReturn(new ArrayCollection([$contactPoint1, $contactPoint2]));
  552. $syncService = $this->newDolibarrSyncService();
  553. $this->assertEquals(
  554. $contactPoint2,
  555. $syncService->getPersonContact($person)
  556. );
  557. $person2 = $this->getMockBuilder(Person::class)->getMock();
  558. $person2->expects($this->once())->method('getContactPoints')->willReturn(new ArrayCollection([]));
  559. $this->assertEquals(null, $syncService->getPersonContact($person2));
  560. }
  561. public function testFormatContactPosition() {
  562. $this->translator->method('trans')->willReturnMap(
  563. [
  564. [FunctionEnum::PRESIDENT()->getValue(), ['gender' => 'X'], null, null, 'Président(e)'],
  565. [FunctionEnum::PRESIDENT()->getValue(), ['gender' => 'M'], null, null, 'Président'],
  566. [FunctionEnum::PRESIDENT()->getValue(), ['gender' => 'F'], null, null, 'Présidente'],
  567. [FunctionEnum::DIRECTOR()->getValue(), ['gender' => 'X'], null, null, 'Directeur(ice)'],
  568. [FunctionEnum::DIRECTOR()->getValue(), ['gender' => 'M'], null, null, 'Directeur'],
  569. [FunctionEnum::DIRECTOR()->getValue(), ['gender' => 'F'], null, null, 'Directrice'],
  570. ]
  571. );
  572. $syncService = $this->newDolibarrSyncService();
  573. $this->assertEquals(
  574. 'Président(e)',
  575. $syncService->formatContactPosition([FunctionEnum::PRESIDENT()->getValue()])
  576. );
  577. $this->assertEquals(
  578. 'Président',
  579. $syncService->formatContactPosition([FunctionEnum::PRESIDENT()->getValue()], 'MISTER')
  580. );
  581. $this->assertEquals(
  582. 'Présidente',
  583. $syncService->formatContactPosition([FunctionEnum::PRESIDENT()->getValue()], 'MISS')
  584. );
  585. $this->assertEquals(
  586. 'Présidente, Directrice',
  587. $syncService->formatContactPosition(
  588. [FunctionEnum::PRESIDENT()->getValue(), FunctionEnum::DIRECTOR()->getValue()],
  589. 'MISS'
  590. )
  591. );
  592. $this->assertEquals(
  593. 'Président, Directeur',
  594. $syncService->formatContactPosition(
  595. [FunctionEnum::PRESIDENT()->getValue(), FunctionEnum::DIRECTOR()->getValue(), FunctionEnum::ADHERENT()->getValue()],
  596. 'MISTER'
  597. )
  598. );
  599. }
  600. public function testFormatPhoneNumber() {
  601. $phoneUtil = PhoneNumberUtil::getInstance();
  602. $phoneNumber = $phoneUtil->parse('01 02 03 04 05', "FR");
  603. $this->assertEquals(
  604. '+33102030405',
  605. TestableDolibarrSyncService::formatPhoneNumber($phoneNumber)
  606. );
  607. }
  608. }