DolibarrSyncServiceTest.php 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516
  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. $dolibarrSyncService->method('validateResponse')->with($response, $operation1)->willThrowException(new \RuntimeException());
  654. $operation2 = $this->getMockBuilder(CreateOperation::class)->disableOriginalConstructor()->getMock();
  655. $operation2->method('getStatus')->willReturn(
  656. BaseRestOperation::STATUS_READY, BaseRestOperation::STATUS_ERROR, BaseRestOperation::STATUS_ERROR // An error happened
  657. );
  658. $operation2->expects(self::once())->method('execute');
  659. $operation3 = $this->getMockBuilder(DeleteOperation::class)->disableOriginalConstructor()->getMock();
  660. $operation3->method('getStatus')->willReturn(BaseRestOperation::STATUS_DONE); // Invalid status, should log a warning and not execute
  661. $operation3->expects(self::never())->method('execute');
  662. $this->logger->expects(self::exactly(3))->method('warning'); // 1 warning from validateResponse on the Update Op, and 2 because of the bad status of the Create Op
  663. $this->logger->expects(self::exactly(3))->method('error'); // The exception thrown during the execution of the Delete op will log 3 errors
  664. $dolibarrSyncService->execute([$operation1, $operation2, $operation3], $progressionCallback);
  665. }
  666. /**
  667. * @see DolibarrSyncService::run()
  668. */
  669. public function testRun()
  670. {
  671. $dolibarrSyncService = $this->getMockForMethod('run');
  672. $operations = ['operation1', 'operation2'];
  673. $dolibarrSyncService->expects(self::once())->method('scan')->willReturn($operations);
  674. $dolibarrSyncService->expects(self::once())->method('execute')->with($operations);
  675. $result = $dolibarrSyncService->run();
  676. $this->assertEquals($operations, $result);
  677. }
  678. /**
  679. * @see DolibarrSyncService::getDolibarrSocietiesIndex()
  680. */
  681. public function testGetDolibarrSocietiesIndex(): void
  682. {
  683. $dolibarrSyncService = $this->getMockForMethod('getDolibarrSocietiesIndex');
  684. $this->dolibarrApiService
  685. ->expects($this->once())
  686. ->method('getAllClients')
  687. ->willReturn(
  688. [
  689. ['id' => 1, 'array_options' => ['options_2iopen_organization_id' => 101]],
  690. ['id' => 2, 'array_options' => ['options_2iopen_organization_id' => 102]],
  691. ['id' => 3, 'array_options' => ['options_2iopen_organization_id' => null]], // No org id but also no contract, so it's ok
  692. ['id' => 4, 'name' => 'foo', 'array_options' => ['options_2iopen_organization_id' => null]], // No org id but has a contract, a warning should be logged
  693. ]
  694. );
  695. $this->dolibarrApiService->expects(self::exactly(2))->method('getActiveContract')->willReturnMap([
  696. [3, null],
  697. [4, ['dummy non-empty data']],
  698. ]);
  699. $this->logger->expects(self::once())->method('warning')->with('Dolibarr client has no organization id: foo (4)');
  700. $index = $dolibarrSyncService->getDolibarrSocietiesIndex();
  701. $this->assertEqualsCanonicalizing(
  702. [
  703. 1 => ['id' => 1, 'array_options' => ['options_2iopen_organization_id' => 101]],
  704. 2 => ['id' => 2, 'array_options' => ['options_2iopen_organization_id' => 102]],
  705. ],
  706. $index
  707. );
  708. }
  709. /**
  710. * @see DolibarrSyncService::getActiveMembersIndex()
  711. */
  712. public function testGetActiveMembersIndex(): void
  713. {
  714. $dolibarrSyncService = $this->getMockForMethod('getActiveMembersIndex');
  715. $this->accessRepository
  716. ->expects($this->once())
  717. ->method('getAllActiveMembersAndMissions')
  718. ->willReturn(
  719. [
  720. ['id' => 1, 'organization_id' => 1, 'mission' => FunctionEnum::PRESIDENT],
  721. ['id' => 2, 'organization_id' => 1, 'mission' => FunctionEnum::STUDENT],
  722. ['id' => 3, 'organization_id' => 2, 'mission' => FunctionEnum::PRESIDENT],
  723. ['id' => 3, 'organization_id' => 2, 'mission' => FunctionEnum::TEACHER],
  724. ]
  725. );
  726. $index = $dolibarrSyncService->getActiveMembersIndex();
  727. $this->assertEqualsCanonicalizing([
  728. 1 => [1 => [FunctionEnum::PRESIDENT->value], 2 => [FunctionEnum::STUDENT->value]],
  729. 2 => [3 => [FunctionEnum::PRESIDENT->value, FunctionEnum::TEACHER->value]],
  730. ], $index);
  731. }
  732. /**
  733. * @see DolibarrSyncService::findDolibarrContactFor()
  734. */
  735. public function testFindDolibarrContactForById(): void
  736. {
  737. $dolibarrSyncService = $this->getMockForMethod('findDolibarrContactFor');
  738. $contacts = [
  739. ['id' => 1, 'array_options' => ['options_2iopen_person_id' => 101]],
  740. ['id' => 2, 'array_options' => ['options_2iopen_person_id' => 102]],
  741. ];
  742. // Find by id
  743. $person = $this->getMockBuilder(Person::class)->getMock();
  744. $person->method('getId')->willReturn(102);
  745. $contact = $dolibarrSyncService->findDolibarrContactFor($contacts, $person);
  746. $this->assertEquals(2, $contact['id']);
  747. }
  748. /**
  749. * @see DolibarrSyncService::findDolibarrContactFor()
  750. */
  751. public function testFindDolibarrContactForByName(): void
  752. {
  753. $dolibarrSyncService = $this->getMockForMethod('findDolibarrContactFor');
  754. $contacts = [
  755. ['id' => 1, 'firstname' => 'mister', 'lastname' => 'X', 'array_options' => ['options_2iopen_person_id' => null]],
  756. ['id' => 2, 'firstname' => 'miss', 'lastname' => 'Y', 'array_options' => ['options_2iopen_person_id' => null]],
  757. ];
  758. // Find by full name (contact has no person id, it should be returned)
  759. $person = $this->getMockBuilder(Person::class)->getMock();
  760. $person->method('getId')->willReturn(101);
  761. $person->method('getName')->willReturn('X');
  762. $person->method('getGivenName')->willReturn('mister');
  763. $result = $dolibarrSyncService->findDolibarrContactFor($contacts, $person);
  764. $this->assertEquals(1, $result['id']);
  765. }
  766. /**
  767. * @see DolibarrSyncService::findDolibarrContactFor()
  768. */
  769. public function testFindDolibarrContactForByNameWithConflict(): void
  770. {
  771. $dolibarrSyncService = $this->getMockForMethod('findDolibarrContactFor');
  772. $contacts = [
  773. ['id' => 1, 'firstname' => 'mister', 'lastname' => 'X', 'array_options' => ['options_2iopen_person_id' => 1]],
  774. ['id' => 2, 'firstname' => 'miss', 'lastname' => 'Y', 'array_options' => ['options_2iopen_person_id' => 2]],
  775. ];
  776. // Find by full name (contact already has another person id, it should not be returned)
  777. $person = $this->getMockBuilder(Person::class)->getMock();
  778. $person->method('getId')->willReturn(101);
  779. $person->method('getName')->willReturn('X');
  780. $person->method('getGivenName')->willReturn('mister');
  781. $result = $dolibarrSyncService->findDolibarrContactFor($contacts, $person);
  782. $this->assertEquals(null, $result);
  783. }
  784. /**
  785. * @see DolibarrSyncService::findDolibarrContactFor()
  786. */
  787. public function testFindDolibarrContactNotFound(): void
  788. {
  789. $dolibarrSyncService = $this->getMockForMethod('findDolibarrContactFor');
  790. $contacts = [
  791. ['id' => 1, 'firstname' => 'mister', 'lastname' => 'X', 'array_options' => ['options_2iopen_person_id' => 1]],
  792. ['id' => 2, 'firstname' => 'miss', 'lastname' => 'Y', 'array_options' => ['options_2iopen_person_id' => 2]],
  793. ];
  794. // Do not find
  795. $person = $this->getMockBuilder(Person::class)->getMock();
  796. $person->method('getId')->willReturn(-1);
  797. $person->method('getName')->willReturn('Presley');
  798. $person->method('getGivenName')->willReturn('Elvis');
  799. $result = $dolibarrSyncService->findDolibarrContactFor($contacts, $person);
  800. $this->assertEquals(null, $result);
  801. }
  802. /**
  803. * @see DolibarrSyncService::sanitizeDolibarrData()
  804. */
  805. public function testSanitizeDolibarrData(): void
  806. {
  807. $dolibarrSyncService = $this->getMockForMethod('sanitizeDolibarrData');
  808. $result = $dolibarrSyncService->sanitizeDolibarrData(['a' => 'A', 'b' => '', 'c' => ['d' => 'D', 'e' => '']]);
  809. $this->assertEquals(
  810. ['a' => 'A', 'b' => null, 'c' => ['d' => 'D', 'e' => null]],
  811. $result
  812. );
  813. }
  814. /**
  815. * @see DolibarrSyncService::sanitizeDolibarrData()
  816. */
  817. public function testSanitizeDolibarrDataWithNull(): void
  818. {
  819. $dolibarrSyncService = $this->getMockForMethod('sanitizeDolibarrData');
  820. $result = $dolibarrSyncService->sanitizeDolibarrData(null);
  821. $this->assertEquals(null, $result);
  822. }
  823. /**
  824. * @see DolibarrSyncService::getOrganizationPostalAddress()
  825. */
  826. public function testGetOrganizationPostalAddress(): void
  827. {
  828. $dolibarrSyncService = $this->getMockForMethod('getOrganizationPostalAddress');
  829. $organization = $this->getMockBuilder(Organization::class)->getMock();
  830. $organizationAddressPostal1 = $this->getMockBuilder(OrganizationAddressPostal::class)->getMock();
  831. $organizationAddressPostal2 = $this->getMockBuilder(OrganizationAddressPostal::class)->getMock();
  832. $organizationAddressPostal3 = $this->getMockBuilder(OrganizationAddressPostal::class)->getMock();
  833. $addressPostal = $this->getMockBuilder(AddressPostal::class)->getMock();
  834. $organizationAddressPostal1->method('getType')->willReturn(AddressPostalOrganizationTypeEnum::ADDRESS_PRACTICE);
  835. $organizationAddressPostal2->method('getType')->willReturn(AddressPostalOrganizationTypeEnum::ADDRESS_BILL);
  836. $organizationAddressPostal3->method('getType')->willReturn(AddressPostalOrganizationTypeEnum::ADDRESS_OTHER);
  837. $organizationAddressPostal2->method('getAddressPostal')->willReturn($addressPostal);
  838. $organization->expects($this->once())
  839. ->method('getOrganizationAddressPostals')
  840. ->willReturn(
  841. new ArrayCollection([$organizationAddressPostal1, $organizationAddressPostal2, $organizationAddressPostal3])
  842. );
  843. $this->assertEquals(
  844. $addressPostal,
  845. $dolibarrSyncService->getOrganizationPostalAddress($organization)
  846. );
  847. }
  848. /**
  849. * @see DolibarrSyncService::getOrganizationPostalAddress()
  850. */
  851. public function testGetOrganizationPostalAddressNoResult(): void
  852. {
  853. $dolibarrSyncService = $this->getMockForMethod('getOrganizationPostalAddress');
  854. $organization = $this->getMockBuilder(Organization::class)->getMock();
  855. $organization->expects($this->once())
  856. ->method('getOrganizationAddressPostals')
  857. ->willReturn(new ArrayCollection([]));
  858. $this->assertEquals(
  859. null,
  860. $dolibarrSyncService->getOrganizationPostalAddress($organization)
  861. );
  862. }
  863. /**
  864. * @see DolibarrSyncService::getOrganizationPhone()
  865. */
  866. public function testGetOrganizationPhoneWithExistingPhone(): void
  867. {
  868. $dolibarrSyncService = $this->getMockForMethod('getOrganizationPhone');
  869. $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
  870. $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
  871. $contactPoint3 = $this->getMockBuilder(ContactPoint::class)->getMock();
  872. $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER);
  873. $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::BILL);
  874. $contactPoint3->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL);
  875. $phone = $this->getMockBuilder(PhoneNumber::class)->disableOriginalConstructor()->getMock();
  876. $contactPoint2->method('getTelphone')->willReturn($phone);
  877. $organization = $this->getMockBuilder(Organization::class)->getMock();
  878. $organization
  879. ->expects($this->once())
  880. ->method('getContactPoints')
  881. ->willReturn(
  882. new ArrayCollection([$contactPoint1, $contactPoint2, $contactPoint3])
  883. );
  884. $dolibarrSyncService->expects(self::once())->method('formatPhoneNumber')->with($phone)->willReturn('+33161626365');
  885. $this->assertEquals(
  886. '+33161626365',
  887. $dolibarrSyncService->getOrganizationPhone($organization)
  888. );
  889. }
  890. /**
  891. * @see DolibarrSyncService::getOrganizationPhone()
  892. */
  893. public function testGetOrganizationPhoneWithMobilePhone()
  894. {
  895. $dolibarrSyncService = $this->getMockForMethod('getOrganizationPhone');
  896. $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
  897. $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
  898. $contactPoint3 = $this->getMockBuilder(ContactPoint::class)->getMock();
  899. $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER);
  900. $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::BILL);
  901. $contactPoint3->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL);
  902. $contactPoint2->expects($this->once())->method('getTelphone')->willReturn(null);
  903. $mobilePhone = $this->getMockBuilder(PhoneNumber::class)->disableOriginalConstructor()->getMock();
  904. $contactPoint2->method('getMobilPhone')->willReturn($mobilePhone);
  905. $organization = $this->getMockBuilder(Organization::class)->getMock();
  906. $organization
  907. ->expects($this->once())
  908. ->method('getContactPoints')
  909. ->willReturn(
  910. new ArrayCollection([$contactPoint1, $contactPoint2, $contactPoint3])
  911. );
  912. $dolibarrSyncService->expects(self::once())->method('formatPhoneNumber')->with($mobilePhone)->willReturn('+33661626365');
  913. $this->assertEquals(
  914. '+33661626365',
  915. $dolibarrSyncService->getOrganizationPhone($organization)
  916. );
  917. }
  918. /**
  919. * @see DolibarrSyncService::getOrganizationPhone()
  920. */
  921. public function testGetOrganizationPhoneWithNoPhone()
  922. {
  923. $dolibarrSyncService = $this->getMockForMethod('getOrganizationPhone');
  924. $organization = $this->getMockBuilder(Organization::class)->getMock();
  925. $organization
  926. ->expects($this->once())
  927. ->method('getContactPoints')
  928. ->willReturn(new ArrayCollection([]));
  929. $dolibarrSyncService->expects(self::never())->method('formatPhoneNumber');
  930. $this->assertEquals(
  931. null,
  932. $dolibarrSyncService->getOrganizationPhone($organization)
  933. );
  934. }
  935. /**
  936. * @see DolibarrSyncService::getOrganizationEmail()
  937. */
  938. public function testGetOrganizationEmailWithExistingEmail()
  939. {
  940. $dolibarrSyncService = $this->getMockForMethod('getOrganizationEmail');
  941. $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
  942. $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
  943. $contactPoint3 = $this->getMockBuilder(ContactPoint::class)->getMock();
  944. $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER);
  945. $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::BILL);
  946. $contactPoint3->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL);
  947. $contactPoint2->method('getEmail')->willReturn('email@email.com');
  948. $organization = $this->getMockBuilder(Organization::class)->getMock();
  949. $organization
  950. ->expects($this->once())
  951. ->method('getContactPoints')
  952. ->willReturn(
  953. new ArrayCollection([$contactPoint1, $contactPoint2, $contactPoint3])
  954. );
  955. $this->assertEquals(
  956. 'email@email.com',
  957. $dolibarrSyncService->getOrganizationEmail($organization)
  958. );
  959. }
  960. /**
  961. * @see DolibarrSyncService::getOrganizationEmail()
  962. */
  963. public function testGetOrganizationEmailWithNoEmail()
  964. {
  965. $dolibarrSyncService = $this->getMockForMethod('getOrganizationEmail');
  966. $organization = $this->getMockBuilder(Organization::class)->getMock();
  967. $organization
  968. ->expects($this->once())
  969. ->method('getContactPoints')
  970. ->willReturn(new ArrayCollection([]));
  971. $this->assertEquals(
  972. null,
  973. $dolibarrSyncService->getOrganizationEmail($organization)
  974. );
  975. }
  976. /**
  977. * @see DolibarrSyncService::getOrganizationNetworkId()
  978. */
  979. public function testGetOrganizationNetworkId()
  980. {
  981. $dolibarrSyncService = $this->getMockForMethod('getOrganizationNetworkId');
  982. $organization = $this->getMockBuilder(Organization::class)->getMock();
  983. $network = $this->getMockBuilder(Network::class)->getMock();
  984. $network->method('getId')->willReturn(3);
  985. $networkOrganization = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  986. $networkOrganization->method('getNetwork')->willReturn($network);
  987. $organization->method('getNetworkOrganizations')->willReturn(new ArrayCollection([$networkOrganization]));
  988. $this->assertEquals(
  989. 3,
  990. $dolibarrSyncService->getOrganizationNetworkId($organization)
  991. );
  992. }
  993. /**
  994. * @see DolibarrSyncService::getOrganizationNetworkId()
  995. */
  996. public function testGetOrganizationNetworkIdWithMultipleResult()
  997. {
  998. $dolibarrSyncService = $this->getMockForMethod('getOrganizationNetworkId');
  999. $network1 = $this->getMockBuilder(Network::class)->getMock();
  1000. $network1->method('getId')->willReturn(3);
  1001. $networkOrganization1 = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  1002. $networkOrganization1->method('getNetwork')->willReturn($network1);
  1003. $networkOrganization1->method('getEndDate')->willReturn(new \DateTime('2000-01-01'));
  1004. $network2 = $this->getMockBuilder(Network::class)->getMock();
  1005. $network2->method('getId')->willReturn(4);
  1006. $networkOrganization2 = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  1007. $networkOrganization2->method('getNetwork')->willReturn($network2);
  1008. $networkOrganization2->method('getEndDate')->willReturn(null);
  1009. $organization = $this->getMockBuilder(Organization::class)->getMock();
  1010. $organization->method('getNetworkOrganizations')->willReturn(
  1011. new ArrayCollection([$networkOrganization1, $networkOrganization2])
  1012. );
  1013. $this->assertEquals(
  1014. 4,
  1015. $dolibarrSyncService->getOrganizationNetworkId($organization)
  1016. );
  1017. }
  1018. /**
  1019. * @see DolibarrSyncService::getOrganizationNetworkId()
  1020. */
  1021. public function testGetOrganizationNetworkIdWithNoResult()
  1022. {
  1023. $dolibarrSyncService = $this->getMockForMethod('getOrganizationNetworkId');
  1024. $organization = $this->getMockBuilder(Organization::class)->getMock();
  1025. $network = $this->getMockBuilder(Network::class)->getMock();
  1026. $network->method('getId')->willReturn(3);
  1027. $networkOrganization = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  1028. $networkOrganization->method('getNetwork')->willReturn($network);
  1029. $networkOrganization->method('getEndDate')->willReturn(new \DateTime('2000-01-01'));
  1030. $organization->method('getNetworkOrganizations')->willReturn(new ArrayCollection([$networkOrganization]));
  1031. $this->assertEquals(
  1032. null,
  1033. $dolibarrSyncService->getOrganizationNetworkId($organization)
  1034. );
  1035. }
  1036. /**
  1037. * @see DolibarrSyncService::countWithMission()
  1038. */
  1039. public function testCountWithMission()
  1040. {
  1041. $dolibarrSyncService = $this->getMockForMethod('countWithMission');
  1042. $members = [
  1043. 123 => [FunctionEnum::PRESIDENT->value, FunctionEnum::TEACHER->value],
  1044. 124 => [FunctionEnum::TEACHER->value],
  1045. 125 => [FunctionEnum::STUDENT->value],
  1046. 126 => [FunctionEnum::TREASURER->value],
  1047. ];
  1048. $this->assertEquals(
  1049. 2,
  1050. $dolibarrSyncService->countWithMission([FunctionEnum::TEACHER->value], $members)
  1051. );
  1052. $this->assertEquals(
  1053. 3,
  1054. $dolibarrSyncService->countWithMission(
  1055. [FunctionEnum::TEACHER->value, FunctionEnum::TREASURER->value],
  1056. $members
  1057. )
  1058. );
  1059. $this->assertEquals(
  1060. 1,
  1061. $dolibarrSyncService->countWithMission([FunctionEnum::STUDENT->value], $members)
  1062. );
  1063. $this->assertEquals(
  1064. 0,
  1065. $dolibarrSyncService->countWithMission([FunctionEnum::ARCHIVIST->value], $members)
  1066. );
  1067. }
  1068. /**
  1069. * @see DolibarrSyncService::getPersonContact()
  1070. */
  1071. public function testGetPersonContact()
  1072. {
  1073. $dolibarrSyncService = $this->getMockForMethod('getPersonContact');
  1074. $person = $this->getMockBuilder(Person::class)->getMock();
  1075. $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
  1076. $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
  1077. $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER);
  1078. $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL);
  1079. $person->expects($this->once())->method('getContactPoints')->willReturn(new ArrayCollection([$contactPoint1, $contactPoint2]));
  1080. $this->assertEquals(
  1081. $contactPoint2,
  1082. $dolibarrSyncService->getPersonContact($person)
  1083. );
  1084. $person2 = $this->getMockBuilder(Person::class)->getMock();
  1085. $person2->expects($this->once())->method('getContactPoints')->willReturn(new ArrayCollection([]));
  1086. $this->assertEquals(
  1087. null,
  1088. $dolibarrSyncService->getPersonContact($person2)
  1089. );
  1090. }
  1091. /**
  1092. * @see DolibarrSyncService::formatContactPosition()
  1093. */
  1094. public function testFormatContactPosition()
  1095. {
  1096. $dolibarrSyncService = $this->getMockForMethod('formatContactPosition');
  1097. $this->translator->method('trans')->willReturnMap(
  1098. [
  1099. [FunctionEnum::PRESIDENT->value, ['gender' => 'X'], null, null, 'Président(e)'],
  1100. [FunctionEnum::PRESIDENT->value, ['gender' => 'M'], null, null, 'Président'],
  1101. [FunctionEnum::PRESIDENT->value, ['gender' => 'F'], null, null, 'Présidente'],
  1102. [FunctionEnum::DIRECTOR->value, ['gender' => 'X'], null, null, 'Directeur(ice)'],
  1103. [FunctionEnum::DIRECTOR->value, ['gender' => 'M'], null, null, 'Directeur'],
  1104. [FunctionEnum::DIRECTOR->value, ['gender' => 'F'], null, null, 'Directrice'],
  1105. [FunctionEnum::TEACHER->value, ['gender' => 'X'], null, null, 'Professeur(e)'],
  1106. [FunctionEnum::ARCHIVIST->value, ['gender' => 'X'], null, null, 'Archiviste'],
  1107. [FunctionEnum::TREASURER->value, ['gender' => 'X'], null, null, 'Trésorier(ère)'],
  1108. [FunctionEnum::ADMINISTRATIVE_STAFF->value, ['gender' => 'X'], null, null, 'Personnel administratif'],
  1109. ]
  1110. );
  1111. $this->assertEquals(
  1112. 'Président(e)',
  1113. $dolibarrSyncService->formatContactPosition([FunctionEnum::PRESIDENT->value])
  1114. );
  1115. $this->assertEquals(
  1116. 'Président',
  1117. $dolibarrSyncService->formatContactPosition([FunctionEnum::PRESIDENT->value], 'MISTER')
  1118. );
  1119. $this->assertEquals(
  1120. 'Présidente',
  1121. $dolibarrSyncService->formatContactPosition([FunctionEnum::PRESIDENT->value], 'MISS')
  1122. );
  1123. $this->assertEquals(
  1124. 'Présidente, Directrice',
  1125. $dolibarrSyncService->formatContactPosition(
  1126. [FunctionEnum::PRESIDENT->value, FunctionEnum::DIRECTOR->value],
  1127. 'MISS'
  1128. )
  1129. );
  1130. $this->assertEquals(
  1131. 'Président, Directeur',
  1132. $dolibarrSyncService->formatContactPosition(
  1133. [FunctionEnum::PRESIDENT->value, FunctionEnum::DIRECTOR->value, FunctionEnum::ADHERENT->value],
  1134. 'MISTER'
  1135. )
  1136. );
  1137. $this->assertEquals(
  1138. 'Président, Directeur',
  1139. $dolibarrSyncService->formatContactPosition(
  1140. [FunctionEnum::PRESIDENT->value, FunctionEnum::DIRECTOR->value, FunctionEnum::ADHERENT->value],
  1141. 'MISTER'
  1142. )
  1143. );
  1144. $this->assertEquals(
  1145. 'Président(e), Directeur(ice), Professeur(e), Archiviste, Trésorier(ère), Pers...',
  1146. $dolibarrSyncService->formatContactPosition(
  1147. [
  1148. FunctionEnum::PRESIDENT->value,
  1149. FunctionEnum::DIRECTOR->value,
  1150. FunctionEnum::TEACHER->value,
  1151. FunctionEnum::ARCHIVIST->value,
  1152. FunctionEnum::TREASURER->value,
  1153. FunctionEnum::ADMINISTRATIVE_STAFF->value,
  1154. ],
  1155. 'X'
  1156. )
  1157. );
  1158. }
  1159. /**
  1160. * @see DolibarrSyncService::formatPhoneNumber()
  1161. */
  1162. public function testFormatPhoneNumber()
  1163. {
  1164. $dolibarrSyncService = $this->getMockForMethod('formatPhoneNumber');
  1165. $phoneUtil = PhoneNumberUtil::getInstance();
  1166. $phoneNumber = $phoneUtil->parse('01 02 03 04 05', 'FR');
  1167. $this->assertEquals(
  1168. '+33102030405',
  1169. $dolibarrSyncService->formatPhoneNumber($phoneNumber)
  1170. );
  1171. }
  1172. public function testGetExpectedTagsFor()
  1173. {
  1174. $dolibarrSyncService = $this->getMockForMethod('getExpectedTagsFor');
  1175. $organization1 = $this->getMockBuilder(Organization::class)->getMock();
  1176. $organization1->method('getLegalStatus')->willReturn(LegalEnum::ASSOCIATION_LAW_1901);
  1177. $settings1 = $this->getMockBuilder(Settings::class)->getMock();
  1178. $settings1->method('getProduct')->willReturn(SettingsProductEnum::SCHOOL);
  1179. $organization1->method('getSettings')->willReturn($settings1);
  1180. $organization2 = $this->getMockBuilder(Organization::class)->getMock();
  1181. $organization2->method('getLegalStatus')->willReturn(LegalEnum::ASSOCIATION_LAW_1901);
  1182. $settings2 = $this->getMockBuilder(Settings::class)->getMock();
  1183. $settings2->method('getProduct')->willReturn(SettingsProductEnum::SCHOOL_PREMIUM);
  1184. $organization2->method('getSettings')->willReturn($settings2);
  1185. $organization3 = $this->getMockBuilder(Organization::class)->getMock();
  1186. $organization3->method('getLegalStatus')->willReturn(LegalEnum::LOCAL_AUTHORITY);
  1187. $settings3 = $this->getMockBuilder(Settings::class)->getMock();
  1188. $settings3->method('getProduct')->willReturn(SettingsProductEnum::SCHOOL);
  1189. $organization3->method('getSettings')->willReturn($settings3);
  1190. $organization4 = $this->getMockBuilder(Organization::class)->getMock();
  1191. $organization4->method('getLegalStatus')->willReturn(LegalEnum::LOCAL_AUTHORITY);
  1192. $settings4 = $this->getMockBuilder(Settings::class)->getMock();
  1193. $settings4->method('getProduct')->willReturn(SettingsProductEnum::SCHOOL_PREMIUM);
  1194. $organization4->method('getSettings')->willReturn($settings4);
  1195. $organization5 = $this->getMockBuilder(Organization::class)->getMock();
  1196. $organization5->method('getLegalStatus')->willReturn(LegalEnum::LOCAL_AUTHORITY);
  1197. $settings5 = $this->getMockBuilder(Settings::class)->getMock();
  1198. $settings5->method('getProduct')->willReturn(SettingsProductEnum::ARTIST);
  1199. $organization5->method('getSettings')->willReturn($settings5);
  1200. $this->assertEquals(
  1201. $dolibarrSyncService->getExpectedTagsFor($organization1),
  1202. [67]
  1203. );
  1204. $this->assertEquals(
  1205. $dolibarrSyncService->getExpectedTagsFor($organization2),
  1206. [69]
  1207. );
  1208. $this->assertEquals(
  1209. $dolibarrSyncService->getExpectedTagsFor($organization3),
  1210. [68]
  1211. );
  1212. $this->assertEquals(
  1213. $dolibarrSyncService->getExpectedTagsFor($organization4),
  1214. [70]
  1215. );
  1216. $this->assertEquals(
  1217. $dolibarrSyncService->getExpectedTagsFor($organization5),
  1218. []
  1219. );
  1220. }
  1221. /**
  1222. * @see DolibarrSyncService::validateResponse()
  1223. */
  1224. public function testValidateResponse(): void
  1225. {
  1226. $dolibarrSyncService = $this->getMockForMethod('validateResponse');
  1227. $response = $this->getMockBuilder(ResponseInterface::class)->getMock();
  1228. $response->method('toArray')->willReturn(['a' => 1]);
  1229. $operation = $this->getMockBuilder(CreateOperation::class)->disableOriginalConstructor()->getMock();
  1230. $operation->method('getData')->willReturn(['a' => 1]);
  1231. $dolibarrSyncService->expects(self::exactly(2))->method('sanitizeDolibarrData')->with(['a' => 1])->willReturn(['a' => 1]);
  1232. $this->arrayUtils->expects(self::once())->method('getChanges')->with(['a' => 1], ['a' => 1], true)->willReturn([]);
  1233. $dolibarrSyncService->validateResponse($response, $operation);
  1234. }
  1235. /**
  1236. * @see DolibarrSyncService::validateResponse()
  1237. */
  1238. public function testValidateResponseInvalid(): void
  1239. {
  1240. $dolibarrSyncService = $this->getMockForMethod('validateResponse');
  1241. $response = $this->getMockBuilder(ResponseInterface::class)->getMock();
  1242. $response->method('toArray')->willReturn(['a' => 1]);
  1243. $operation = $this->getMockBuilder(CreateOperation::class)->disableOriginalConstructor()->getMock();
  1244. $operation->method('getData')->willReturn(['a' => 0]);
  1245. $dolibarrSyncService->expects(self::exactly(2))
  1246. ->method('sanitizeDolibarrData')
  1247. ->willReturnMap([
  1248. [['a' => 1], ['a' => 1]],
  1249. [['a' => 0], ['a' => 0]],
  1250. ]);
  1251. $this->arrayUtils->expects(self::once())->method('getChanges')->with(['a' => 1], ['a' => 0], true)->willReturn(['a' => 0]);
  1252. $this->expectException(\RuntimeException::class);
  1253. $dolibarrSyncService->validateResponse($response, $operation);
  1254. }
  1255. /**
  1256. * @see DolibarrSyncService::validateResponse()
  1257. */
  1258. public function testValidateResponseRequestError(): void
  1259. {
  1260. $dolibarrSyncService = $this->getMockForMethod('validateResponse');
  1261. $response = $this->getMockBuilder(ResponseInterface::class)->getMock();
  1262. $response->method('getInfo')->willReturnMap([
  1263. ['http_code', '200'], ['url', 'http://url.com'], ['response_headers', []],
  1264. ]);
  1265. $response->method('getContent')->willReturn('');
  1266. $response->method('toArray')->willThrowException(new ServerException($response));
  1267. $operation = $this->getMockBuilder(CreateOperation::class)->disableOriginalConstructor()->getMock();
  1268. $operation->method('getData')->willReturn(['a' => 0]);
  1269. $dolibarrSyncService->expects(self::never())->method('sanitizeDolibarrData');
  1270. $this->arrayUtils->expects(self::never())->method('getChanges');
  1271. $this->expectException(\RuntimeException::class);
  1272. $dolibarrSyncService->validateResponse($response, $operation);
  1273. }
  1274. }