DolibarrSyncServiceTest.php 35 KB

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