DolibarrSyncServiceTest.php 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442
  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 Doctrine\Common\Collections\ArrayCollection;
  35. use libphonenumber\PhoneNumber;
  36. use libphonenumber\PhoneNumberUtil;
  37. use PHPUnit\Framework\MockObject\MockObject;
  38. use PHPUnit\Framework\TestCase;
  39. use Psr\Log\LoggerInterface;
  40. use Symfony\Component\HttpClient\Exception\ServerException;
  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. private function getMockForMethod(string $method)
  152. {
  153. $dolibarrSyncService = $this->getMockBuilder(TestableDolibarrSyncService::class)
  154. ->setConstructorArgs([$this->organizationRepository, $this->accessRepository, $this->functionTypeRepository,
  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. $this->functionTypeRepository
  356. ->expects($this->once())
  357. ->method('findBy')
  358. ->with(['roleByDefault' => RoleEnum::ROLE_ADMIN])
  359. ->willReturn([$functionType1, $functionType2]);
  360. // Get CMF and FFEC ids
  361. $this->dolibarrApiService->method('getSociety')->willReturnMap(
  362. [
  363. [$cmfId, ['id' => $cmfDolibarrId]],
  364. [$ffecId, ['id' => $ffecDolibarrId]],
  365. ]
  366. );
  367. // -- Loop over organizations --
  368. $dolibarrSyncService
  369. ->expects(self::exactly(5)) // 3 organizations and 2 contacts
  370. ->method('sanitizeDolibarrData')
  371. ->willReturnCallback(function ($args) { return $args; });
  372. $mainPostalAddress = $this->getMockBuilder(AddressPostal::class)->getMock();
  373. $mainPostalAddress->method('getStreetAddress')->willReturn($organizationData1['fullAddress']);
  374. $mainPostalAddress->method('getAddressOwner')->willReturn($organizationData1['addressOwner']);
  375. $mainPostalAddress->method('getPostalCode')->willReturn($organizationData1['postalCode']);
  376. $mainPostalAddress->method('getAddressCity')->willReturn($organizationData1['city']);
  377. $billingPostalAddress = $this->getMockBuilder(AddressPostal::class)->getMock();
  378. $billingPostalAddress->method('getStreetAddress')->willReturn($organizationData1['fullBillingAddress']);
  379. $billingPostalAddress->method('getAddressOwner')->willReturn($organizationData1['addressOwner']);
  380. $billingPostalAddress->method('getPostalCode')->willReturn($organizationData1['postalCode']);
  381. $billingPostalAddress->method('getAddressCity')->willReturn($organizationData1['city']);
  382. $dolibarrSyncService
  383. ->expects(self::exactly(2))
  384. ->method('getOrganizationPostalAddress')
  385. ->willReturnMap([
  386. [$organization1, $mainPostalAddress],
  387. [$organization2, null],
  388. ]);
  389. $dolibarrSyncService
  390. ->expects(self::exactly(1))
  391. ->method('getOrganizationBillingPostalAddress')
  392. ->willReturnMap([
  393. [$organization1, $billingPostalAddress],
  394. [$organization2, null],
  395. ]);
  396. $this->addressPostalUtils
  397. ->method('getFullStreetAddress')
  398. ->willReturnCallback(function ($a) { return $a->getStreetAddress(); });
  399. $dolibarrSyncService
  400. ->expects(self::exactly(2))
  401. ->method('getOrganizationEmail')
  402. ->willReturnMap([
  403. [$organization1, $organizationData1['email']],
  404. [$organization2, $organizationData2['email']],
  405. ]);
  406. $dolibarrSyncService
  407. ->expects(self::exactly(2))
  408. ->method('getOrganizationPhone')
  409. ->willReturnMap([
  410. [$organization1, $organizationData1['phone']],
  411. [$organization2, $organizationData2['phone']],
  412. ]);
  413. $dolibarrSyncService
  414. ->expects(self::exactly(2))
  415. ->method('getOrganizationNetworkId')
  416. ->willReturnMap([
  417. [$organization1, $organizationData1['networkId']],
  418. [$organization2, $organizationData2['networkId']],
  419. ]);
  420. $settings1 = $this->getMockBuilder(Settings::class)->getMock();
  421. $settings1->method('getProduct')->willReturn($organizationData1['product']);
  422. $organization1->method('getSettings')->willReturn($settings1);
  423. $settings2 = $this->getMockBuilder(Settings::class)->getMock();
  424. $settings2->method('getProduct')->willReturn($organizationData2['product']);
  425. $organization2->method('getSettings')->willReturn($settings2);
  426. $this->organizationUtils->method('isSchool')->willReturnMap([
  427. [$organization1, true],
  428. [$organization2, false],
  429. ]);
  430. $this->organizationUtils->method('isArtist')->willReturnMap([
  431. [$organization1, false],
  432. [$organization2, true],
  433. ]);
  434. $dolibarrSyncService->method('countWithMission')->willReturnMap([
  435. [[FunctionEnum::STUDENT->value], $activeMembers1, 1],
  436. [[FunctionEnum::ADHERENT->value], $activeMembers1, 2],
  437. [[FunctionEnum::ADHERENT->value], $activeMembers2, 0],
  438. [[FunctionEnum::DIRECTOR->value, FunctionEnum::PRESIDENT->value], $activeMembers1, 1],
  439. [[FunctionEnum::DIRECTOR->value, FunctionEnum::PRESIDENT->value], $activeMembers2, 2],
  440. ]);
  441. $this->arrayUtils
  442. ->expects(self::exactly(4))
  443. ->method('getChanges')
  444. ->willReturnCallback(
  445. function (array $initialArray, array $newArray, bool $recursive = false, ?callable $callback = null) {
  446. if (in_array('name', $newArray, true)) {
  447. // Organization 1 name is already defined and has not been changed
  448. unset($newArray['name']);
  449. }
  450. if (in_array('statut', $newArray, true)) {
  451. // Contact 1 statut is already defined and has not been changed
  452. unset($newArray['statut']);
  453. }
  454. return $newArray;
  455. }
  456. );
  457. $this->dolibarrApiService->method('getContacts')->willReturnMap([
  458. [$socId1, $dolibarrSocietyContacts1],
  459. [$socId2, $dolibarrSocietyContacts2],
  460. ]);
  461. // Loop over contacts
  462. // NB: Student will be skipped since it has no office role
  463. $person1 = $this->getMockBuilder(Person::class)->getMock();
  464. $person1->method('getId')->willReturn($personId1);
  465. $person1->method('getName')->willReturn($personData1['name']);
  466. $person1->method('getGivenName')->willReturn($personData1['givenName']);
  467. $person1->method('getGender')->willReturn($personData1['gender']);
  468. $person2 = $this->getMockBuilder(Person::class)->getMock();
  469. $person2->method('getId')->willReturn($personId2);
  470. $person2->method('getName')->willReturn($personData2['name']);
  471. $person2->method('getGivenName')->willReturn($personData2['givenName']);
  472. $person2->method('getGender')->willReturn($personData2['gender']);
  473. // An invalid person that should be ignored
  474. $person5 = $this->getMockBuilder(Person::class)->getMock();
  475. $person5->method('getId')->willReturn($personId5);
  476. $person5->method('getName')->willReturn($personData5['name']);
  477. $person5->method('getGivenName')->willReturn($personData5['givenName']);
  478. $access1 = $this->getMockBuilder(Access::class)->getMock();
  479. $access1->method('getPerson')->willReturn($person1);
  480. $access2 = $this->getMockBuilder(Access::class)->getMock();
  481. $access2->method('getPerson')->willReturn($person2);
  482. $access5 = $this->getMockBuilder(Access::class)->getMock();
  483. $access5->method('getPerson')->willReturn($person5);
  484. $this->accessRepository->method('find')->willReturnMap([
  485. [$accessId1, null, null, $access1],
  486. [$accessId2, null, null, $access2],
  487. [$accessId5, null, null, $access5],
  488. ]);
  489. $dolibarrSyncService->method('findDolibarrContactFor')->willReturnMap([
  490. [$dolibarrSocietyContacts1, $person1, $contactData1],
  491. [$dolibarrSocietyContacts2, $person2, null],
  492. ]);
  493. $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
  494. $contactPoint1->method('getEmail')->willReturn($personData1['email']);
  495. $phone = $this->getMockBuilder(PhoneNumber::class)->getMock();
  496. $mobilePhone = $this->getMockBuilder(PhoneNumber::class)->getMock();
  497. $dolibarrSyncService->method('formatPhoneNumber')->willReturnMap([
  498. [$phone, $personData1['phone']],
  499. [$mobilePhone, $personData1['mobilePhone']],
  500. ]);
  501. $contactPoint1->method('getTelphone')->willReturn($phone);
  502. $contactPoint1->method('getMobilPhone')->willReturn($mobilePhone);
  503. $dolibarrSyncService->method('getPersonContact')->willReturnMap([
  504. [$person1, $contactPoint1],
  505. [$person2, null],
  506. ]);
  507. $dolibarrSyncService->method('formatContactPosition')->willReturnMap([
  508. [[FunctionEnum::PRESIDENT->value], GenderEnum::MISTER->value, 'Président'],
  509. [[FunctionEnum::PRESIDENT->value], null, 'Président(e)'],
  510. ]);
  511. // Expected logged error messages
  512. $this->logger->expects(self::exactly(2))->method('error')->withConsecutive(
  513. ['Person 900 miss a lastname and/or a firstname, ignored.'],
  514. ['Organization 30 not found in the Opentalent DB'],
  515. );
  516. // Tags
  517. $this->dolibarrApiService->method('getSocietyTagsIds')->willReturnMap([
  518. [$socId1, [1, 68]],
  519. [$socId2, [3, 67]],
  520. ]);
  521. $dolibarrSyncService->method('getExpectedTagsFor')->willReturnMap([
  522. [$organization1, [67]],
  523. [$organization2, []],
  524. ]);
  525. // Expected progression callback triggers
  526. $progressionCallbackExpectedCalls = [[1, 3], [2, 3], [3, 3]];
  527. $progressionCallback = static function ($i, $total) use (&$progressionCallbackExpectedCalls) {
  528. [$expectedI, $expectedTotal] = array_shift($progressionCallbackExpectedCalls);
  529. if ($i !== $expectedI || $total !== $expectedTotal) {
  530. throw new \AssertionError('Progression callback error, expected parameters are ('.$expectedI.','.$expectedTotal.'), got ('.$i.', '.$total.')');
  531. }
  532. };
  533. $operations = $dolibarrSyncService->scan($progressionCallback);
  534. $this->assertCount(9, $operations);
  535. $this->assertEqualsCanonicalizing(
  536. [
  537. '[PUT thirdparties/1]',
  538. "address : `1 Rue Qwerty` => `Mr Keyboard\n1 Rue Azerty`",
  539. 'zip : `01024` => `01110`',
  540. 'town : `Ram` => `ByteCity`',
  541. 'email : `some@email.com` => `foo@bar.net`',
  542. 'phone : `` => `0102030405`',
  543. 'parent : `0` => `12098`',
  544. 'array_options.options_2iopen_software_opentalent : `Opentalent Artist` => `Opentalent School`',
  545. "array_options.options_2iopeninfoopentalent : `` => `Nombre d'élèves : 1\nNombre d'adhérents : 2\nNombre d'accès admin : 1`",
  546. 'status : `0` => `1`',
  547. ],
  548. $operations[0]->getChangeLog()
  549. );
  550. $this->assertEqualsCanonicalizing(
  551. [
  552. '[PUT contacts/1]',
  553. 'civility_code : `` => `MR`',
  554. 'lastname : `Dupond` => `Dupont`',
  555. 'firstname : `Bob` => `Hercules`',
  556. 'email : `abcd@mail.com` => `an@email.net`',
  557. 'phone_pro : `+33478570000` => `0102030405`',
  558. 'phone_mobile : `+33682980000` => `0607080910`',
  559. 'poste : `Secrétaire` => `Président`',
  560. 'array_options.options_2iopen_person_id : `` => `100`',
  561. ],
  562. $operations[1]->getChangeLog()
  563. );
  564. $this->assertEqualsCanonicalizing(
  565. [
  566. '[PUT contacts/4]',
  567. 'statut : `1` => `0`',
  568. ],
  569. $operations[2]->getChangeLog()
  570. );
  571. $this->assertEqualsCanonicalizing(
  572. [
  573. '[PUT contacts/5]',
  574. 'address : `` => `2 Rue Ascii`',
  575. 'town : `` => `ByteCity`',
  576. 'zip : `` => `01110`',
  577. ],
  578. $operations[3]->getChangeLog()
  579. );
  580. $this->assertEqualsCanonicalizing(
  581. [
  582. '[DELETE /thirdparties/1/categories/68]',
  583. ],
  584. $operations[4]->getChangeLog()
  585. );
  586. $this->assertEqualsCanonicalizing(
  587. [
  588. '[POST /thirdparties/1/categories/67]',
  589. ],
  590. $operations[5]->getChangeLog()
  591. );
  592. $this->assertEqualsCanonicalizing(
  593. [
  594. '[PUT thirdparties/2]',
  595. 'address : (new) => ``',
  596. 'email : (new) => ``',
  597. 'name : (new) => `Organization 20`',
  598. 'parent : (new) => ``',
  599. 'phone : (new) => ``',
  600. 'status : (new) => `1`',
  601. 'town : (new) => ``',
  602. 'zip : (new) => ``',
  603. 'array_options.options_2iopen_software_opentalent : (new) => `Opentalent Artist`',
  604. "array_options.options_2iopeninfoopentalent : (new) => `Nombre d'adhérents : 0\nNombre d'accès admin : 2`",
  605. ],
  606. $operations[6]->getChangeLog()
  607. );
  608. $this->assertEqualsCanonicalizing(
  609. [
  610. '[POST contacts]',
  611. 'civility_code : (new) => ``',
  612. 'lastname : (new) => `Simpson`',
  613. 'firstname : (new) => `Lisa`',
  614. 'email : (new) => ``',
  615. 'phone_pro : (new) => ``',
  616. 'phone_mobile : (new) => ``',
  617. 'poste : (new) => `Président(e)`',
  618. 'statut : (new) => `1`',
  619. 'array_options.options_2iopen_person_id : (new) => `200`',
  620. 'socid : (new) => `2`',
  621. ],
  622. $operations[7]->getChangeLog()
  623. );
  624. $this->assertEqualsCanonicalizing(
  625. [
  626. '[DELETE /thirdparties/2/categories/67]',
  627. ],
  628. $operations[8]->getChangeLog()
  629. );
  630. $this->assertCount(0, $progressionCallbackExpectedCalls);
  631. }
  632. /**
  633. * All valid operations shall be executed
  634. * If an operation is not in status READY, a warning shall be logged, and the operation shall be skipped.
  635. *
  636. * @see DolibarrSyncService::execute()
  637. */
  638. public function testExecute(): void
  639. {
  640. $dolibarrSyncService = $this->getMockForMethod('execute');
  641. $progressionCallbackExpectedCalls = [[1, 3], [2, 3], [3, 3]];
  642. $progressionCallback = static function ($i, $total) use (&$progressionCallbackExpectedCalls) {
  643. [$expectedI, $expectedTotal] = array_shift($progressionCallbackExpectedCalls);
  644. if ($i !== $expectedI || $total !== $expectedTotal) {
  645. throw new \AssertionError('Progression callback error, expected parameters are ('.$expectedI.','.$expectedTotal.'), got ('.$i.', '.$total.')');
  646. }
  647. };
  648. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  649. $operation1 = $this->getMockBuilder(UpdateOperation::class)->disableOriginalConstructor()->getMock();
  650. $operation1->method('getStatus')->willReturnOnConsecutiveCalls(BaseRestOperation::STATUS_READY, BaseRestOperation::STATUS_DONE);
  651. $operation1->method('getChangeLog')->willReturn(['foo']);
  652. $operation1->expects(self::once())->method('execute')->willReturn($response);
  653. $operation2 = $this->getMockBuilder(CreateOperation::class)->disableOriginalConstructor()->getMock();
  654. $operation2->method('getStatus')->willReturn(
  655. BaseRestOperation::STATUS_READY, BaseRestOperation::STATUS_ERROR, BaseRestOperation::STATUS_ERROR // An error happened
  656. );
  657. $operation2->expects(self::once())->method('execute');
  658. $operation3 = $this->getMockBuilder(DeleteOperation::class)->disableOriginalConstructor()->getMock();
  659. $operation3->method('getStatus')->willReturn(BaseRestOperation::STATUS_DONE); // Invalid status, should log a warning and not execute
  660. $operation3->expects(self::never())->method('execute');
  661. $this->logger->expects(self::exactly(2))->method('warning'); // 2 warnings because of the bad status of the Create Op
  662. $this->logger->expects(self::exactly(3))->method('error'); // The exception thrown during the execution of the Delete op will log 3 errors
  663. $dolibarrSyncService->execute([$operation1, $operation2, $operation3], $progressionCallback);
  664. }
  665. /**
  666. * @see DolibarrSyncService::run()
  667. */
  668. public function testRun()
  669. {
  670. $dolibarrSyncService = $this->getMockForMethod('run');
  671. $operations = ['operation1', 'operation2'];
  672. $dolibarrSyncService->expects(self::once())->method('scan')->willReturn($operations);
  673. $dolibarrSyncService->expects(self::once())->method('execute')->with($operations);
  674. $result = $dolibarrSyncService->run();
  675. $this->assertEquals($operations, $result);
  676. }
  677. /**
  678. * @see DolibarrSyncService::getDolibarrSocietiesIndex()
  679. */
  680. public function testGetDolibarrSocietiesIndex(): void
  681. {
  682. $dolibarrSyncService = $this->getMockForMethod('getDolibarrSocietiesIndex');
  683. $this->dolibarrApiService
  684. ->expects($this->once())
  685. ->method('getAllClients')
  686. ->willReturn(
  687. [
  688. ['id' => 1, 'array_options' => ['options_2iopen_organization_id' => 101]],
  689. ['id' => 2, 'array_options' => ['options_2iopen_organization_id' => 102]],
  690. ['id' => 3, 'array_options' => ['options_2iopen_organization_id' => null]], // No org id but also no contract, so it's ok
  691. ['id' => 4, 'name' => 'foo', 'array_options' => ['options_2iopen_organization_id' => null]], // No org id but has a contract, a warning should be logged
  692. ]
  693. );
  694. $this->dolibarrApiService->expects(self::exactly(2))->method('getActiveContract')->willReturnMap([
  695. [3, null],
  696. [4, ['dummy non-empty data']],
  697. ]);
  698. $this->logger->expects(self::once())->method('warning')->with('Dolibarr client has no organization id: foo (4)');
  699. $index = $dolibarrSyncService->getDolibarrSocietiesIndex();
  700. $this->assertEqualsCanonicalizing(
  701. [
  702. 1 => ['id' => 1, 'array_options' => ['options_2iopen_organization_id' => 101]],
  703. 2 => ['id' => 2, 'array_options' => ['options_2iopen_organization_id' => 102]],
  704. ],
  705. $index
  706. );
  707. }
  708. /**
  709. * @see DolibarrSyncService::getActiveMembersIndex()
  710. */
  711. public function testGetActiveMembersIndex(): void
  712. {
  713. $dolibarrSyncService = $this->getMockForMethod('getActiveMembersIndex');
  714. $this->accessRepository
  715. ->expects($this->once())
  716. ->method('getAllActiveMembersAndMissions')
  717. ->willReturn(
  718. [
  719. ['id' => 1, 'organization_id' => 1, 'mission' => FunctionEnum::PRESIDENT],
  720. ['id' => 2, 'organization_id' => 1, 'mission' => FunctionEnum::STUDENT],
  721. ['id' => 3, 'organization_id' => 2, 'mission' => FunctionEnum::PRESIDENT],
  722. ['id' => 3, 'organization_id' => 2, 'mission' => FunctionEnum::TEACHER],
  723. ]
  724. );
  725. $index = $dolibarrSyncService->getActiveMembersIndex();
  726. $this->assertEqualsCanonicalizing([
  727. 1 => [1 => [FunctionEnum::PRESIDENT->value], 2 => [FunctionEnum::STUDENT->value]],
  728. 2 => [3 => [FunctionEnum::PRESIDENT->value, FunctionEnum::TEACHER->value]],
  729. ], $index);
  730. }
  731. /**
  732. * @see DolibarrSyncService::findDolibarrContactFor()
  733. */
  734. public function testFindDolibarrContactForById(): void
  735. {
  736. $dolibarrSyncService = $this->getMockForMethod('findDolibarrContactFor');
  737. $contacts = [
  738. ['id' => 1, 'array_options' => ['options_2iopen_person_id' => 101]],
  739. ['id' => 2, 'array_options' => ['options_2iopen_person_id' => 102]],
  740. ];
  741. // Find by id
  742. $person = $this->getMockBuilder(Person::class)->getMock();
  743. $person->method('getId')->willReturn(102);
  744. $contact = $dolibarrSyncService->findDolibarrContactFor($contacts, $person);
  745. $this->assertEquals(2, $contact['id']);
  746. }
  747. /**
  748. * @see DolibarrSyncService::findDolibarrContactFor()
  749. */
  750. public function testFindDolibarrContactForByName(): void
  751. {
  752. $dolibarrSyncService = $this->getMockForMethod('findDolibarrContactFor');
  753. $contacts = [
  754. ['id' => 1, 'firstname' => 'mister', 'lastname' => 'X', 'array_options' => ['options_2iopen_person_id' => null]],
  755. ['id' => 2, 'firstname' => 'miss', 'lastname' => 'Y', 'array_options' => ['options_2iopen_person_id' => null]],
  756. ];
  757. // Find by full name (contact has no person id, it should be returned)
  758. $person = $this->getMockBuilder(Person::class)->getMock();
  759. $person->method('getId')->willReturn(101);
  760. $person->method('getName')->willReturn('X');
  761. $person->method('getGivenName')->willReturn('mister');
  762. $result = $dolibarrSyncService->findDolibarrContactFor($contacts, $person);
  763. $this->assertEquals(1, $result['id']);
  764. }
  765. /**
  766. * @see DolibarrSyncService::findDolibarrContactFor()
  767. */
  768. public function testFindDolibarrContactForByNameWithConflict(): void
  769. {
  770. $dolibarrSyncService = $this->getMockForMethod('findDolibarrContactFor');
  771. $contacts = [
  772. ['id' => 1, 'firstname' => 'mister', 'lastname' => 'X', 'array_options' => ['options_2iopen_person_id' => 1]],
  773. ['id' => 2, 'firstname' => 'miss', 'lastname' => 'Y', 'array_options' => ['options_2iopen_person_id' => 2]],
  774. ];
  775. // Find by full name (contact already has another person id, it should not be returned)
  776. $person = $this->getMockBuilder(Person::class)->getMock();
  777. $person->method('getId')->willReturn(101);
  778. $person->method('getName')->willReturn('X');
  779. $person->method('getGivenName')->willReturn('mister');
  780. $result = $dolibarrSyncService->findDolibarrContactFor($contacts, $person);
  781. $this->assertEquals(null, $result);
  782. }
  783. /**
  784. * @see DolibarrSyncService::findDolibarrContactFor()
  785. */
  786. public function testFindDolibarrContactNotFound(): void
  787. {
  788. $dolibarrSyncService = $this->getMockForMethod('findDolibarrContactFor');
  789. $contacts = [
  790. ['id' => 1, 'firstname' => 'mister', 'lastname' => 'X', 'array_options' => ['options_2iopen_person_id' => 1]],
  791. ['id' => 2, 'firstname' => 'miss', 'lastname' => 'Y', 'array_options' => ['options_2iopen_person_id' => 2]],
  792. ];
  793. // Do not find
  794. $person = $this->getMockBuilder(Person::class)->getMock();
  795. $person->method('getId')->willReturn(-1);
  796. $person->method('getName')->willReturn('Presley');
  797. $person->method('getGivenName')->willReturn('Elvis');
  798. $result = $dolibarrSyncService->findDolibarrContactFor($contacts, $person);
  799. $this->assertEquals(null, $result);
  800. }
  801. /**
  802. * @see DolibarrSyncService::sanitizeDolibarrData()
  803. */
  804. public function testSanitizeDolibarrData(): void
  805. {
  806. $dolibarrSyncService = $this->getMockForMethod('sanitizeDolibarrData');
  807. $result = $dolibarrSyncService->sanitizeDolibarrData(['a' => 'A', 'b' => '', 'c' => ['d' => 'D', 'e' => '']]);
  808. $this->assertEquals(
  809. ['a' => 'A', 'b' => null, 'c' => ['d' => 'D', 'e' => null]],
  810. $result
  811. );
  812. }
  813. /**
  814. * @see DolibarrSyncService::sanitizeDolibarrData()
  815. */
  816. public function testSanitizeDolibarrDataWithNull(): void
  817. {
  818. $dolibarrSyncService = $this->getMockForMethod('sanitizeDolibarrData');
  819. $result = $dolibarrSyncService->sanitizeDolibarrData(null);
  820. $this->assertEquals(null, $result);
  821. }
  822. /**
  823. * @see DolibarrSyncService::getOrganizationPostalAddress()
  824. */
  825. public function testGetOrganizationPostalAddress(): void
  826. {
  827. $dolibarrSyncService = $this->getMockForMethod('getOrganizationPostalAddress');
  828. $organization = $this->getMockBuilder(Organization::class)->getMock();
  829. $organizationAddressPostal1 = $this->getMockBuilder(OrganizationAddressPostal::class)->getMock();
  830. $organizationAddressPostal2 = $this->getMockBuilder(OrganizationAddressPostal::class)->getMock();
  831. $organizationAddressPostal3 = $this->getMockBuilder(OrganizationAddressPostal::class)->getMock();
  832. $addressPostal = $this->getMockBuilder(AddressPostal::class)->getMock();
  833. $organizationAddressPostal1->method('getType')->willReturn(AddressPostalOrganizationTypeEnum::ADDRESS_PRACTICE);
  834. $organizationAddressPostal2->method('getType')->willReturn(AddressPostalOrganizationTypeEnum::ADDRESS_BILL);
  835. $organizationAddressPostal3->method('getType')->willReturn(AddressPostalOrganizationTypeEnum::ADDRESS_OTHER);
  836. $organizationAddressPostal2->method('getAddressPostal')->willReturn($addressPostal);
  837. $organization->expects($this->once())
  838. ->method('getOrganizationAddressPostals')
  839. ->willReturn(
  840. new ArrayCollection([$organizationAddressPostal1, $organizationAddressPostal2, $organizationAddressPostal3])
  841. );
  842. $this->assertEquals(
  843. $addressPostal,
  844. $dolibarrSyncService->getOrganizationPostalAddress($organization)
  845. );
  846. }
  847. /**
  848. * @see DolibarrSyncService::getOrganizationPostalAddress()
  849. */
  850. public function testGetOrganizationPostalAddressNoResult(): void
  851. {
  852. $dolibarrSyncService = $this->getMockForMethod('getOrganizationPostalAddress');
  853. $organization = $this->getMockBuilder(Organization::class)->getMock();
  854. $organization->expects($this->once())
  855. ->method('getOrganizationAddressPostals')
  856. ->willReturn(new ArrayCollection([]));
  857. $this->assertEquals(
  858. null,
  859. $dolibarrSyncService->getOrganizationPostalAddress($organization)
  860. );
  861. }
  862. /**
  863. * @see DolibarrSyncService::getOrganizationPhone()
  864. */
  865. public function testGetOrganizationPhoneWithExistingPhone(): void
  866. {
  867. $dolibarrSyncService = $this->getMockForMethod('getOrganizationPhone');
  868. $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
  869. $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
  870. $contactPoint3 = $this->getMockBuilder(ContactPoint::class)->getMock();
  871. $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER);
  872. $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::BILL);
  873. $contactPoint3->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL);
  874. $phone = $this->getMockBuilder(PhoneNumber::class)->disableOriginalConstructor()->getMock();
  875. $contactPoint2->method('getTelphone')->willReturn($phone);
  876. $organization = $this->getMockBuilder(Organization::class)->getMock();
  877. $organization
  878. ->expects($this->once())
  879. ->method('getContactPoints')
  880. ->willReturn(
  881. new ArrayCollection([$contactPoint1, $contactPoint2, $contactPoint3])
  882. );
  883. $dolibarrSyncService->expects(self::once())->method('formatPhoneNumber')->with($phone)->willReturn('+33161626365');
  884. $this->assertEquals(
  885. '+33161626365',
  886. $dolibarrSyncService->getOrganizationPhone($organization)
  887. );
  888. }
  889. /**
  890. * @see DolibarrSyncService::getOrganizationPhone()
  891. */
  892. public function testGetOrganizationPhoneWithMobilePhone()
  893. {
  894. $dolibarrSyncService = $this->getMockForMethod('getOrganizationPhone');
  895. $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
  896. $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
  897. $contactPoint3 = $this->getMockBuilder(ContactPoint::class)->getMock();
  898. $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER);
  899. $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::BILL);
  900. $contactPoint3->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL);
  901. $contactPoint2->expects($this->once())->method('getTelphone')->willReturn(null);
  902. $mobilePhone = $this->getMockBuilder(PhoneNumber::class)->disableOriginalConstructor()->getMock();
  903. $contactPoint2->method('getMobilPhone')->willReturn($mobilePhone);
  904. $organization = $this->getMockBuilder(Organization::class)->getMock();
  905. $organization
  906. ->expects($this->once())
  907. ->method('getContactPoints')
  908. ->willReturn(
  909. new ArrayCollection([$contactPoint1, $contactPoint2, $contactPoint3])
  910. );
  911. $dolibarrSyncService->expects(self::once())->method('formatPhoneNumber')->with($mobilePhone)->willReturn('+33661626365');
  912. $this->assertEquals(
  913. '+33661626365',
  914. $dolibarrSyncService->getOrganizationPhone($organization)
  915. );
  916. }
  917. /**
  918. * @see DolibarrSyncService::getOrganizationPhone()
  919. */
  920. public function testGetOrganizationPhoneWithNoPhone()
  921. {
  922. $dolibarrSyncService = $this->getMockForMethod('getOrganizationPhone');
  923. $organization = $this->getMockBuilder(Organization::class)->getMock();
  924. $organization
  925. ->expects($this->once())
  926. ->method('getContactPoints')
  927. ->willReturn(new ArrayCollection([]));
  928. $dolibarrSyncService->expects(self::never())->method('formatPhoneNumber');
  929. $this->assertEquals(
  930. null,
  931. $dolibarrSyncService->getOrganizationPhone($organization)
  932. );
  933. }
  934. /**
  935. * @see DolibarrSyncService::getOrganizationEmail()
  936. */
  937. public function testGetOrganizationEmailWithExistingEmail()
  938. {
  939. $dolibarrSyncService = $this->getMockForMethod('getOrganizationEmail');
  940. $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
  941. $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
  942. $contactPoint3 = $this->getMockBuilder(ContactPoint::class)->getMock();
  943. $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER);
  944. $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::BILL);
  945. $contactPoint3->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL);
  946. $contactPoint2->method('getEmail')->willReturn('email@email.com');
  947. $organization = $this->getMockBuilder(Organization::class)->getMock();
  948. $organization
  949. ->expects($this->once())
  950. ->method('getContactPoints')
  951. ->willReturn(
  952. new ArrayCollection([$contactPoint1, $contactPoint2, $contactPoint3])
  953. );
  954. $this->assertEquals(
  955. 'email@email.com',
  956. $dolibarrSyncService->getOrganizationEmail($organization)
  957. );
  958. }
  959. /**
  960. * @see DolibarrSyncService::getOrganizationEmail()
  961. */
  962. public function testGetOrganizationEmailWithNoEmail()
  963. {
  964. $dolibarrSyncService = $this->getMockForMethod('getOrganizationEmail');
  965. $organization = $this->getMockBuilder(Organization::class)->getMock();
  966. $organization
  967. ->expects($this->once())
  968. ->method('getContactPoints')
  969. ->willReturn(new ArrayCollection([]));
  970. $this->assertEquals(
  971. null,
  972. $dolibarrSyncService->getOrganizationEmail($organization)
  973. );
  974. }
  975. /**
  976. * @see DolibarrSyncService::getOrganizationNetworkId()
  977. */
  978. public function testGetOrganizationNetworkId()
  979. {
  980. $dolibarrSyncService = $this->getMockForMethod('getOrganizationNetworkId');
  981. $organization = $this->getMockBuilder(Organization::class)->getMock();
  982. $network = $this->getMockBuilder(Network::class)->getMock();
  983. $network->method('getId')->willReturn(3);
  984. $networkOrganization = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  985. $networkOrganization->method('getNetwork')->willReturn($network);
  986. $organization->method('getNetworkOrganizations')->willReturn(new ArrayCollection([$networkOrganization]));
  987. $this->assertEquals(
  988. 3,
  989. $dolibarrSyncService->getOrganizationNetworkId($organization)
  990. );
  991. }
  992. /**
  993. * @see DolibarrSyncService::getOrganizationNetworkId()
  994. */
  995. public function testGetOrganizationNetworkIdWithMultipleResult()
  996. {
  997. $dolibarrSyncService = $this->getMockForMethod('getOrganizationNetworkId');
  998. $network1 = $this->getMockBuilder(Network::class)->getMock();
  999. $network1->method('getId')->willReturn(3);
  1000. $networkOrganization1 = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  1001. $networkOrganization1->method('getNetwork')->willReturn($network1);
  1002. $networkOrganization1->method('getEndDate')->willReturn(new \DateTime('2000-01-01'));
  1003. $network2 = $this->getMockBuilder(Network::class)->getMock();
  1004. $network2->method('getId')->willReturn(4);
  1005. $networkOrganization2 = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  1006. $networkOrganization2->method('getNetwork')->willReturn($network2);
  1007. $networkOrganization2->method('getEndDate')->willReturn(null);
  1008. $organization = $this->getMockBuilder(Organization::class)->getMock();
  1009. $organization->method('getNetworkOrganizations')->willReturn(
  1010. new ArrayCollection([$networkOrganization1, $networkOrganization2])
  1011. );
  1012. $this->assertEquals(
  1013. 4,
  1014. $dolibarrSyncService->getOrganizationNetworkId($organization)
  1015. );
  1016. }
  1017. /**
  1018. * @see DolibarrSyncService::getOrganizationNetworkId()
  1019. */
  1020. public function testGetOrganizationNetworkIdWithNoResult()
  1021. {
  1022. $dolibarrSyncService = $this->getMockForMethod('getOrganizationNetworkId');
  1023. $organization = $this->getMockBuilder(Organization::class)->getMock();
  1024. $network = $this->getMockBuilder(Network::class)->getMock();
  1025. $network->method('getId')->willReturn(3);
  1026. $networkOrganization = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  1027. $networkOrganization->method('getNetwork')->willReturn($network);
  1028. $networkOrganization->method('getEndDate')->willReturn(new \DateTime('2000-01-01'));
  1029. $organization->method('getNetworkOrganizations')->willReturn(new ArrayCollection([$networkOrganization]));
  1030. $this->assertEquals(
  1031. null,
  1032. $dolibarrSyncService->getOrganizationNetworkId($organization)
  1033. );
  1034. }
  1035. /**
  1036. * @see DolibarrSyncService::countWithMission()
  1037. */
  1038. public function testCountWithMission()
  1039. {
  1040. $dolibarrSyncService = $this->getMockForMethod('countWithMission');
  1041. $members = [
  1042. 123 => [FunctionEnum::PRESIDENT->value, FunctionEnum::TEACHER->value],
  1043. 124 => [FunctionEnum::TEACHER->value],
  1044. 125 => [FunctionEnum::STUDENT->value],
  1045. 126 => [FunctionEnum::TREASURER->value],
  1046. ];
  1047. $this->assertEquals(
  1048. 2,
  1049. $dolibarrSyncService->countWithMission([FunctionEnum::TEACHER->value], $members)
  1050. );
  1051. $this->assertEquals(
  1052. 3,
  1053. $dolibarrSyncService->countWithMission(
  1054. [FunctionEnum::TEACHER->value, FunctionEnum::TREASURER->value],
  1055. $members
  1056. )
  1057. );
  1058. $this->assertEquals(
  1059. 1,
  1060. $dolibarrSyncService->countWithMission([FunctionEnum::STUDENT->value], $members)
  1061. );
  1062. $this->assertEquals(
  1063. 0,
  1064. $dolibarrSyncService->countWithMission([FunctionEnum::ARCHIVIST->value], $members)
  1065. );
  1066. }
  1067. /**
  1068. * @see DolibarrSyncService::getPersonContact()
  1069. */
  1070. public function testGetPersonContact()
  1071. {
  1072. $dolibarrSyncService = $this->getMockForMethod('getPersonContact');
  1073. $person = $this->getMockBuilder(Person::class)->getMock();
  1074. $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
  1075. $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
  1076. $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER);
  1077. $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL);
  1078. $person->expects($this->once())->method('getContactPoints')->willReturn(new ArrayCollection([$contactPoint1, $contactPoint2]));
  1079. $this->assertEquals(
  1080. $contactPoint2,
  1081. $dolibarrSyncService->getPersonContact($person)
  1082. );
  1083. $person2 = $this->getMockBuilder(Person::class)->getMock();
  1084. $person2->expects($this->once())->method('getContactPoints')->willReturn(new ArrayCollection([]));
  1085. $this->assertEquals(
  1086. null,
  1087. $dolibarrSyncService->getPersonContact($person2)
  1088. );
  1089. }
  1090. /**
  1091. * @see DolibarrSyncService::formatContactPosition()
  1092. */
  1093. public function testFormatContactPosition()
  1094. {
  1095. $dolibarrSyncService = $this->getMockForMethod('formatContactPosition');
  1096. $this->translator->method('trans')->willReturnMap(
  1097. [
  1098. [FunctionEnum::PRESIDENT->value, ['gender' => 'X'], null, null, 'Président(e)'],
  1099. [FunctionEnum::PRESIDENT->value, ['gender' => 'M'], null, null, 'Président'],
  1100. [FunctionEnum::PRESIDENT->value, ['gender' => 'F'], null, null, 'Présidente'],
  1101. [FunctionEnum::DIRECTOR->value, ['gender' => 'X'], null, null, 'Directeur(ice)'],
  1102. [FunctionEnum::DIRECTOR->value, ['gender' => 'M'], null, null, 'Directeur'],
  1103. [FunctionEnum::DIRECTOR->value, ['gender' => 'F'], null, null, 'Directrice'],
  1104. [FunctionEnum::TEACHER->value, ['gender' => 'X'], null, null, 'Professeur(e)'],
  1105. [FunctionEnum::ARCHIVIST->value, ['gender' => 'X'], null, null, 'Archiviste'],
  1106. [FunctionEnum::TREASURER->value, ['gender' => 'X'], null, null, 'Trésorier(ère)'],
  1107. [FunctionEnum::ADMINISTRATIVE_STAFF->value, ['gender' => 'X'], null, null, 'Personnel administratif'],
  1108. ]
  1109. );
  1110. $this->assertEquals(
  1111. 'Président(e)',
  1112. $dolibarrSyncService->formatContactPosition([FunctionEnum::PRESIDENT->value])
  1113. );
  1114. $this->assertEquals(
  1115. 'Président',
  1116. $dolibarrSyncService->formatContactPosition([FunctionEnum::PRESIDENT->value], 'MISTER')
  1117. );
  1118. $this->assertEquals(
  1119. 'Présidente',
  1120. $dolibarrSyncService->formatContactPosition([FunctionEnum::PRESIDENT->value], 'MISS')
  1121. );
  1122. $this->assertEquals(
  1123. 'Présidente, Directrice',
  1124. $dolibarrSyncService->formatContactPosition(
  1125. [FunctionEnum::PRESIDENT->value, FunctionEnum::DIRECTOR->value],
  1126. 'MISS'
  1127. )
  1128. );
  1129. $this->assertEquals(
  1130. 'Président, Directeur',
  1131. $dolibarrSyncService->formatContactPosition(
  1132. [FunctionEnum::PRESIDENT->value, FunctionEnum::DIRECTOR->value, FunctionEnum::ADHERENT->value],
  1133. 'MISTER'
  1134. )
  1135. );
  1136. $this->assertEquals(
  1137. 'Président, Directeur',
  1138. $dolibarrSyncService->formatContactPosition(
  1139. [FunctionEnum::PRESIDENT->value, FunctionEnum::DIRECTOR->value, FunctionEnum::ADHERENT->value],
  1140. 'MISTER'
  1141. )
  1142. );
  1143. $this->assertEquals(
  1144. 'Président(e), Directeur(ice), Professeur(e), Archiviste, Trésorier(ère), Pers...',
  1145. $dolibarrSyncService->formatContactPosition(
  1146. [
  1147. FunctionEnum::PRESIDENT->value,
  1148. FunctionEnum::DIRECTOR->value,
  1149. FunctionEnum::TEACHER->value,
  1150. FunctionEnum::ARCHIVIST->value,
  1151. FunctionEnum::TREASURER->value,
  1152. FunctionEnum::ADMINISTRATIVE_STAFF->value,
  1153. ],
  1154. 'X'
  1155. )
  1156. );
  1157. }
  1158. /**
  1159. * @see DolibarrSyncService::formatPhoneNumber()
  1160. */
  1161. public function testFormatPhoneNumber()
  1162. {
  1163. $dolibarrSyncService = $this->getMockForMethod('formatPhoneNumber');
  1164. $phoneUtil = PhoneNumberUtil::getInstance();
  1165. $phoneNumber = $phoneUtil->parse('01 02 03 04 05', 'FR');
  1166. $this->assertEquals(
  1167. '+33102030405',
  1168. $dolibarrSyncService->formatPhoneNumber($phoneNumber)
  1169. );
  1170. }
  1171. public function testGetExpectedTagsFor()
  1172. {
  1173. $dolibarrSyncService = $this->getMockForMethod('getExpectedTagsFor');
  1174. $organization1 = $this->getMockBuilder(Organization::class)->getMock();
  1175. $organization1->method('getLegalStatus')->willReturn(LegalEnum::ASSOCIATION_LAW_1901);
  1176. $settings1 = $this->getMockBuilder(Settings::class)->getMock();
  1177. $settings1->method('getProduct')->willReturn(SettingsProductEnum::SCHOOL);
  1178. $organization1->method('getSettings')->willReturn($settings1);
  1179. $organization2 = $this->getMockBuilder(Organization::class)->getMock();
  1180. $organization2->method('getLegalStatus')->willReturn(LegalEnum::ASSOCIATION_LAW_1901);
  1181. $settings2 = $this->getMockBuilder(Settings::class)->getMock();
  1182. $settings2->method('getProduct')->willReturn(SettingsProductEnum::SCHOOL_PREMIUM);
  1183. $organization2->method('getSettings')->willReturn($settings2);
  1184. $organization3 = $this->getMockBuilder(Organization::class)->getMock();
  1185. $organization3->method('getLegalStatus')->willReturn(LegalEnum::LOCAL_AUTHORITY);
  1186. $settings3 = $this->getMockBuilder(Settings::class)->getMock();
  1187. $settings3->method('getProduct')->willReturn(SettingsProductEnum::SCHOOL);
  1188. $organization3->method('getSettings')->willReturn($settings3);
  1189. $organization4 = $this->getMockBuilder(Organization::class)->getMock();
  1190. $organization4->method('getLegalStatus')->willReturn(LegalEnum::LOCAL_AUTHORITY);
  1191. $settings4 = $this->getMockBuilder(Settings::class)->getMock();
  1192. $settings4->method('getProduct')->willReturn(SettingsProductEnum::SCHOOL_PREMIUM);
  1193. $organization4->method('getSettings')->willReturn($settings4);
  1194. $organization5 = $this->getMockBuilder(Organization::class)->getMock();
  1195. $organization5->method('getLegalStatus')->willReturn(LegalEnum::LOCAL_AUTHORITY);
  1196. $settings5 = $this->getMockBuilder(Settings::class)->getMock();
  1197. $settings5->method('getProduct')->willReturn(SettingsProductEnum::ARTIST);
  1198. $organization5->method('getSettings')->willReturn($settings5);
  1199. $this->assertEquals(
  1200. $dolibarrSyncService->getExpectedTagsFor($organization1),
  1201. [67]
  1202. );
  1203. $this->assertEquals(
  1204. $dolibarrSyncService->getExpectedTagsFor($organization2),
  1205. [69]
  1206. );
  1207. $this->assertEquals(
  1208. $dolibarrSyncService->getExpectedTagsFor($organization3),
  1209. [68]
  1210. );
  1211. $this->assertEquals(
  1212. $dolibarrSyncService->getExpectedTagsFor($organization4),
  1213. [70]
  1214. );
  1215. $this->assertEquals(
  1216. $dolibarrSyncService->getExpectedTagsFor($organization5),
  1217. []
  1218. );
  1219. }
  1220. }