DolibarrSyncServiceTest.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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\ApiRequestService;
  24. use App\Service\Rest\Operation\CreateOperation;
  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 function getDolibarrContactsIndex(int $socId): array { return parent::getDolibarrContactsIndex($socId); }
  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 filterDiff(array $initialData, array $newData): array { return parent::filterDiff($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('getOpentalentContacts')
  173. ->with(1726)
  174. ->willReturn(
  175. array_filter(
  176. $this->getJsonContentFromFixture('contacts.json'),
  177. 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 : ``',
  271. 'lastname : `Watson`',
  272. 'firstname : `John`',
  273. 'email : ``',
  274. 'phone_pro : ``',
  275. 'phone_mobile : ``',
  276. 'poste : ``',
  277. 'socid : `1726`',
  278. 'array_options.options_2iopen_person_id : `1000`'
  279. ],
  280. $operations[2]->getChangeLog()
  281. );
  282. $this->assertEquals(
  283. ['[PUT contacts/5869]', 'statut : `1` => `0`'],
  284. $operations[3]->getChangeLog()
  285. );
  286. }
  287. public function testExecuteOk() {
  288. $operation = new CreateOperation('operation 1', 'thirdparty', ['data' => 1]);
  289. $this->assertEquals($operation->getStatus(), $operation::STATUS_READY);
  290. $response = $this->getMockBuilder(ResponseInterface::class)->getMock();
  291. $response->method('getStatusCode')->willReturn(200);
  292. $this->dolibarrApiService->method('request')->willReturn($response);
  293. $syncService = $this->newDolibarrSyncService();
  294. $operations = $syncService->execute([$operation]);
  295. $this->assertEquals($operation::STATUS_DONE, $operations[0]->getStatus());
  296. }
  297. public function testGetDolibarrSocietiesIndex() {
  298. $this->dolibarrApiService
  299. ->expects($this->once())
  300. ->method('getAllClients')
  301. ->willReturn(
  302. $this->getJsonContentFromFixture('thirdparties.json')
  303. );
  304. $syncService = $this->newDolibarrSyncService();
  305. $index = $syncService->getDolibarrSocietiesIndex();
  306. $this->assertEquals("13930", $index[13930]['array_options']['options_2iopen_organization_id']);
  307. }
  308. public function testDolibarrContactsIndex() {
  309. $this->dolibarrApiService
  310. ->expects($this->once())
  311. ->method('getOpentalentContacts')
  312. ->with(9)
  313. ->willReturn(
  314. $this->getJsonContentFromFixture('contacts.json')
  315. );
  316. $syncService = $this->newDolibarrSyncService();
  317. $index = $syncService->getDolibarrContactsIndex(9);
  318. $this->assertEquals("302117", $index[302117]['array_options']['options_2iopen_person_id']);
  319. }
  320. public function testActiveMembersIndex() {
  321. $this->accessRepository
  322. ->expects($this->once())
  323. ->method('getAllActiveMembersAndMissions')
  324. ->willReturn(
  325. [
  326. ['id' => 123, 'organization_id' => 1, 'mission' => FunctionEnum::PRESIDENT()->getValue()],
  327. ['id' => 123, 'organization_id' => 1, 'mission' => FunctionEnum::TEACHER()->getValue()],
  328. ['id' => 124, 'organization_id' => 1, 'mission' => FunctionEnum::ADMINISTRATIVE_STAFF()->getValue()],
  329. ['id' => 125, 'organization_id' => 2, 'mission' => FunctionEnum::ADHERENT()->getValue()],
  330. ]
  331. );
  332. $syncService = $this->newDolibarrSyncService();
  333. $this->assertEquals(
  334. [
  335. 1 => [
  336. 123 => [FunctionEnum::PRESIDENT()->getValue(), FunctionEnum::TEACHER()->getValue()],
  337. 124 => [FunctionEnum::ADMINISTRATIVE_STAFF()->getValue()]
  338. ],
  339. 2 => [
  340. 125 => [FunctionEnum::ADHERENT()->getValue()]
  341. ]
  342. ],
  343. $syncService->getActiveMembersIndex()
  344. );
  345. }
  346. public function testSanitizeDolibarrData() {
  347. $this->assertEquals(null, TestableDolibarrSyncService::sanitizeDolibarrData(null));
  348. $this->assertEquals(
  349. ['a' => 'A', 'b' => null, 'c' => ['d' => 'D', 'e' => null]],
  350. TestableDolibarrSyncService::sanitizeDolibarrData(
  351. ['a' => 'A', 'b' => '', 'c' => ['d' => 'D', 'e' => '']]
  352. )
  353. );
  354. }
  355. public function testGetOrganizationPostalAddress() {
  356. $organization = $this->getMockBuilder(Organization::class)->getMock();
  357. $organizationAddressPostal1 = $this->getMockBuilder(OrganizationAddressPostal::class)->getMock();
  358. $organizationAddressPostal2 = $this->getMockBuilder(OrganizationAddressPostal::class)->getMock();
  359. $organizationAddressPostal3 = $this->getMockBuilder(OrganizationAddressPostal::class)->getMock();
  360. $addressPostal = $this->getMockBuilder(AddressPostal::class)->getMock();
  361. $organizationAddressPostal1->method('getType')->willReturn(AddressPostalOrganizationTypeEnum::ADDRESS_PRACTICE()->getValue());
  362. $organizationAddressPostal2->method('getType')->willReturn(AddressPostalOrganizationTypeEnum::ADDRESS_BILL()->getValue());
  363. $organizationAddressPostal3->method('getType')->willReturn(AddressPostalOrganizationTypeEnum::ADDRESS_OTHER()->getValue());
  364. $organizationAddressPostal2->method('getAddressPostal')->willReturn($addressPostal);
  365. $organization->expects($this->once())
  366. ->method('getOrganizationAddressPostals')
  367. ->willReturn(
  368. new ArrayCollection([$organizationAddressPostal1, $organizationAddressPostal2, $organizationAddressPostal3])
  369. );
  370. $syncService = $this->newDolibarrSyncService($organization);
  371. $this->assertEquals(
  372. $addressPostal,
  373. $syncService->getOrganizationPostalAddress($organization)
  374. );
  375. $organization2 = $this->getMockBuilder(Organization::class)->getMock();
  376. $organization2->expects($this->once())
  377. ->method('getOrganizationAddressPostals')
  378. ->willReturn(new ArrayCollection([]));
  379. $this->assertEquals(
  380. null,
  381. $syncService->getOrganizationPostalAddress($organization2)
  382. );
  383. }
  384. public function testGetOrganizationPhoneWithExistingPhone()
  385. {
  386. $organization = $this->getMockBuilder(Organization::class)->getMock();
  387. $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
  388. $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
  389. $contactPoint3 = $this->getMockBuilder(ContactPoint::class)->getMock();
  390. $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER()->getValue());
  391. $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::BILL()->getValue());
  392. $contactPoint3->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL()->getValue());
  393. $phoneUtil = PhoneNumberUtil::getInstance();
  394. $phoneNumber = $phoneUtil->parse('0161626365', "FR");
  395. $contactPoint2->method('getTelphone')->willReturn($phoneNumber);
  396. $organization
  397. ->expects($this->once())
  398. ->method('getContactPoints')
  399. ->willReturn(
  400. new ArrayCollection([$contactPoint1, $contactPoint2, $contactPoint3])
  401. );
  402. $syncService = $this->newDolibarrSyncService();
  403. $this->assertEquals(
  404. '+33161626365',
  405. $syncService->getOrganizationPhone($organization)
  406. );
  407. }
  408. public function testGetOrganizationPhoneWithMobilePhone() {
  409. $organization = $this->getMockBuilder(Organization::class)->getMock();
  410. $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
  411. $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
  412. $contactPoint3 = $this->getMockBuilder(ContactPoint::class)->getMock();
  413. $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER()->getValue());
  414. $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::BILL()->getValue());
  415. $contactPoint3->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL()->getValue());
  416. $contactPoint2->expects($this->once())->method('getTelphone')->willReturn(null);
  417. $phoneUtil = PhoneNumberUtil::getInstance();
  418. $phoneNumber = $phoneUtil->parse('0661626365', "FR");
  419. $contactPoint2->method('getMobilPhone')->willReturn($phoneNumber);
  420. $organization
  421. ->expects($this->once())
  422. ->method('getContactPoints')
  423. ->willReturn(
  424. new ArrayCollection([$contactPoint1, $contactPoint2, $contactPoint3])
  425. );
  426. $syncService = $this->newDolibarrSyncService();
  427. $this->assertEquals(
  428. '+33661626365',
  429. $syncService->getOrganizationPhone($organization)
  430. );
  431. }
  432. public function testGetOrganizationPhoneWithNoPhone() {
  433. $organization = $this->getMockBuilder(Organization::class)->getMock();
  434. $organization
  435. ->expects($this->once())
  436. ->method('getContactPoints')
  437. ->willReturn(new ArrayCollection([]));
  438. $syncService = $this->newDolibarrSyncService();
  439. $this->assertEquals(
  440. null,
  441. $syncService->getOrganizationPhone($organization)
  442. );
  443. }
  444. public function testGetOrganizationEmailWithExistingEmail() {
  445. $organization = $this->getMockBuilder(Organization::class)->getMock();
  446. $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
  447. $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
  448. $contactPoint3 = $this->getMockBuilder(ContactPoint::class)->getMock();
  449. $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER()->getValue());
  450. $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::BILL()->getValue());
  451. $contactPoint3->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL()->getValue());
  452. $contactPoint2->method('getEmail')->willReturn('email@email.com');
  453. $organization
  454. ->expects($this->once())
  455. ->method('getContactPoints')
  456. ->willReturn(
  457. new ArrayCollection([$contactPoint1, $contactPoint2, $contactPoint3])
  458. );
  459. $syncService = $this->newDolibarrSyncService();
  460. $this->assertEquals(
  461. 'email@email.com',
  462. $syncService->getOrganizationEmail($organization)
  463. );
  464. }
  465. public function testGetOrganizationEmailWithNoEmail() {
  466. $organization = $this->getMockBuilder(Organization::class)->getMock();
  467. $organization
  468. ->expects($this->once())
  469. ->method('getContactPoints')
  470. ->willReturn(new ArrayCollection([]));
  471. $syncService = $this->newDolibarrSyncService();
  472. $this->assertEquals(
  473. null,
  474. $syncService->getOrganizationEmail($organization)
  475. );
  476. }
  477. public function testCountWithMission() {
  478. $members = [
  479. 123 => [FunctionEnum::PRESIDENT()->getValue(), FunctionEnum::TEACHER()->getValue()],
  480. 124 => [FunctionEnum::TEACHER()->getValue()],
  481. 125 => [FunctionEnum::STUDENT()->getValue()],
  482. 126 => [FunctionEnum::TREASURER()->getValue()],
  483. ];
  484. $this->assertEquals(
  485. 2,
  486. TestableDolibarrSyncService::countWithMission([FunctionEnum::TEACHER()->getValue()], $members)
  487. );
  488. $this->assertEquals(
  489. 3,
  490. TestableDolibarrSyncService::countWithMission(
  491. [FunctionEnum::TEACHER()->getValue(), FunctionEnum::TREASURER()->getValue()],
  492. $members
  493. )
  494. );
  495. $this->assertEquals(
  496. 1,
  497. TestableDolibarrSyncService::countWithMission([FunctionEnum::STUDENT()->getValue()], $members)
  498. );
  499. $this->assertEquals(
  500. 0,
  501. TestableDolibarrSyncService::countWithMission([FunctionEnum::ARCHIVIST()->getValue()], $members)
  502. );
  503. }
  504. public function testGetPersonContact() {
  505. $person = $this->getMockBuilder(Person::class)->getMock();
  506. $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
  507. $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
  508. $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER()->getValue());
  509. $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL()->getValue());
  510. $person->expects($this->once())->method('getContactPoints')->willReturn(new ArrayCollection([$contactPoint1, $contactPoint2]));
  511. $syncService = $this->newDolibarrSyncService();
  512. $this->assertEquals(
  513. $contactPoint2,
  514. $syncService->getPersonContact($person)
  515. );
  516. $person2 = $this->getMockBuilder(Person::class)->getMock();
  517. $person2->expects($this->once())->method('getContactPoints')->willReturn(new ArrayCollection([]));
  518. $this->assertEquals(null, $syncService->getPersonContact($person2));
  519. }
  520. public function testFormatContactPosition() {
  521. $this->translator->method('trans')->willReturnMap(
  522. [
  523. [FunctionEnum::PRESIDENT()->getValue(), ['gender' => 'X'], null, null, 'Président(e)'],
  524. [FunctionEnum::PRESIDENT()->getValue(), ['gender' => 'M'], null, null, 'Président'],
  525. [FunctionEnum::PRESIDENT()->getValue(), ['gender' => 'F'], null, null, 'Présidente'],
  526. [FunctionEnum::DIRECTOR()->getValue(), ['gender' => 'X'], null, null, 'Directeur(ice)'],
  527. [FunctionEnum::DIRECTOR()->getValue(), ['gender' => 'M'], null, null, 'Directeur'],
  528. [FunctionEnum::DIRECTOR()->getValue(), ['gender' => 'F'], null, null, 'Directrice'],
  529. ]
  530. );
  531. $syncService = $this->newDolibarrSyncService();
  532. $this->assertEquals(
  533. 'Président(e)',
  534. $syncService->formatContactPosition([FunctionEnum::PRESIDENT()->getValue()])
  535. );
  536. $this->assertEquals(
  537. 'Président',
  538. $syncService->formatContactPosition([FunctionEnum::PRESIDENT()->getValue()], 'MISTER')
  539. );
  540. $this->assertEquals(
  541. 'Présidente',
  542. $syncService->formatContactPosition([FunctionEnum::PRESIDENT()->getValue()], 'MISS')
  543. );
  544. $this->assertEquals(
  545. 'Présidente, Directrice',
  546. $syncService->formatContactPosition(
  547. [FunctionEnum::PRESIDENT()->getValue(), FunctionEnum::DIRECTOR()->getValue()],
  548. 'MISS'
  549. )
  550. );
  551. $this->assertEquals(
  552. 'Président, Directeur',
  553. $syncService->formatContactPosition(
  554. [FunctionEnum::PRESIDENT()->getValue(), FunctionEnum::DIRECTOR()->getValue(), FunctionEnum::ADHERENT()->getValue()],
  555. 'MISTER'
  556. )
  557. );
  558. }
  559. public function testFormatPhoneNumber() {
  560. $phoneUtil = PhoneNumberUtil::getInstance();
  561. $phoneNumber = $phoneUtil->parse('01 02 03 04 05', "FR");
  562. $this->assertEquals(
  563. '+33102030405',
  564. TestableDolibarrSyncService::formatPhoneNumber($phoneNumber)
  565. );
  566. }
  567. public function testFilterDiff() {
  568. $this->assertEquals(
  569. ['b' => -2, 'c' => ['d' => 4, 'e' => ['f' => -5]], 'g' => 7],
  570. TestableDolibarrSyncService::filterDiff(
  571. ['a' => 1, 'b' => 2, 'c' => ['d' => 4, 'e' => ['f' => 5]]],
  572. ['a' => 1, 'b' => -2, 'c' => ['d' => 4, 'e' => ['f' => -5]], 'g' => 7],
  573. )
  574. );
  575. $this->assertEquals(
  576. [],
  577. TestableDolibarrSyncService::filterDiff(
  578. ['a' => 1, 'b' => 2, 'c' => ['d' => 4, 'e' => ['f' => 5]]],
  579. ['a' => 1, 'b' => 2, 'c' => ['d' => 4, 'e' => ['f' => 5]]],
  580. )
  581. );
  582. $this->assertEquals(
  583. [],
  584. TestableDolibarrSyncService::filterDiff(
  585. [],
  586. [],
  587. )
  588. );
  589. $this->assertEquals(
  590. ['a' => 1],
  591. TestableDolibarrSyncService::filterDiff(
  592. [],
  593. ['a' => 1],
  594. )
  595. );
  596. }
  597. }