| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786 |
- <?php
- namespace App\Tests\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\Organization\AddressPostalOrganizationTypeEnum;
- use App\Enum\Organization\SettingsProductEnum;
- use App\Repository\Access\AccessRepository;
- use App\Repository\Access\FunctionTypeRepository;
- use App\Repository\Organization\OrganizationRepository;
- use App\Service\Dolibarr\DolibarrApiService;
- use App\Service\Dolibarr\DolibarrSyncService;
- use App\Service\Rest\Operation\CreateOperation;
- use App\Service\Rest\Operation\UpdateOperation;
- use Doctrine\Common\Collections\ArrayCollection;
- use JetBrains\PhpStorm\Pure;
- use libphonenumber\PhoneNumber;
- use libphonenumber\PhoneNumberUtil;
- 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 static function findDolibarrContactFor(array $dolibarrContacts, Person $person): ?array { return parent::findDolibarrContactFor($dolibarrContacts, $person); }
- public function getActiveMembersIndex(): array { return parent::getActiveMembersIndex(); }
- public static function sanitizeDolibarrData(?array $data): ?array { return parent::sanitizeDolibarrData($data); }
- public function getOrganizationPostalAddress(Organization $organization): ?AddressPostal { return parent::getOrganizationPostalAddress($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 static 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 static function formatPhoneNumber(PhoneNumber $phoneNumber): string { return parent::formatPhoneNumber($phoneNumber); }
- public static function getChanges(array $initialData, array $newData): array { return parent::getChanges($initialData, $newData); }
- }
- class DolibarrSyncServiceTest extends TestCase
- {
- private OrganizationRepository $organizationRepository;
- private AccessRepository $accessRepository;
- private FunctionTypeRepository $functionTypeRepository;
- private DolibarrApiService $dolibarrApiService;
- private TranslatorInterface $translator;
- private LoggerInterface $logger;
- 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->translator = $this->getMockBuilder(TranslatorInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->logger = $this->getMockBuilder(LoggerInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->logger->method('info')->willReturnSelf();
- $this->logger->method('debug')->willReturnSelf();
- $this->logger->method('warning')->willReturnSelf();
- $this->logger->method('error')->willReturnSelf();
- }
- #[Pure]
- private function newDolibarrSyncService(): TestableDolibarrSyncService
- {
- return new TestableDolibarrSyncService(
- $this->organizationRepository,
- $this->accessRepository,
- $this->functionTypeRepository,
- $this->dolibarrApiService,
- $this->translator,
- $this->logger
- );
- }
- private function getJsonContentFromFixture(string $filename): array {
- $filepath = dirname(__FILE__) . '/fixtures/' . $filename;
- return json_decode(file_get_contents($filepath), true);
- }
- public function testScan() {
- // mock services and special methods from repos
- $this->dolibarrApiService
- ->expects($this->once())
- ->method('getAllClients')
- ->willReturn(
- $this->getJsonContentFromFixture('thirdparty.json')
- );
- $this->dolibarrApiService->method('getSociety')->willReturnMap(
- [
- [12097, ['id' => 711]],
- [91295, ['id' => 5086]]
- ]
- );
- $this->accessRepository
- ->expects($this->once())
- ->method('getAllActiveMembersAndMissions')
- ->willReturn(
- [
- ['id' => 1, 'organization_id' => 37306, 'mission' => FunctionEnum::PRESIDENT()->getValue()],
- ['id' => 1, 'organization_id' => 37306, 'mission' => FunctionEnum::ADHERENT()->getValue()],
- ['id' => 2, 'organization_id' => 37306, 'mission' => FunctionEnum::TREASURER()->getValue()],
- ['id' => 2, 'organization_id' => 37306, 'mission' => FunctionEnum::ADHERENT()->getValue()],
- ['id' => 3, 'organization_id' => 37306, 'mission' => FunctionEnum::STUDENT()->getValue()],
- ['id' => 3, 'organization_id' => 37306, 'mission' => FunctionEnum::ADHERENT()->getValue()],
- ]
- );
- // Function types
- $functionType1 = $this->getMockBuilder(FunctionType::class)->getMock();
- $functionType1->method('getMission')->willReturn(FunctionEnum::DIRECTOR()->getValue());
- $functionType2 = $this->getMockBuilder(FunctionType::class)->getMock();
- $functionType2->method('getMission')->willReturn(FunctionEnum::PRESIDENT()->getValue());
- $this->functionTypeRepository
- ->expects($this->once())
- ->method('findBy')
- ->with(['roleByDefault' => RoleEnum::ROLE_ADMIN()->getValue()])
- ->willReturn([$functionType1, $functionType2]);
- // Organization's name
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $organization->method('getId')->willReturn(37306);
- $organization->method('getName')->willReturn("Etablissement d'Enseignement Artistique");
- // Postal address
- $organizationAddressPostal = $this->getMockBuilder(OrganizationAddressPostal::class)->getMock();
- $addressPostal = $this->getMockBuilder(AddressPostal::class)->getMock();
- $addressPostal->method('getStreetAddress')->willReturn('21b baker street');
- $addressPostal->method('getStreetAddressSecond')->willReturn('');
- $addressPostal->method('getStreetAddressThird')->willReturn('');
- $addressPostal->method('getAddressOwner')->willReturn('');
- $addressPostal->method('getPostalCode')->willReturn('250 329');
- $addressPostal->method('getAddressCity')->willReturn('Londres');
- $organizationAddressPostal->method('getType')->willReturn(AddressPostalOrganizationTypeEnum::ADDRESS_CONTACT()->getValue());
- $organizationAddressPostal->method('getAddressPostal')->willReturn($addressPostal);
- $organization->method('getOrganizationAddressPostals')->willReturn(
- new ArrayCollection([$organizationAddressPostal])
- );
- // Email and phone
- $phoneUtil = PhoneNumberUtil::getInstance();
- $contactPoint = $this->getMockBuilder(ContactPoint::class)->getMock();
- $contactPoint->method('getContactType')->willReturn(ContactPointTypeEnum::CONTACT()->getValue());
- $contactPoint->method('getEmail')->willReturn('email@email.com');
- $phoneNumber = $phoneUtil->parse('01 02 03 04 05', 'FR');
- $contactPoint->method('getTelphone')->willReturn($phoneNumber);
- $organization->method('getContactPoints')->willReturn(
- new ArrayCollection([$contactPoint])
- );
- // Network
- $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]));
- // Product
- $settings = $this->getMockBuilder(Settings::class)->getMock();
- $settings->method('getProduct')->willReturn(SettingsProductEnum::SCHOOL()->getValue());
- $organization->method('getSettings')->willReturn($settings);
- // Get dolibarr contacts
- $this->dolibarrApiService
- ->method('getContacts')
- ->with(1726)
- ->willReturn(
- array_filter(
- $this->getJsonContentFromFixture('contacts.json'),
- static function ($c) {
- return in_array(
- (int)$c["array_options"]["options_2iopen_person_id"],
- [
- 108939, // existing person, to be updated
- 156252 // no matching person, to be deleted
- // a new contact shall be created too
- ]
- ); }
- )
- );
- $this->organizationRepository->method('find')->willReturn($organization);
- $access1 = $this->getMockBuilder(Access::class)->getMock();
- $access1->method('getId')->willReturn(1);
- $person1 = $this->getMockBuilder(Person::class)->getMock();
- $person1->method('getId')->willReturn(108939);
- $person1->method('getName')->willReturn('Holmes');
- $person1->method('getGender')->willReturn('MISTER');
- $person1->method('getGivenName')->willReturn('Sherlock');
- $person1->method('getGivenName')->willReturn('Sherlock');
- $personContactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
- $personContactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::CONTACT()->getValue());
- $personContactPoint1->method('getEmail')->willReturn('sherlock@holmes.com');
- $phoneNumber1 = $phoneUtil->parse('02 98 76 54 32', 'FR');
- $personContactPoint1->method('getTelphone')->willReturn($phoneNumber1);
- $personContactPoint1->method('getMobilPhone')->willReturn(null);
- $person1->method('getContactPoints')->willReturn(
- new ArrayCollection([$personContactPoint1])
- );
- $access1->method('getPerson')->willReturn($person1);
- $access2 = $this->getMockBuilder(Access::class)->getMock();
- $access2->method('getId')->willReturn(1);
- $person2 = $this->getMockBuilder(Person::class)->getMock();
- $person2->method('getId')->willReturn(1000);
- $person2->method('getName')->willReturn('Watson');
- $person2->method('getGender')->willReturn('MISTER');
- $person2->method('getGivenName')->willReturn('John');
- $person2->method('getGivenName')->willReturn('John');
- $personContactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
- $personContactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::CONTACT()->getValue());
- $personContactPoint2->method('getEmail')->willReturn('docteur@watson.com');
- $phoneNumber2 = $phoneUtil->parse('02 10 11 12 13', 'FR');
- $personContactPoint2->method('getTelphone')->willReturn($phoneNumber2);
- $personContactPoint2->method('getMobilPhone')->willReturn(null);
- $person2->method('getContactPoints')->willReturn(
- new ArrayCollection([$personContactPoint2])
- );
- $access2->method('getPerson')->willReturn($person2);
- $this->accessRepository->method('find')->willReturnMap(
- [
- [1, null, null, $access1],
- [2, null, null, $access2],
- ]
- );
- $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"],
- ]
- );
- $syncService = $this->newDolibarrSyncService();
- $operations = $syncService->scan();
- $this->assertCount(4, $operations);
- $this->assertEquals(
- [
- '[PUT thirdparties/1726]',
- "address : `\n217, rue Raoul Follereau\n` => `21b baker street`",
- 'zip : `74300` => `250 329`',
- 'town : `CLUSES` => `Londres`',
- 'email : `` => `email@email.com`',
- 'phone : `+33678403010` => `+33102030405`',
- 'parent : `` => `711`',
- "array_options.options_2iopeninfoopentalent : `` => `Nombre d'élèves : 1\nNombre d'adhérents : 3\nNombre d'accès admin : 1`"
- ],
- $operations[0]->getChangeLog()
- );
- $this->assertEquals(
- [
- '[PUT contacts/5868]',
- 'civility_code : `MME` => ``',
- 'lastname : `DUPONT` => `Holmes`',
- 'firstname : `Valerie` => `Sherlock`',
- 'email : `abcd@hotmail.com` => ``',
- 'phone_pro : `+33478570000` => ``',
- 'phone_mobile : `+33682980000` => ``',
- 'poste : `Secrétaire` => ``'
- ],
- $operations[1]->getChangeLog()
- );
- $this->assertEquals(
- [
- '[POST contacts]',
- 'civility_code : (new) => ``',
- 'lastname : (new) => `Watson`',
- 'firstname : (new) => `John`',
- 'email : (new) => ``',
- 'phone_pro : (new) => ``',
- 'phone_mobile : (new) => ``',
- 'poste : (new) => ``',
- 'statut : (new) => `1`',
- 'array_options.options_2iopen_person_id : (new) => `1000`',
- 'socid : (new) => `1726`'
- ],
- $operations[2]->getChangeLog()
- );
- $this->assertEquals(
- ['[PUT contacts/5869]', 'statut : `1` => `0`'],
- $operations[3]->getChangeLog()
- );
- }
- public function testExecuteError()
- {
- $operation = new CreateOperation('operation 1', 'thirdparty', ['data' => 1]);
- $this->assertEquals($operation->getStatus(), $operation::STATUS_READY);
- $responseError = $this->getMockBuilder(ResponseInterface::class)->getMock();
- $responseError->method('getStatusCode')->willReturn(500);
- $this->dolibarrApiService->method('request')->willReturn($responseError);
- // POST operation will returned a server error
- $syncService = $this->newDolibarrSyncService();
- $operation = $syncService->execute([$operation])[0];
- $this->assertEquals($operation::STATUS_ERROR, $operation->getStatus());
- }
- public function testExecuteInvalid()
- {
- $operation = new UpdateOperation('operation 1', 'thirdparty', 1, ['data' => 1]);
- $responseInvalid = $this->getMockBuilder(ResponseInterface::class)->getMock();
- $responseInvalid->method('getStatusCode')->willReturn(200);
- $responseInvalid->method('toArray')->willReturn(['data' => 0]);
- $this->dolibarrApiService->method('request')->willReturn($responseInvalid);
- // POST operation will return a different content that the one which were posted, this should log a warning
- $this->logger->expects($this->once())->method('warning');
- $syncService = $this->newDolibarrSyncService();
- $operation = $syncService->execute([$operation])[0];
- $this->assertEquals($operation::STATUS_DONE, $operation->getStatus());
- }
- public function testExecuteOk() {
- $operation = new CreateOperation('operation 1', 'thirdparty', ['data' => 1]);
- $responseOk = $this->getMockBuilder(ResponseInterface::class)->getMock();
- $responseOk->method('getStatusCode')->willReturn(200);
- $responseOk->method('toArray')->willReturn(['data' => 1]);
- $this->dolibarrApiService->method('request')->willReturn($responseOk);
- $syncService = $this->newDolibarrSyncService();
- $operation = $syncService->execute([$operation])[0];
- $this->assertEquals($operation::STATUS_DONE, $operation->getStatus());
- }
- public function testGetDolibarrSocietiesIndex() {
- $this->dolibarrApiService
- ->expects($this->once())
- ->method('getAllClients')
- ->willReturn(
- $this->getJsonContentFromFixture('thirdparties.json')
- );
- $syncService = $this->newDolibarrSyncService();
- $index = $syncService->getDolibarrSocietiesIndex();
- $this->assertEquals("13930", $index[13930]['array_options']['options_2iopen_organization_id']);
- }
- public function testFindDolibarrContactFor() {
- $contacts = $this->getJsonContentFromFixture('contacts.json');
- // Find by id
- $person1 = $this->getMockBuilder(Person::class)->getMock();
- $person1->method('getId')->willReturn(108939);
- $contact1 = TestableDolibarrSyncService::findDolibarrContactFor($contacts, $person1);
- $this->assertEquals("5868", $contact1['id']);
- // Find by full name (contact already has another person id, it should not be returned)
- $person2 = $this->getMockBuilder(Person::class)->getMock();
- $person2->method('getId')->willReturn(-1);
- $person2->method('getName')->willReturn('dupont');
- $person2->method('getGivenName')->willReturn('Valerie');
- $contact2 = TestableDolibarrSyncService::findDolibarrContactFor($contacts, $person2);
- $this->assertEquals(null, $contact2);
- // Find by full name (contact has no person id, it should be returned)
- $person3 = $this->getMockBuilder(Person::class)->getMock();
- $person3->method('getId')->willReturn(-1);
- $person3->method('getName')->willReturn('ZORRO');
- $person3->method('getGivenName')->willReturn('Fabrice');
- $contact3 = TestableDolibarrSyncService::findDolibarrContactFor($contacts, $person3);
- $this->assertEquals("5872", $contact3['id']);
- // Do not find
- $person4 = $this->getMockBuilder(Person::class)->getMock();
- $person4->method('getId')->willReturn(-1);
- $contact4 = TestableDolibarrSyncService::findDolibarrContactFor($contacts, $person4);
- $this->assertEquals(null, $contact4);
- }
- public function testActiveMembersIndex() {
- $this->accessRepository
- ->expects($this->once())
- ->method('getAllActiveMembersAndMissions')
- ->willReturn(
- [
- ['id' => 123, 'organization_id' => 1, 'mission' => FunctionEnum::PRESIDENT()->getValue()],
- ['id' => 123, 'organization_id' => 1, 'mission' => FunctionEnum::TEACHER()->getValue()],
- ['id' => 124, 'organization_id' => 1, 'mission' => FunctionEnum::ADMINISTRATIVE_STAFF()->getValue()],
- ['id' => 125, 'organization_id' => 2, 'mission' => FunctionEnum::ADHERENT()->getValue()],
- ]
- );
- $syncService = $this->newDolibarrSyncService();
- $this->assertEquals(
- [
- 1 => [
- 123 => [FunctionEnum::PRESIDENT()->getValue(), FunctionEnum::TEACHER()->getValue()],
- 124 => [FunctionEnum::ADMINISTRATIVE_STAFF()->getValue()]
- ],
- 2 => [
- 125 => [FunctionEnum::ADHERENT()->getValue()]
- ]
- ],
- $syncService->getActiveMembersIndex()
- );
- }
- public function testSanitizeDolibarrData() {
- $this->assertEquals(null, TestableDolibarrSyncService::sanitizeDolibarrData(null));
- $this->assertEquals(
- ['a' => 'A', 'b' => null, 'c' => ['d' => 'D', 'e' => null]],
- TestableDolibarrSyncService::sanitizeDolibarrData(
- ['a' => 'A', 'b' => '', 'c' => ['d' => 'D', 'e' => '']]
- )
- );
- }
- public function testGetOrganizationPostalAddress() {
- $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()->getValue());
- $organizationAddressPostal2->method('getType')->willReturn(AddressPostalOrganizationTypeEnum::ADDRESS_BILL()->getValue());
- $organizationAddressPostal3->method('getType')->willReturn(AddressPostalOrganizationTypeEnum::ADDRESS_OTHER()->getValue());
- $organizationAddressPostal2->method('getAddressPostal')->willReturn($addressPostal);
- $organization->expects($this->once())
- ->method('getOrganizationAddressPostals')
- ->willReturn(
- new ArrayCollection([$organizationAddressPostal1, $organizationAddressPostal2, $organizationAddressPostal3])
- );
- $syncService = $this->newDolibarrSyncService($organization);
- $this->assertEquals(
- $addressPostal,
- $syncService->getOrganizationPostalAddress($organization)
- );
- $organization2 = $this->getMockBuilder(Organization::class)->getMock();
- $organization2->expects($this->once())
- ->method('getOrganizationAddressPostals')
- ->willReturn(new ArrayCollection([]));
- $this->assertEquals(
- null,
- $syncService->getOrganizationPostalAddress($organization2)
- );
- }
- public function testGetOrganizationPhoneWithExistingPhone()
- {
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
- $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
- $contactPoint3 = $this->getMockBuilder(ContactPoint::class)->getMock();
- $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER()->getValue());
- $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::BILL()->getValue());
- $contactPoint3->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL()->getValue());
- $phoneUtil = PhoneNumberUtil::getInstance();
- $phoneNumber = $phoneUtil->parse('0161626365', "FR");
- $contactPoint2->method('getTelphone')->willReturn($phoneNumber);
- $organization
- ->expects($this->once())
- ->method('getContactPoints')
- ->willReturn(
- new ArrayCollection([$contactPoint1, $contactPoint2, $contactPoint3])
- );
- $syncService = $this->newDolibarrSyncService();
- $this->assertEquals(
- '+33161626365',
- $syncService->getOrganizationPhone($organization)
- );
- }
- public function testGetOrganizationPhoneWithMobilePhone() {
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
- $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
- $contactPoint3 = $this->getMockBuilder(ContactPoint::class)->getMock();
- $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER()->getValue());
- $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::BILL()->getValue());
- $contactPoint3->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL()->getValue());
- $contactPoint2->expects($this->once())->method('getTelphone')->willReturn(null);
- $phoneUtil = PhoneNumberUtil::getInstance();
- $phoneNumber = $phoneUtil->parse('0661626365', "FR");
- $contactPoint2->method('getMobilPhone')->willReturn($phoneNumber);
- $organization
- ->expects($this->once())
- ->method('getContactPoints')
- ->willReturn(
- new ArrayCollection([$contactPoint1, $contactPoint2, $contactPoint3])
- );
- $syncService = $this->newDolibarrSyncService();
- $this->assertEquals(
- '+33661626365',
- $syncService->getOrganizationPhone($organization)
- );
- }
- public function testGetOrganizationPhoneWithNoPhone() {
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $organization
- ->expects($this->once())
- ->method('getContactPoints')
- ->willReturn(new ArrayCollection([]));
- $syncService = $this->newDolibarrSyncService();
- $this->assertEquals(
- null,
- $syncService->getOrganizationPhone($organization)
- );
- }
- public function testGetOrganizationEmailWithExistingEmail() {
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
- $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
- $contactPoint3 = $this->getMockBuilder(ContactPoint::class)->getMock();
- $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER()->getValue());
- $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::BILL()->getValue());
- $contactPoint3->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL()->getValue());
- $contactPoint2->method('getEmail')->willReturn('email@email.com');
- $organization
- ->expects($this->once())
- ->method('getContactPoints')
- ->willReturn(
- new ArrayCollection([$contactPoint1, $contactPoint2, $contactPoint3])
- );
- $syncService = $this->newDolibarrSyncService();
- $this->assertEquals(
- 'email@email.com',
- $syncService->getOrganizationEmail($organization)
- );
- }
- public function testGetOrganizationEmailWithNoEmail() {
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $organization
- ->expects($this->once())
- ->method('getContactPoints')
- ->willReturn(new ArrayCollection([]));
- $syncService = $this->newDolibarrSyncService();
- $this->assertEquals(
- null,
- $syncService->getOrganizationEmail($organization)
- );
- }
- public function testGetOrganizationNetworkId() {
- $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]));
- $syncService = $this->newDolibarrSyncService();
- $this->assertEquals(
- 3,
- $syncService->getOrganizationNetworkId($organization)
- );
- }
- public function testGetOrganizationNetworkIdWithMultipleResult() {
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $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->method('getNetworkOrganizations')->willReturn(
- new ArrayCollection([$networkOrganization1, $networkOrganization2])
- );
- $syncService = $this->newDolibarrSyncService();
- $this->assertEquals(
- 4,
- $syncService->getOrganizationNetworkId($organization)
- );
- }
- public function testGetOrganizationNetworkIdWithNoResult() {
- $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]));
- $syncService = $this->newDolibarrSyncService();
- $this->assertEquals(
- null,
- $syncService->getOrganizationNetworkId($organization)
- );
- }
- public function testCountWithMission() {
- $members = [
- 123 => [FunctionEnum::PRESIDENT()->getValue(), FunctionEnum::TEACHER()->getValue()],
- 124 => [FunctionEnum::TEACHER()->getValue()],
- 125 => [FunctionEnum::STUDENT()->getValue()],
- 126 => [FunctionEnum::TREASURER()->getValue()],
- ];
- $this->assertEquals(
- 2,
- TestableDolibarrSyncService::countWithMission([FunctionEnum::TEACHER()->getValue()], $members)
- );
- $this->assertEquals(
- 3,
- TestableDolibarrSyncService::countWithMission(
- [FunctionEnum::TEACHER()->getValue(), FunctionEnum::TREASURER()->getValue()],
- $members
- )
- );
- $this->assertEquals(
- 1,
- TestableDolibarrSyncService::countWithMission([FunctionEnum::STUDENT()->getValue()], $members)
- );
- $this->assertEquals(
- 0,
- TestableDolibarrSyncService::countWithMission([FunctionEnum::ARCHIVIST()->getValue()], $members)
- );
- }
- public function testGetPersonContact() {
- $person = $this->getMockBuilder(Person::class)->getMock();
- $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
- $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
- $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER()->getValue());
- $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL()->getValue());
- $person->expects($this->once())->method('getContactPoints')->willReturn(new ArrayCollection([$contactPoint1, $contactPoint2]));
- $syncService = $this->newDolibarrSyncService();
- $this->assertEquals(
- $contactPoint2,
- $syncService->getPersonContact($person)
- );
- $person2 = $this->getMockBuilder(Person::class)->getMock();
- $person2->expects($this->once())->method('getContactPoints')->willReturn(new ArrayCollection([]));
- $this->assertEquals(null, $syncService->getPersonContact($person2));
- }
- public function testFormatContactPosition() {
- $this->translator->method('trans')->willReturnMap(
- [
- [FunctionEnum::PRESIDENT()->getValue(), ['gender' => 'X'], null, null, 'Président(e)'],
- [FunctionEnum::PRESIDENT()->getValue(), ['gender' => 'M'], null, null, 'Président'],
- [FunctionEnum::PRESIDENT()->getValue(), ['gender' => 'F'], null, null, 'Présidente'],
- [FunctionEnum::DIRECTOR()->getValue(), ['gender' => 'X'], null, null, 'Directeur(ice)'],
- [FunctionEnum::DIRECTOR()->getValue(), ['gender' => 'M'], null, null, 'Directeur'],
- [FunctionEnum::DIRECTOR()->getValue(), ['gender' => 'F'], null, null, 'Directrice'],
- ]
- );
- $syncService = $this->newDolibarrSyncService();
- $this->assertEquals(
- 'Président(e)',
- $syncService->formatContactPosition([FunctionEnum::PRESIDENT()->getValue()])
- );
- $this->assertEquals(
- 'Président',
- $syncService->formatContactPosition([FunctionEnum::PRESIDENT()->getValue()], 'MISTER')
- );
- $this->assertEquals(
- 'Présidente',
- $syncService->formatContactPosition([FunctionEnum::PRESIDENT()->getValue()], 'MISS')
- );
- $this->assertEquals(
- 'Présidente, Directrice',
- $syncService->formatContactPosition(
- [FunctionEnum::PRESIDENT()->getValue(), FunctionEnum::DIRECTOR()->getValue()],
- 'MISS'
- )
- );
- $this->assertEquals(
- 'Président, Directeur',
- $syncService->formatContactPosition(
- [FunctionEnum::PRESIDENT()->getValue(), FunctionEnum::DIRECTOR()->getValue(), FunctionEnum::ADHERENT()->getValue()],
- 'MISTER'
- )
- );
- }
- public function testFormatPhoneNumber() {
- $phoneUtil = PhoneNumberUtil::getInstance();
- $phoneNumber = $phoneUtil->parse('01 02 03 04 05', "FR");
- $this->assertEquals(
- '+33102030405',
- TestableDolibarrSyncService::formatPhoneNumber($phoneNumber)
- );
- }
- }
|