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