DolibarrSyncServiceTest.php 56 KB

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