DolibarrSyncServiceTest.php 58 KB

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