| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454 |
- <?php
- /** @noinspection ALL */
- namespace App\Tests\Unit\Service\Dolibarr;
- use App\Entity\Access\Access;
- use App\Entity\Access\FunctionType;
- use App\Entity\Core\AddressPostal;
- use App\Entity\Core\ContactPoint;
- use App\Entity\Network\Network;
- use App\Entity\Network\NetworkOrganization;
- use App\Entity\Organization\Organization;
- use App\Entity\Organization\OrganizationAddressPostal;
- use App\Entity\Organization\Settings;
- use App\Entity\Person\Person;
- use App\Enum\Access\FunctionEnum;
- use App\Enum\Access\RoleEnum;
- use App\Enum\Core\ContactPointTypeEnum;
- use App\Enum\Network\NetworkEnum;
- use App\Enum\Organization\AddressPostalOrganizationTypeEnum;
- use App\Enum\Organization\LegalEnum;
- use App\Enum\Organization\SettingsProductEnum;
- use App\Enum\Person\GenderEnum;
- use App\Repository\Access\AccessRepository;
- use App\Repository\Access\FunctionTypeRepository;
- use App\Repository\Organization\OrganizationRepository;
- use App\Service\Core\AddressPostalUtils;
- use App\Service\Dolibarr\DolibarrApiService;
- use App\Service\Dolibarr\DolibarrSyncService;
- use App\Service\Organization\Utils;
- use App\Service\Rest\Operation\BaseRestOperation;
- use App\Service\Rest\Operation\CreateOperation;
- use App\Service\Rest\Operation\DeleteOperation;
- use App\Service\Rest\Operation\UpdateOperation;
- use App\Service\Utils\ArrayUtils;
- use App\Service\Utils\DatesUtils;
- use Doctrine\Common\Collections\ArrayCollection;
- use libphonenumber\PhoneNumber;
- use libphonenumber\PhoneNumberUtil;
- use PHPUnit\Framework\MockObject\MockObject;
- use PHPUnit\Framework\TestCase;
- use Psr\Log\LoggerInterface;
- use Symfony\Contracts\HttpClient\ResponseInterface;
- use Symfony\Contracts\Translation\TranslatorInterface;
- class TestableDolibarrSyncService extends DolibarrSyncService
- {
- public function getDolibarrSocietiesIndex(): array
- {
- return parent::getDolibarrSocietiesIndex();
- }
- public function findDolibarrContactFor(array $dolibarrContacts, Person $person): ?array
- {
- return parent::findDolibarrContactFor($dolibarrContacts, $person);
- }
- public function getActiveMembersIndex(): array
- {
- return parent::getActiveMembersIndex();
- }
- public function sanitizeDolibarrData(?array $data): ?array
- {
- return parent::sanitizeDolibarrData($data);
- }
- public function getOrganizationPostalAddress(Organization $organization): ?AddressPostal
- {
- return parent::getOrganizationPostalAddress($organization);
- }
- public function getOrganizationBillingPostalAddress(Organization $organization): ?AddressPostal
- {
- return parent::getOrganizationBillingPostalAddress($organization);
- }
- public function getOrganizationPhone(Organization $organization): ?string
- {
- return parent::getOrganizationPhone($organization);
- }
- public function getOrganizationEmail(Organization $organization): ?string
- {
- return parent::getOrganizationEmail($organization);
- }
- public function getOrganizationNetworkId(Organization $organization): ?int
- {
- return parent::getOrganizationNetworkId($organization);
- }
- public function countWithMission(array $missions, array $members): int
- {
- return parent::countWithMission($missions, $members);
- }
- public function getPersonContact(Person $person): ?ContactPoint
- {
- return parent::getPersonContact($person);
- }
- public function formatContactPosition(array $missions, ?string $gender = 'X'): string
- {
- return parent::formatContactPosition($missions, $gender);
- }
- public function formatPhoneNumber(PhoneNumber $phoneNumber): string
- {
- return parent::formatPhoneNumber($phoneNumber);
- }
- public function getExpectedTagsFor(Organization $organization): array
- {
- return parent::getExpectedTagsFor($organization);
- }
- public function validateResponse(ResponseInterface $response, BaseRestOperation $operation): void
- {
- parent::validateResponse($response, $operation);
- }
- }
- class DolibarrSyncServiceTest extends TestCase
- {
- private MockObject|OrganizationRepository $organizationRepository;
- private MockObject|AccessRepository $accessRepository;
- private MockObject|FunctionTypeRepository $functionTypeRepository;
- private MockObject|DolibarrApiService $dolibarrApiService;
- private MockObject|AddressPostalUtils $addressPostalUtils;
- private MockObject|ArrayUtils $arrayUtils;
- private MockObject|TranslatorInterface $translator;
- private MockObject|LoggerInterface $logger;
- private MockObject|Utils $organizationUtils;
- public function setUp(): void
- {
- $this->organizationRepository = $this->getMockBuilder(OrganizationRepository::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->accessRepository = $this->getMockBuilder(AccessRepository::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->functionTypeRepository = $this->getMockBuilder(FunctionTypeRepository::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->addressPostalUtils = $this->getMockBuilder(AddressPostalUtils::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->arrayUtils = $this->getMockBuilder(ArrayUtils::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->translator = $this->getMockBuilder(TranslatorInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->logger = $this->getMockBuilder(LoggerInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->organizationUtils = $this->getMockBuilder(Utils::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->logger->method('info')->willReturnSelf();
- $this->logger->method('debug')->willReturnSelf();
- $this->logger->method('warning')->willReturnSelf();
- $this->logger->method('error')->willReturnSelf();
- }
- public function tearDown(): void
- {
- DatesUtils::clearFakeDatetime();
- }
- private function getMockForMethod(string $method)
- {
- $dolibarrSyncService = $this->getMockBuilder(TestableDolibarrSyncService::class)
- ->setConstructorArgs([$this->organizationRepository, $this->accessRepository, $this->functionTypeRepository,
- $this->dolibarrApiService, $this->addressPostalUtils, $this->arrayUtils, $this->translator, $this->organizationUtils])
- ->setMethodsExcept([$method, 'setLoggerInterface'])
- ->getMock();
- $dolibarrSyncService->setLoggerInterface($this->logger);
- return $dolibarrSyncService;
- }
- /**
- * Full test of the scan method.
- *
- * @see DolibarrSyncService::scan()
- */
- public function testScan(): void
- {
- $dolibarrSyncService = $this->getMockForMethod('scan');
- // ----- Opentalent Organizations -----
- $orgId1 = 10;
- $organization1 = $this->getMockBuilder(Organization::class)->getMock();
- $organization1->method('getId')->willReturn($orgId1);
- $organization1->method('getName')->willReturn('Organization 10');
- $organizationData1 = [
- 'fullAddress' => '1 Rue Azerty',
- 'fullBillingAddress' => '2 Rue Ascii',
- 'addressOwner' => 'Mr Keyboard',
- 'postalCode' => '01110',
- 'city' => 'ByteCity',
- 'email' => 'foo@bar.net',
- 'phone' => '0102030405',
- 'networkId' => NetworkEnum::CMF->value,
- 'product' => SettingsProductEnum::SCHOOL,
- 'networkId' => NetworkEnum::CMF->value,
- 'legalStatus' => LegalEnum::LOCAL_AUTHORITY,
- ];
- $orgId2 = 20;
- $organization2 = $this->getMockBuilder(Organization::class)->getMock();
- $organization2->method('getId')->willReturn($orgId2);
- $organization2->method('getName')->willReturn('Organization 20');
- $organizationData2 = [
- 'email' => null,
- 'phone' => null,
- 'networkId' => null,
- 'product' => SettingsProductEnum::ARTIST,
- 'legalStatus' => LegalEnum::ASSOCIATION_LAW_1901,
- ];
- $orgId3 = 30;
- $organization3 = null; // This organization does not exist
- // Persons
- $accessId1 = 11; // Person 1
- $accessId2 = 21; // Person 2
- $accessId3 = 12; // Shall be ignored, because not an office member
- // access 4 does not exist
- $accessId5 = 13; // Invalid Person
- $personId1 = 100;
- $personData1 = [
- 'name' => 'Dupont',
- 'givenName' => 'Hercules',
- 'gender' => GenderEnum::MISTER,
- 'email' => 'an@email.net',
- 'phone' => '0102030405',
- 'mobilePhone' => '0607080910',
- ];
- $personId2 = 200;
- $personData2 = [
- 'name' => 'Simpson',
- 'givenName' => 'Lisa',
- 'gender' => null,
- ];
- $personId3 = 300; // Obsolete contact, does not exist anymore in the Opentalent DB
- $personId4 = 400; // Obsolete contact, does not exist anymore in the Opentalent DB
- $personId5 = 900; // Invalid contact with no firstname and no lastname
- $personData5 = [
- 'name' => '',
- 'givenName' => '',
- 'gender' => null,
- ];
- $activeMembers1 = [
- $accessId1 => [FunctionEnum::PRESIDENT->value],
- $accessId3 => [FunctionEnum::STUDENT->value],
- $accessId5 => [FunctionEnum::TREASURER->value],
- ];
- $activeMembers2 = [
- $accessId2 => [FunctionEnum::PRESIDENT->value],
- ];
- // ----- Opentalent : other vars -----
- $cmfId = 12097;
- $cmfDolibarrId = 12098;
- $ffecId = 91295;
- $ffecDolibarrId = 91296;
- // ----- Dolibarr societies -----
- // Existing society about to be updated
- $socId1 = 1;
- $dolibarrSociety1 = [
- 'id' => $socId1,
- 'name' => 'Organization 10',
- 'address' => '1 Rue Qwerty',
- 'zip' => '01024',
- 'town' => 'Ram',
- 'email' => 'some@email.com',
- 'phone' => null,
- 'status' => 0,
- 'parent' => '0',
- 'array_options' => [
- 'options_2iopeninfoopentalent' => '',
- 'options_2iopen_software_opentalent' => 'Opentalent Artist',
- ],
- ];
- // Existing society with no data
- $socId2 = 2;
- $dolibarrSociety2 = [
- 'id' => $socId2,
- 'array_options' => [],
- ];
- // This organization does not exist in the opentalent DB
- $socId3 = 3;
- $dolibarrSociety3 = null;
- // Dolibarr contacts
- $contactData1 = [
- 'id' => '1',
- 'civility_code' => '',
- 'lastname' => 'Dupond',
- 'firstname' => 'Bob',
- 'email' => 'abcd@mail.com',
- 'phone_pro' => '+33478570000',
- 'phone_mobile' => '+33682980000',
- 'poste' => 'Secrétaire',
- 'statut' => '1',
- 'array_options' => [
- 'options_2iopen_person_id' => '',
- ],
- ];
- // An obsolete contact that should be disabled
- $obsoleteContactData = [
- 'id' => '4',
- 'lastname' => 'Doe',
- 'firstname' => 'John',
- 'statut' => '1',
- 'array_options' => [
- 'options_2iopen_person_id' => $personId3,
- ],
- ];
- // An obsolete contact that should is already disabled
- $obsoleteContactData2 = [
- 'id' => '5',
- 'lastname' => 'Foo',
- 'firstname' => 'John',
- 'statut' => '0',
- 'array_options' => [
- 'options_2iopen_person_id' => $personId4,
- ],
- ];
- $billingContactData = [
- 'id' => '5',
- 'lastname' => 'Service facturation',
- 'firstname' => null,
- 'address' => '',
- 'zip' => '',
- 'town' => '',
- 'statut' => '1',
- 'array_options' => [
- 'options_2iopen_person_id' => null,
- ],
- ];
- $dolibarrSocietyContacts1 = [$contactData1, $obsoleteContactData, $obsoleteContactData2, $billingContactData];
- $dolibarrSocietyContacts2 = [];
- // ----- Setup Mocks -----
- $this->translator->method('trans')->willReturnMap([
- ['STUDENTS_COUNT', [], null, null, "Nombre d'élèves"],
- ['ADHERENTS_COUNT', [], null, null, "Nombre d'adhérents"],
- ['ADMIN_ACCESS_COUNT', [], null, null, "Nombre d'accès admin"],
- [SettingsProductEnum::SCHOOL->value, [], null, null, 'Opentalent School'],
- [SettingsProductEnum::ARTIST->value, [], null, null, 'Opentalent Artist'],
- [GenderEnum::MISTER->value, [], null, null, 'MR'],
- ]);
- // Get societies
- $this->organizationRepository->method('find')
- ->willReturnMap([
- [$orgId1, null, null, $organization1],
- [$orgId2, null, null, $organization2],
- [$orgId3, null, null, $organization3],
- ]);
- $dolibarrSyncService
- ->expects(self::once())
- ->method('getDolibarrSocietiesIndex')
- ->willReturn([
- $orgId1 => $dolibarrSociety1,
- $orgId2 => $dolibarrSociety2,
- $orgId3 => $dolibarrSociety3,
- ]);
- $dolibarrSyncService
- ->expects(self::once())
- ->method('getActiveMembersIndex')
- ->willReturn([
- $orgId1 => $activeMembers1,
- $orgId2 => $activeMembers2,
- $orgId3 => [],
- ]);
- // Function types
- $functionType1 = $this->getMockBuilder(FunctionType::class)->getMock();
- $functionType1->method('getMission')->willReturn(FunctionEnum::DIRECTOR);
- $functionType2 = $this->getMockBuilder(FunctionType::class)->getMock();
- $functionType2->method('getMission')->willReturn(FunctionEnum::PRESIDENT);
- $this->functionTypeRepository
- ->expects($this->once())
- ->method('findBy')
- ->with(['roleByDefault' => RoleEnum::ROLE_ADMIN])
- ->willReturn([$functionType1, $functionType2]);
- // Get CMF and FFEC ids
- $this->dolibarrApiService->method('getSociety')->willReturnMap(
- [
- [$cmfId, ['id' => $cmfDolibarrId]],
- [$ffecId, ['id' => $ffecDolibarrId]],
- ]
- );
- // -- Loop over organizations --
- $dolibarrSyncService
- ->expects(self::exactly(5)) // 3 organizations and 2 contacts
- ->method('sanitizeDolibarrData')
- ->willReturnCallback(function ($args) { return $args; });
- $mainPostalAddress = $this->getMockBuilder(AddressPostal::class)->getMock();
- $mainPostalAddress->method('getStreetAddress')->willReturn($organizationData1['fullAddress']);
- $mainPostalAddress->method('getAddressOwner')->willReturn($organizationData1['addressOwner']);
- $mainPostalAddress->method('getPostalCode')->willReturn($organizationData1['postalCode']);
- $mainPostalAddress->method('getAddressCity')->willReturn($organizationData1['city']);
- $billingPostalAddress = $this->getMockBuilder(AddressPostal::class)->getMock();
- $billingPostalAddress->method('getStreetAddress')->willReturn($organizationData1['fullBillingAddress']);
- $billingPostalAddress->method('getAddressOwner')->willReturn($organizationData1['addressOwner']);
- $billingPostalAddress->method('getPostalCode')->willReturn($organizationData1['postalCode']);
- $billingPostalAddress->method('getAddressCity')->willReturn($organizationData1['city']);
- $dolibarrSyncService
- ->expects(self::exactly(2))
- ->method('getOrganizationPostalAddress')
- ->willReturnMap([
- [$organization1, $mainPostalAddress],
- [$organization2, null],
- ]);
- $dolibarrSyncService
- ->expects(self::exactly(1))
- ->method('getOrganizationBillingPostalAddress')
- ->willReturnMap([
- [$organization1, $billingPostalAddress],
- [$organization2, null],
- ]);
- $this->addressPostalUtils
- ->method('getFullStreetAddress')
- ->willReturnCallback(function ($a) { return $a->getStreetAddress(); });
- $dolibarrSyncService
- ->expects(self::exactly(2))
- ->method('getOrganizationEmail')
- ->willReturnMap([
- [$organization1, $organizationData1['email']],
- [$organization2, $organizationData2['email']],
- ]);
- $dolibarrSyncService
- ->expects(self::exactly(2))
- ->method('getOrganizationPhone')
- ->willReturnMap([
- [$organization1, $organizationData1['phone']],
- [$organization2, $organizationData2['phone']],
- ]);
- $dolibarrSyncService
- ->expects(self::exactly(2))
- ->method('getOrganizationNetworkId')
- ->willReturnMap([
- [$organization1, $organizationData1['networkId']],
- [$organization2, $organizationData2['networkId']],
- ]);
- $settings1 = $this->getMockBuilder(Settings::class)->getMock();
- $settings1->method('getProduct')->willReturn($organizationData1['product']);
- $organization1->method('getSettings')->willReturn($settings1);
- $settings2 = $this->getMockBuilder(Settings::class)->getMock();
- $settings2->method('getProduct')->willReturn($organizationData2['product']);
- $organization2->method('getSettings')->willReturn($settings2);
- $this->organizationUtils->method('isSchool')->willReturnMap([
- [$organization1, true],
- [$organization2, false],
- ]);
- $this->organizationUtils->method('isArtist')->willReturnMap([
- [$organization1, false],
- [$organization2, true],
- ]);
- $dolibarrSyncService->method('countWithMission')->willReturnMap([
- [[FunctionEnum::STUDENT->value], $activeMembers1, 1],
- [[FunctionEnum::ADHERENT->value], $activeMembers1, 2],
- [[FunctionEnum::ADHERENT->value], $activeMembers2, 0],
- [[FunctionEnum::DIRECTOR->value, FunctionEnum::PRESIDENT->value], $activeMembers1, 1],
- [[FunctionEnum::DIRECTOR->value, FunctionEnum::PRESIDENT->value], $activeMembers2, 2],
- ]);
- $this->arrayUtils
- ->expects(self::exactly(4))
- ->method('getChanges')
- ->willReturnCallback(
- function (array $initialArray, array $newArray, bool $recursive = false, ?callable $callback = null) {
- if (in_array('name', $newArray, true)) {
- // Organization 1 name is already defined and has not been changed
- unset($newArray['name']);
- }
- if (in_array('statut', $newArray, true)) {
- // Contact 1 statut is already defined and has not been changed
- unset($newArray['statut']);
- }
- return $newArray;
- }
- );
- $this->dolibarrApiService->method('getContacts')->willReturnMap([
- [$socId1, $dolibarrSocietyContacts1],
- [$socId2, $dolibarrSocietyContacts2],
- ]);
- // Loop over contacts
- // NB: Student will be skipped since it has no office role
- $person1 = $this->getMockBuilder(Person::class)->getMock();
- $person1->method('getId')->willReturn($personId1);
- $person1->method('getName')->willReturn($personData1['name']);
- $person1->method('getGivenName')->willReturn($personData1['givenName']);
- $person1->method('getGender')->willReturn($personData1['gender']);
- $person2 = $this->getMockBuilder(Person::class)->getMock();
- $person2->method('getId')->willReturn($personId2);
- $person2->method('getName')->willReturn($personData2['name']);
- $person2->method('getGivenName')->willReturn($personData2['givenName']);
- $person2->method('getGender')->willReturn($personData2['gender']);
- // An invalid person that should be ignored
- $person5 = $this->getMockBuilder(Person::class)->getMock();
- $person5->method('getId')->willReturn($personId5);
- $person5->method('getName')->willReturn($personData5['name']);
- $person5->method('getGivenName')->willReturn($personData5['givenName']);
- $access1 = $this->getMockBuilder(Access::class)->getMock();
- $access1->method('getPerson')->willReturn($person1);
- $access2 = $this->getMockBuilder(Access::class)->getMock();
- $access2->method('getPerson')->willReturn($person2);
- $access5 = $this->getMockBuilder(Access::class)->getMock();
- $access5->method('getPerson')->willReturn($person5);
- $this->accessRepository->method('find')->willReturnMap([
- [$accessId1, null, null, $access1],
- [$accessId2, null, null, $access2],
- [$accessId5, null, null, $access5],
- ]);
- $dolibarrSyncService->method('findDolibarrContactFor')->willReturnMap([
- [$dolibarrSocietyContacts1, $person1, $contactData1],
- [$dolibarrSocietyContacts2, $person2, null],
- ]);
- $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
- $contactPoint1->method('getEmail')->willReturn($personData1['email']);
- $phone = $this->getMockBuilder(PhoneNumber::class)->getMock();
- $mobilePhone = $this->getMockBuilder(PhoneNumber::class)->getMock();
- $dolibarrSyncService->method('formatPhoneNumber')->willReturnMap([
- [$phone, $personData1['phone']],
- [$mobilePhone, $personData1['mobilePhone']],
- ]);
- $contactPoint1->method('getTelphone')->willReturn($phone);
- $contactPoint1->method('getMobilPhone')->willReturn($mobilePhone);
- $dolibarrSyncService->method('getPersonContact')->willReturnMap([
- [$person1, $contactPoint1],
- [$person2, null],
- ]);
- $dolibarrSyncService->method('formatContactPosition')->willReturnMap([
- [[FunctionEnum::PRESIDENT->value], GenderEnum::MISTER->value, 'Président'],
- [[FunctionEnum::PRESIDENT->value], null, 'Président(e)'],
- ]);
- // Expected logged error messages
- $this->logger->expects(self::exactly(2))->method('error')->withConsecutive(
- ['Person 900 miss a lastname and/or a firstname, ignored.'],
- ['Organization 30 not found in the Opentalent DB'],
- );
- // Tags
- $this->dolibarrApiService->method('getSocietyTagsIds')->willReturnMap([
- [$socId1, [1, 68]],
- [$socId2, [3, 67]],
- ]);
- $dolibarrSyncService->method('getExpectedTagsFor')->willReturnMap([
- [$organization1, [67]],
- [$organization2, []],
- ]);
- // Expected progression callback triggers
- $progressionCallbackExpectedCalls = [[1, 3], [2, 3], [3, 3]];
- $progressionCallback = static function ($i, $total) use (&$progressionCallbackExpectedCalls) {
- [$expectedI, $expectedTotal] = array_shift($progressionCallbackExpectedCalls);
- if ($i !== $expectedI || $total !== $expectedTotal) {
- throw new \AssertionError('Progression callback error, expected parameters are ('.$expectedI.','.$expectedTotal.'), got ('.$i.', '.$total.')');
- }
- };
- DatesUtils::setFakeDatetime('2024-01-01T00:00:00+00:00');
- $operations = $dolibarrSyncService->scan($progressionCallback);
- $this->assertCount(9, $operations);
- $this->assertEqualsCanonicalizing(
- [
- '[PUT thirdparties/1]',
- "address : `1 Rue Qwerty` => `Mr Keyboard\n1 Rue Azerty`",
- 'zip : `01024` => `01110`',
- 'town : `Ram` => `ByteCity`',
- 'email : `some@email.com` => `foo@bar.net`',
- 'phone : `` => `0102030405`',
- 'parent : `0` => `12098`',
- 'array_options.options_2iopen_software_opentalent : `Opentalent Artist` => `Opentalent School`',
- "array_options.options_2iopeninfoopentalent : `` => `Nombre d'élèves : 1\nNombre d'adhérents : 2\nNombre d'accès admin : 1`",
- 'array_options.options_2iopen_last_sync_date : (new) => `2024-01-01T00:00:00+00:00`',
- 'status : `0` => `1`',
- ],
- $operations[0]->getChangeLog()
- );
- $this->assertEqualsCanonicalizing(
- [
- '[PUT contacts/1]',
- 'civility_code : `` => `MR`',
- 'lastname : `Dupond` => `Dupont`',
- 'firstname : `Bob` => `Hercules`',
- 'email : `abcd@mail.com` => `an@email.net`',
- 'phone_pro : `+33478570000` => `0102030405`',
- 'phone_mobile : `+33682980000` => `0607080910`',
- 'poste : `Secrétaire` => `Président`',
- 'array_options.options_2iopen_person_id : `` => `100`',
- 'array_options.options_2iopen_last_sync_date : (new) => `2024-01-01T00:00:00+00:00`',
- ],
- $operations[1]->getChangeLog()
- );
- $this->assertEqualsCanonicalizing(
- [
- '[PUT contacts/4]',
- 'statut : `1` => `0`',
- 'array_options.options_2iopen_last_sync_date : (new) => `2024-01-01T00:00:00+00:00`',
- ],
- $operations[2]->getChangeLog()
- );
- $this->assertEqualsCanonicalizing(
- [
- '[PUT contacts/5]',
- 'address : `` => `2 Rue Ascii`',
- 'town : `` => `ByteCity`',
- 'zip : `` => `01110`',
- ],
- $operations[3]->getChangeLog()
- );
- $this->assertEqualsCanonicalizing(
- [
- '[DELETE /thirdparties/1/categories/68]',
- ],
- $operations[4]->getChangeLog()
- );
- $this->assertEqualsCanonicalizing(
- [
- '[POST /thirdparties/1/categories/67]',
- ],
- $operations[5]->getChangeLog()
- );
- $this->assertEqualsCanonicalizing(
- [
- '[PUT thirdparties/2]',
- 'address : (new) => ``',
- 'email : (new) => ``',
- 'name : (new) => `Organization 20`',
- 'parent : (new) => ``',
- 'phone : (new) => ``',
- 'status : (new) => `1`',
- 'town : (new) => ``',
- 'zip : (new) => ``',
- 'array_options.options_2iopen_software_opentalent : (new) => `Opentalent Artist`',
- "array_options.options_2iopeninfoopentalent : (new) => `Nombre d'adhérents : 0\nNombre d'accès admin : 2`",
- 'array_options.options_2iopen_last_sync_date : (new) => `2024-01-01T00:00:00+00:00`',
- ],
- $operations[6]->getChangeLog()
- );
- $this->assertEqualsCanonicalizing(
- [
- '[POST contacts]',
- 'civility_code : (new) => ``',
- 'lastname : (new) => `Simpson`',
- 'firstname : (new) => `Lisa`',
- 'email : (new) => ``',
- 'phone_pro : (new) => ``',
- 'phone_mobile : (new) => ``',
- 'poste : (new) => `Président(e)`',
- 'statut : (new) => `1`',
- 'array_options.options_2iopen_person_id : (new) => `200`',
- 'array_options.options_2iopen_last_sync_date : (new) => `2024-01-01T00:00:00+00:00`',
- 'socid : (new) => `2`',
- ],
- $operations[7]->getChangeLog()
- );
- $this->assertEqualsCanonicalizing(
- [
- '[DELETE /thirdparties/2/categories/67]',
- ],
- $operations[8]->getChangeLog()
- );
- $this->assertCount(0, $progressionCallbackExpectedCalls);
- }
- /**
- * All valid operations shall be executed
- * If an operation is not in status READY, a warning shall be logged, and the operation shall be skipped.
- *
- * @see DolibarrSyncService::execute()
- */
- public function testExecute(): void
- {
- $dolibarrSyncService = $this->getMockForMethod('execute');
- $progressionCallbackExpectedCalls = [[1, 3], [2, 3], [3, 3]];
- $progressionCallback = static function ($i, $total) use (&$progressionCallbackExpectedCalls) {
- [$expectedI, $expectedTotal] = array_shift($progressionCallbackExpectedCalls);
- if ($i !== $expectedI || $total !== $expectedTotal) {
- throw new \AssertionError('Progression callback error, expected parameters are ('.$expectedI.','.$expectedTotal.'), got ('.$i.', '.$total.')');
- }
- };
- $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
- $operation1 = $this->getMockBuilder(UpdateOperation::class)->disableOriginalConstructor()->getMock();
- $operation1->method('getStatus')->willReturnOnConsecutiveCalls(BaseRestOperation::STATUS_READY, BaseRestOperation::STATUS_DONE);
- $operation1->method('getChangeLog')->willReturn(['foo']);
- $operation1->expects(self::once())->method('execute')->willReturn($response);
- $operation2 = $this->getMockBuilder(CreateOperation::class)->disableOriginalConstructor()->getMock();
- $operation2->method('getStatus')->willReturn(
- BaseRestOperation::STATUS_READY, BaseRestOperation::STATUS_ERROR, BaseRestOperation::STATUS_ERROR // An error happened
- );
- $operation2->expects(self::once())->method('execute');
- $operation3 = $this->getMockBuilder(DeleteOperation::class)->disableOriginalConstructor()->getMock();
- $operation3->method('getStatus')->willReturn(BaseRestOperation::STATUS_DONE); // Invalid status, should log a warning and not execute
- $operation3->expects(self::never())->method('execute');
- $this->logger->expects(self::exactly(2))->method('warning'); // 2 warnings because of the bad status of the Create Op
- $this->logger->expects(self::exactly(3))->method('error'); // The exception thrown during the execution of the Delete op will log 3 errors
- $dolibarrSyncService->execute([$operation1, $operation2, $operation3], $progressionCallback);
- }
- /**
- * @see DolibarrSyncService::run()
- */
- public function testRun()
- {
- $dolibarrSyncService = $this->getMockForMethod('run');
- $operations = ['operation1', 'operation2'];
- $dolibarrSyncService->expects(self::once())->method('scan')->willReturn($operations);
- $dolibarrSyncService->expects(self::once())->method('execute')->with($operations);
- $result = $dolibarrSyncService->run();
- $this->assertEquals($operations, $result);
- }
- /**
- * @see DolibarrSyncService::getDolibarrSocietiesIndex()
- */
- public function testGetDolibarrSocietiesIndex(): void
- {
- $dolibarrSyncService = $this->getMockForMethod('getDolibarrSocietiesIndex');
- $this->dolibarrApiService
- ->expects($this->once())
- ->method('getAllClients')
- ->willReturn(
- [
- ['id' => 1, 'array_options' => ['options_2iopen_organization_id' => 101]],
- ['id' => 2, 'array_options' => ['options_2iopen_organization_id' => 102]],
- ['id' => 3, 'array_options' => ['options_2iopen_organization_id' => null]], // No org id but also no contract, so it's ok
- ['id' => 4, 'name' => 'foo', 'array_options' => ['options_2iopen_organization_id' => null]], // No org id but has a contract, a warning should be logged
- ]
- );
- $this->dolibarrApiService->expects(self::exactly(2))->method('getActiveContract')->willReturnMap([
- [3, null],
- [4, ['dummy non-empty data']],
- ]);
- $this->logger->expects(self::once())->method('warning')->with('Dolibarr client has no organization id: foo (4)');
- $index = $dolibarrSyncService->getDolibarrSocietiesIndex();
- $this->assertEqualsCanonicalizing(
- [
- 1 => ['id' => 1, 'array_options' => ['options_2iopen_organization_id' => 101]],
- 2 => ['id' => 2, 'array_options' => ['options_2iopen_organization_id' => 102]],
- ],
- $index
- );
- }
- /**
- * @see DolibarrSyncService::getActiveMembersIndex()
- */
- public function testGetActiveMembersIndex(): void
- {
- $dolibarrSyncService = $this->getMockForMethod('getActiveMembersIndex');
- $this->accessRepository
- ->expects($this->once())
- ->method('getAllActiveMembersAndMissions')
- ->willReturn(
- [
- ['id' => 1, 'organization_id' => 1, 'mission' => FunctionEnum::PRESIDENT],
- ['id' => 2, 'organization_id' => 1, 'mission' => FunctionEnum::STUDENT],
- ['id' => 3, 'organization_id' => 2, 'mission' => FunctionEnum::PRESIDENT],
- ['id' => 3, 'organization_id' => 2, 'mission' => FunctionEnum::TEACHER],
- ]
- );
- $index = $dolibarrSyncService->getActiveMembersIndex();
- $this->assertEqualsCanonicalizing([
- 1 => [1 => [FunctionEnum::PRESIDENT->value], 2 => [FunctionEnum::STUDENT->value]],
- 2 => [3 => [FunctionEnum::PRESIDENT->value, FunctionEnum::TEACHER->value]],
- ], $index);
- }
- /**
- * @see DolibarrSyncService::findDolibarrContactFor()
- */
- public function testFindDolibarrContactForById(): void
- {
- $dolibarrSyncService = $this->getMockForMethod('findDolibarrContactFor');
- $contacts = [
- ['id' => 1, 'array_options' => ['options_2iopen_person_id' => 101]],
- ['id' => 2, 'array_options' => ['options_2iopen_person_id' => 102]],
- ];
- // Find by id
- $person = $this->getMockBuilder(Person::class)->getMock();
- $person->method('getId')->willReturn(102);
- $contact = $dolibarrSyncService->findDolibarrContactFor($contacts, $person);
- $this->assertEquals(2, $contact['id']);
- }
- /**
- * @see DolibarrSyncService::findDolibarrContactFor()
- */
- public function testFindDolibarrContactForByName(): void
- {
- $dolibarrSyncService = $this->getMockForMethod('findDolibarrContactFor');
- $contacts = [
- ['id' => 1, 'firstname' => 'mister', 'lastname' => 'X', 'array_options' => ['options_2iopen_person_id' => null]],
- ['id' => 2, 'firstname' => 'miss', 'lastname' => 'Y', 'array_options' => ['options_2iopen_person_id' => null]],
- ];
- // Find by full name (contact has no person id, it should be returned)
- $person = $this->getMockBuilder(Person::class)->getMock();
- $person->method('getId')->willReturn(101);
- $person->method('getName')->willReturn('X');
- $person->method('getGivenName')->willReturn('mister');
- $result = $dolibarrSyncService->findDolibarrContactFor($contacts, $person);
- $this->assertEquals(1, $result['id']);
- }
- /**
- * @see DolibarrSyncService::findDolibarrContactFor()
- */
- public function testFindDolibarrContactForByNameWithConflict(): void
- {
- $dolibarrSyncService = $this->getMockForMethod('findDolibarrContactFor');
- $contacts = [
- ['id' => 1, 'firstname' => 'mister', 'lastname' => 'X', 'array_options' => ['options_2iopen_person_id' => 1]],
- ['id' => 2, 'firstname' => 'miss', 'lastname' => 'Y', 'array_options' => ['options_2iopen_person_id' => 2]],
- ];
- // Find by full name (contact already has another person id, it should not be returned)
- $person = $this->getMockBuilder(Person::class)->getMock();
- $person->method('getId')->willReturn(101);
- $person->method('getName')->willReturn('X');
- $person->method('getGivenName')->willReturn('mister');
- $result = $dolibarrSyncService->findDolibarrContactFor($contacts, $person);
- $this->assertEquals(null, $result);
- }
- /**
- * @see DolibarrSyncService::findDolibarrContactFor()
- */
- public function testFindDolibarrContactNotFound(): void
- {
- $dolibarrSyncService = $this->getMockForMethod('findDolibarrContactFor');
- $contacts = [
- ['id' => 1, 'firstname' => 'mister', 'lastname' => 'X', 'array_options' => ['options_2iopen_person_id' => 1]],
- ['id' => 2, 'firstname' => 'miss', 'lastname' => 'Y', 'array_options' => ['options_2iopen_person_id' => 2]],
- ];
- // Do not find
- $person = $this->getMockBuilder(Person::class)->getMock();
- $person->method('getId')->willReturn(-1);
- $person->method('getName')->willReturn('Presley');
- $person->method('getGivenName')->willReturn('Elvis');
- $result = $dolibarrSyncService->findDolibarrContactFor($contacts, $person);
- $this->assertEquals(null, $result);
- }
- /**
- * @see DolibarrSyncService::sanitizeDolibarrData()
- */
- public function testSanitizeDolibarrData(): void
- {
- $dolibarrSyncService = $this->getMockForMethod('sanitizeDolibarrData');
- $result = $dolibarrSyncService->sanitizeDolibarrData(['a' => 'A', 'b' => '', 'c' => ['d' => 'D', 'e' => '']]);
- $this->assertEquals(
- ['a' => 'A', 'b' => null, 'c' => ['d' => 'D', 'e' => null]],
- $result
- );
- }
- /**
- * @see DolibarrSyncService::sanitizeDolibarrData()
- */
- public function testSanitizeDolibarrDataWithNull(): void
- {
- $dolibarrSyncService = $this->getMockForMethod('sanitizeDolibarrData');
- $result = $dolibarrSyncService->sanitizeDolibarrData(null);
- $this->assertEquals(null, $result);
- }
- /**
- * @see DolibarrSyncService::getOrganizationPostalAddress()
- */
- public function testGetOrganizationPostalAddress(): void
- {
- $dolibarrSyncService = $this->getMockForMethod('getOrganizationPostalAddress');
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $organizationAddressPostal1 = $this->getMockBuilder(OrganizationAddressPostal::class)->getMock();
- $organizationAddressPostal2 = $this->getMockBuilder(OrganizationAddressPostal::class)->getMock();
- $organizationAddressPostal3 = $this->getMockBuilder(OrganizationAddressPostal::class)->getMock();
- $addressPostal = $this->getMockBuilder(AddressPostal::class)->getMock();
- $organizationAddressPostal1->method('getType')->willReturn(AddressPostalOrganizationTypeEnum::ADDRESS_PRACTICE);
- $organizationAddressPostal2->method('getType')->willReturn(AddressPostalOrganizationTypeEnum::ADDRESS_BILL);
- $organizationAddressPostal3->method('getType')->willReturn(AddressPostalOrganizationTypeEnum::ADDRESS_OTHER);
- $organizationAddressPostal2->method('getAddressPostal')->willReturn($addressPostal);
- $organization->expects($this->once())
- ->method('getOrganizationAddressPostals')
- ->willReturn(
- new ArrayCollection([$organizationAddressPostal1, $organizationAddressPostal2, $organizationAddressPostal3])
- );
- $this->assertEquals(
- $addressPostal,
- $dolibarrSyncService->getOrganizationPostalAddress($organization)
- );
- }
- /**
- * @see DolibarrSyncService::getOrganizationPostalAddress()
- */
- public function testGetOrganizationPostalAddressNoResult(): void
- {
- $dolibarrSyncService = $this->getMockForMethod('getOrganizationPostalAddress');
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $organization->expects($this->once())
- ->method('getOrganizationAddressPostals')
- ->willReturn(new ArrayCollection([]));
- $this->assertEquals(
- null,
- $dolibarrSyncService->getOrganizationPostalAddress($organization)
- );
- }
- /**
- * @see DolibarrSyncService::getOrganizationPhone()
- */
- public function testGetOrganizationPhoneWithExistingPhone(): void
- {
- $dolibarrSyncService = $this->getMockForMethod('getOrganizationPhone');
- $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
- $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
- $contactPoint3 = $this->getMockBuilder(ContactPoint::class)->getMock();
- $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER);
- $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::BILL);
- $contactPoint3->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL);
- $phone = $this->getMockBuilder(PhoneNumber::class)->disableOriginalConstructor()->getMock();
- $contactPoint2->method('getTelphone')->willReturn($phone);
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $organization
- ->expects($this->once())
- ->method('getContactPoints')
- ->willReturn(
- new ArrayCollection([$contactPoint1, $contactPoint2, $contactPoint3])
- );
- $dolibarrSyncService->expects(self::once())->method('formatPhoneNumber')->with($phone)->willReturn('+33161626365');
- $this->assertEquals(
- '+33161626365',
- $dolibarrSyncService->getOrganizationPhone($organization)
- );
- }
- /**
- * @see DolibarrSyncService::getOrganizationPhone()
- */
- public function testGetOrganizationPhoneWithMobilePhone()
- {
- $dolibarrSyncService = $this->getMockForMethod('getOrganizationPhone');
- $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
- $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
- $contactPoint3 = $this->getMockBuilder(ContactPoint::class)->getMock();
- $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER);
- $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::BILL);
- $contactPoint3->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL);
- $contactPoint2->expects($this->once())->method('getTelphone')->willReturn(null);
- $mobilePhone = $this->getMockBuilder(PhoneNumber::class)->disableOriginalConstructor()->getMock();
- $contactPoint2->method('getMobilPhone')->willReturn($mobilePhone);
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $organization
- ->expects($this->once())
- ->method('getContactPoints')
- ->willReturn(
- new ArrayCollection([$contactPoint1, $contactPoint2, $contactPoint3])
- );
- $dolibarrSyncService->expects(self::once())->method('formatPhoneNumber')->with($mobilePhone)->willReturn('+33661626365');
- $this->assertEquals(
- '+33661626365',
- $dolibarrSyncService->getOrganizationPhone($organization)
- );
- }
- /**
- * @see DolibarrSyncService::getOrganizationPhone()
- */
- public function testGetOrganizationPhoneWithNoPhone()
- {
- $dolibarrSyncService = $this->getMockForMethod('getOrganizationPhone');
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $organization
- ->expects($this->once())
- ->method('getContactPoints')
- ->willReturn(new ArrayCollection([]));
- $dolibarrSyncService->expects(self::never())->method('formatPhoneNumber');
- $this->assertEquals(
- null,
- $dolibarrSyncService->getOrganizationPhone($organization)
- );
- }
- /**
- * @see DolibarrSyncService::getOrganizationEmail()
- */
- public function testGetOrganizationEmailWithExistingEmail()
- {
- $dolibarrSyncService = $this->getMockForMethod('getOrganizationEmail');
- $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
- $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
- $contactPoint3 = $this->getMockBuilder(ContactPoint::class)->getMock();
- $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER);
- $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::BILL);
- $contactPoint3->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL);
- $contactPoint2->method('getEmail')->willReturn('email@email.com');
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $organization
- ->expects($this->once())
- ->method('getContactPoints')
- ->willReturn(
- new ArrayCollection([$contactPoint1, $contactPoint2, $contactPoint3])
- );
- $this->assertEquals(
- 'email@email.com',
- $dolibarrSyncService->getOrganizationEmail($organization)
- );
- }
- /**
- * @see DolibarrSyncService::getOrganizationEmail()
- */
- public function testGetOrganizationEmailWithNoEmail()
- {
- $dolibarrSyncService = $this->getMockForMethod('getOrganizationEmail');
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $organization
- ->expects($this->once())
- ->method('getContactPoints')
- ->willReturn(new ArrayCollection([]));
- $this->assertEquals(
- null,
- $dolibarrSyncService->getOrganizationEmail($organization)
- );
- }
- /**
- * @see DolibarrSyncService::getOrganizationNetworkId()
- */
- public function testGetOrganizationNetworkId()
- {
- $dolibarrSyncService = $this->getMockForMethod('getOrganizationNetworkId');
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $network = $this->getMockBuilder(Network::class)->getMock();
- $network->method('getId')->willReturn(3);
- $networkOrganization = $this->getMockBuilder(NetworkOrganization::class)->getMock();
- $networkOrganization->method('getNetwork')->willReturn($network);
- $organization->method('getNetworkOrganizations')->willReturn(new ArrayCollection([$networkOrganization]));
- $this->assertEquals(
- 3,
- $dolibarrSyncService->getOrganizationNetworkId($organization)
- );
- }
- /**
- * @see DolibarrSyncService::getOrganizationNetworkId()
- */
- public function testGetOrganizationNetworkIdWithMultipleResult()
- {
- $dolibarrSyncService = $this->getMockForMethod('getOrganizationNetworkId');
- $network1 = $this->getMockBuilder(Network::class)->getMock();
- $network1->method('getId')->willReturn(3);
- $networkOrganization1 = $this->getMockBuilder(NetworkOrganization::class)->getMock();
- $networkOrganization1->method('getNetwork')->willReturn($network1);
- $networkOrganization1->method('getEndDate')->willReturn(new \DateTime('2000-01-01'));
- $network2 = $this->getMockBuilder(Network::class)->getMock();
- $network2->method('getId')->willReturn(4);
- $networkOrganization2 = $this->getMockBuilder(NetworkOrganization::class)->getMock();
- $networkOrganization2->method('getNetwork')->willReturn($network2);
- $networkOrganization2->method('getEndDate')->willReturn(null);
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $organization->method('getNetworkOrganizations')->willReturn(
- new ArrayCollection([$networkOrganization1, $networkOrganization2])
- );
- $this->assertEquals(
- 4,
- $dolibarrSyncService->getOrganizationNetworkId($organization)
- );
- }
- /**
- * @see DolibarrSyncService::getOrganizationNetworkId()
- */
- public function testGetOrganizationNetworkIdWithNoResult()
- {
- $dolibarrSyncService = $this->getMockForMethod('getOrganizationNetworkId');
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $network = $this->getMockBuilder(Network::class)->getMock();
- $network->method('getId')->willReturn(3);
- $networkOrganization = $this->getMockBuilder(NetworkOrganization::class)->getMock();
- $networkOrganization->method('getNetwork')->willReturn($network);
- $networkOrganization->method('getEndDate')->willReturn(new \DateTime('2000-01-01'));
- $organization->method('getNetworkOrganizations')->willReturn(new ArrayCollection([$networkOrganization]));
- $this->assertEquals(
- null,
- $dolibarrSyncService->getOrganizationNetworkId($organization)
- );
- }
- /**
- * @see DolibarrSyncService::countWithMission()
- */
- public function testCountWithMission()
- {
- $dolibarrSyncService = $this->getMockForMethod('countWithMission');
- $members = [
- 123 => [FunctionEnum::PRESIDENT->value, FunctionEnum::TEACHER->value],
- 124 => [FunctionEnum::TEACHER->value],
- 125 => [FunctionEnum::STUDENT->value],
- 126 => [FunctionEnum::TREASURER->value],
- ];
- $this->assertEquals(
- 2,
- $dolibarrSyncService->countWithMission([FunctionEnum::TEACHER->value], $members)
- );
- $this->assertEquals(
- 3,
- $dolibarrSyncService->countWithMission(
- [FunctionEnum::TEACHER->value, FunctionEnum::TREASURER->value],
- $members
- )
- );
- $this->assertEquals(
- 1,
- $dolibarrSyncService->countWithMission([FunctionEnum::STUDENT->value], $members)
- );
- $this->assertEquals(
- 0,
- $dolibarrSyncService->countWithMission([FunctionEnum::ARCHIVIST->value], $members)
- );
- }
- /**
- * @see DolibarrSyncService::getPersonContact()
- */
- public function testGetPersonContact()
- {
- $dolibarrSyncService = $this->getMockForMethod('getPersonContact');
- $person = $this->getMockBuilder(Person::class)->getMock();
- $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
- $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
- $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER);
- $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL);
- $person->expects($this->once())->method('getContactPoints')->willReturn(new ArrayCollection([$contactPoint1, $contactPoint2]));
- $this->assertEquals(
- $contactPoint2,
- $dolibarrSyncService->getPersonContact($person)
- );
- $person2 = $this->getMockBuilder(Person::class)->getMock();
- $person2->expects($this->once())->method('getContactPoints')->willReturn(new ArrayCollection([]));
- $this->assertEquals(
- null,
- $dolibarrSyncService->getPersonContact($person2)
- );
- }
- /**
- * @see DolibarrSyncService::formatContactPosition()
- */
- public function testFormatContactPosition()
- {
- $dolibarrSyncService = $this->getMockForMethod('formatContactPosition');
- $this->translator->method('trans')->willReturnMap(
- [
- [FunctionEnum::PRESIDENT->value, ['gender' => 'X'], null, null, 'Président(e)'],
- [FunctionEnum::PRESIDENT->value, ['gender' => 'M'], null, null, 'Président'],
- [FunctionEnum::PRESIDENT->value, ['gender' => 'F'], null, null, 'Présidente'],
- [FunctionEnum::DIRECTOR->value, ['gender' => 'X'], null, null, 'Directeur(ice)'],
- [FunctionEnum::DIRECTOR->value, ['gender' => 'M'], null, null, 'Directeur'],
- [FunctionEnum::DIRECTOR->value, ['gender' => 'F'], null, null, 'Directrice'],
- [FunctionEnum::TEACHER->value, ['gender' => 'X'], null, null, 'Professeur(e)'],
- [FunctionEnum::ARCHIVIST->value, ['gender' => 'X'], null, null, 'Archiviste'],
- [FunctionEnum::TREASURER->value, ['gender' => 'X'], null, null, 'Trésorier(ère)'],
- [FunctionEnum::ADMINISTRATIVE_STAFF->value, ['gender' => 'X'], null, null, 'Personnel administratif'],
- ]
- );
- $this->assertEquals(
- 'Président(e)',
- $dolibarrSyncService->formatContactPosition([FunctionEnum::PRESIDENT->value])
- );
- $this->assertEquals(
- 'Président',
- $dolibarrSyncService->formatContactPosition([FunctionEnum::PRESIDENT->value], 'MISTER')
- );
- $this->assertEquals(
- 'Présidente',
- $dolibarrSyncService->formatContactPosition([FunctionEnum::PRESIDENT->value], 'MISS')
- );
- $this->assertEquals(
- 'Présidente, Directrice',
- $dolibarrSyncService->formatContactPosition(
- [FunctionEnum::PRESIDENT->value, FunctionEnum::DIRECTOR->value],
- 'MISS'
- )
- );
- $this->assertEquals(
- 'Président, Directeur',
- $dolibarrSyncService->formatContactPosition(
- [FunctionEnum::PRESIDENT->value, FunctionEnum::DIRECTOR->value, FunctionEnum::ADHERENT->value],
- 'MISTER'
- )
- );
- $this->assertEquals(
- 'Président, Directeur',
- $dolibarrSyncService->formatContactPosition(
- [FunctionEnum::PRESIDENT->value, FunctionEnum::DIRECTOR->value, FunctionEnum::ADHERENT->value],
- 'MISTER'
- )
- );
- $this->assertEquals(
- 'Président(e), Directeur(ice), Professeur(e), Archiviste, Trésorier(ère), Pers...',
- $dolibarrSyncService->formatContactPosition(
- [
- FunctionEnum::PRESIDENT->value,
- FunctionEnum::DIRECTOR->value,
- FunctionEnum::TEACHER->value,
- FunctionEnum::ARCHIVIST->value,
- FunctionEnum::TREASURER->value,
- FunctionEnum::ADMINISTRATIVE_STAFF->value,
- ],
- 'X'
- )
- );
- }
- /**
- * @see DolibarrSyncService::formatPhoneNumber()
- */
- public function testFormatPhoneNumber()
- {
- $dolibarrSyncService = $this->getMockForMethod('formatPhoneNumber');
- $phoneUtil = PhoneNumberUtil::getInstance();
- $phoneNumber = $phoneUtil->parse('01 02 03 04 05', 'FR');
- $this->assertEquals(
- '+33102030405',
- $dolibarrSyncService->formatPhoneNumber($phoneNumber)
- );
- }
- public function testGetExpectedTagsFor()
- {
- $dolibarrSyncService = $this->getMockForMethod('getExpectedTagsFor');
- $organization1 = $this->getMockBuilder(Organization::class)->getMock();
- $organization1->method('getLegalStatus')->willReturn(LegalEnum::ASSOCIATION_LAW_1901);
- $settings1 = $this->getMockBuilder(Settings::class)->getMock();
- $settings1->method('getProduct')->willReturn(SettingsProductEnum::SCHOOL);
- $organization1->method('getSettings')->willReturn($settings1);
- $organization2 = $this->getMockBuilder(Organization::class)->getMock();
- $organization2->method('getLegalStatus')->willReturn(LegalEnum::ASSOCIATION_LAW_1901);
- $settings2 = $this->getMockBuilder(Settings::class)->getMock();
- $settings2->method('getProduct')->willReturn(SettingsProductEnum::SCHOOL_PREMIUM);
- $organization2->method('getSettings')->willReturn($settings2);
- $organization3 = $this->getMockBuilder(Organization::class)->getMock();
- $organization3->method('getLegalStatus')->willReturn(LegalEnum::LOCAL_AUTHORITY);
- $settings3 = $this->getMockBuilder(Settings::class)->getMock();
- $settings3->method('getProduct')->willReturn(SettingsProductEnum::SCHOOL);
- $organization3->method('getSettings')->willReturn($settings3);
- $organization4 = $this->getMockBuilder(Organization::class)->getMock();
- $organization4->method('getLegalStatus')->willReturn(LegalEnum::LOCAL_AUTHORITY);
- $settings4 = $this->getMockBuilder(Settings::class)->getMock();
- $settings4->method('getProduct')->willReturn(SettingsProductEnum::SCHOOL_PREMIUM);
- $organization4->method('getSettings')->willReturn($settings4);
- $organization5 = $this->getMockBuilder(Organization::class)->getMock();
- $organization5->method('getLegalStatus')->willReturn(LegalEnum::LOCAL_AUTHORITY);
- $settings5 = $this->getMockBuilder(Settings::class)->getMock();
- $settings5->method('getProduct')->willReturn(SettingsProductEnum::ARTIST);
- $organization5->method('getSettings')->willReturn($settings5);
- $this->assertEquals(
- $dolibarrSyncService->getExpectedTagsFor($organization1),
- [67]
- );
- $this->assertEquals(
- $dolibarrSyncService->getExpectedTagsFor($organization2),
- [69]
- );
- $this->assertEquals(
- $dolibarrSyncService->getExpectedTagsFor($organization3),
- [68]
- );
- $this->assertEquals(
- $dolibarrSyncService->getExpectedTagsFor($organization4),
- [70]
- );
- $this->assertEquals(
- $dolibarrSyncService->getExpectedTagsFor($organization5),
- []
- );
- }
- }
|