DolibarrSyncServiceTest.php 58 KB

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