DolibarrSyncServiceTest.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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 Doctrine\Common\Collections\ArrayCollection;
  24. use JetBrains\PhpStorm\Pure;
  25. use libphonenumber\PhoneNumber;
  26. use PHPUnit\Framework\TestCase;
  27. use Psr\Log\LoggerInterface;
  28. use Symfony\Contracts\Translation\TranslatorInterface;
  29. class TestableDolibarrSyncService extends DolibarrSyncService {
  30. public function getDolibarrSocietiesIndex(): array { return parent::getDolibarrSocietiesIndex(); }
  31. public function getDolibarrContactsIndex(int $socId): array { return parent::getDolibarrContactsIndex($socId); }
  32. public function getActiveMembersIndex(): array { return parent::getActiveMembersIndex(); }
  33. public static function sanitizeDolibarrData(?array $data): ?array { return parent::sanitizeDolibarrData($data); }
  34. public function getOrganizationPostalAddress(Organization $organization): AddressPostal { return parent::getOrganizationPostalAddress($organization); }
  35. public function getOrganizationPhone(Organization $organization): ?string { return parent::getOrganizationPhone($organization); }
  36. public function getOrganizationEmail(Organization $organization): ?string { return parent::getOrganizationEmail($organization); }
  37. public static function countWithMission(array $missions, array $members): int { return parent::countWithMission($missions, $members); }
  38. public function getPersonContact(Person $person): ?ContactPoint { return parent::getPersonContact($person); }
  39. public function formatContactPosition(array $missions, ?string $gender = 'X'): string { return parent::formatContactPosition($missions, $gender); }
  40. public static function formatPhoneNumber(PhoneNumber $phoneNumber): string { return parent::formatPhoneNumber($phoneNumber); }
  41. public static function filterDiff(array $initialData, array $newData): array { return parent::filterDiff($initialData, $newData); }
  42. }
  43. class DolibarrSyncServiceTest extends TestCase
  44. {
  45. private OrganizationRepository $organizationRepository;
  46. private AccessRepository $accessRepository;
  47. private FunctionTypeRepository $functionTypeRepository;
  48. private DolibarrApiService $dolibarrApiService;
  49. private TranslatorInterface $translator;
  50. private LoggerInterface $logger;
  51. public function setUp(): void {
  52. $this->organizationRepository = $this->getMockBuilder(OrganizationRepository::class)
  53. ->disableOriginalConstructor()
  54. ->getMock();
  55. $this->accessRepository = $this->getMockBuilder(AccessRepository::class)
  56. ->disableOriginalConstructor()
  57. ->getMock();
  58. $this->functionTypeRepository = $this->getMockBuilder(FunctionTypeRepository::class)
  59. ->disableOriginalConstructor()
  60. ->getMock();
  61. $this->dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  62. ->disableOriginalConstructor()
  63. ->getMock();
  64. $this->translator = $this->getMockBuilder(TranslatorInterface::class)
  65. ->disableOriginalConstructor()
  66. ->getMock();
  67. $this->logger = $this->getMockBuilder(LoggerInterface::class)
  68. ->disableOriginalConstructor()
  69. ->getMock();
  70. $this->logger->method('info')->willReturnSelf();
  71. $this->logger->method('debug')->willReturnSelf();
  72. $this->logger->method('warning')->willReturnSelf();
  73. $this->logger->method('error')->willReturnSelf();
  74. }
  75. #[Pure]
  76. private function newDolibarrSyncService(): TestableDolibarrSyncService
  77. {
  78. return new TestableDolibarrSyncService(
  79. $this->organizationRepository,
  80. $this->accessRepository,
  81. $this->functionTypeRepository,
  82. $this->dolibarrApiService,
  83. $this->translator,
  84. $this->logger
  85. );
  86. }
  87. private function getJsonContentFromFixture(string $filename): array {
  88. $filepath = dirname(__FILE__) . '/fixtures/' . $filename;
  89. return json_decode(file_get_contents($filepath), true);
  90. }
  91. public function testScan() {
  92. // mock services and special methods from repos
  93. $this->dolibarrApiService
  94. ->expects($this->once())
  95. ->method('getAllClients')
  96. ->willReturn(
  97. $this->getJsonContentFromFixture('thirdparty.json')
  98. );
  99. $this->dolibarrApiService->method('getSociety')->willReturnMap(
  100. [
  101. [12097, ['id' => 711]],
  102. [91295, ['id' => 5086]]
  103. ]
  104. );
  105. $this->accessRepository
  106. ->expects($this->once())
  107. ->method('getAllActiveMembersAndMissions')
  108. ->willReturn(
  109. [
  110. ['id' => 108939, 'organization_id' => 37306, 'mission' => FunctionEnum::PRESIDENT()->getValue()],
  111. ['id' => 108939, 'organization_id' => 37306, 'mission' => FunctionEnum::ADHERENT()->getValue()],
  112. ['id' => 156252, 'organization_id' => 37306, 'mission' => FunctionEnum::TREASURER()->getValue()],
  113. ['id' => 156252, 'organization_id' => 37306, 'mission' => FunctionEnum::ADHERENT()->getValue()],
  114. ['id' => 112775, 'organization_id' => 37306, 'mission' => FunctionEnum::STUDENT()->getValue()],
  115. ['id' => 112775, 'organization_id' => 37306, 'mission' => FunctionEnum::ADHERENT()->getValue()],
  116. ]
  117. );
  118. // Function types
  119. $functionType1 = $this->getMockBuilder(FunctionType::class)->getMock();
  120. $functionType1->method('getMission')->willReturn(FunctionEnum::DIRECTOR()->getValue());
  121. $functionType2 = $this->getMockBuilder(FunctionType::class)->getMock();
  122. $functionType2->method('getMission')->willReturn(FunctionEnum::PRESIDENT()->getValue());
  123. $this->functionTypeRepository
  124. ->expects($this->once())
  125. ->method('findBy')
  126. ->with(['roleByDefault' => RoleEnum::ROLE_ADMIN()->getValue()])
  127. ->willReturn([$functionType1, $functionType2]);
  128. // Organization's name
  129. $organization = $this->getMockBuilder(Organization::class)->getMock();
  130. $organization->method('getName')->willReturn("Etablissement d'Enseignement Artistique");
  131. // Postal address
  132. $organizationAddressPostal = $this->getMockBuilder(OrganizationAddressPostal::class)->getMock();
  133. $addressPostal = $this->getMockBuilder(AddressPostal::class)->getMock();
  134. $addressPostal->method('getStreetAddress')->willReturn('21b baker street');
  135. $addressPostal->method('getStreetAddressSecond')->willReturn('');
  136. $addressPostal->method('getStreetAddressThird')->willReturn('');
  137. $addressPostal->method('getAddressOwner')->willReturn('');
  138. $addressPostal->method('getPostalCode')->willReturn('250 329');
  139. $addressPostal->method('getAddressCity')->willReturn('Londres');
  140. $organizationAddressPostal->method('getType')->willReturn(AddressPostalOrganizationTypeEnum::ADDRESS_CONTACT()->getValue());
  141. $organizationAddressPostal->method('getAddressPostal')->willReturn($addressPostal);
  142. $organization->method('getOrganizationAddressPostals')->willReturn(
  143. new ArrayCollection([$organizationAddressPostal])
  144. );
  145. // Email and phone
  146. $contactPoint = $this->getMockBuilder(ContactPoint::class)->getMock();
  147. $contactPoint->method('getContactType')->willReturn(ContactPointTypeEnum::CONTACT()->getValue());
  148. $contactPoint->method('getEmail')->willReturn('email@email.com');
  149. $phoneNumber = new PhoneNumber();
  150. $phoneNumber->setCountryCode(33);
  151. $phoneNumber->setNationalNumber('1 02 03 04 05');
  152. $contactPoint->method('getTelphone')->willReturn($phoneNumber);
  153. $organization->method('getContactPoints')->willReturn(
  154. new ArrayCollection([$contactPoint])
  155. );
  156. // Network
  157. $network = $this->getMockBuilder(Network::class)->getMock();
  158. $network->method('getId')->willReturn(91295);
  159. $networkOrganization = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  160. $networkOrganization->method('getNetwork')->willReturn($network);
  161. $organization->method('getNetworkOrganizations')->willReturn(new ArrayCollection([$networkOrganization]));
  162. // Product
  163. $settings = $this->getMockBuilder(Settings::class)->getMock();
  164. $settings->method('getProduct')->willReturn(SettingsProductEnum::SCHOOL()->getValue());
  165. $organization->method('getSettings')->willReturn($settings);
  166. // Get dolibarr contacts
  167. $this->dolibarrApiService
  168. ->method('getOpentalentContacts')
  169. ->with(1726)
  170. ->willReturn(
  171. array_filter(
  172. $this->getJsonContentFromFixture('contacts.json'),
  173. function ($c) {
  174. return in_array(
  175. (int)$c["array_options"]["options_2iopen_person_id"],
  176. [108939, 156252, 302117]
  177. ); }
  178. )
  179. );
  180. $this->organizationRepository->method('find')->willReturn($organization);
  181. $access = $this->getMockBuilder(Access::class)->getMock();
  182. $person = $this->getMockBuilder(Person::class)->getMock();
  183. $person->method('getId')->willReturn(108939);
  184. $person->method('getName')->willReturn('Holmes');
  185. $person->method('getGender')->willReturn('MISTER');
  186. $person->method('getGivenName')->willReturn('Sherlock');
  187. $person->method('getGivenName')->willReturn('Sherlock');
  188. $personContactPoint = $this->getMockBuilder(ContactPoint::class)->getMock();
  189. $personContactPoint->method('getContactType')->willReturn(ContactPointTypeEnum::CONTACT()->getValue());
  190. $personContactPoint->method('getEmail')->willReturn('sherlock@holmes.com');
  191. $phoneNumber = new PhoneNumber();
  192. $phoneNumber->setCountryCode(33);
  193. $phoneNumber->setNationalNumber('2 98 76 54 32');
  194. $personContactPoint->method('getTelphone')->willReturn($phoneNumber);
  195. $personContactPoint->method('getMobilPhone')->willReturn(null);
  196. $person->method('getContactPoints')->willReturn(
  197. new ArrayCollection([$personContactPoint])
  198. );
  199. $access->method('getPerson')->willReturn($person);
  200. $this->accessRepository->method('find')->willReturn($access);
  201. $syncService = $this->newDolibarrSyncService();
  202. $operations = $syncService->scan();
  203. $this->assertCount(4, $operations);
  204. $this->assertEquals(
  205. [
  206. '[PUT thirdparties/1726]',
  207. 'address : `\n
  208. 217, rue Raoul Follereau\n
  209. ` => `21b baker street`',
  210. 'zip : `74300` => `250 329`',
  211. 'town : `CLUSES` => `Londres`',
  212. 'email : `` => `email@email.com`',
  213. 'phone : `+33678403010` => `+331 02 03 04 05`',
  214. 'parent : `` => `5086`',
  215. 'array_options.options_2iopeninfoopentalent : `` => ` : 3\n : 1\n : 1`'
  216. ],
  217. $operations[0]->getChangeLog()
  218. );
  219. $this->assertEquals(
  220. ['PUT contact/5868', ''],
  221. $operations[1]->getChangeLog()
  222. );
  223. $this->assertEquals(
  224. ['PUT thirdparty/5869', ''],
  225. $operations[2]->getChangeLog()
  226. );
  227. $this->assertEquals(
  228. ['PUT thirdparty/5871', ''],
  229. $operations[2]->getChangeLog()
  230. );
  231. }
  232. public function testGetDolibarrSocietiesIndex() {
  233. $this->dolibarrApiService
  234. ->expects($this->once())
  235. ->method('getAllClients')
  236. ->willReturn(
  237. $this->getJsonContentFromFixture('thirdparties.json')
  238. );
  239. $syncService = $this->newDolibarrSyncService();
  240. $index = $syncService->getDolibarrSocietiesIndex();
  241. $this->assertEquals("13930", $index[13930]['array_options']['options_2iopen_organization_id']);
  242. }
  243. public function testDolibarrContactsIndex() {
  244. $this->dolibarrApiService
  245. ->expects($this->once())
  246. ->method('getOpentalentContacts')
  247. ->with(9)
  248. ->willReturn(
  249. $this->getJsonContentFromFixture('contacts.json')
  250. );
  251. $syncService = $this->newDolibarrSyncService();
  252. $index = $syncService->getDolibarrContactsIndex(9);
  253. $this->assertEquals("302117", $index[302117]['array_options']['options_2iopen_person_id']);
  254. }
  255. public function testActiveMembersIndex() {
  256. $this->accessRepository
  257. ->expects($this->once())
  258. ->method('getAllActiveMembersAndMissions')
  259. ->willReturn(
  260. [
  261. ['id' => 123, 'organization_id' => 1, 'mission' => FunctionEnum::PRESIDENT()->getValue()],
  262. ['id' => 123, 'organization_id' => 1, 'mission' => FunctionEnum::TEACHER()->getValue()],
  263. ['id' => 124, 'organization_id' => 1, 'mission' => FunctionEnum::ADMINISTRATIVE_STAFF()->getValue()],
  264. ['id' => 125, 'organization_id' => 2, 'mission' => FunctionEnum::ADHERENT()->getValue()],
  265. ]
  266. );
  267. $syncService = $this->newDolibarrSyncService();
  268. $this->assertEquals(
  269. [
  270. 1 => [
  271. 123 => [FunctionEnum::PRESIDENT()->getValue(), FunctionEnum::TEACHER()->getValue()],
  272. 124 => [FunctionEnum::ADMINISTRATIVE_STAFF()->getValue()]
  273. ],
  274. 2 => [
  275. 125 => [FunctionEnum::ADHERENT()->getValue()]
  276. ]
  277. ],
  278. $syncService->getActiveMembersIndex()
  279. );
  280. }
  281. public function testSanitizeDolibarrData() {
  282. $this->assertEquals(null, TestableDolibarrSyncService::sanitizeDolibarrData(null));
  283. $this->assertEquals(
  284. ['a' => 'A', 'b' => null, 'c' => ['d' => 'D', 'e' => null]],
  285. TestableDolibarrSyncService::sanitizeDolibarrData(
  286. ['a' => 'A', 'b' => '', 'c' => ['d' => 'D', 'e' => '']]
  287. )
  288. );
  289. }
  290. public function testGetOrganizationPostalAddress() {
  291. $organization = $this->getMockBuilder(Organization::class)->getMock();
  292. $organizationAddressPostal1 = $this->getMockBuilder(OrganizationAddressPostal::class)->getMock();
  293. $organizationAddressPostal2 = $this->getMockBuilder(OrganizationAddressPostal::class)->getMock();
  294. $organizationAddressPostal3 = $this->getMockBuilder(OrganizationAddressPostal::class)->getMock();
  295. $addressPostal = $this->getMockBuilder(AddressPostal::class)->getMock();
  296. $organizationAddressPostal1->method('getType')->willReturn(AddressPostalOrganizationTypeEnum::ADDRESS_PRACTICE()->getValue());
  297. $organizationAddressPostal2->method('getType')->willReturn(AddressPostalOrganizationTypeEnum::ADDRESS_BILL()->getValue());
  298. $organizationAddressPostal3->method('getType')->willReturn(AddressPostalOrganizationTypeEnum::ADDRESS_OTHER()->getValue());
  299. $organizationAddressPostal2->method('getAddressPostal')->willReturn($addressPostal);
  300. $organization->expects($this->once())
  301. ->method('getOrganizationAddressPostals')
  302. ->willReturn(
  303. [$organizationAddressPostal1, $organizationAddressPostal2, $organizationAddressPostal3]
  304. );
  305. $syncService = $this->newDolibarrSyncService($organization);
  306. $this->assertEquals(
  307. $addressPostal,
  308. $syncService->getOrganizationPostalAddress($organization)
  309. );
  310. $organization2 = $this->getMockBuilder(Organization::class)->getMock();
  311. $organization->expects($this->once())
  312. ->method('getOrganizationAddressPostals')
  313. ->willReturn([]);
  314. $this->assertEquals(
  315. null,
  316. $syncService->getOrganizationPostalAddress($organization2)
  317. );
  318. }
  319. public function testGetOrganizationPhoneWithExistingPhone()
  320. {
  321. $organization = $this->getMockBuilder(Organization::class)->getMock();
  322. $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
  323. $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
  324. $contactPoint3 = $this->getMockBuilder(ContactPoint::class)->getMock();
  325. $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER()->getValue());
  326. $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::BILL()->getValue());
  327. $contactPoint3->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL()->getValue());
  328. $contactPoint2->expects($this->once())->method('getTelphone')->willReturn('0161626365');
  329. $organization
  330. ->expects($this->once())
  331. ->method('getContactPoints')
  332. ->willReturn(
  333. [$contactPoint1, $contactPoint2, $contactPoint3]
  334. );
  335. $syncService = $this->newDolibarrSyncService();
  336. $this->assertEquals(
  337. '+331 61 62 63 65',
  338. $syncService->getOrganizationPhone($organization)
  339. );
  340. }
  341. public function testGetOrganizationPhoneWithMobilePhone() {
  342. $organization = $this->getMockBuilder(Organization::class)->getMock();
  343. $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
  344. $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
  345. $contactPoint3 = $this->getMockBuilder(ContactPoint::class)->getMock();
  346. $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER()->getValue());
  347. $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::BILL()->getValue());
  348. $contactPoint3->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL()->getValue());
  349. $contactPoint2->expects($this->once())->method('getTelphone')->willReturn(null);
  350. $contactPoint2->expects($this->once())->method('getMobilPhone')->willReturn('0661626365');
  351. $organization
  352. ->expects($this->once())
  353. ->method('getContactPoints')
  354. ->willReturn(
  355. [$contactPoint1, $contactPoint2, $contactPoint3]
  356. );
  357. $syncService = $this->newDolibarrSyncService();
  358. $this->assertEquals(
  359. '+336 61 62 63 65',
  360. $syncService->getOrganizationPhone($organization)
  361. );
  362. }
  363. public function testGetOrganizationPhoneWithNoPhone() {
  364. $organization = $this->getMockBuilder(Organization::class)->getMock();
  365. $organization
  366. ->expects($this->once())
  367. ->method('getContactPoints')
  368. ->willReturn([]);
  369. $syncService = $this->newDolibarrSyncService();
  370. $this->assertEquals(
  371. null,
  372. $syncService->getOrganizationPhone($organization)
  373. );
  374. }
  375. public function testGetOrganizationEmailWithExistingEmail() {
  376. $organization = $this->getMockBuilder(Organization::class)->getMock();
  377. $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
  378. $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
  379. $contactPoint3 = $this->getMockBuilder(ContactPoint::class)->getMock();
  380. $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER()->getValue());
  381. $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::BILL()->getValue());
  382. $contactPoint3->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL()->getValue());
  383. $contactPoint2->method('getEmail')->willReturn('email@email.com');
  384. $organization
  385. ->expects($this->once())
  386. ->method('getContactPoints')
  387. ->willReturn(
  388. [$contactPoint1, $contactPoint2, $contactPoint3]
  389. );
  390. $syncService = $this->newDolibarrSyncService();
  391. $this->assertEquals(
  392. 'email@email.com',
  393. $syncService->getOrganizationEmail($organization)
  394. );
  395. }
  396. public function testGetOrganizationEmailWithNoEmail() {
  397. $organization = $this->getMockBuilder(Organization::class)->getMock();
  398. $organization
  399. ->expects($this->once())
  400. ->method('getContactPoints')
  401. ->willReturn([]);
  402. $syncService = $this->newDolibarrSyncService();
  403. $this->assertEquals(
  404. null,
  405. $syncService->getOrganizationEmail($organization)
  406. );
  407. }
  408. public function testCountWithMission() {
  409. $members = [
  410. 123 => [FunctionEnum::PRESIDENT()->getValue(), FunctionEnum::TEACHER()->getValue()],
  411. 124 => [FunctionEnum::TEACHER()->getValue()],
  412. 125 => [FunctionEnum::STUDENT()->getValue()],
  413. 126 => [FunctionEnum::TREASURER()->getValue()],
  414. ];
  415. $this->assertEquals(
  416. 2,
  417. TestableDolibarrSyncService::countWithMission([FunctionEnum::TEACHER()->getValue()], $members)
  418. );
  419. $this->assertEquals(
  420. 3,
  421. TestableDolibarrSyncService::countWithMission(
  422. [FunctionEnum::TEACHER()->getValue(), FunctionEnum::TREASURER()->getValue()],
  423. $members
  424. )
  425. );
  426. $this->assertEquals(
  427. 1,
  428. TestableDolibarrSyncService::countWithMission([FunctionEnum::STUDENT()->getValue()], $members)
  429. );
  430. $this->assertEquals(
  431. 0,
  432. TestableDolibarrSyncService::countWithMission([FunctionEnum::ARCHIVIST()->getValue()], $members)
  433. );
  434. }
  435. public function testGetPersonContact() {
  436. $person = $this->getMockBuilder(Person::class)->getMock();
  437. $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
  438. $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
  439. $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER()->getValue());
  440. $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL()->getValue());
  441. $person->expects($this->once())->method('getContactPoints')->willReturn([$contactPoint1, $contactPoint2]);
  442. $syncService = $this->newDolibarrSyncService();
  443. $this->assertEquals(
  444. $contactPoint2,
  445. $syncService->getPersonContact($person)
  446. );
  447. $person2 = $this->getMockBuilder(Person::class)->getMock();
  448. $person2->expects($this->once())->method('getContactPoints')->willReturn([]);
  449. $this->assertEquals(null, $syncService->getPersonContact($person2));
  450. }
  451. public function testFormatContactPosition() {
  452. $this->translator->method('trans')->will(
  453. $this->returnCallback(function($mission, $params) {
  454. if ($mission == FunctionEnum::PRESIDENT()) {
  455. if ($params === ['gender' => 'X']) { return 'Président(e)'; }
  456. elseif ($params === ['gender' => 'M']) { return 'Président'; }
  457. elseif ($params === ['gender' => 'F']) { return 'Présidente'; }
  458. } elseif ($mission == FunctionEnum::DIRECTOR()) {
  459. if ($params === ['gender' => 'X']) { return 'Directeur(ice)'; }
  460. elseif ($params === ['gender' => 'M']) { return 'Directeur'; }
  461. elseif ($params === ['gender' => 'F']) { return 'Directrice'; }
  462. }
  463. throw new \AssertionError('translator->trans stub has no matching call for arguments ' . json_encode([$mission, $params]));
  464. })
  465. );
  466. $syncService = $this->newDolibarrSyncService();
  467. $this->assertEquals(
  468. 'Président(e)',
  469. $syncService->formatContactPosition([FunctionEnum::PRESIDENT()->getValue()])
  470. );
  471. $this->assertEquals(
  472. 'Président',
  473. $syncService->formatContactPosition([FunctionEnum::PRESIDENT()->getValue()], 'MISTER')
  474. );
  475. $this->assertEquals(
  476. 'Présidente',
  477. $syncService->formatContactPosition([FunctionEnum::PRESIDENT()->getValue()], 'MISS')
  478. );
  479. $this->assertEquals(
  480. 'Présidente, Directrice',
  481. $syncService->formatContactPosition(
  482. [FunctionEnum::PRESIDENT()->getValue(), FunctionEnum::DIRECTOR()->getValue()],
  483. 'MISS'
  484. )
  485. );
  486. $this->assertEquals(
  487. 'Président, Directeur',
  488. $syncService->formatContactPosition(
  489. [FunctionEnum::PRESIDENT()->getValue(), FunctionEnum::DIRECTOR()->getValue(), FunctionEnum::ADHERENT()->getValue()],
  490. 'MISTER'
  491. )
  492. );
  493. }
  494. public function testFormatPhoneNumber() {
  495. $phoneNumber = new PhoneNumber();
  496. $phoneNumber->setCountryCode(33);
  497. $phoneNumber->setNationalNumber('1 02 03 04 05');
  498. $this->assertEquals(
  499. '+33 1 02 03 04 05',
  500. TestableDolibarrSyncService::formatPhoneNumber($phoneNumber)
  501. );
  502. }
  503. public function testFilterDiff() {
  504. $this->assertEquals(
  505. ['b' => -2, 'c' => ['e' => ['f' => -5]], 'g' => 7],
  506. TestableDolibarrSyncService::filterDiff(
  507. ['a' => 1, 'b' => 2, 'c' => ['d' => 4, 'e' => ['f' => 5]]],
  508. ['a' => 1, 'b' => -2, 'c' => ['d' => 4, 'e' => ['f' => -5]], 'g' => 7],
  509. )
  510. );
  511. $this->assertEquals(
  512. [],
  513. TestableDolibarrSyncService::filterDiff(
  514. ['a' => 1, 'b' => 2, 'c' => ['d' => 4, 'e' => ['f' => 5]]],
  515. ['a' => 1, 'b' => 2, 'c' => ['d' => 4, 'e' => ['f' => 5]]],
  516. )
  517. );
  518. $this->assertEquals(
  519. [],
  520. TestableDolibarrSyncService::filterDiff(
  521. [],
  522. [],
  523. )
  524. );
  525. $this->assertEquals(
  526. ['a' => 1],
  527. TestableDolibarrSyncService::filterDiff(
  528. [],
  529. ['a' => 1],
  530. )
  531. );
  532. }
  533. }