DolibarrSyncServiceTest.php 57 KB

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