DolibarrSyncServiceTest.php 54 KB

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