DolibarrSyncServiceTest.php 58 KB

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