DolibarrSyncServiceTest.php 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406
  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. ->method('getOrganizationEmail')
  328. ->willReturnMap([
  329. [$organization1, $organizationData1['email']],
  330. [$organization2, $organizationData2['email']],
  331. ]);
  332. $dolibarrSyncService
  333. ->method('getOrganizationPhone')
  334. ->willReturnMap([
  335. [$organization1, $organizationData1['phone']],
  336. [$organization2, $organizationData2['phone']],
  337. ]);
  338. $dolibarrSyncService
  339. ->expects(self::exactly(2))
  340. ->method('getOrganizationNetworkId')
  341. ->willReturnMap([
  342. [$organization1, $organizationData1['networkId']],
  343. [$organization2, $organizationData2['networkId']],
  344. ]);
  345. $settings1 = $this->getMockBuilder(Settings::class)->getMock();
  346. $settings1->method('getProduct')->willReturn($organizationData1['product']);
  347. $organization1->method('getSettings')->willReturn($settings1);
  348. $settings2 = $this->getMockBuilder(Settings::class)->getMock();
  349. $settings2->method('getProduct')->willReturn($organizationData2['product']);
  350. $organization2->method('getSettings')->willReturn($settings2);
  351. $this->organizationUtils->method('isSchool')->willReturnMap([
  352. [$organization1, true],
  353. [$organization2, false]
  354. ]);
  355. $this->organizationUtils->method('isArtist')->willReturnMap([
  356. [$organization1, false],
  357. [$organization2, true]
  358. ]);
  359. $dolibarrSyncService->method('countWithMission')->willReturnMap([
  360. [[FunctionEnum::STUDENT->value], $activeMembers1, 1],
  361. [[FunctionEnum::ADHERENT->value], $activeMembers1, 2],
  362. [[FunctionEnum::ADHERENT->value], $activeMembers2, 0],
  363. [[FunctionEnum::DIRECTOR->value, FunctionEnum::PRESIDENT->value], $activeMembers1, 1],
  364. [[FunctionEnum::DIRECTOR->value, FunctionEnum::PRESIDENT->value], $activeMembers2, 2]
  365. ]);
  366. $this->arrayUtils
  367. ->expects(self::exactly(3))
  368. ->method('getChanges')
  369. ->willReturnCallback(
  370. function (array $initialArray, array $newArray, bool $recursive = false, ?callable $callback = null) {
  371. if (in_array('name', $newArray, true)) {
  372. // Organization 1 name is already defined and has not been changed
  373. unset($newArray['name']);
  374. }
  375. if (in_array('statut', $newArray, true)) {
  376. // Contact 1 statut is already defined and has not been changed
  377. unset($newArray['statut']);
  378. }
  379. return $newArray;
  380. }
  381. );
  382. $this->dolibarrApiService->method('getContacts')->willReturnMap([
  383. [$socId1, $dolibarrSocietyContacts1],
  384. [$socId2, $dolibarrSocietyContacts2]
  385. ]);
  386. // Loop over contacts
  387. // NB: Student will be skipped since it has no office role
  388. $person1 = $this->getMockBuilder(Person::class)->getMock();
  389. $person1->method('getId')->willReturn($personId1);
  390. $person1->method('getName')->willReturn($personData1['name']);
  391. $person1->method('getGivenName')->willReturn($personData1['givenName']);
  392. $person1->method('getGender')->willReturn($personData1['gender']);
  393. $person2 = $this->getMockBuilder(Person::class)->getMock();
  394. $person2->method('getId')->willReturn($personId2);
  395. $person2->method('getName')->willReturn($personData2['name']);
  396. $person2->method('getGivenName')->willReturn($personData2['givenName']);
  397. $person2->method('getGender')->willReturn($personData2['gender']);
  398. // An invalid person that should be ignored
  399. $person5 = $this->getMockBuilder(Person::class)->getMock();
  400. $person5->method('getId')->willReturn($personId5);
  401. $person5->method('getName')->willReturn($personData5['name']);
  402. $person5->method('getGivenName')->willReturn($personData5['givenName']);
  403. $access1 = $this->getMockBuilder(Access::class)->getMock();
  404. $access1->method('getPerson')->willReturn($person1);
  405. $access2 = $this->getMockBuilder(Access::class)->getMock();
  406. $access2->method('getPerson')->willReturn($person2);
  407. $access5 = $this->getMockBuilder(Access::class)->getMock();
  408. $access5->method('getPerson')->willReturn($person5);
  409. $this->accessRepository->method('find')->willReturnMap([
  410. [$accessId1, null, null, $access1],
  411. [$accessId2, null, null, $access2],
  412. [$accessId5, null, null, $access5],
  413. ]);
  414. $dolibarrSyncService->method('findDolibarrContactFor')->willReturnMap([
  415. [$dolibarrSocietyContacts1, $person1, $contactData1],
  416. [$dolibarrSocietyContacts2, $person2, null]
  417. ]);
  418. $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
  419. $contactPoint1->method('getEmail')->willReturn($personData1['email']);
  420. $phone = $this->getMockBuilder(PhoneNumber::class)->getMock();
  421. $mobilePhone = $this->getMockBuilder(PhoneNumber::class)->getMock();
  422. $dolibarrSyncService->method('formatPhoneNumber')->willReturnMap([
  423. [$phone, $personData1['phone']],
  424. [$mobilePhone, $personData1['mobilePhone']],
  425. ]);
  426. $contactPoint1->method('getTelphone')->willReturn($phone);
  427. $contactPoint1->method('getMobilPhone')->willReturn($mobilePhone);
  428. $dolibarrSyncService->method('getPersonContact')->willReturnMap([
  429. [$person1, $contactPoint1],
  430. [$person2, null],
  431. ]);
  432. $dolibarrSyncService->method('formatContactPosition')->willReturnMap([
  433. [[FunctionEnum::PRESIDENT->value], GenderEnum::MISTER->value, 'Président'],
  434. [[FunctionEnum::PRESIDENT->value], null, 'Président(e)'],
  435. ]);
  436. // Expected logged error messages
  437. $this->logger->expects(self::exactly(2))->method('error')->withConsecutive(
  438. ["Person 900 miss a lastname and/or a firstname, ignored."],
  439. ["Organization 30 not found in the Opentalent DB"],
  440. );
  441. // Tags
  442. $this->dolibarrApiService->method('getSocietyTagsIds')->willReturnMap([
  443. [$socId1, [1, 68]],
  444. [$socId2, [3, 67]]
  445. ]);
  446. $dolibarrSyncService->method('getExpectedTagsFor')->willReturnMap([
  447. [$organization1, [67]],
  448. [$organization2, []]
  449. ]);
  450. // Expected progression callback triggers
  451. $progressionCallbackExpectedCalls = [[1, 3], [2, 3], [3, 3]];
  452. $progressionCallback = static function ($i, $total) use (&$progressionCallbackExpectedCalls) {
  453. [$expectedI, $expectedTotal] = array_shift($progressionCallbackExpectedCalls);
  454. if ($i !== $expectedI || $total !== $expectedTotal) {
  455. throw new \AssertionError(
  456. 'Progression callback error, expected parameters are (' . $expectedI . ',' . $expectedTotal . ')' .
  457. ', got (' . $i . ', ' . $total . ')'
  458. );
  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. 'name : (new) => `Organization 20`',
  515. 'parent : (new) => ``',
  516. 'status : (new) => `1`',
  517. 'array_options.options_2iopen_software_opentalent : (new) => `Opentalent Artist`',
  518. "array_options.options_2iopeninfoopentalent : (new) => `Nombre d'adhérents : 0\nNombre d'accès admin : 2`",
  519. ],
  520. $operations[5]->getChangeLog()
  521. );
  522. $this->assertEqualsCanonicalizing(
  523. [
  524. '[POST contacts]',
  525. 'civility_code : (new) => ``',
  526. 'lastname : (new) => `Simpson`',
  527. 'firstname : (new) => `Lisa`',
  528. 'email : (new) => ``',
  529. 'phone_pro : (new) => ``',
  530. 'phone_mobile : (new) => ``',
  531. 'poste : (new) => `Président(e)`',
  532. 'statut : (new) => `1`',
  533. 'array_options.options_2iopen_person_id : (new) => `200`',
  534. 'socid : (new) => `2`'
  535. ],
  536. $operations[6]->getChangeLog()
  537. );
  538. $this->assertEqualsCanonicalizing(
  539. [
  540. '[DELETE /thirdparties/2/categories/67]'
  541. ],
  542. $operations[7]->getChangeLog()
  543. );
  544. $this->assertCount(0, $progressionCallbackExpectedCalls);
  545. }
  546. /**
  547. * All valid operations shall be executed
  548. * If an operation is not in status READY, a warning shall be logged, and the operation shall be skipped
  549. *
  550. * @see DolibarrSyncService::execute()
  551. */
  552. public function testExecute(): void
  553. {
  554. $dolibarrSyncService = $this->getMockForMethod('execute');
  555. $progressionCallbackExpectedCalls = [[1, 3], [2, 3], [3, 3]];
  556. $progressionCallback = static function ($i, $total) use (&$progressionCallbackExpectedCalls) {
  557. [$expectedI, $expectedTotal] = array_shift($progressionCallbackExpectedCalls);
  558. if ($i !== $expectedI || $total !== $expectedTotal) {
  559. throw new \AssertionError(
  560. 'Progression callback error, expected parameters are (' . $expectedI . ',' . $expectedTotal . ')' .
  561. ', got (' . $i . ', ' . $total . ')'
  562. );
  563. }
  564. };
  565. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  566. $operation1 = $this->getMockBuilder(UpdateOperation::class)->disableOriginalConstructor()->getMock();
  567. $operation1->method('getStatus')->willReturnOnConsecutiveCalls(BaseRestOperation::STATUS_READY, BaseRestOperation::STATUS_DONE);
  568. $operation1->method('getChangeLog')->willReturn(['foo']);
  569. $operation1->expects(self::once())->method('execute')->willReturn($response);
  570. $dolibarrSyncService->method('validateResponse')->with($response, $operation1)->willThrowException(new RuntimeException());
  571. $operation2 = $this->getMockBuilder(CreateOperation::class)->disableOriginalConstructor()->getMock();
  572. $operation2->method('getStatus')->willReturn(
  573. BaseRestOperation::STATUS_READY, BaseRestOperation::STATUS_ERROR, BaseRestOperation::STATUS_ERROR // An error happened
  574. );
  575. $operation2->expects(self::once())->method('execute');
  576. $operation3 = $this->getMockBuilder(DeleteOperation::class)->disableOriginalConstructor()->getMock();
  577. $operation3->method('getStatus')->willReturn(BaseRestOperation::STATUS_DONE); // Invalid status, should log a warning and not execute
  578. $operation3->expects(self::never())->method('execute');
  579. $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
  580. $this->logger->expects(self::exactly(3))->method('error'); // The exception thrown during the execution of the Delete op will log 3 errors
  581. $dolibarrSyncService->execute([$operation1, $operation2, $operation3], $progressionCallback);
  582. }
  583. /**
  584. * @see DolibarrSyncService::run()
  585. */
  586. public function testRun() {
  587. $dolibarrSyncService = $this->getMockForMethod('run');
  588. $operations = ['operation1', 'operation2'];
  589. $dolibarrSyncService->expects(self::once())->method('scan')->willReturn($operations);
  590. $dolibarrSyncService->expects(self::once())->method('execute')->with($operations);
  591. $result = $dolibarrSyncService->run();
  592. $this->assertEquals($operations, $result);
  593. }
  594. /**
  595. * @see DolibarrSyncService::getDolibarrSocietiesIndex()
  596. */
  597. public function testGetDolibarrSocietiesIndex(): void
  598. {
  599. $dolibarrSyncService = $this->getMockForMethod('getDolibarrSocietiesIndex');
  600. $this->dolibarrApiService
  601. ->expects($this->once())
  602. ->method('getAllClients')
  603. ->willReturn(
  604. [
  605. ['id' => 1, 'array_options' => ['options_2iopen_organization_id' => 101]],
  606. ['id' => 2, 'array_options' => ['options_2iopen_organization_id' => 102]],
  607. ['id' => 3, 'array_options' => ['options_2iopen_organization_id' => null]], // No org id but also no contract, so it's ok
  608. ['id' => 4, 'name' => 'foo', 'array_options' => ['options_2iopen_organization_id' => null]], // No org id but has a contract, a warning should be logged
  609. ]
  610. );
  611. $this->dolibarrApiService->expects(self::exactly(2))->method('getActiveContract')->willReturnMap([
  612. [3, null],
  613. [4, ['dummy non-empty data']]
  614. ]);
  615. $this->logger->expects(self::once())->method('warning')->with('Dolibarr client has no organization id: foo (4)');
  616. $index = $dolibarrSyncService->getDolibarrSocietiesIndex();
  617. $this->assertEqualsCanonicalizing(
  618. [
  619. 1 => ['id' => 1, 'array_options' => ['options_2iopen_organization_id' => 101]],
  620. 2 => ['id' => 2, 'array_options' => ['options_2iopen_organization_id' => 102]]
  621. ],
  622. $index
  623. );
  624. }
  625. /**
  626. * @see DolibarrSyncService::getActiveMembersIndex()
  627. */
  628. public function testGetActiveMembersIndex(): void
  629. {
  630. $dolibarrSyncService = $this->getMockForMethod('getActiveMembersIndex');
  631. $this->accessRepository
  632. ->expects($this->once())
  633. ->method('getAllActiveMembersAndMissions')
  634. ->willReturn(
  635. [
  636. ['id' => 1, 'organization_id' => 1, 'mission' => FunctionEnum::PRESIDENT],
  637. ['id' => 2, 'organization_id' => 1, 'mission' => FunctionEnum::STUDENT],
  638. ['id' => 3, 'organization_id' => 2, 'mission' => FunctionEnum::PRESIDENT],
  639. ['id' => 3, 'organization_id' => 2, 'mission' => FunctionEnum::TEACHER]
  640. ]
  641. );
  642. $index = $dolibarrSyncService->getActiveMembersIndex();
  643. $this->assertEqualsCanonicalizing([
  644. 1 => [1 => [FunctionEnum::PRESIDENT->value], 2 => [FunctionEnum::STUDENT->value]],
  645. 2 => [3 => [FunctionEnum::PRESIDENT->value, FunctionEnum::TEACHER->value]]
  646. ], $index);
  647. }
  648. /**
  649. * @see DolibarrSyncService::findDolibarrContactFor()
  650. */
  651. public function testFindDolibarrContactForById(): void
  652. {
  653. $dolibarrSyncService = $this->getMockForMethod('findDolibarrContactFor');
  654. $contacts = [
  655. ['id' => 1, 'array_options' => ['options_2iopen_person_id' => 101]],
  656. ['id' => 2, 'array_options' => ['options_2iopen_person_id' => 102]],
  657. ];
  658. // Find by id
  659. $person = $this->getMockBuilder(Person::class)->getMock();
  660. $person->method('getId')->willReturn(102);
  661. $contact = $dolibarrSyncService->findDolibarrContactFor($contacts, $person);
  662. $this->assertEquals(2, $contact['id']);
  663. }
  664. /**
  665. * @see DolibarrSyncService::findDolibarrContactFor()
  666. */
  667. public function testFindDolibarrContactForByName(): void
  668. {
  669. $dolibarrSyncService = $this->getMockForMethod('findDolibarrContactFor');
  670. $contacts = [
  671. ['id' => 1, 'firstname' => 'mister', 'lastname' => 'X', 'array_options' => ['options_2iopen_person_id' => null]],
  672. ['id' => 2, 'firstname' => 'miss', 'lastname' => 'Y', 'array_options' => ['options_2iopen_person_id' => null]],
  673. ];
  674. // Find by full name (contact has no person id, it should be returned)
  675. $person = $this->getMockBuilder(Person::class)->getMock();
  676. $person->method('getId')->willReturn(101);
  677. $person->method('getName')->willReturn('X');
  678. $person->method('getGivenName')->willReturn('mister');
  679. $result = $dolibarrSyncService->findDolibarrContactFor($contacts, $person);
  680. $this->assertEquals(1, $result['id']);
  681. }
  682. /**
  683. * @see DolibarrSyncService::findDolibarrContactFor()
  684. */
  685. public function testFindDolibarrContactForByNameWithConflict(): void
  686. {
  687. $dolibarrSyncService = $this->getMockForMethod('findDolibarrContactFor');
  688. $contacts = [
  689. ['id' => 1, 'firstname' => 'mister', 'lastname' => 'X', 'array_options' => ['options_2iopen_person_id' => 1]],
  690. ['id' => 2, 'firstname' => 'miss', 'lastname' => 'Y', 'array_options' => ['options_2iopen_person_id' => 2]],
  691. ];
  692. // Find by full name (contact already has another person id, it should not be returned)
  693. $person = $this->getMockBuilder(Person::class)->getMock();
  694. $person->method('getId')->willReturn(101);
  695. $person->method('getName')->willReturn('X');
  696. $person->method('getGivenName')->willReturn('mister');
  697. $result = $dolibarrSyncService->findDolibarrContactFor($contacts, $person);
  698. $this->assertEquals(null, $result);
  699. }
  700. /**
  701. * @see DolibarrSyncService::findDolibarrContactFor()
  702. */
  703. public function testFindDolibarrContactNotFound(): void
  704. {
  705. $dolibarrSyncService = $this->getMockForMethod('findDolibarrContactFor');
  706. $contacts = [
  707. ['id' => 1, 'firstname' => 'mister', 'lastname' => 'X', 'array_options' => ['options_2iopen_person_id' => 1]],
  708. ['id' => 2, 'firstname' => 'miss', 'lastname' => 'Y', 'array_options' => ['options_2iopen_person_id' => 2]],
  709. ];
  710. // Do not find
  711. $person = $this->getMockBuilder(Person::class)->getMock();
  712. $person->method('getId')->willReturn(-1);
  713. $person->method('getName')->willReturn('Presley');
  714. $person->method('getGivenName')->willReturn('Elvis');
  715. $result = $dolibarrSyncService->findDolibarrContactFor($contacts, $person);
  716. $this->assertEquals(null, $result);
  717. }
  718. /**
  719. * @see DolibarrSyncService::sanitizeDolibarrData()
  720. */
  721. public function testSanitizeDolibarrData(): void
  722. {
  723. $dolibarrSyncService = $this->getMockForMethod('sanitizeDolibarrData');
  724. $result = $dolibarrSyncService->sanitizeDolibarrData(['a' => 'A', 'b' => '', 'c' => ['d' => 'D', 'e' => '']]);
  725. $this->assertEquals(
  726. ['a' => 'A', 'b' => null, 'c' => ['d' => 'D', 'e' => null]],
  727. $result
  728. );
  729. }
  730. /**
  731. * @see DolibarrSyncService::sanitizeDolibarrData()
  732. */
  733. public function testSanitizeDolibarrDataWithNull(): void
  734. {
  735. $dolibarrSyncService = $this->getMockForMethod('sanitizeDolibarrData');
  736. $result = $dolibarrSyncService->sanitizeDolibarrData(null);
  737. $this->assertEquals(null, $result);
  738. }
  739. /**
  740. * @see DolibarrSyncService::getOrganizationPostalAddress()
  741. */
  742. public function testGetOrganizationPostalAddress(): void
  743. {
  744. $dolibarrSyncService = $this->getMockForMethod('getOrganizationPostalAddress');
  745. $organization = $this->getMockBuilder(Organization::class)->getMock();
  746. $organizationAddressPostal1 = $this->getMockBuilder(OrganizationAddressPostal::class)->getMock();
  747. $organizationAddressPostal2 = $this->getMockBuilder(OrganizationAddressPostal::class)->getMock();
  748. $organizationAddressPostal3 = $this->getMockBuilder(OrganizationAddressPostal::class)->getMock();
  749. $addressPostal = $this->getMockBuilder(AddressPostal::class)->getMock();
  750. $organizationAddressPostal1->method('getType')->willReturn(AddressPostalOrganizationTypeEnum::ADDRESS_PRACTICE);
  751. $organizationAddressPostal2->method('getType')->willReturn(AddressPostalOrganizationTypeEnum::ADDRESS_BILL);
  752. $organizationAddressPostal3->method('getType')->willReturn(AddressPostalOrganizationTypeEnum::ADDRESS_OTHER);
  753. $organizationAddressPostal2->method('getAddressPostal')->willReturn($addressPostal);
  754. $organization->expects($this->once())
  755. ->method('getOrganizationAddressPostals')
  756. ->willReturn(
  757. new ArrayCollection([$organizationAddressPostal1, $organizationAddressPostal2, $organizationAddressPostal3])
  758. );
  759. $this->assertEquals(
  760. $addressPostal,
  761. $dolibarrSyncService->getOrganizationPostalAddress($organization)
  762. );
  763. }
  764. /**
  765. * @see DolibarrSyncService::getOrganizationPostalAddress()
  766. */
  767. public function testGetOrganizationPostalAddressNoResult(): void
  768. {
  769. $dolibarrSyncService = $this->getMockForMethod('getOrganizationPostalAddress');
  770. $organization = $this->getMockBuilder(Organization::class)->getMock();
  771. $organization->expects($this->once())
  772. ->method('getOrganizationAddressPostals')
  773. ->willReturn(new ArrayCollection([]));
  774. $this->assertEquals(
  775. null,
  776. $dolibarrSyncService->getOrganizationPostalAddress($organization)
  777. );
  778. }
  779. /**
  780. * @see DolibarrSyncService::getOrganizationPhone()
  781. */
  782. public function testGetOrganizationPhoneWithExistingPhone(): void
  783. {
  784. $dolibarrSyncService = $this->getMockForMethod('getOrganizationPhone');
  785. $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
  786. $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
  787. $contactPoint3 = $this->getMockBuilder(ContactPoint::class)->getMock();
  788. $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER);
  789. $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::BILL);
  790. $contactPoint3->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL);
  791. $phone = $this->getMockBuilder(PhoneNumber::class)->disableOriginalConstructor()->getMock();
  792. $contactPoint2->method('getTelphone')->willReturn($phone);
  793. $organization = $this->getMockBuilder(Organization::class)->getMock();
  794. $organization
  795. ->expects($this->once())
  796. ->method('getContactPoints')
  797. ->willReturn(
  798. new ArrayCollection([$contactPoint1, $contactPoint2, $contactPoint3])
  799. );
  800. $dolibarrSyncService->expects(self::once())->method('formatPhoneNumber')->with($phone)->willReturn('+33161626365');
  801. $this->assertEquals(
  802. '+33161626365',
  803. $dolibarrSyncService->getOrganizationPhone($organization)
  804. );
  805. }
  806. /**
  807. * @see DolibarrSyncService::getOrganizationPhone()
  808. */
  809. public function testGetOrganizationPhoneWithMobilePhone()
  810. {
  811. $dolibarrSyncService = $this->getMockForMethod('getOrganizationPhone');
  812. $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
  813. $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
  814. $contactPoint3 = $this->getMockBuilder(ContactPoint::class)->getMock();
  815. $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER);
  816. $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::BILL);
  817. $contactPoint3->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL);
  818. $contactPoint2->expects($this->once())->method('getTelphone')->willReturn(null);
  819. $mobilePhone = $this->getMockBuilder(PhoneNumber::class)->disableOriginalConstructor()->getMock();
  820. $contactPoint2->method('getMobilPhone')->willReturn($mobilePhone);
  821. $organization = $this->getMockBuilder(Organization::class)->getMock();
  822. $organization
  823. ->expects($this->once())
  824. ->method('getContactPoints')
  825. ->willReturn(
  826. new ArrayCollection([$contactPoint1, $contactPoint2, $contactPoint3])
  827. );
  828. $dolibarrSyncService->expects(self::once())->method('formatPhoneNumber')->with($mobilePhone)->willReturn('+33661626365');
  829. $this->assertEquals(
  830. '+33661626365',
  831. $dolibarrSyncService->getOrganizationPhone($organization)
  832. );
  833. }
  834. /**
  835. * @see DolibarrSyncService::getOrganizationPhone()
  836. */
  837. public function testGetOrganizationPhoneWithNoPhone() {
  838. $dolibarrSyncService = $this->getMockForMethod('getOrganizationPhone');
  839. $organization = $this->getMockBuilder(Organization::class)->getMock();
  840. $organization
  841. ->expects($this->once())
  842. ->method('getContactPoints')
  843. ->willReturn(new ArrayCollection([]));
  844. $dolibarrSyncService->expects(self::never())->method('formatPhoneNumber');
  845. $this->assertEquals(
  846. null,
  847. $dolibarrSyncService->getOrganizationPhone($organization)
  848. );
  849. }
  850. /**
  851. * @see DolibarrSyncService::getOrganizationEmail()
  852. */
  853. public function testGetOrganizationEmailWithExistingEmail() {
  854. $dolibarrSyncService = $this->getMockForMethod('getOrganizationEmail');
  855. $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
  856. $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
  857. $contactPoint3 = $this->getMockBuilder(ContactPoint::class)->getMock();
  858. $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER);
  859. $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::BILL);
  860. $contactPoint3->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL);
  861. $contactPoint2->method('getEmail')->willReturn('email@email.com');
  862. $organization = $this->getMockBuilder(Organization::class)->getMock();
  863. $organization
  864. ->expects($this->once())
  865. ->method('getContactPoints')
  866. ->willReturn(
  867. new ArrayCollection([$contactPoint1, $contactPoint2, $contactPoint3])
  868. );
  869. $this->assertEquals(
  870. 'email@email.com',
  871. $dolibarrSyncService->getOrganizationEmail($organization)
  872. );
  873. }
  874. /**
  875. * @see DolibarrSyncService::getOrganizationEmail()
  876. */
  877. public function testGetOrganizationEmailWithNoEmail() {
  878. $dolibarrSyncService = $this->getMockForMethod('getOrganizationEmail');
  879. $organization = $this->getMockBuilder(Organization::class)->getMock();
  880. $organization
  881. ->expects($this->once())
  882. ->method('getContactPoints')
  883. ->willReturn(new ArrayCollection([]));
  884. $this->assertEquals(
  885. null,
  886. $dolibarrSyncService->getOrganizationEmail($organization)
  887. );
  888. }
  889. /**
  890. * @see DolibarrSyncService::getOrganizationNetworkId()
  891. */
  892. public function testGetOrganizationNetworkId() {
  893. $dolibarrSyncService = $this->getMockForMethod('getOrganizationNetworkId');
  894. $organization = $this->getMockBuilder(Organization::class)->getMock();
  895. $network = $this->getMockBuilder(Network::class)->getMock();
  896. $network->method('getId')->willReturn(3);
  897. $networkOrganization = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  898. $networkOrganization->method('getNetwork')->willReturn($network);
  899. $organization->method('getNetworkOrganizations')->willReturn(new ArrayCollection([$networkOrganization]));
  900. $this->assertEquals(
  901. 3,
  902. $dolibarrSyncService->getOrganizationNetworkId($organization)
  903. );
  904. }
  905. /**
  906. * @see DolibarrSyncService::getOrganizationNetworkId()
  907. */
  908. public function testGetOrganizationNetworkIdWithMultipleResult() {
  909. $dolibarrSyncService = $this->getMockForMethod('getOrganizationNetworkId');
  910. $network1 = $this->getMockBuilder(Network::class)->getMock();
  911. $network1->method('getId')->willReturn(3);
  912. $networkOrganization1 = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  913. $networkOrganization1->method('getNetwork')->willReturn($network1);
  914. $networkOrganization1->method('getEndDate')->willReturn(new \DateTime('2000-01-01'));
  915. $network2 = $this->getMockBuilder(Network::class)->getMock();
  916. $network2->method('getId')->willReturn(4);
  917. $networkOrganization2 = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  918. $networkOrganization2->method('getNetwork')->willReturn($network2);
  919. $networkOrganization2->method('getEndDate')->willReturn(null);
  920. $organization = $this->getMockBuilder(Organization::class)->getMock();
  921. $organization->method('getNetworkOrganizations')->willReturn(
  922. new ArrayCollection([$networkOrganization1, $networkOrganization2])
  923. );
  924. $this->assertEquals(
  925. 4,
  926. $dolibarrSyncService->getOrganizationNetworkId($organization)
  927. );
  928. }
  929. /**
  930. * @see DolibarrSyncService::getOrganizationNetworkId()
  931. */
  932. public function testGetOrganizationNetworkIdWithNoResult() {
  933. $dolibarrSyncService = $this->getMockForMethod('getOrganizationNetworkId');
  934. $organization = $this->getMockBuilder(Organization::class)->getMock();
  935. $network = $this->getMockBuilder(Network::class)->getMock();
  936. $network->method('getId')->willReturn(3);
  937. $networkOrganization = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  938. $networkOrganization->method('getNetwork')->willReturn($network);
  939. $networkOrganization->method('getEndDate')->willReturn(new \DateTime('2000-01-01'));
  940. $organization->method('getNetworkOrganizations')->willReturn(new ArrayCollection([$networkOrganization]));
  941. $this->assertEquals(
  942. null,
  943. $dolibarrSyncService->getOrganizationNetworkId($organization)
  944. );
  945. }
  946. /**
  947. * @see DolibarrSyncService::countWithMission()
  948. */
  949. public function testCountWithMission() {
  950. $dolibarrSyncService = $this->getMockForMethod('countWithMission');
  951. $members = [
  952. 123 => [FunctionEnum::PRESIDENT->value, FunctionEnum::TEACHER->value],
  953. 124 => [FunctionEnum::TEACHER->value],
  954. 125 => [FunctionEnum::STUDENT->value],
  955. 126 => [FunctionEnum::TREASURER->value],
  956. ];
  957. $this->assertEquals(
  958. 2,
  959. $dolibarrSyncService->countWithMission([FunctionEnum::TEACHER->value], $members)
  960. );
  961. $this->assertEquals(
  962. 3,
  963. $dolibarrSyncService->countWithMission(
  964. [FunctionEnum::TEACHER->value, FunctionEnum::TREASURER->value],
  965. $members
  966. )
  967. );
  968. $this->assertEquals(
  969. 1,
  970. $dolibarrSyncService->countWithMission([FunctionEnum::STUDENT->value], $members)
  971. );
  972. $this->assertEquals(
  973. 0,
  974. $dolibarrSyncService->countWithMission([FunctionEnum::ARCHIVIST->value], $members)
  975. );
  976. }
  977. /**
  978. * @see DolibarrSyncService::getPersonContact()
  979. */
  980. public function testGetPersonContact() {
  981. $dolibarrSyncService = $this->getMockForMethod('getPersonContact');
  982. $person = $this->getMockBuilder(Person::class)->getMock();
  983. $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
  984. $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
  985. $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER);
  986. $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL);
  987. $person->expects($this->once())->method('getContactPoints')->willReturn(new ArrayCollection([$contactPoint1, $contactPoint2]));
  988. $this->assertEquals(
  989. $contactPoint2,
  990. $dolibarrSyncService->getPersonContact($person)
  991. );
  992. $person2 = $this->getMockBuilder(Person::class)->getMock();
  993. $person2->expects($this->once())->method('getContactPoints')->willReturn(new ArrayCollection([]));
  994. $this->assertEquals(
  995. null,
  996. $dolibarrSyncService->getPersonContact($person2)
  997. );
  998. }
  999. /**
  1000. * @see DolibarrSyncService::formatContactPosition()
  1001. */
  1002. public function testFormatContactPosition() {
  1003. $dolibarrSyncService = $this->getMockForMethod('formatContactPosition');
  1004. $this->translator->method('trans')->willReturnMap(
  1005. [
  1006. [FunctionEnum::PRESIDENT->value, ['gender' => 'X'], null, null, 'Président(e)'],
  1007. [FunctionEnum::PRESIDENT->value, ['gender' => 'M'], null, null, 'Président'],
  1008. [FunctionEnum::PRESIDENT->value, ['gender' => 'F'], null, null, 'Présidente'],
  1009. [FunctionEnum::DIRECTOR->value, ['gender' => 'X'], null, null, 'Directeur(ice)'],
  1010. [FunctionEnum::DIRECTOR->value, ['gender' => 'M'], null, null, 'Directeur'],
  1011. [FunctionEnum::DIRECTOR->value, ['gender' => 'F'], null, null, 'Directrice'],
  1012. [FunctionEnum::TEACHER->value, ['gender' => 'X'], null, null, 'Professeur(e)'],
  1013. [FunctionEnum::ARCHIVIST->value, ['gender' => 'X'], null, null, 'Archiviste'],
  1014. [FunctionEnum::TREASURER->value, ['gender' => 'X'], null, null, 'Trésorier(ère)'],
  1015. [FunctionEnum::ADMINISTRATIVE_STAFF->value, ['gender' => 'X'], null, null, 'Personnel administratif'],
  1016. ]
  1017. );
  1018. $this->assertEquals(
  1019. 'Président(e)',
  1020. $dolibarrSyncService->formatContactPosition([FunctionEnum::PRESIDENT->value])
  1021. );
  1022. $this->assertEquals(
  1023. 'Président',
  1024. $dolibarrSyncService->formatContactPosition([FunctionEnum::PRESIDENT->value], 'MISTER')
  1025. );
  1026. $this->assertEquals(
  1027. 'Présidente',
  1028. $dolibarrSyncService->formatContactPosition([FunctionEnum::PRESIDENT->value], 'MISS')
  1029. );
  1030. $this->assertEquals(
  1031. 'Présidente, Directrice',
  1032. $dolibarrSyncService->formatContactPosition(
  1033. [FunctionEnum::PRESIDENT->value, FunctionEnum::DIRECTOR->value],
  1034. 'MISS'
  1035. )
  1036. );
  1037. $this->assertEquals(
  1038. 'Président, Directeur',
  1039. $dolibarrSyncService->formatContactPosition(
  1040. [FunctionEnum::PRESIDENT->value, FunctionEnum::DIRECTOR->value, FunctionEnum::ADHERENT->value],
  1041. 'MISTER'
  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(e), Directeur(ice), Professeur(e), Archiviste, Trésorier(ère), Pers...',
  1053. $dolibarrSyncService->formatContactPosition(
  1054. [
  1055. FunctionEnum::PRESIDENT->value,
  1056. FunctionEnum::DIRECTOR->value,
  1057. FunctionEnum::TEACHER->value,
  1058. FunctionEnum::ARCHIVIST->value,
  1059. FunctionEnum::TREASURER->value,
  1060. FunctionEnum::ADMINISTRATIVE_STAFF->value,
  1061. ],
  1062. 'X'
  1063. )
  1064. );
  1065. }
  1066. /**
  1067. * @see DolibarrSyncService::formatPhoneNumber()
  1068. */
  1069. public function testFormatPhoneNumber() {
  1070. $dolibarrSyncService = $this->getMockForMethod('formatPhoneNumber');
  1071. $phoneUtil = PhoneNumberUtil::getInstance();
  1072. $phoneNumber = $phoneUtil->parse('01 02 03 04 05', "FR");
  1073. $this->assertEquals(
  1074. '+33102030405',
  1075. $dolibarrSyncService->formatPhoneNumber($phoneNumber)
  1076. );
  1077. }
  1078. public function testGetExpectedTagsFor()
  1079. {
  1080. $dolibarrSyncService = $this->getMockForMethod('getExpectedTagsFor');
  1081. $organization1 = $this->getMockBuilder(Organization::class)->getMock();
  1082. $organization1->method('getLegalStatus')->willReturn(LegalEnum::ASSOCIATION_LAW_1901);
  1083. $settings1 = $this->getMockBuilder(Settings::class)->getMock();
  1084. $settings1->method('getProduct')->willReturn(SettingsProductEnum::SCHOOL);
  1085. $organization1->method('getSettings')->willReturn($settings1);
  1086. $organization2 = $this->getMockBuilder(Organization::class)->getMock();
  1087. $organization2->method('getLegalStatus')->willReturn(LegalEnum::ASSOCIATION_LAW_1901);
  1088. $settings2 = $this->getMockBuilder(Settings::class)->getMock();
  1089. $settings2->method('getProduct')->willReturn(SettingsProductEnum::SCHOOL_PREMIUM);
  1090. $organization2->method('getSettings')->willReturn($settings2);
  1091. $organization3 = $this->getMockBuilder(Organization::class)->getMock();
  1092. $organization3->method('getLegalStatus')->willReturn(LegalEnum::LOCAL_AUTHORITY);
  1093. $settings3 = $this->getMockBuilder(Settings::class)->getMock();
  1094. $settings3->method('getProduct')->willReturn(SettingsProductEnum::SCHOOL);
  1095. $organization3->method('getSettings')->willReturn($settings3);
  1096. $organization4 = $this->getMockBuilder(Organization::class)->getMock();
  1097. $organization4->method('getLegalStatus')->willReturn(LegalEnum::LOCAL_AUTHORITY);
  1098. $settings4 = $this->getMockBuilder(Settings::class)->getMock();
  1099. $settings4->method('getProduct')->willReturn(SettingsProductEnum::SCHOOL_PREMIUM);
  1100. $organization4->method('getSettings')->willReturn($settings4);
  1101. $organization5 = $this->getMockBuilder(Organization::class)->getMock();
  1102. $organization5->method('getLegalStatus')->willReturn(LegalEnum::LOCAL_AUTHORITY);
  1103. $settings5 = $this->getMockBuilder(Settings::class)->getMock();
  1104. $settings5->method('getProduct')->willReturn(SettingsProductEnum::ARTIST);
  1105. $organization5->method('getSettings')->willReturn($settings5);
  1106. $this->assertEquals(
  1107. $dolibarrSyncService->getExpectedTagsFor($organization1),
  1108. [67]
  1109. );
  1110. $this->assertEquals(
  1111. $dolibarrSyncService->getExpectedTagsFor($organization2),
  1112. [69]
  1113. );
  1114. $this->assertEquals(
  1115. $dolibarrSyncService->getExpectedTagsFor($organization3),
  1116. [68]
  1117. );
  1118. $this->assertEquals(
  1119. $dolibarrSyncService->getExpectedTagsFor($organization4),
  1120. [70]
  1121. );
  1122. $this->assertEquals(
  1123. $dolibarrSyncService->getExpectedTagsFor($organization5),
  1124. []
  1125. );
  1126. }
  1127. /**
  1128. * @see DolibarrSyncService::validateResponse()
  1129. */
  1130. public function testValidateResponse(): void {
  1131. $dolibarrSyncService = $this->getMockForMethod('validateResponse');
  1132. $response = $this->getMockBuilder(ResponseInterface::class)->getMock();
  1133. $response->method('toArray')->willReturn(['a' => 1]);
  1134. $operation = $this->getMockBuilder(CreateOperation::class)->disableOriginalConstructor()->getMock();
  1135. $operation->method('getData')->willReturn(['a' => 1]);
  1136. $dolibarrSyncService->expects(self::exactly(2))->method('sanitizeDolibarrData')->with(['a' => 1])->willReturn(['a' => 1]);
  1137. $this->arrayUtils->expects(self::once())->method('getChanges')->with(['a' => 1], ['a' => 1], true)->willReturn([]);
  1138. $dolibarrSyncService->validateResponse($response, $operation);
  1139. }
  1140. /**
  1141. * @see DolibarrSyncService::validateResponse()
  1142. */
  1143. public function testValidateResponseInvalid(): void {
  1144. $dolibarrSyncService = $this->getMockForMethod('validateResponse');
  1145. $response = $this->getMockBuilder(ResponseInterface::class)->getMock();
  1146. $response->method('toArray')->willReturn(['a' => 1]);
  1147. $operation = $this->getMockBuilder(CreateOperation::class)->disableOriginalConstructor()->getMock();
  1148. $operation->method('getData')->willReturn(['a' => 0]);
  1149. $dolibarrSyncService->expects(self::exactly(2))
  1150. ->method('sanitizeDolibarrData')
  1151. ->willReturnMap([
  1152. [['a' => 1], ['a' => 1]],
  1153. [['a' => 0], ['a' => 0]]
  1154. ]);
  1155. $this->arrayUtils->expects(self::once())->method('getChanges')->with(['a' => 1], ['a' => 0], true)->willReturn(['a' => 0]);
  1156. $this->expectException(RuntimeException::class);
  1157. $dolibarrSyncService->validateResponse($response, $operation);
  1158. }
  1159. /**
  1160. * @see DolibarrSyncService::validateResponse()
  1161. */
  1162. public function testValidateResponseRequestError(): void {
  1163. $dolibarrSyncService = $this->getMockForMethod('validateResponse');
  1164. $response = $this->getMockBuilder(ResponseInterface::class)->getMock();
  1165. $response->method('getInfo')->willReturnMap([
  1166. ['http_code', '200'], ['url', 'http://url.com'], ['response_headers', []]
  1167. ]);
  1168. $response->method('getContent')->willReturn("");
  1169. $response->method('toArray')->willThrowException(new ServerException($response));
  1170. $operation = $this->getMockBuilder(CreateOperation::class)->disableOriginalConstructor()->getMock();
  1171. $operation->method('getData')->willReturn(['a' => 0]);
  1172. $dolibarrSyncService->expects(self::never())->method('sanitizeDolibarrData');
  1173. $this->arrayUtils->expects(self::never())->method('getChanges');
  1174. $this->expectException(RuntimeException::class);
  1175. $dolibarrSyncService->validateResponse($response, $operation);
  1176. }
  1177. }