DolibarrSyncServiceTest.php 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454
  1. <?php
  2. /** @noinspection ALL */
  3. namespace App\Tests\Unit\Service\Dolibarr;
  4. use App\Entity\Access\Access;
  5. use App\Entity\Access\FunctionType;
  6. use App\Entity\Core\AddressPostal;
  7. use App\Entity\Core\ContactPoint;
  8. use App\Entity\Network\Network;
  9. use App\Entity\Network\NetworkOrganization;
  10. use App\Entity\Organization\Organization;
  11. use App\Entity\Organization\OrganizationAddressPostal;
  12. use App\Entity\Organization\Settings;
  13. use App\Entity\Person\Person;
  14. use App\Enum\Access\FunctionEnum;
  15. use App\Enum\Access\RoleEnum;
  16. use App\Enum\Core\ContactPointTypeEnum;
  17. use App\Enum\Network\NetworkEnum;
  18. use App\Enum\Organization\AddressPostalOrganizationTypeEnum;
  19. use App\Enum\Organization\LegalEnum;
  20. use App\Enum\Organization\SettingsProductEnum;
  21. use App\Enum\Person\GenderEnum;
  22. use App\Repository\Access\AccessRepository;
  23. use App\Repository\Access\FunctionTypeRepository;
  24. use App\Repository\Organization\OrganizationRepository;
  25. use App\Service\Core\AddressPostalUtils;
  26. use App\Service\Dolibarr\DolibarrApiService;
  27. use App\Service\Dolibarr\DolibarrSyncService;
  28. use App\Service\Organization\Utils;
  29. use App\Service\Rest\Operation\BaseRestOperation;
  30. use App\Service\Rest\Operation\CreateOperation;
  31. use App\Service\Rest\Operation\DeleteOperation;
  32. use App\Service\Rest\Operation\UpdateOperation;
  33. use App\Service\Utils\ArrayUtils;
  34. use App\Service\Utils\DatesUtils;
  35. use Doctrine\Common\Collections\ArrayCollection;
  36. use libphonenumber\PhoneNumber;
  37. use libphonenumber\PhoneNumberUtil;
  38. use PHPUnit\Framework\MockObject\MockObject;
  39. use PHPUnit\Framework\TestCase;
  40. use Psr\Log\LoggerInterface;
  41. use Symfony\Contracts\HttpClient\ResponseInterface;
  42. use Symfony\Contracts\Translation\TranslatorInterface;
  43. class TestableDolibarrSyncService extends DolibarrSyncService
  44. {
  45. public function getDolibarrSocietiesIndex(): array
  46. {
  47. return parent::getDolibarrSocietiesIndex();
  48. }
  49. public function findDolibarrContactFor(array $dolibarrContacts, Person $person): ?array
  50. {
  51. return parent::findDolibarrContactFor($dolibarrContacts, $person);
  52. }
  53. public function getActiveMembersIndex(): array
  54. {
  55. return parent::getActiveMembersIndex();
  56. }
  57. public function sanitizeDolibarrData(?array $data): ?array
  58. {
  59. return parent::sanitizeDolibarrData($data);
  60. }
  61. public function getOrganizationPostalAddress(Organization $organization): ?AddressPostal
  62. {
  63. return parent::getOrganizationPostalAddress($organization);
  64. }
  65. public function getOrganizationBillingPostalAddress(Organization $organization): ?AddressPostal
  66. {
  67. return parent::getOrganizationBillingPostalAddress($organization);
  68. }
  69. public function getOrganizationPhone(Organization $organization): ?string
  70. {
  71. return parent::getOrganizationPhone($organization);
  72. }
  73. public function getOrganizationEmail(Organization $organization): ?string
  74. {
  75. return parent::getOrganizationEmail($organization);
  76. }
  77. public function getOrganizationNetworkId(Organization $organization): ?int
  78. {
  79. return parent::getOrganizationNetworkId($organization);
  80. }
  81. public function countWithMission(array $missions, array $members): int
  82. {
  83. return parent::countWithMission($missions, $members);
  84. }
  85. public function getPersonContact(Person $person): ?ContactPoint
  86. {
  87. return parent::getPersonContact($person);
  88. }
  89. public function formatContactPosition(array $missions, ?string $gender = 'X'): string
  90. {
  91. return parent::formatContactPosition($missions, $gender);
  92. }
  93. public function formatPhoneNumber(PhoneNumber $phoneNumber): string
  94. {
  95. return parent::formatPhoneNumber($phoneNumber);
  96. }
  97. public function getExpectedTagsFor(Organization $organization): array
  98. {
  99. return parent::getExpectedTagsFor($organization);
  100. }
  101. public function validateResponse(ResponseInterface $response, BaseRestOperation $operation): void
  102. {
  103. parent::validateResponse($response, $operation);
  104. }
  105. }
  106. class DolibarrSyncServiceTest extends TestCase
  107. {
  108. private MockObject|OrganizationRepository $organizationRepository;
  109. private MockObject|AccessRepository $accessRepository;
  110. private MockObject|FunctionTypeRepository $functionTypeRepository;
  111. private MockObject|DolibarrApiService $dolibarrApiService;
  112. private MockObject|AddressPostalUtils $addressPostalUtils;
  113. private MockObject|ArrayUtils $arrayUtils;
  114. private MockObject|TranslatorInterface $translator;
  115. private MockObject|LoggerInterface $logger;
  116. private MockObject|Utils $organizationUtils;
  117. public function setUp(): void
  118. {
  119. $this->organizationRepository = $this->getMockBuilder(OrganizationRepository::class)
  120. ->disableOriginalConstructor()
  121. ->getMock();
  122. $this->accessRepository = $this->getMockBuilder(AccessRepository::class)
  123. ->disableOriginalConstructor()
  124. ->getMock();
  125. $this->functionTypeRepository = $this->getMockBuilder(FunctionTypeRepository::class)
  126. ->disableOriginalConstructor()
  127. ->getMock();
  128. $this->dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  129. ->disableOriginalConstructor()
  130. ->getMock();
  131. $this->addressPostalUtils = $this->getMockBuilder(AddressPostalUtils::class)
  132. ->disableOriginalConstructor()
  133. ->getMock();
  134. $this->arrayUtils = $this->getMockBuilder(ArrayUtils::class)
  135. ->disableOriginalConstructor()
  136. ->getMock();
  137. $this->translator = $this->getMockBuilder(TranslatorInterface::class)
  138. ->disableOriginalConstructor()
  139. ->getMock();
  140. $this->logger = $this->getMockBuilder(LoggerInterface::class)
  141. ->disableOriginalConstructor()
  142. ->getMock();
  143. $this->organizationUtils = $this->getMockBuilder(Utils::class)
  144. ->disableOriginalConstructor()
  145. ->getMock();
  146. $this->logger->method('info')->willReturnSelf();
  147. $this->logger->method('debug')->willReturnSelf();
  148. $this->logger->method('warning')->willReturnSelf();
  149. $this->logger->method('error')->willReturnSelf();
  150. }
  151. public function tearDown(): void
  152. {
  153. DatesUtils::clearFakeDatetime();
  154. }
  155. private function getMockForMethod(string $method)
  156. {
  157. $dolibarrSyncService = $this->getMockBuilder(TestableDolibarrSyncService::class)
  158. ->setConstructorArgs([$this->organizationRepository, $this->accessRepository, $this->functionTypeRepository,
  159. $this->dolibarrApiService, $this->addressPostalUtils, $this->arrayUtils, $this->translator, $this->organizationUtils])
  160. ->setMethodsExcept([$method, 'setLoggerInterface'])
  161. ->getMock();
  162. $dolibarrSyncService->setLoggerInterface($this->logger);
  163. return $dolibarrSyncService;
  164. }
  165. /**
  166. * Full test of the scan method.
  167. *
  168. * @see DolibarrSyncService::scan()
  169. */
  170. public function testScan(): void
  171. {
  172. $dolibarrSyncService = $this->getMockForMethod('scan');
  173. // ----- Opentalent Organizations -----
  174. $orgId1 = 10;
  175. $organization1 = $this->getMockBuilder(Organization::class)->getMock();
  176. $organization1->method('getId')->willReturn($orgId1);
  177. $organization1->method('getName')->willReturn('Organization 10');
  178. $organizationData1 = [
  179. 'fullAddress' => '1 Rue Azerty',
  180. 'fullBillingAddress' => '2 Rue Ascii',
  181. 'addressOwner' => 'Mr Keyboard',
  182. 'postalCode' => '01110',
  183. 'city' => 'ByteCity',
  184. 'email' => 'foo@bar.net',
  185. 'phone' => '0102030405',
  186. 'networkId' => NetworkEnum::CMF->value,
  187. 'product' => SettingsProductEnum::SCHOOL,
  188. 'networkId' => NetworkEnum::CMF->value,
  189. 'legalStatus' => LegalEnum::LOCAL_AUTHORITY,
  190. ];
  191. $orgId2 = 20;
  192. $organization2 = $this->getMockBuilder(Organization::class)->getMock();
  193. $organization2->method('getId')->willReturn($orgId2);
  194. $organization2->method('getName')->willReturn('Organization 20');
  195. $organizationData2 = [
  196. 'email' => null,
  197. 'phone' => null,
  198. 'networkId' => null,
  199. 'product' => SettingsProductEnum::ARTIST,
  200. 'legalStatus' => LegalEnum::ASSOCIATION_LAW_1901,
  201. ];
  202. $orgId3 = 30;
  203. $organization3 = null; // This organization does not exist
  204. // Persons
  205. $accessId1 = 11; // Person 1
  206. $accessId2 = 21; // Person 2
  207. $accessId3 = 12; // Shall be ignored, because not an office member
  208. // access 4 does not exist
  209. $accessId5 = 13; // Invalid Person
  210. $personId1 = 100;
  211. $personData1 = [
  212. 'name' => 'Dupont',
  213. 'givenName' => 'Hercules',
  214. 'gender' => GenderEnum::MISTER,
  215. 'email' => 'an@email.net',
  216. 'phone' => '0102030405',
  217. 'mobilePhone' => '0607080910',
  218. ];
  219. $personId2 = 200;
  220. $personData2 = [
  221. 'name' => 'Simpson',
  222. 'givenName' => 'Lisa',
  223. 'gender' => null,
  224. ];
  225. $personId3 = 300; // Obsolete contact, does not exist anymore in the Opentalent DB
  226. $personId4 = 400; // Obsolete contact, does not exist anymore in the Opentalent DB
  227. $personId5 = 900; // Invalid contact with no firstname and no lastname
  228. $personData5 = [
  229. 'name' => '',
  230. 'givenName' => '',
  231. 'gender' => null,
  232. ];
  233. $activeMembers1 = [
  234. $accessId1 => [FunctionEnum::PRESIDENT->value],
  235. $accessId3 => [FunctionEnum::STUDENT->value],
  236. $accessId5 => [FunctionEnum::TREASURER->value],
  237. ];
  238. $activeMembers2 = [
  239. $accessId2 => [FunctionEnum::PRESIDENT->value],
  240. ];
  241. // ----- Opentalent : other vars -----
  242. $cmfId = 12097;
  243. $cmfDolibarrId = 12098;
  244. $ffecId = 91295;
  245. $ffecDolibarrId = 91296;
  246. // ----- Dolibarr societies -----
  247. // Existing society about to be updated
  248. $socId1 = 1;
  249. $dolibarrSociety1 = [
  250. 'id' => $socId1,
  251. 'name' => 'Organization 10',
  252. 'address' => '1 Rue Qwerty',
  253. 'zip' => '01024',
  254. 'town' => 'Ram',
  255. 'email' => 'some@email.com',
  256. 'phone' => null,
  257. 'status' => 0,
  258. 'parent' => '0',
  259. 'array_options' => [
  260. 'options_2iopeninfoopentalent' => '',
  261. 'options_2iopen_software_opentalent' => 'Opentalent Artist',
  262. ],
  263. ];
  264. // Existing society with no data
  265. $socId2 = 2;
  266. $dolibarrSociety2 = [
  267. 'id' => $socId2,
  268. 'array_options' => [],
  269. ];
  270. // This organization does not exist in the opentalent DB
  271. $socId3 = 3;
  272. $dolibarrSociety3 = null;
  273. // Dolibarr contacts
  274. $contactData1 = [
  275. 'id' => '1',
  276. 'civility_code' => '',
  277. 'lastname' => 'Dupond',
  278. 'firstname' => 'Bob',
  279. 'email' => 'abcd@mail.com',
  280. 'phone_pro' => '+33478570000',
  281. 'phone_mobile' => '+33682980000',
  282. 'poste' => 'Secrétaire',
  283. 'statut' => '1',
  284. 'array_options' => [
  285. 'options_2iopen_person_id' => '',
  286. ],
  287. ];
  288. // An obsolete contact that should be disabled
  289. $obsoleteContactData = [
  290. 'id' => '4',
  291. 'lastname' => 'Doe',
  292. 'firstname' => 'John',
  293. 'statut' => '1',
  294. 'array_options' => [
  295. 'options_2iopen_person_id' => $personId3,
  296. ],
  297. ];
  298. // An obsolete contact that should is already disabled
  299. $obsoleteContactData2 = [
  300. 'id' => '5',
  301. 'lastname' => 'Foo',
  302. 'firstname' => 'John',
  303. 'statut' => '0',
  304. 'array_options' => [
  305. 'options_2iopen_person_id' => $personId4,
  306. ],
  307. ];
  308. $billingContactData = [
  309. 'id' => '5',
  310. 'lastname' => 'Service facturation',
  311. 'firstname' => null,
  312. 'address' => '',
  313. 'zip' => '',
  314. 'town' => '',
  315. 'statut' => '1',
  316. 'array_options' => [
  317. 'options_2iopen_person_id' => null,
  318. ],
  319. ];
  320. $dolibarrSocietyContacts1 = [$contactData1, $obsoleteContactData, $obsoleteContactData2, $billingContactData];
  321. $dolibarrSocietyContacts2 = [];
  322. // ----- Setup Mocks -----
  323. $this->translator->method('trans')->willReturnMap([
  324. ['STUDENTS_COUNT', [], null, null, "Nombre d'élèves"],
  325. ['ADHERENTS_COUNT', [], null, null, "Nombre d'adhérents"],
  326. ['ADMIN_ACCESS_COUNT', [], null, null, "Nombre d'accès admin"],
  327. [SettingsProductEnum::SCHOOL->value, [], null, null, 'Opentalent School'],
  328. [SettingsProductEnum::ARTIST->value, [], null, null, 'Opentalent Artist'],
  329. [GenderEnum::MISTER->value, [], null, null, 'MR'],
  330. ]);
  331. // Get societies
  332. $this->organizationRepository->method('find')
  333. ->willReturnMap([
  334. [$orgId1, null, null, $organization1],
  335. [$orgId2, null, null, $organization2],
  336. [$orgId3, null, null, $organization3],
  337. ]);
  338. $dolibarrSyncService
  339. ->expects(self::once())
  340. ->method('getDolibarrSocietiesIndex')
  341. ->willReturn([
  342. $orgId1 => $dolibarrSociety1,
  343. $orgId2 => $dolibarrSociety2,
  344. $orgId3 => $dolibarrSociety3,
  345. ]);
  346. $dolibarrSyncService
  347. ->expects(self::once())
  348. ->method('getActiveMembersIndex')
  349. ->willReturn([
  350. $orgId1 => $activeMembers1,
  351. $orgId2 => $activeMembers2,
  352. $orgId3 => [],
  353. ]);
  354. // Function types
  355. $functionType1 = $this->getMockBuilder(FunctionType::class)->getMock();
  356. $functionType1->method('getMission')->willReturn(FunctionEnum::DIRECTOR);
  357. $functionType2 = $this->getMockBuilder(FunctionType::class)->getMock();
  358. $functionType2->method('getMission')->willReturn(FunctionEnum::PRESIDENT);
  359. $this->functionTypeRepository
  360. ->expects($this->once())
  361. ->method('findBy')
  362. ->with(['roleByDefault' => RoleEnum::ROLE_ADMIN])
  363. ->willReturn([$functionType1, $functionType2]);
  364. // Get CMF and FFEC ids
  365. $this->dolibarrApiService->method('getSociety')->willReturnMap(
  366. [
  367. [$cmfId, ['id' => $cmfDolibarrId]],
  368. [$ffecId, ['id' => $ffecDolibarrId]],
  369. ]
  370. );
  371. // -- Loop over organizations --
  372. $dolibarrSyncService
  373. ->expects(self::exactly(5)) // 3 organizations and 2 contacts
  374. ->method('sanitizeDolibarrData')
  375. ->willReturnCallback(function ($args) { return $args; });
  376. $mainPostalAddress = $this->getMockBuilder(AddressPostal::class)->getMock();
  377. $mainPostalAddress->method('getStreetAddress')->willReturn($organizationData1['fullAddress']);
  378. $mainPostalAddress->method('getAddressOwner')->willReturn($organizationData1['addressOwner']);
  379. $mainPostalAddress->method('getPostalCode')->willReturn($organizationData1['postalCode']);
  380. $mainPostalAddress->method('getAddressCity')->willReturn($organizationData1['city']);
  381. $billingPostalAddress = $this->getMockBuilder(AddressPostal::class)->getMock();
  382. $billingPostalAddress->method('getStreetAddress')->willReturn($organizationData1['fullBillingAddress']);
  383. $billingPostalAddress->method('getAddressOwner')->willReturn($organizationData1['addressOwner']);
  384. $billingPostalAddress->method('getPostalCode')->willReturn($organizationData1['postalCode']);
  385. $billingPostalAddress->method('getAddressCity')->willReturn($organizationData1['city']);
  386. $dolibarrSyncService
  387. ->expects(self::exactly(2))
  388. ->method('getOrganizationPostalAddress')
  389. ->willReturnMap([
  390. [$organization1, $mainPostalAddress],
  391. [$organization2, null],
  392. ]);
  393. $dolibarrSyncService
  394. ->expects(self::exactly(1))
  395. ->method('getOrganizationBillingPostalAddress')
  396. ->willReturnMap([
  397. [$organization1, $billingPostalAddress],
  398. [$organization2, null],
  399. ]);
  400. $this->addressPostalUtils
  401. ->method('getFullStreetAddress')
  402. ->willReturnCallback(function ($a) { return $a->getStreetAddress(); });
  403. $dolibarrSyncService
  404. ->expects(self::exactly(2))
  405. ->method('getOrganizationEmail')
  406. ->willReturnMap([
  407. [$organization1, $organizationData1['email']],
  408. [$organization2, $organizationData2['email']],
  409. ]);
  410. $dolibarrSyncService
  411. ->expects(self::exactly(2))
  412. ->method('getOrganizationPhone')
  413. ->willReturnMap([
  414. [$organization1, $organizationData1['phone']],
  415. [$organization2, $organizationData2['phone']],
  416. ]);
  417. $dolibarrSyncService
  418. ->expects(self::exactly(2))
  419. ->method('getOrganizationNetworkId')
  420. ->willReturnMap([
  421. [$organization1, $organizationData1['networkId']],
  422. [$organization2, $organizationData2['networkId']],
  423. ]);
  424. $settings1 = $this->getMockBuilder(Settings::class)->getMock();
  425. $settings1->method('getProduct')->willReturn($organizationData1['product']);
  426. $organization1->method('getSettings')->willReturn($settings1);
  427. $settings2 = $this->getMockBuilder(Settings::class)->getMock();
  428. $settings2->method('getProduct')->willReturn($organizationData2['product']);
  429. $organization2->method('getSettings')->willReturn($settings2);
  430. $this->organizationUtils->method('isSchool')->willReturnMap([
  431. [$organization1, true],
  432. [$organization2, false],
  433. ]);
  434. $this->organizationUtils->method('isArtist')->willReturnMap([
  435. [$organization1, false],
  436. [$organization2, true],
  437. ]);
  438. $dolibarrSyncService->method('countWithMission')->willReturnMap([
  439. [[FunctionEnum::STUDENT->value], $activeMembers1, 1],
  440. [[FunctionEnum::ADHERENT->value], $activeMembers1, 2],
  441. [[FunctionEnum::ADHERENT->value], $activeMembers2, 0],
  442. [[FunctionEnum::DIRECTOR->value, FunctionEnum::PRESIDENT->value], $activeMembers1, 1],
  443. [[FunctionEnum::DIRECTOR->value, FunctionEnum::PRESIDENT->value], $activeMembers2, 2],
  444. ]);
  445. $this->arrayUtils
  446. ->expects(self::exactly(4))
  447. ->method('getChanges')
  448. ->willReturnCallback(
  449. function (array $initialArray, array $newArray, bool $recursive = false, ?callable $callback = null) {
  450. if (in_array('name', $newArray, true)) {
  451. // Organization 1 name is already defined and has not been changed
  452. unset($newArray['name']);
  453. }
  454. if (in_array('statut', $newArray, true)) {
  455. // Contact 1 statut is already defined and has not been changed
  456. unset($newArray['statut']);
  457. }
  458. return $newArray;
  459. }
  460. );
  461. $this->dolibarrApiService->method('getContacts')->willReturnMap([
  462. [$socId1, $dolibarrSocietyContacts1],
  463. [$socId2, $dolibarrSocietyContacts2],
  464. ]);
  465. // Loop over contacts
  466. // NB: Student will be skipped since it has no office role
  467. $person1 = $this->getMockBuilder(Person::class)->getMock();
  468. $person1->method('getId')->willReturn($personId1);
  469. $person1->method('getName')->willReturn($personData1['name']);
  470. $person1->method('getGivenName')->willReturn($personData1['givenName']);
  471. $person1->method('getGender')->willReturn($personData1['gender']);
  472. $person2 = $this->getMockBuilder(Person::class)->getMock();
  473. $person2->method('getId')->willReturn($personId2);
  474. $person2->method('getName')->willReturn($personData2['name']);
  475. $person2->method('getGivenName')->willReturn($personData2['givenName']);
  476. $person2->method('getGender')->willReturn($personData2['gender']);
  477. // An invalid person that should be ignored
  478. $person5 = $this->getMockBuilder(Person::class)->getMock();
  479. $person5->method('getId')->willReturn($personId5);
  480. $person5->method('getName')->willReturn($personData5['name']);
  481. $person5->method('getGivenName')->willReturn($personData5['givenName']);
  482. $access1 = $this->getMockBuilder(Access::class)->getMock();
  483. $access1->method('getPerson')->willReturn($person1);
  484. $access2 = $this->getMockBuilder(Access::class)->getMock();
  485. $access2->method('getPerson')->willReturn($person2);
  486. $access5 = $this->getMockBuilder(Access::class)->getMock();
  487. $access5->method('getPerson')->willReturn($person5);
  488. $this->accessRepository->method('find')->willReturnMap([
  489. [$accessId1, null, null, $access1],
  490. [$accessId2, null, null, $access2],
  491. [$accessId5, null, null, $access5],
  492. ]);
  493. $dolibarrSyncService->method('findDolibarrContactFor')->willReturnMap([
  494. [$dolibarrSocietyContacts1, $person1, $contactData1],
  495. [$dolibarrSocietyContacts2, $person2, null],
  496. ]);
  497. $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
  498. $contactPoint1->method('getEmail')->willReturn($personData1['email']);
  499. $phone = $this->getMockBuilder(PhoneNumber::class)->getMock();
  500. $mobilePhone = $this->getMockBuilder(PhoneNumber::class)->getMock();
  501. $dolibarrSyncService->method('formatPhoneNumber')->willReturnMap([
  502. [$phone, $personData1['phone']],
  503. [$mobilePhone, $personData1['mobilePhone']],
  504. ]);
  505. $contactPoint1->method('getTelphone')->willReturn($phone);
  506. $contactPoint1->method('getMobilPhone')->willReturn($mobilePhone);
  507. $dolibarrSyncService->method('getPersonContact')->willReturnMap([
  508. [$person1, $contactPoint1],
  509. [$person2, null],
  510. ]);
  511. $dolibarrSyncService->method('formatContactPosition')->willReturnMap([
  512. [[FunctionEnum::PRESIDENT->value], GenderEnum::MISTER->value, 'Président'],
  513. [[FunctionEnum::PRESIDENT->value], null, 'Président(e)'],
  514. ]);
  515. // Expected logged error messages
  516. $this->logger->expects(self::exactly(2))->method('error')->withConsecutive(
  517. ['Person 900 miss a lastname and/or a firstname, ignored.'],
  518. ['Organization 30 not found in the Opentalent DB'],
  519. );
  520. // Tags
  521. $this->dolibarrApiService->method('getSocietyTagsIds')->willReturnMap([
  522. [$socId1, [1, 68]],
  523. [$socId2, [3, 67]],
  524. ]);
  525. $dolibarrSyncService->method('getExpectedTagsFor')->willReturnMap([
  526. [$organization1, [67]],
  527. [$organization2, []],
  528. ]);
  529. // Expected progression callback triggers
  530. $progressionCallbackExpectedCalls = [[1, 3], [2, 3], [3, 3]];
  531. $progressionCallback = static function ($i, $total) use (&$progressionCallbackExpectedCalls) {
  532. [$expectedI, $expectedTotal] = array_shift($progressionCallbackExpectedCalls);
  533. if ($i !== $expectedI || $total !== $expectedTotal) {
  534. throw new \AssertionError('Progression callback error, expected parameters are ('.$expectedI.','.$expectedTotal.'), got ('.$i.', '.$total.')');
  535. }
  536. };
  537. DatesUtils::setFakeDatetime('2024-01-01T00:00:00+00:00');
  538. $operations = $dolibarrSyncService->scan($progressionCallback);
  539. $this->assertCount(9, $operations);
  540. $this->assertEqualsCanonicalizing(
  541. [
  542. '[PUT thirdparties/1]',
  543. "address : `1 Rue Qwerty` => `Mr Keyboard\n1 Rue Azerty`",
  544. 'zip : `01024` => `01110`',
  545. 'town : `Ram` => `ByteCity`',
  546. 'email : `some@email.com` => `foo@bar.net`',
  547. 'phone : `` => `0102030405`',
  548. 'parent : `0` => `12098`',
  549. 'array_options.options_2iopen_software_opentalent : `Opentalent Artist` => `Opentalent School`',
  550. "array_options.options_2iopeninfoopentalent : `` => `Nombre d'élèves : 1\nNombre d'adhérents : 2\nNombre d'accès admin : 1`",
  551. 'array_options.options_2iopen_last_sync_date : (new) => `2024-01-01T00:00:00+00:00`',
  552. 'status : `0` => `1`',
  553. ],
  554. $operations[0]->getChangeLog()
  555. );
  556. $this->assertEqualsCanonicalizing(
  557. [
  558. '[PUT contacts/1]',
  559. 'civility_code : `` => `MR`',
  560. 'lastname : `Dupond` => `Dupont`',
  561. 'firstname : `Bob` => `Hercules`',
  562. 'email : `abcd@mail.com` => `an@email.net`',
  563. 'phone_pro : `+33478570000` => `0102030405`',
  564. 'phone_mobile : `+33682980000` => `0607080910`',
  565. 'poste : `Secrétaire` => `Président`',
  566. 'array_options.options_2iopen_person_id : `` => `100`',
  567. 'array_options.options_2iopen_last_sync_date : (new) => `2024-01-01T00:00:00+00:00`',
  568. ],
  569. $operations[1]->getChangeLog()
  570. );
  571. $this->assertEqualsCanonicalizing(
  572. [
  573. '[PUT contacts/4]',
  574. 'statut : `1` => `0`',
  575. 'array_options.options_2iopen_last_sync_date : (new) => `2024-01-01T00:00:00+00:00`',
  576. ],
  577. $operations[2]->getChangeLog()
  578. );
  579. $this->assertEqualsCanonicalizing(
  580. [
  581. '[PUT contacts/5]',
  582. 'address : `` => `2 Rue Ascii`',
  583. 'town : `` => `ByteCity`',
  584. 'zip : `` => `01110`',
  585. ],
  586. $operations[3]->getChangeLog()
  587. );
  588. $this->assertEqualsCanonicalizing(
  589. [
  590. '[DELETE /thirdparties/1/categories/68]',
  591. ],
  592. $operations[4]->getChangeLog()
  593. );
  594. $this->assertEqualsCanonicalizing(
  595. [
  596. '[POST /thirdparties/1/categories/67]',
  597. ],
  598. $operations[5]->getChangeLog()
  599. );
  600. $this->assertEqualsCanonicalizing(
  601. [
  602. '[PUT thirdparties/2]',
  603. 'address : (new) => ``',
  604. 'email : (new) => ``',
  605. 'name : (new) => `Organization 20`',
  606. 'parent : (new) => ``',
  607. 'phone : (new) => ``',
  608. 'status : (new) => `1`',
  609. 'town : (new) => ``',
  610. 'zip : (new) => ``',
  611. 'array_options.options_2iopen_software_opentalent : (new) => `Opentalent Artist`',
  612. "array_options.options_2iopeninfoopentalent : (new) => `Nombre d'adhérents : 0\nNombre d'accès admin : 2`",
  613. 'array_options.options_2iopen_last_sync_date : (new) => `2024-01-01T00:00:00+00:00`',
  614. ],
  615. $operations[6]->getChangeLog()
  616. );
  617. $this->assertEqualsCanonicalizing(
  618. [
  619. '[POST contacts]',
  620. 'civility_code : (new) => ``',
  621. 'lastname : (new) => `Simpson`',
  622. 'firstname : (new) => `Lisa`',
  623. 'email : (new) => ``',
  624. 'phone_pro : (new) => ``',
  625. 'phone_mobile : (new) => ``',
  626. 'poste : (new) => `Président(e)`',
  627. 'statut : (new) => `1`',
  628. 'array_options.options_2iopen_person_id : (new) => `200`',
  629. 'array_options.options_2iopen_last_sync_date : (new) => `2024-01-01T00:00:00+00:00`',
  630. 'socid : (new) => `2`',
  631. ],
  632. $operations[7]->getChangeLog()
  633. );
  634. $this->assertEqualsCanonicalizing(
  635. [
  636. '[DELETE /thirdparties/2/categories/67]',
  637. ],
  638. $operations[8]->getChangeLog()
  639. );
  640. $this->assertCount(0, $progressionCallbackExpectedCalls);
  641. }
  642. /**
  643. * All valid operations shall be executed
  644. * If an operation is not in status READY, a warning shall be logged, and the operation shall be skipped.
  645. *
  646. * @see DolibarrSyncService::execute()
  647. */
  648. public function testExecute(): void
  649. {
  650. $dolibarrSyncService = $this->getMockForMethod('execute');
  651. $progressionCallbackExpectedCalls = [[1, 3], [2, 3], [3, 3]];
  652. $progressionCallback = static function ($i, $total) use (&$progressionCallbackExpectedCalls) {
  653. [$expectedI, $expectedTotal] = array_shift($progressionCallbackExpectedCalls);
  654. if ($i !== $expectedI || $total !== $expectedTotal) {
  655. throw new \AssertionError('Progression callback error, expected parameters are ('.$expectedI.','.$expectedTotal.'), got ('.$i.', '.$total.')');
  656. }
  657. };
  658. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  659. $operation1 = $this->getMockBuilder(UpdateOperation::class)->disableOriginalConstructor()->getMock();
  660. $operation1->method('getStatus')->willReturnOnConsecutiveCalls(BaseRestOperation::STATUS_READY, BaseRestOperation::STATUS_DONE);
  661. $operation1->method('getChangeLog')->willReturn(['foo']);
  662. $operation1->expects(self::once())->method('execute')->willReturn($response);
  663. $operation2 = $this->getMockBuilder(CreateOperation::class)->disableOriginalConstructor()->getMock();
  664. $operation2->method('getStatus')->willReturn(
  665. BaseRestOperation::STATUS_READY, BaseRestOperation::STATUS_ERROR, BaseRestOperation::STATUS_ERROR // An error happened
  666. );
  667. $operation2->expects(self::once())->method('execute');
  668. $operation3 = $this->getMockBuilder(DeleteOperation::class)->disableOriginalConstructor()->getMock();
  669. $operation3->method('getStatus')->willReturn(BaseRestOperation::STATUS_DONE); // Invalid status, should log a warning and not execute
  670. $operation3->expects(self::never())->method('execute');
  671. $this->logger->expects(self::exactly(2))->method('warning'); // 2 warnings because of the bad status of the Create Op
  672. $this->logger->expects(self::exactly(3))->method('error'); // The exception thrown during the execution of the Delete op will log 3 errors
  673. $dolibarrSyncService->execute([$operation1, $operation2, $operation3], $progressionCallback);
  674. }
  675. /**
  676. * @see DolibarrSyncService::run()
  677. */
  678. public function testRun()
  679. {
  680. $dolibarrSyncService = $this->getMockForMethod('run');
  681. $operations = ['operation1', 'operation2'];
  682. $dolibarrSyncService->expects(self::once())->method('scan')->willReturn($operations);
  683. $dolibarrSyncService->expects(self::once())->method('execute')->with($operations);
  684. $result = $dolibarrSyncService->run();
  685. $this->assertEquals($operations, $result);
  686. }
  687. /**
  688. * @see DolibarrSyncService::getDolibarrSocietiesIndex()
  689. */
  690. public function testGetDolibarrSocietiesIndex(): void
  691. {
  692. $dolibarrSyncService = $this->getMockForMethod('getDolibarrSocietiesIndex');
  693. $this->dolibarrApiService
  694. ->expects($this->once())
  695. ->method('getAllClients')
  696. ->willReturn(
  697. [
  698. ['id' => 1, 'array_options' => ['options_2iopen_organization_id' => 101]],
  699. ['id' => 2, 'array_options' => ['options_2iopen_organization_id' => 102]],
  700. ['id' => 3, 'array_options' => ['options_2iopen_organization_id' => null]], // No org id but also no contract, so it's ok
  701. ['id' => 4, 'name' => 'foo', 'array_options' => ['options_2iopen_organization_id' => null]], // No org id but has a contract, a warning should be logged
  702. ]
  703. );
  704. $this->dolibarrApiService->expects(self::exactly(2))->method('getActiveContract')->willReturnMap([
  705. [3, null],
  706. [4, ['dummy non-empty data']],
  707. ]);
  708. $this->logger->expects(self::once())->method('warning')->with('Dolibarr client has no organization id: foo (4)');
  709. $index = $dolibarrSyncService->getDolibarrSocietiesIndex();
  710. $this->assertEqualsCanonicalizing(
  711. [
  712. 1 => ['id' => 1, 'array_options' => ['options_2iopen_organization_id' => 101]],
  713. 2 => ['id' => 2, 'array_options' => ['options_2iopen_organization_id' => 102]],
  714. ],
  715. $index
  716. );
  717. }
  718. /**
  719. * @see DolibarrSyncService::getActiveMembersIndex()
  720. */
  721. public function testGetActiveMembersIndex(): void
  722. {
  723. $dolibarrSyncService = $this->getMockForMethod('getActiveMembersIndex');
  724. $this->accessRepository
  725. ->expects($this->once())
  726. ->method('getAllActiveMembersAndMissions')
  727. ->willReturn(
  728. [
  729. ['id' => 1, 'organization_id' => 1, 'mission' => FunctionEnum::PRESIDENT],
  730. ['id' => 2, 'organization_id' => 1, 'mission' => FunctionEnum::STUDENT],
  731. ['id' => 3, 'organization_id' => 2, 'mission' => FunctionEnum::PRESIDENT],
  732. ['id' => 3, 'organization_id' => 2, 'mission' => FunctionEnum::TEACHER],
  733. ]
  734. );
  735. $index = $dolibarrSyncService->getActiveMembersIndex();
  736. $this->assertEqualsCanonicalizing([
  737. 1 => [1 => [FunctionEnum::PRESIDENT->value], 2 => [FunctionEnum::STUDENT->value]],
  738. 2 => [3 => [FunctionEnum::PRESIDENT->value, FunctionEnum::TEACHER->value]],
  739. ], $index);
  740. }
  741. /**
  742. * @see DolibarrSyncService::findDolibarrContactFor()
  743. */
  744. public function testFindDolibarrContactForById(): void
  745. {
  746. $dolibarrSyncService = $this->getMockForMethod('findDolibarrContactFor');
  747. $contacts = [
  748. ['id' => 1, 'array_options' => ['options_2iopen_person_id' => 101]],
  749. ['id' => 2, 'array_options' => ['options_2iopen_person_id' => 102]],
  750. ];
  751. // Find by id
  752. $person = $this->getMockBuilder(Person::class)->getMock();
  753. $person->method('getId')->willReturn(102);
  754. $contact = $dolibarrSyncService->findDolibarrContactFor($contacts, $person);
  755. $this->assertEquals(2, $contact['id']);
  756. }
  757. /**
  758. * @see DolibarrSyncService::findDolibarrContactFor()
  759. */
  760. public function testFindDolibarrContactForByName(): void
  761. {
  762. $dolibarrSyncService = $this->getMockForMethod('findDolibarrContactFor');
  763. $contacts = [
  764. ['id' => 1, 'firstname' => 'mister', 'lastname' => 'X', 'array_options' => ['options_2iopen_person_id' => null]],
  765. ['id' => 2, 'firstname' => 'miss', 'lastname' => 'Y', 'array_options' => ['options_2iopen_person_id' => null]],
  766. ];
  767. // Find by full name (contact has no person id, it should be returned)
  768. $person = $this->getMockBuilder(Person::class)->getMock();
  769. $person->method('getId')->willReturn(101);
  770. $person->method('getName')->willReturn('X');
  771. $person->method('getGivenName')->willReturn('mister');
  772. $result = $dolibarrSyncService->findDolibarrContactFor($contacts, $person);
  773. $this->assertEquals(1, $result['id']);
  774. }
  775. /**
  776. * @see DolibarrSyncService::findDolibarrContactFor()
  777. */
  778. public function testFindDolibarrContactForByNameWithConflict(): void
  779. {
  780. $dolibarrSyncService = $this->getMockForMethod('findDolibarrContactFor');
  781. $contacts = [
  782. ['id' => 1, 'firstname' => 'mister', 'lastname' => 'X', 'array_options' => ['options_2iopen_person_id' => 1]],
  783. ['id' => 2, 'firstname' => 'miss', 'lastname' => 'Y', 'array_options' => ['options_2iopen_person_id' => 2]],
  784. ];
  785. // Find by full name (contact already has another person id, it should not be returned)
  786. $person = $this->getMockBuilder(Person::class)->getMock();
  787. $person->method('getId')->willReturn(101);
  788. $person->method('getName')->willReturn('X');
  789. $person->method('getGivenName')->willReturn('mister');
  790. $result = $dolibarrSyncService->findDolibarrContactFor($contacts, $person);
  791. $this->assertEquals(null, $result);
  792. }
  793. /**
  794. * @see DolibarrSyncService::findDolibarrContactFor()
  795. */
  796. public function testFindDolibarrContactNotFound(): void
  797. {
  798. $dolibarrSyncService = $this->getMockForMethod('findDolibarrContactFor');
  799. $contacts = [
  800. ['id' => 1, 'firstname' => 'mister', 'lastname' => 'X', 'array_options' => ['options_2iopen_person_id' => 1]],
  801. ['id' => 2, 'firstname' => 'miss', 'lastname' => 'Y', 'array_options' => ['options_2iopen_person_id' => 2]],
  802. ];
  803. // Do not find
  804. $person = $this->getMockBuilder(Person::class)->getMock();
  805. $person->method('getId')->willReturn(-1);
  806. $person->method('getName')->willReturn('Presley');
  807. $person->method('getGivenName')->willReturn('Elvis');
  808. $result = $dolibarrSyncService->findDolibarrContactFor($contacts, $person);
  809. $this->assertEquals(null, $result);
  810. }
  811. /**
  812. * @see DolibarrSyncService::sanitizeDolibarrData()
  813. */
  814. public function testSanitizeDolibarrData(): void
  815. {
  816. $dolibarrSyncService = $this->getMockForMethod('sanitizeDolibarrData');
  817. $result = $dolibarrSyncService->sanitizeDolibarrData(['a' => 'A', 'b' => '', 'c' => ['d' => 'D', 'e' => '']]);
  818. $this->assertEquals(
  819. ['a' => 'A', 'b' => null, 'c' => ['d' => 'D', 'e' => null]],
  820. $result
  821. );
  822. }
  823. /**
  824. * @see DolibarrSyncService::sanitizeDolibarrData()
  825. */
  826. public function testSanitizeDolibarrDataWithNull(): void
  827. {
  828. $dolibarrSyncService = $this->getMockForMethod('sanitizeDolibarrData');
  829. $result = $dolibarrSyncService->sanitizeDolibarrData(null);
  830. $this->assertEquals(null, $result);
  831. }
  832. /**
  833. * @see DolibarrSyncService::getOrganizationPostalAddress()
  834. */
  835. public function testGetOrganizationPostalAddress(): void
  836. {
  837. $dolibarrSyncService = $this->getMockForMethod('getOrganizationPostalAddress');
  838. $organization = $this->getMockBuilder(Organization::class)->getMock();
  839. $organizationAddressPostal1 = $this->getMockBuilder(OrganizationAddressPostal::class)->getMock();
  840. $organizationAddressPostal2 = $this->getMockBuilder(OrganizationAddressPostal::class)->getMock();
  841. $organizationAddressPostal3 = $this->getMockBuilder(OrganizationAddressPostal::class)->getMock();
  842. $addressPostal = $this->getMockBuilder(AddressPostal::class)->getMock();
  843. $organizationAddressPostal1->method('getType')->willReturn(AddressPostalOrganizationTypeEnum::ADDRESS_PRACTICE);
  844. $organizationAddressPostal2->method('getType')->willReturn(AddressPostalOrganizationTypeEnum::ADDRESS_BILL);
  845. $organizationAddressPostal3->method('getType')->willReturn(AddressPostalOrganizationTypeEnum::ADDRESS_OTHER);
  846. $organizationAddressPostal2->method('getAddressPostal')->willReturn($addressPostal);
  847. $organization->expects($this->once())
  848. ->method('getOrganizationAddressPostals')
  849. ->willReturn(
  850. new ArrayCollection([$organizationAddressPostal1, $organizationAddressPostal2, $organizationAddressPostal3])
  851. );
  852. $this->assertEquals(
  853. $addressPostal,
  854. $dolibarrSyncService->getOrganizationPostalAddress($organization)
  855. );
  856. }
  857. /**
  858. * @see DolibarrSyncService::getOrganizationPostalAddress()
  859. */
  860. public function testGetOrganizationPostalAddressNoResult(): void
  861. {
  862. $dolibarrSyncService = $this->getMockForMethod('getOrganizationPostalAddress');
  863. $organization = $this->getMockBuilder(Organization::class)->getMock();
  864. $organization->expects($this->once())
  865. ->method('getOrganizationAddressPostals')
  866. ->willReturn(new ArrayCollection([]));
  867. $this->assertEquals(
  868. null,
  869. $dolibarrSyncService->getOrganizationPostalAddress($organization)
  870. );
  871. }
  872. /**
  873. * @see DolibarrSyncService::getOrganizationPhone()
  874. */
  875. public function testGetOrganizationPhoneWithExistingPhone(): void
  876. {
  877. $dolibarrSyncService = $this->getMockForMethod('getOrganizationPhone');
  878. $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
  879. $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
  880. $contactPoint3 = $this->getMockBuilder(ContactPoint::class)->getMock();
  881. $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER);
  882. $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::BILL);
  883. $contactPoint3->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL);
  884. $phone = $this->getMockBuilder(PhoneNumber::class)->disableOriginalConstructor()->getMock();
  885. $contactPoint2->method('getTelphone')->willReturn($phone);
  886. $organization = $this->getMockBuilder(Organization::class)->getMock();
  887. $organization
  888. ->expects($this->once())
  889. ->method('getContactPoints')
  890. ->willReturn(
  891. new ArrayCollection([$contactPoint1, $contactPoint2, $contactPoint3])
  892. );
  893. $dolibarrSyncService->expects(self::once())->method('formatPhoneNumber')->with($phone)->willReturn('+33161626365');
  894. $this->assertEquals(
  895. '+33161626365',
  896. $dolibarrSyncService->getOrganizationPhone($organization)
  897. );
  898. }
  899. /**
  900. * @see DolibarrSyncService::getOrganizationPhone()
  901. */
  902. public function testGetOrganizationPhoneWithMobilePhone()
  903. {
  904. $dolibarrSyncService = $this->getMockForMethod('getOrganizationPhone');
  905. $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
  906. $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
  907. $contactPoint3 = $this->getMockBuilder(ContactPoint::class)->getMock();
  908. $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER);
  909. $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::BILL);
  910. $contactPoint3->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL);
  911. $contactPoint2->expects($this->once())->method('getTelphone')->willReturn(null);
  912. $mobilePhone = $this->getMockBuilder(PhoneNumber::class)->disableOriginalConstructor()->getMock();
  913. $contactPoint2->method('getMobilPhone')->willReturn($mobilePhone);
  914. $organization = $this->getMockBuilder(Organization::class)->getMock();
  915. $organization
  916. ->expects($this->once())
  917. ->method('getContactPoints')
  918. ->willReturn(
  919. new ArrayCollection([$contactPoint1, $contactPoint2, $contactPoint3])
  920. );
  921. $dolibarrSyncService->expects(self::once())->method('formatPhoneNumber')->with($mobilePhone)->willReturn('+33661626365');
  922. $this->assertEquals(
  923. '+33661626365',
  924. $dolibarrSyncService->getOrganizationPhone($organization)
  925. );
  926. }
  927. /**
  928. * @see DolibarrSyncService::getOrganizationPhone()
  929. */
  930. public function testGetOrganizationPhoneWithNoPhone()
  931. {
  932. $dolibarrSyncService = $this->getMockForMethod('getOrganizationPhone');
  933. $organization = $this->getMockBuilder(Organization::class)->getMock();
  934. $organization
  935. ->expects($this->once())
  936. ->method('getContactPoints')
  937. ->willReturn(new ArrayCollection([]));
  938. $dolibarrSyncService->expects(self::never())->method('formatPhoneNumber');
  939. $this->assertEquals(
  940. null,
  941. $dolibarrSyncService->getOrganizationPhone($organization)
  942. );
  943. }
  944. /**
  945. * @see DolibarrSyncService::getOrganizationEmail()
  946. */
  947. public function testGetOrganizationEmailWithExistingEmail()
  948. {
  949. $dolibarrSyncService = $this->getMockForMethod('getOrganizationEmail');
  950. $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
  951. $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
  952. $contactPoint3 = $this->getMockBuilder(ContactPoint::class)->getMock();
  953. $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER);
  954. $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::BILL);
  955. $contactPoint3->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL);
  956. $contactPoint2->method('getEmail')->willReturn('email@email.com');
  957. $organization = $this->getMockBuilder(Organization::class)->getMock();
  958. $organization
  959. ->expects($this->once())
  960. ->method('getContactPoints')
  961. ->willReturn(
  962. new ArrayCollection([$contactPoint1, $contactPoint2, $contactPoint3])
  963. );
  964. $this->assertEquals(
  965. 'email@email.com',
  966. $dolibarrSyncService->getOrganizationEmail($organization)
  967. );
  968. }
  969. /**
  970. * @see DolibarrSyncService::getOrganizationEmail()
  971. */
  972. public function testGetOrganizationEmailWithNoEmail()
  973. {
  974. $dolibarrSyncService = $this->getMockForMethod('getOrganizationEmail');
  975. $organization = $this->getMockBuilder(Organization::class)->getMock();
  976. $organization
  977. ->expects($this->once())
  978. ->method('getContactPoints')
  979. ->willReturn(new ArrayCollection([]));
  980. $this->assertEquals(
  981. null,
  982. $dolibarrSyncService->getOrganizationEmail($organization)
  983. );
  984. }
  985. /**
  986. * @see DolibarrSyncService::getOrganizationNetworkId()
  987. */
  988. public function testGetOrganizationNetworkId()
  989. {
  990. $dolibarrSyncService = $this->getMockForMethod('getOrganizationNetworkId');
  991. $organization = $this->getMockBuilder(Organization::class)->getMock();
  992. $network = $this->getMockBuilder(Network::class)->getMock();
  993. $network->method('getId')->willReturn(3);
  994. $networkOrganization = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  995. $networkOrganization->method('getNetwork')->willReturn($network);
  996. $organization->method('getNetworkOrganizations')->willReturn(new ArrayCollection([$networkOrganization]));
  997. $this->assertEquals(
  998. 3,
  999. $dolibarrSyncService->getOrganizationNetworkId($organization)
  1000. );
  1001. }
  1002. /**
  1003. * @see DolibarrSyncService::getOrganizationNetworkId()
  1004. */
  1005. public function testGetOrganizationNetworkIdWithMultipleResult()
  1006. {
  1007. $dolibarrSyncService = $this->getMockForMethod('getOrganizationNetworkId');
  1008. $network1 = $this->getMockBuilder(Network::class)->getMock();
  1009. $network1->method('getId')->willReturn(3);
  1010. $networkOrganization1 = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  1011. $networkOrganization1->method('getNetwork')->willReturn($network1);
  1012. $networkOrganization1->method('getEndDate')->willReturn(new \DateTime('2000-01-01'));
  1013. $network2 = $this->getMockBuilder(Network::class)->getMock();
  1014. $network2->method('getId')->willReturn(4);
  1015. $networkOrganization2 = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  1016. $networkOrganization2->method('getNetwork')->willReturn($network2);
  1017. $networkOrganization2->method('getEndDate')->willReturn(null);
  1018. $organization = $this->getMockBuilder(Organization::class)->getMock();
  1019. $organization->method('getNetworkOrganizations')->willReturn(
  1020. new ArrayCollection([$networkOrganization1, $networkOrganization2])
  1021. );
  1022. $this->assertEquals(
  1023. 4,
  1024. $dolibarrSyncService->getOrganizationNetworkId($organization)
  1025. );
  1026. }
  1027. /**
  1028. * @see DolibarrSyncService::getOrganizationNetworkId()
  1029. */
  1030. public function testGetOrganizationNetworkIdWithNoResult()
  1031. {
  1032. $dolibarrSyncService = $this->getMockForMethod('getOrganizationNetworkId');
  1033. $organization = $this->getMockBuilder(Organization::class)->getMock();
  1034. $network = $this->getMockBuilder(Network::class)->getMock();
  1035. $network->method('getId')->willReturn(3);
  1036. $networkOrganization = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  1037. $networkOrganization->method('getNetwork')->willReturn($network);
  1038. $networkOrganization->method('getEndDate')->willReturn(new \DateTime('2000-01-01'));
  1039. $organization->method('getNetworkOrganizations')->willReturn(new ArrayCollection([$networkOrganization]));
  1040. $this->assertEquals(
  1041. null,
  1042. $dolibarrSyncService->getOrganizationNetworkId($organization)
  1043. );
  1044. }
  1045. /**
  1046. * @see DolibarrSyncService::countWithMission()
  1047. */
  1048. public function testCountWithMission()
  1049. {
  1050. $dolibarrSyncService = $this->getMockForMethod('countWithMission');
  1051. $members = [
  1052. 123 => [FunctionEnum::PRESIDENT->value, FunctionEnum::TEACHER->value],
  1053. 124 => [FunctionEnum::TEACHER->value],
  1054. 125 => [FunctionEnum::STUDENT->value],
  1055. 126 => [FunctionEnum::TREASURER->value],
  1056. ];
  1057. $this->assertEquals(
  1058. 2,
  1059. $dolibarrSyncService->countWithMission([FunctionEnum::TEACHER->value], $members)
  1060. );
  1061. $this->assertEquals(
  1062. 3,
  1063. $dolibarrSyncService->countWithMission(
  1064. [FunctionEnum::TEACHER->value, FunctionEnum::TREASURER->value],
  1065. $members
  1066. )
  1067. );
  1068. $this->assertEquals(
  1069. 1,
  1070. $dolibarrSyncService->countWithMission([FunctionEnum::STUDENT->value], $members)
  1071. );
  1072. $this->assertEquals(
  1073. 0,
  1074. $dolibarrSyncService->countWithMission([FunctionEnum::ARCHIVIST->value], $members)
  1075. );
  1076. }
  1077. /**
  1078. * @see DolibarrSyncService::getPersonContact()
  1079. */
  1080. public function testGetPersonContact()
  1081. {
  1082. $dolibarrSyncService = $this->getMockForMethod('getPersonContact');
  1083. $person = $this->getMockBuilder(Person::class)->getMock();
  1084. $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
  1085. $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
  1086. $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER);
  1087. $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL);
  1088. $person->expects($this->once())->method('getContactPoints')->willReturn(new ArrayCollection([$contactPoint1, $contactPoint2]));
  1089. $this->assertEquals(
  1090. $contactPoint2,
  1091. $dolibarrSyncService->getPersonContact($person)
  1092. );
  1093. $person2 = $this->getMockBuilder(Person::class)->getMock();
  1094. $person2->expects($this->once())->method('getContactPoints')->willReturn(new ArrayCollection([]));
  1095. $this->assertEquals(
  1096. null,
  1097. $dolibarrSyncService->getPersonContact($person2)
  1098. );
  1099. }
  1100. /**
  1101. * @see DolibarrSyncService::formatContactPosition()
  1102. */
  1103. public function testFormatContactPosition()
  1104. {
  1105. $dolibarrSyncService = $this->getMockForMethod('formatContactPosition');
  1106. $this->translator->method('trans')->willReturnMap(
  1107. [
  1108. [FunctionEnum::PRESIDENT->value, ['gender' => 'X'], null, null, 'Président(e)'],
  1109. [FunctionEnum::PRESIDENT->value, ['gender' => 'M'], null, null, 'Président'],
  1110. [FunctionEnum::PRESIDENT->value, ['gender' => 'F'], null, null, 'Présidente'],
  1111. [FunctionEnum::DIRECTOR->value, ['gender' => 'X'], null, null, 'Directeur(ice)'],
  1112. [FunctionEnum::DIRECTOR->value, ['gender' => 'M'], null, null, 'Directeur'],
  1113. [FunctionEnum::DIRECTOR->value, ['gender' => 'F'], null, null, 'Directrice'],
  1114. [FunctionEnum::TEACHER->value, ['gender' => 'X'], null, null, 'Professeur(e)'],
  1115. [FunctionEnum::ARCHIVIST->value, ['gender' => 'X'], null, null, 'Archiviste'],
  1116. [FunctionEnum::TREASURER->value, ['gender' => 'X'], null, null, 'Trésorier(ère)'],
  1117. [FunctionEnum::ADMINISTRATIVE_STAFF->value, ['gender' => 'X'], null, null, 'Personnel administratif'],
  1118. ]
  1119. );
  1120. $this->assertEquals(
  1121. 'Président(e)',
  1122. $dolibarrSyncService->formatContactPosition([FunctionEnum::PRESIDENT->value])
  1123. );
  1124. $this->assertEquals(
  1125. 'Président',
  1126. $dolibarrSyncService->formatContactPosition([FunctionEnum::PRESIDENT->value], 'MISTER')
  1127. );
  1128. $this->assertEquals(
  1129. 'Présidente',
  1130. $dolibarrSyncService->formatContactPosition([FunctionEnum::PRESIDENT->value], 'MISS')
  1131. );
  1132. $this->assertEquals(
  1133. 'Présidente, Directrice',
  1134. $dolibarrSyncService->formatContactPosition(
  1135. [FunctionEnum::PRESIDENT->value, FunctionEnum::DIRECTOR->value],
  1136. 'MISS'
  1137. )
  1138. );
  1139. $this->assertEquals(
  1140. 'Président, Directeur',
  1141. $dolibarrSyncService->formatContactPosition(
  1142. [FunctionEnum::PRESIDENT->value, FunctionEnum::DIRECTOR->value, FunctionEnum::ADHERENT->value],
  1143. 'MISTER'
  1144. )
  1145. );
  1146. $this->assertEquals(
  1147. 'Président, Directeur',
  1148. $dolibarrSyncService->formatContactPosition(
  1149. [FunctionEnum::PRESIDENT->value, FunctionEnum::DIRECTOR->value, FunctionEnum::ADHERENT->value],
  1150. 'MISTER'
  1151. )
  1152. );
  1153. $this->assertEquals(
  1154. 'Président(e), Directeur(ice), Professeur(e), Archiviste, Trésorier(ère), Pers...',
  1155. $dolibarrSyncService->formatContactPosition(
  1156. [
  1157. FunctionEnum::PRESIDENT->value,
  1158. FunctionEnum::DIRECTOR->value,
  1159. FunctionEnum::TEACHER->value,
  1160. FunctionEnum::ARCHIVIST->value,
  1161. FunctionEnum::TREASURER->value,
  1162. FunctionEnum::ADMINISTRATIVE_STAFF->value,
  1163. ],
  1164. 'X'
  1165. )
  1166. );
  1167. }
  1168. /**
  1169. * @see DolibarrSyncService::formatPhoneNumber()
  1170. */
  1171. public function testFormatPhoneNumber()
  1172. {
  1173. $dolibarrSyncService = $this->getMockForMethod('formatPhoneNumber');
  1174. $phoneUtil = PhoneNumberUtil::getInstance();
  1175. $phoneNumber = $phoneUtil->parse('01 02 03 04 05', 'FR');
  1176. $this->assertEquals(
  1177. '+33102030405',
  1178. $dolibarrSyncService->formatPhoneNumber($phoneNumber)
  1179. );
  1180. }
  1181. public function testGetExpectedTagsFor()
  1182. {
  1183. $dolibarrSyncService = $this->getMockForMethod('getExpectedTagsFor');
  1184. $organization1 = $this->getMockBuilder(Organization::class)->getMock();
  1185. $organization1->method('getLegalStatus')->willReturn(LegalEnum::ASSOCIATION_LAW_1901);
  1186. $settings1 = $this->getMockBuilder(Settings::class)->getMock();
  1187. $settings1->method('getProduct')->willReturn(SettingsProductEnum::SCHOOL);
  1188. $organization1->method('getSettings')->willReturn($settings1);
  1189. $organization2 = $this->getMockBuilder(Organization::class)->getMock();
  1190. $organization2->method('getLegalStatus')->willReturn(LegalEnum::ASSOCIATION_LAW_1901);
  1191. $settings2 = $this->getMockBuilder(Settings::class)->getMock();
  1192. $settings2->method('getProduct')->willReturn(SettingsProductEnum::SCHOOL_PREMIUM);
  1193. $organization2->method('getSettings')->willReturn($settings2);
  1194. $organization3 = $this->getMockBuilder(Organization::class)->getMock();
  1195. $organization3->method('getLegalStatus')->willReturn(LegalEnum::LOCAL_AUTHORITY);
  1196. $settings3 = $this->getMockBuilder(Settings::class)->getMock();
  1197. $settings3->method('getProduct')->willReturn(SettingsProductEnum::SCHOOL);
  1198. $organization3->method('getSettings')->willReturn($settings3);
  1199. $organization4 = $this->getMockBuilder(Organization::class)->getMock();
  1200. $organization4->method('getLegalStatus')->willReturn(LegalEnum::LOCAL_AUTHORITY);
  1201. $settings4 = $this->getMockBuilder(Settings::class)->getMock();
  1202. $settings4->method('getProduct')->willReturn(SettingsProductEnum::SCHOOL_PREMIUM);
  1203. $organization4->method('getSettings')->willReturn($settings4);
  1204. $organization5 = $this->getMockBuilder(Organization::class)->getMock();
  1205. $organization5->method('getLegalStatus')->willReturn(LegalEnum::LOCAL_AUTHORITY);
  1206. $settings5 = $this->getMockBuilder(Settings::class)->getMock();
  1207. $settings5->method('getProduct')->willReturn(SettingsProductEnum::ARTIST);
  1208. $organization5->method('getSettings')->willReturn($settings5);
  1209. $this->assertEquals(
  1210. $dolibarrSyncService->getExpectedTagsFor($organization1),
  1211. [67]
  1212. );
  1213. $this->assertEquals(
  1214. $dolibarrSyncService->getExpectedTagsFor($organization2),
  1215. [69]
  1216. );
  1217. $this->assertEquals(
  1218. $dolibarrSyncService->getExpectedTagsFor($organization3),
  1219. [68]
  1220. );
  1221. $this->assertEquals(
  1222. $dolibarrSyncService->getExpectedTagsFor($organization4),
  1223. [70]
  1224. );
  1225. $this->assertEquals(
  1226. $dolibarrSyncService->getExpectedTagsFor($organization5),
  1227. []
  1228. );
  1229. }
  1230. }