DolibarrSyncServiceTest.php 58 KB

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