| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259 |
- <?php
- declare(strict_types=1);
- namespace App\Tests\Unit\Service\Organization;
- use App\ApiResources\Organization\OrganizationCreationRequest;
- use App\ApiResources\Organization\OrganizationDeletionRequest;
- use App\ApiResources\Organization\OrganizationMemberCreationRequest;
- use App\Entity\Access\Access;
- use App\Entity\Access\FunctionType;
- use App\Entity\Core\ContactPoint;
- use App\Entity\Core\Country;
- use App\Entity\Education\Cycle;
- use App\Entity\Network\Network;
- use App\Entity\Network\NetworkOrganization;
- use App\Entity\Organization\Organization;
- use App\Entity\Organization\OrganizationAddressPostal;
- use App\Entity\Organization\Parameters;
- use App\Entity\Organization\Settings;
- use App\Entity\Organization\Subdomain;
- use App\Entity\Person\Person;
- use App\Entity\Person\PersonAddressPostal;
- use App\Enum\Access\FunctionEnum;
- use App\Enum\Core\ContactPointTypeEnum;
- use App\Enum\Education\CycleEnum;
- use App\Enum\Network\NetworkEnum;
- use App\Enum\Organization\AddressPostalOrganizationTypeEnum;
- use App\Enum\Organization\LegalEnum;
- use App\Enum\Organization\PrincipalTypeEnum;
- use App\Enum\Organization\SettingsProductEnum;
- use App\Enum\Person\AddressPostalPersonTypeEnum;
- use App\Enum\Person\GenderEnum;
- use App\Repository\Access\FunctionTypeRepository;
- use App\Repository\Core\CountryRepository;
- use App\Repository\Organization\OrganizationIdentificationRepository;
- use App\Repository\Organization\OrganizationRepository;
- use App\Repository\Person\PersonRepository;
- use App\Service\ApiLegacy\ApiLegacyRequestService;
- use App\Service\Dolibarr\DolibarrApiService;
- use App\Service\File\FileManager;
- use App\Service\Organization\OrganizationFactory;
- use App\Service\Organization\Utils as OrganizationUtils;
- use App\Service\Typo3\BindFileService;
- use App\Service\Typo3\SubdomainService;
- use App\Service\Typo3\Typo3Service;
- use App\Service\Utils\DatesUtils;
- use Doctrine\Common\Collections\ArrayCollection;
- use Doctrine\ORM\EntityManagerInterface;
- use libphonenumber\PhoneNumber;
- use libphonenumber\PhoneNumberUtil;
- use PHPUnit\Framework\MockObject\MockObject;
- use PHPUnit\Framework\TestCase;
- use Psr\Log\LoggerInterface;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Contracts\HttpClient\ResponseInterface;
- class TestableOrganizationFactory extends OrganizationFactory
- {
- public function setPhoneNumberUtil(PhoneNumberUtil $phoneNumberUtil): void
- {
- $this->phoneNumberUtil = $phoneNumberUtil;
- }
- public function interruptIfOrganizationExists(OrganizationCreationRequest $organizationCreationRequest): void
- {
- parent::interruptIfOrganizationExists($organizationCreationRequest);
- }
- public function validateSubdomain(string $subdomainValue): void
- {
- parent::validateSubdomain($subdomainValue);
- }
- public function makeOrganizationWithRelations(
- OrganizationCreationRequest $organizationCreationRequest,
- ): Organization {
- return parent::makeOrganizationWithRelations($organizationCreationRequest);
- }
- public function makeOrganization(OrganizationCreationRequest $organizationCreationRequest): Organization
- {
- return parent::makeOrganization($organizationCreationRequest);
- }
- public function makeParameters(OrganizationCreationRequest $organizationCreationRequest): Parameters
- {
- return parent::makeParameters($organizationCreationRequest);
- }
- public function makeSettings(OrganizationCreationRequest $organizationCreationRequest): Settings
- {
- return parent::makeSettings($organizationCreationRequest);
- }
- public function makePostalAddress(OrganizationCreationRequest $organizationCreationRequest): OrganizationAddressPostal
- {
- return parent::makePostalAddress($organizationCreationRequest);
- }
- public function makeContactPoint(OrganizationCreationRequest $organizationCreationRequest): ContactPoint
- {
- return parent::makeContactPoint($organizationCreationRequest);
- }
- public function makeNetworkOrganization(OrganizationCreationRequest $organizationCreationRequest): NetworkOrganization
- {
- return parent::makeNetworkOrganization($organizationCreationRequest);
- }
- public function makeAdminAccess(OrganizationCreationRequest $organizationCreationRequest): Access
- {
- return parent::makeAdminAccess($organizationCreationRequest);
- }
- public function makeCycles(): array
- {
- return parent::makeCycles();
- }
- public function makeAccess(int|OrganizationMemberCreationRequest $creationRequestData, FunctionEnum $function, \DateTime $creationDate, ?int $authorId): Access
- {
- return parent::makeAccess($creationRequestData, $function, $creationDate, $authorId);
- }
- public function makePersonPostalAddress(OrganizationMemberCreationRequest $organizationMemberCreationRequest, \DateTime $creationDate, ?int $authorId): PersonAddressPostal
- {
- return parent::makePersonPostalAddress($organizationMemberCreationRequest, $creationDate, $authorId);
- }
- public function makePersonContactPoint(OrganizationMemberCreationRequest $organizationMemberCreationRequest, \DateTime $creationDate, ?int $authorId): ContactPoint
- {
- return parent::makePersonContactPoint($organizationMemberCreationRequest, $creationDate, $authorId);
- }
- public function makeSubdomain(OrganizationCreationRequest $organizationCreationRequest): Subdomain
- {
- return parent::makeSubdomain($organizationCreationRequest);
- }
- public function createTypo3Website(Organization $organization): ?int
- {
- return parent::createTypo3Website($organization);
- }
- public function updateAdminassosDb(Organization $organization): void
- {
- parent::updateAdminassosDb($organization);
- }
- public function normalizeIdentificationField(string $value): string
- {
- return parent::normalizeIdentificationField($value);
- }
- public function deleteTypo3Website(int $organizationId): void
- {
- parent::deleteTypo3Website($organizationId);
- }
- public function switchDolibarrSocietyToProspect(int $organizationId): void
- {
- parent::switchDolibarrSocietyToProspect($organizationId);
- }
- public function getFutureOrphanPersons(Organization $organization): array
- {
- return parent::getFutureOrphanPersons($organization);
- }
- }
- class OrganizationFactoryTest extends TestCase
- {
- private readonly MockObject|SubdomainService $subdomainService;
- private readonly MockObject|OrganizationRepository $organizationRepository;
- private readonly MockObject|CountryRepository $countryRepository;
- private readonly MockObject|OrganizationUtils $organizationUtils;
- private readonly MockObject|Typo3Service $typo3Service;
- private readonly MockObject|DolibarrApiService $dolibarrApiService;
- private readonly MockObject|EntityManagerInterface $entityManager;
- private readonly MockObject|PersonRepository $personRepository;
- private readonly MockObject|BindFileService $bindFileService;
- private readonly MockObject|LoggerInterface $logger;
- private readonly MockObject|OrganizationIdentificationRepository $organizationIdentificationRepository;
- private readonly MockObject|ApiLegacyRequestService $apiLegacyRequestService;
- private readonly MockObject|PhoneNumberUtil $phoneNumberUtil;
- private readonly MockObject|FunctionTypeRepository $functionTypeRepository;
- private readonly MockObject|\App\Repository\Access\AccessRepository $accessRepository;
- public function setUp(): void
- {
- $this->subdomainService = $this->getMockBuilder(SubdomainService::class)->disableOriginalConstructor()->getMock();
- $this->organizationRepository = $this->getMockBuilder(OrganizationRepository::class)->disableOriginalConstructor()->getMock();
- $this->countryRepository = $this->getMockBuilder(CountryRepository::class)->disableOriginalConstructor()->getMock();
- $this->organizationUtils = $this->getMockBuilder(OrganizationUtils::class)->disableOriginalConstructor()->getMock();
- $this->typo3Service = $this->getMockBuilder(Typo3Service::class)->disableOriginalConstructor()->getMock();
- $this->dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)->disableOriginalConstructor()->getMock();
- $this->entityManager = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock();
- $this->personRepository = $this->getMockBuilder(PersonRepository::class)->disableOriginalConstructor()->getMock();
- $this->bindFileService = $this->getMockBuilder(BindFileService::class)->disableOriginalConstructor()->getMock();
- $this->logger = $this->getMockBuilder(LoggerInterface::class)->disableOriginalConstructor()->getMock();
- $this->organizationIdentificationRepository = $this->getMockBuilder(OrganizationIdentificationRepository::class)->disableOriginalConstructor()->getMock();
- $this->apiLegacyRequestService = $this->getMockBuilder(ApiLegacyRequestService::class)->disableOriginalConstructor()->getMock();
- $this->phoneNumberUtil = $this->getMockBuilder(PhoneNumberUtil::class)->disableOriginalConstructor()->getMock();
- $this->functionTypeRepository = $this->getMockBuilder(FunctionTypeRepository::class)->disableOriginalConstructor()->getMock();
- $this->fileManager = $this->getMockBuilder(FileManager::class)->disableOriginalConstructor()->getMock();
- $this->accessRepository = $this->getMockBuilder(\App\Repository\Access\AccessRepository::class)->disableOriginalConstructor()->getMock();
- }
- public function tearDown(): void
- {
- DatesUtils::clearFakeDatetime();
- }
- private function getOrganizationFactoryMockFor(string $methodName): TestableOrganizationFactory|MockObject
- {
- $organizationFactory = $this
- ->getMockBuilder(TestableOrganizationFactory::class)
- ->setConstructorArgs(
- [
- $this->subdomainService,
- $this->organizationRepository,
- $this->countryRepository,
- $this->organizationUtils,
- $this->typo3Service,
- $this->dolibarrApiService,
- $this->entityManager,
- $this->personRepository,
- $this->bindFileService,
- $this->organizationIdentificationRepository,
- $this->apiLegacyRequestService,
- $this->functionTypeRepository,
- $this->fileManager,
- $this->accessRepository,
- ])
- ->setMethodsExcept(['setLoggerInterface', 'setPhoneNumberUtil', $methodName])
- ->getMock();
- $organizationFactory->setLoggerInterface($this->logger);
- $organizationFactory->setPhoneNumberUtil($this->phoneNumberUtil);
- return $organizationFactory;
- }
- public function testCreate(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('create');
- $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
- $organizationCreationRequest->method('getName')->willReturn('foo');
- $organizationCreationRequest->method('getSubdomain')->willReturn('subdomain');
- $organizationCreationRequest->method('isClient')->willReturn(false);
- $organizationCreationRequest->method('getCreateWebsite')->willReturn(true);
- $this->entityManager->expects(self::once())->method('beginTransaction');
- $organizationFactory->expects(self::once())->method('interruptIfOrganizationExists');
- $organizationFactory->expects(self::once())->method('validateSubdomain')->with('subdomain');
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $organizationFactory
- ->expects(self::once())
- ->method('makeOrganizationWithRelations')
- ->with($organizationCreationRequest)
- ->willReturn($organization);
- $this->entityManager->expects(self::once())->method('persist')->with($organization);
- $this->entityManager->expects(self::once())->method('flush');
- $this->entityManager->expects(self::once())->method('commit');
- $this->dolibarrApiService
- ->expects(self::once())
- ->method('createSociety')
- ->with($organization, false)
- ->willReturn(456);
- $this->bindFileService
- ->expects(self::once())
- ->method('registerSubdomain')
- ->with('subdomain');
- $organizationFactory
- ->expects(self::once())
- ->method('createTypo3Website')
- ->with($organization)
- ->willReturn(789);
- $this->logger
- ->method('info')
- ->withConsecutive(
- ["Start the creation of a new organization named 'foo'"],
- ["Subdomain is valid and available : 'subdomain'"],
- ['Organization created with all its relations'],
- ['Organization persisted in the DB'],
- ['New dolibarr structure created (uid : 456)'],
- ['Subdomain registered'],
- ['Typo3 website created (root uid: 789)'],
- ['Adminassos db updated']
- );
- $organizationCreationRequest
- ->expects(self::once())
- ->method('setStatus')
- ->with(OrganizationCreationRequest::STATUS_OK);
- $result = $organizationFactory->create($organizationCreationRequest);
- $this->assertEquals(
- $organization,
- $result
- );
- }
- public function testCreateWithRollback(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('create');
- $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
- $organizationCreationRequest->method('getName')->willReturn('foo');
- $organizationCreationRequest->method('getSubdomain')->willReturn('subdomain');
- $organizationCreationRequest->method('isClient')->willReturn(false);
- $organizationCreationRequest->method('getCreateWebsite')->willReturn(true);
- $this->entityManager->expects(self::once())->method('beginTransaction');
- $organizationFactory->expects(self::once())->method('interruptIfOrganizationExists');
- $organizationFactory->expects(self::once())->method('validateSubdomain')->with('subdomain');
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $organizationFactory
- ->expects(self::once())
- ->method('makeOrganizationWithRelations')
- ->with($organizationCreationRequest)
- ->willReturn($organization);
- $this->entityManager->expects(self::once())->method('persist')->with($organization);
- $this->entityManager->expects(self::once())->method('flush')->willThrowException(new \RuntimeException('some error'));
- $this->entityManager->expects(self::once())->method('rollback');
- $this->dolibarrApiService
- ->expects(self::never())
- ->method('createSociety');
- $this->bindFileService
- ->expects(self::never())
- ->method('registerSubdomain');
- $organizationFactory
- ->expects(self::never())
- ->method('createTypo3Website');
- $this->logger
- ->method('info')
- ->withConsecutive(
- ["Start the creation of a new organization named 'foo'"],
- ["Subdomain is valid and available : 'subdomain'"],
- ['Organization created with all its relations']
- );
- $this->logger
- ->method('critical')
- ->with(
- $this->matchesRegularExpression('/^An error happened, operation cancelled\nRuntimeException: some error.*/')
- );
- $this->expectException(\RuntimeException::class);
- $result = $organizationFactory->create($organizationCreationRequest);
- }
- public function testCreateWithExistingOrganization(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('create');
- $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
- $organizationCreationRequest->method('getName')->willReturn('foo');
- $this->entityManager->expects(self::once())->method('beginTransaction');
- $organizationFactory->expects(self::once())->method('interruptIfOrganizationExists')->willThrowException(new \RuntimeException('An organization named foo already exists'));
- $organizationFactory->expects(self::never())->method('validateSubdomain');
- $organizationFactory
- ->expects(self::never())
- ->method('makeOrganizationWithRelations');
- $this->entityManager->expects(self::never())->method('persist');
- $this->entityManager->expects(self::never())->method('flush');
- $this->entityManager->expects(self::once())->method('rollback');
- $this->dolibarrApiService
- ->expects(self::never())
- ->method('createSociety');
- $this->bindFileService
- ->expects(self::never())
- ->method('registerSubdomain');
- $organizationFactory
- ->expects(self::never())
- ->method('createTypo3Website');
- $this->logger
- ->method('info')
- ->withConsecutive(
- ["Start the creation of a new organization named 'foo'"],
- );
- $this->logger
- ->method('critical')
- ->with(
- $this->matchesRegularExpression("/^An error happened, operation cancelled\nRuntimeException: An organization named foo already exists.*/")
- );
- $this->expectException(\RuntimeException::class);
- $this->expectExceptionMessage('An organization named foo already exists');
- $result = $organizationFactory->create($organizationCreationRequest);
- }
- public function testCreateNoWebsiteAndIsClient(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('create');
- $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
- $organizationCreationRequest->method('getName')->willReturn('foo');
- $organizationCreationRequest->method('getSubdomain')->willReturn('subdomain');
- $organizationCreationRequest->method('isClient')->willReturn(true);
- $organizationCreationRequest->method('getCreateWebsite')->willReturn(false);
- $organizationFactory->expects(self::once())->method('interruptIfOrganizationExists');
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $organizationFactory
- ->expects(self::once())
- ->method('makeOrganizationWithRelations')
- ->with($organizationCreationRequest)
- ->willReturn($organization);
- $this->dolibarrApiService
- ->method('createSociety')
- ->with($organization, true)
- ->willReturn(456);
- $organizationFactory
- ->expects(self::never())
- ->method('createTypo3Website');
- $this->logger
- ->expects(self::once())
- ->method('warning')
- ->with('Typo3 website creation was not required');
- $result = $organizationFactory->create($organizationCreationRequest);
- $this->assertEquals(
- $organization,
- $result
- );
- }
- public function testCreateWithErrors(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('create');
- $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
- $organizationCreationRequest->method('getName')->willReturn('foo');
- $organizationCreationRequest->method('getSubdomain')->willReturn('subdomain');
- $organizationCreationRequest->method('isClient')->willReturn(true);
- $organizationCreationRequest->method('getCreateWebsite')->willReturn(true);
- $organizationFactory->expects(self::once())->method('interruptIfOrganizationExists');
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $organizationFactory
- ->expects(self::once())
- ->method('makeOrganizationWithRelations')
- ->with($organizationCreationRequest)
- ->willReturn($organization);
- $this->dolibarrApiService
- ->method('createSociety')
- ->with($organization, true)
- ->willThrowException(new \RuntimeException('An error happened'));
- $this->bindFileService
- ->method('registerSubdomain')
- ->with('subdomain')
- ->willThrowException(new \RuntimeException('An error happened'));
- $organizationFactory
- ->method('createTypo3Website')
- ->with($organization)
- ->willThrowException(new \RuntimeException('An error happened'));
- $organizationFactory
- ->method('updateAdminassosDb')
- ->with($organization)
- ->willThrowException(new \RuntimeException('An error happened'));
- $this->logger
- ->expects(self::exactly(4))
- ->method('critical')
- ->willReturnOnConsecutiveCalls([
- 'An error happened while creating the dolibarr society, please proceed manually.',
- 'An error happened while updating the bind file, please proceed manually.',
- 'An error happened while creating the typo3 website, please proceed manually.',
- 'An error happened while updating the adminassos db, please proceed manually.',
- ]);
- $this->logger
- ->expects(self::once())
- ->method('warning')
- ->with('-- Operation ended with errors, check the logs for more information --');
- $organizationCreationRequest
- ->expects(self::once())
- ->method('setStatus')
- ->with(OrganizationCreationRequest::STATUS_OK_WITH_ERRORS);
- $result = $organizationFactory->create($organizationCreationRequest);
- $this->assertEquals(
- $organization,
- $result
- );
- }
- public function testInterruptIfOrganizationExistsNotExisting(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('interruptIfOrganizationExists');
- $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
- $organizationCreationRequest->method('getName')->willReturn('foo');
- $organizationCreationRequest->method('getSiretNumber')->willReturn('');
- $organizationCreationRequest->method('getWaldecNumber')->willReturn('');
- $organizationCreationRequest->method('getIdentifier')->willReturn('');
- $organizationCreationRequest->method('getStreetAddress1')->willReturn('part1');
- $organizationCreationRequest->method('getStreetAddress2')->willReturn('part2');
- $organizationCreationRequest->method('getStreetAddress3')->willReturn('part3');
- $organizationCreationRequest->method('getCity')->willReturn('Paris');
- $organizationCreationRequest->method('getPostalCode')->willReturn('75000');
- $this->organizationIdentificationRepository
- ->expects(self::exactly(2))
- ->method('findOneBy')
- ->willReturn(null);
- $organizationFactory
- ->method('normalizeIdentificationField')
- ->willReturnMap([
- ['foo', 'foo'],
- ['part1 part2 part3', 'part1 part2 part3'],
- ]);
- $organizationFactory->interruptIfOrganizationExists($organizationCreationRequest);
- }
- public function testInterruptIfOrganizationExistsNotExistingNonExistingWithIdentifiers(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('interruptIfOrganizationExists');
- $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
- $organizationCreationRequest->method('getName')->willReturn('foo');
- $organizationCreationRequest->method('getSiretNumber')->willReturn('123456');
- $organizationCreationRequest->method('getWaldecNumber')->willReturn('W123456');
- $organizationCreationRequest->method('getIdentifier')->willReturn('FR000000000000');
- $organizationCreationRequest->method('getStreetAddress1')->willReturn('part1');
- $organizationCreationRequest->method('getStreetAddress2')->willReturn('part2');
- $organizationCreationRequest->method('getStreetAddress3')->willReturn('part3');
- $organizationCreationRequest->method('getCity')->willReturn('Paris');
- $organizationCreationRequest->method('getPostalCode')->willReturn('75000');
- $organizationFactory
- ->method('normalizeIdentificationField')
- ->willReturnMap([
- ['foo', 'foo'],
- ['part1 part2 part3', 'part1 part2 part3'],
- ]);
- $this->organizationIdentificationRepository
- ->expects(self::exactly(5))
- ->method('findOneBy')
- ->willReturnMap([
- [['siretNumber' => '123456'], null, null],
- [['waldecNumber' => 'W123456'], null, null],
- [['identifier' => 'FR000000000000'], null, null],
- [['normalizedName' => 'foo', 'addressCity' => 'Paris'], null, null],
- [['normalizedAddress' => 'part1 part2 part3', 'addressCity' => 'Paris', 'postalCode' => '75000'], null, null],
- ]);
- $organizationFactory->interruptIfOrganizationExists($organizationCreationRequest);
- }
- public function testInterruptIfOrganizationExistsExistingWithSiret(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('interruptIfOrganizationExists');
- $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
- $organizationCreationRequest->method('getName')->willReturn('foo');
- $organizationCreationRequest->method('getSiretNumber')->willReturn('123456');
- $organizationCreationRequest->method('getWaldecNumber')->willReturn('W123456');
- $organizationCreationRequest->method('getIdentifier')->willReturn('FR000000000000');
- $organizationCreationRequest->method('getStreetAddress1')->willReturn('part1');
- $organizationCreationRequest->method('getStreetAddress2')->willReturn('part2');
- $organizationCreationRequest->method('getStreetAddress3')->willReturn('part3');
- $organizationCreationRequest->method('getCity')->willReturn('Paris');
- $organizationCreationRequest->method('getPostalCode')->willReturn('75000');
- $organizationFactory
- ->method('normalizeIdentificationField')
- ->willReturnMap([
- ['foo', 'foo'],
- ['part1 part2 part3', 'part1 part2 part3'],
- ]);
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $this->organizationIdentificationRepository
- ->method('findOneBy')
- ->willReturnMap([
- [['siretNumber' => '123456'], null, $organization],
- [['waldecNumber' => 'W123456'], null, null],
- [['identifier' => 'FR000000000000'], null, null],
- [['normalizedName' => 'foo', 'addressCity' => 'Paris'], null, null],
- [['normalizedAddress' => 'part1 part2 part3', 'addressCity' => 'Paris', 'postalCode' => '75000'], null, null],
- ]);
- $this->expectException(\RuntimeException::class);
- $this->expectExceptionMessage("This siret number is already registered : '123456'");
- $organizationFactory->interruptIfOrganizationExists($organizationCreationRequest);
- }
- public function testInterruptIfOrganizationExistsExistingWithWaldec(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('interruptIfOrganizationExists');
- $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
- $organizationCreationRequest->method('getName')->willReturn('foo');
- $organizationCreationRequest->method('getSiretNumber')->willReturn('123456');
- $organizationCreationRequest->method('getWaldecNumber')->willReturn('W123456');
- $organizationCreationRequest->method('getIdentifier')->willReturn('FR000000000000');
- $organizationCreationRequest->method('getStreetAddress1')->willReturn('part1');
- $organizationCreationRequest->method('getStreetAddress2')->willReturn('part2');
- $organizationCreationRequest->method('getStreetAddress3')->willReturn('part3');
- $organizationCreationRequest->method('getCity')->willReturn('Paris');
- $organizationCreationRequest->method('getPostalCode')->willReturn('75000');
- $organizationFactory
- ->method('normalizeIdentificationField')
- ->willReturnMap([
- ['foo', 'foo'],
- ['part1 part2 part3', 'part1 part2 part3'],
- ]);
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $this->organizationIdentificationRepository
- ->method('findOneBy')
- ->willReturnMap([
- [['siretNumber' => '123456'], null, null],
- [['waldecNumber' => 'W123456'], null, $organization],
- [['identifier' => 'FR000000000000'], null, null],
- [['normalizedName' => 'foo', 'addressCity' => 'Paris'], null, null],
- [['normalizedAddress' => 'part1 part2 part3', 'addressCity' => 'Paris', 'postalCode' => '75000'], null, null],
- ]);
- $this->expectException(\RuntimeException::class);
- $this->expectExceptionMessage("This RNA identifier (waldec number) is already registered : 'W123456'");
- $organizationFactory->interruptIfOrganizationExists($organizationCreationRequest);
- }
- public function testInterruptIfOrganizationExistsExistingWithCMFIdentifier(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('interruptIfOrganizationExists');
- $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
- $organizationCreationRequest->method('getName')->willReturn('foo');
- $organizationCreationRequest->method('getSiretNumber')->willReturn('123456');
- $organizationCreationRequest->method('getWaldecNumber')->willReturn('W123456');
- $organizationCreationRequest->method('getIdentifier')->willReturn('FR000000000000');
- $organizationCreationRequest->method('getStreetAddress1')->willReturn('part1');
- $organizationCreationRequest->method('getStreetAddress2')->willReturn('part2');
- $organizationCreationRequest->method('getStreetAddress3')->willReturn('part3');
- $organizationCreationRequest->method('getCity')->willReturn('Paris');
- $organizationCreationRequest->method('getPostalCode')->willReturn('75000');
- $organizationFactory
- ->method('normalizeIdentificationField')
- ->willReturnMap([
- ['foo', 'foo'],
- ['part1 part2 part3', 'part1 part2 part3'],
- ]);
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $this->organizationIdentificationRepository
- ->method('findOneBy')
- ->willReturnMap([
- [['siretNumber' => '123456'], null, null],
- [['waldecNumber' => 'W123456'], null, null],
- [['identifier' => 'FR000000000000'], null, $organization],
- [['normalizedName' => 'foo', 'addressCity' => 'Paris'], null, null],
- [['normalizedAddress' => 'part1 part2 part3', 'addressCity' => 'Paris', 'postalCode' => '75000'], null, null],
- ]);
- $this->expectException(\RuntimeException::class);
- $this->expectExceptionMessage("This CMF identifier is already registered : 'FR000000000000'");
- $organizationFactory->interruptIfOrganizationExists($organizationCreationRequest);
- }
- public function testInterruptIfOrganizationExistsExistingWithSameNameAndCity(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('interruptIfOrganizationExists');
- $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
- $organizationCreationRequest->method('getName')->willReturn('foo');
- $organizationCreationRequest->method('getSiretNumber')->willReturn('123456');
- $organizationCreationRequest->method('getWaldecNumber')->willReturn('W123456');
- $organizationCreationRequest->method('getIdentifier')->willReturn('FR000000000000');
- $organizationCreationRequest->method('getStreetAddress1')->willReturn('part1');
- $organizationCreationRequest->method('getStreetAddress2')->willReturn('part2');
- $organizationCreationRequest->method('getStreetAddress3')->willReturn('part3');
- $organizationCreationRequest->method('getCity')->willReturn('Paris');
- $organizationCreationRequest->method('getPostalCode')->willReturn('75000');
- $organizationFactory
- ->method('normalizeIdentificationField')
- ->willReturnMap([
- ['foo', 'foo'],
- ['part1 part2 part3', 'part1 part2 part3'],
- ]);
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $this->organizationIdentificationRepository
- ->method('findOneBy')
- ->willReturnMap([
- [['siretNumber' => '123456'], null, null],
- [['waldecNumber' => 'W123456'], null, null],
- [['identifier' => 'FR000000000000'], null, null],
- [['normalizedName' => 'foo', 'addressCity' => 'Paris'], null, $organization],
- [['normalizedAddress' => 'part1 part2 part3', 'addressCity' => 'Paris', 'postalCode' => '75000'], null, null],
- ]);
- $this->expectException(\RuntimeException::class);
- $this->expectExceptionMessage("An organization named 'foo' already exists in Paris");
- $organizationFactory->interruptIfOrganizationExists($organizationCreationRequest);
- }
- public function testInterruptIfOrganizationExistsExistingWithSameAddress(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('interruptIfOrganizationExists');
- $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
- $organizationCreationRequest->method('getName')->willReturn('foo');
- $organizationCreationRequest->method('getSiretNumber')->willReturn('123456');
- $organizationCreationRequest->method('getWaldecNumber')->willReturn('W123456');
- $organizationCreationRequest->method('getIdentifier')->willReturn('FR000000000000');
- $organizationCreationRequest->method('getStreetAddress1')->willReturn('part1');
- $organizationCreationRequest->method('getStreetAddress2')->willReturn('part2');
- $organizationCreationRequest->method('getStreetAddress3')->willReturn('part3');
- $organizationCreationRequest->method('getCity')->willReturn('Paris');
- $organizationCreationRequest->method('getPostalCode')->willReturn('75000');
- $organizationFactory
- ->method('normalizeIdentificationField')
- ->willReturnMap([
- ['foo', 'foo'],
- ['part1 part2 part3', 'part1 part2 part3'],
- ]);
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $this->organizationIdentificationRepository
- ->method('findOneBy')
- ->willReturnMap([
- [['siretNumber' => '123456'], null, null],
- [['waldecNumber' => 'W123456'], null, null],
- [['identifier' => 'FR000000000000'], null, null],
- [['normalizedName' => 'foo', 'addressCity' => 'Paris'], null, null],
- [['normalizedAddress' => 'part1 part2 part3', 'addressCity' => 'Paris', 'postalCode' => '75000'], null, $organization],
- ]);
- $this->expectException(\RuntimeException::class);
- $this->expectExceptionMessage('An organization already exists at this address.');
- $organizationFactory->interruptIfOrganizationExists($organizationCreationRequest);
- }
- public function testValidateSubdomain(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('validateSubdomain');
- $this->subdomainService->expects(self::once())->method('isValidSubdomain')->willReturn(true);
- $this->subdomainService->expects(self::once())->method('isReservedSubdomain')->willReturn(false);
- $this->subdomainService->expects(self::once())->method('isRegistered')->willReturn(false);
- $organizationFactory->validateSubdomain('foo');
- }
- public function testValidateSubdomainIsNotValid(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('validateSubdomain');
- $this->subdomainService->method('isValidSubdomain')->willReturn(false);
- $this->subdomainService->method('isReservedSubdomain')->willReturn(false);
- $this->subdomainService->method('isRegistered')->willReturn(false);
- $this->expectException(\RuntimeException::class);
- $this->expectExceptionMessage('Not a valid subdomain : foo');
- $organizationFactory->validateSubdomain('foo');
- }
- public function testValidateSubdomainIsReserved(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('validateSubdomain');
- $this->subdomainService->method('isValidSubdomain')->willReturn(true);
- $this->subdomainService->method('isReservedSubdomain')->willReturn(true);
- $this->subdomainService->method('isRegistered')->willReturn(false);
- $this->expectException(\RuntimeException::class);
- $this->expectExceptionMessage('This subdomain is not available : foo');
- $organizationFactory->validateSubdomain('foo');
- }
- public function testValidateSubdomainIsRegistered(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('validateSubdomain');
- $this->subdomainService->method('isValidSubdomain')->willReturn(true);
- $this->subdomainService->method('isReservedSubdomain')->willReturn(false);
- $this->subdomainService->method('isRegistered')->willReturn(true);
- $this->expectException(\RuntimeException::class);
- $this->expectExceptionMessage('This subdomain is already registered : foo');
- $organizationFactory->validateSubdomain('foo');
- }
- public function testMakeOrganizationWithRelations(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('makeOrganizationWithRelations');
- $creationDate = $this->getMockBuilder(\DateTime::class)->getMock();
- $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
- $organizationCreationRequest->method('getCreationDate')->willReturn($creationDate);
- $organizationCreationRequest->method('getAuthorId')->willReturn(1);
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- // Création de l'organisation
- $organizationFactory
- ->expects(self::once())
- ->method('makeOrganization')
- ->with($organizationCreationRequest)
- ->willReturn($organization);
- // Création des Parameters
- $parameters = $this->getMockBuilder(Parameters::class)->getMock();
- $organizationFactory
- ->expects(self::once())
- ->method('makeParameters')
- ->with($organizationCreationRequest)
- ->willReturn($parameters);
- $organization->expects(self::once())->method('setParameters')->with($parameters);
- // Création des Settings
- $settings = $this->getMockBuilder(Settings::class)->getMock();
- $organizationFactory
- ->expects(self::once())
- ->method('makeSettings')
- ->with($organizationCreationRequest)
- ->willReturn($settings);
- $organization->expects(self::once())->method('setSettings')->with($settings);
- // Création de l'adresse postale
- $organizationAddressPostal = $this->getMockBuilder(OrganizationAddressPostal::class)->getMock();
- $organizationFactory
- ->expects(self::once())
- ->method('makePostalAddress')
- ->with($organizationCreationRequest)
- ->willReturn($organizationAddressPostal);
- $organization->expects(self::once())->method('addOrganizationAddressPostal')->with($organizationAddressPostal);
- // Création du point de contact
- $contactPoint = $this->getMockBuilder(ContactPoint::class)->getMock();
- $organizationFactory
- ->expects(self::once())
- ->method('makeContactPoint')
- ->with($organizationCreationRequest)
- ->willReturn($contactPoint);
- $organization->expects(self::once())->method('addContactPoint')->with($contactPoint);
- // Rattachement au réseau
- $networkOrganization = $this->getMockBuilder(NetworkOrganization::class)->getMock();
- $organizationFactory
- ->expects(self::once())
- ->method('makeNetworkOrganization')
- ->with($organizationCreationRequest)
- ->willReturn($networkOrganization);
- $organization->expects(self::once())->method('addNetworkOrganization')->with($networkOrganization);
- // Créé l'admin
- $adminAccess = $this->getMockBuilder(Access::class)->getMock();
- $organizationFactory
- ->expects(self::once())
- ->method('makeAdminAccess')
- ->with($organizationCreationRequest)
- ->willReturn($adminAccess);
- // Le `$organization->expects(...)->method('addAccess')` est implémenté plus loin,
- // après la création du président et du directeur.
- // Création des cycles
- $cycle1 = $this->getMockBuilder(Cycle::class)->getMock();
- $cycle2 = $this->getMockBuilder(Cycle::class)->getMock();
- $cycle3 = $this->getMockBuilder(Cycle::class)->getMock();
- $organizationFactory
- ->expects(self::once())
- ->method('makeCycles')
- ->willReturn([$cycle1, $cycle2, $cycle3]);
- $organization->expects(self::exactly(3))->method('addCycle')->withConsecutive([$cycle1], [$cycle2], [$cycle3]);
- // Création du président et du directeur
- $organizationMemberCreationRequest1 = $this->getMockBuilder(OrganizationMemberCreationRequest::class)->getMock();
- $organizationCreationRequest->method('getPresident')->willReturn($organizationMemberCreationRequest1);
- $organizationMemberCreationRequest2 = $this->getMockBuilder(OrganizationMemberCreationRequest::class)->getMock();
- $organizationCreationRequest->method('getDirector')->willReturn($organizationMemberCreationRequest2);
- $access1 = $this->getMockBuilder(Access::class)->getMock();
- $access2 = $this->getMockBuilder(Access::class)->getMock();
- $organizationFactory
- ->expects(self::exactly(2))
- ->method('makeAccess')
- ->willReturnMap([
- [$organizationMemberCreationRequest1, FunctionEnum::PRESIDENT, $creationDate, 1, $access1],
- [$organizationMemberCreationRequest2, FunctionEnum::DIRECTOR, $creationDate, 1, $access2],
- ]);
- $organization
- ->expects(self::exactly(3))
- ->method('addAccess')
- ->withConsecutive([$adminAccess], [$access1], [$access2]);
- // Création du sous-domaine
- $subdomain = $this->getMockBuilder(Subdomain::class)->getMock();
- $organizationFactory
- ->expects(self::once())
- ->method('makeSubdomain')
- ->with($organizationCreationRequest)
- ->willReturn($subdomain);
- $organization->expects(self::once())->method('addSubdomain')->with($subdomain);
- // Enregistrement du sous domaine dans Parameters (retrocompatibilité v1)
- $organization->method('getParameters')->willReturn($parameters);
- $organizationCreationRequest->method('getSubdomain')->willReturn('foo');
- $parameters->expects(self::once())->method('setSubDomain')->with('foo');
- $parameters->expects(self::once())->method('setOtherWebsite')->with('https://foo.opentalent.fr');
- $result = $organizationFactory->makeOrganizationWithRelations($organizationCreationRequest);
- $this->assertEquals(
- $result,
- $organization
- );
- }
- public function testMakeOrganizationWithRelationsNoPresidentNoDirector(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('makeOrganizationWithRelations');
- $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- // Création de l'organisation
- $organizationFactory
- ->expects(self::once())
- ->method('makeOrganization')
- ->with($organizationCreationRequest)
- ->willReturn($organization);
- $organizationCreationRequest->method('getPresident')->willReturn(null);
- $organizationCreationRequest->method('getDirector')->willReturn(null);
- // Un seul appel, pour l'adminAccess
- $organization
- ->expects(self::once())
- ->method('addAccess');
- $organizationFactory->makeOrganizationWithRelations($organizationCreationRequest);
- }
- public function testMakeOrganization(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('makeOrganization');
- $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
- $organizationCreationRequest->method('getName')->willReturn('My Organization');
- $organizationCreationRequest->method('getLegalStatus')->willReturn(LegalEnum::ASSOCIATION_LAW_1901);
- $organizationCreationRequest->method('getPrincipalType')->willReturn(PrincipalTypeEnum::ARTISTIC_EDUCATION_ONLY);
- $organization = $organizationFactory->makeOrganization($organizationCreationRequest);
- $this->assertEquals(
- 'My Organization',
- $organization->getName()
- );
- $this->assertEquals(
- LegalEnum::ASSOCIATION_LAW_1901,
- $organization->getLegalStatus()
- );
- $this->assertEquals(
- PrincipalTypeEnum::ARTISTIC_EDUCATION_ONLY,
- $organization->getPrincipalType()
- );
- }
- public function testMakeParameters(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('makeParameters');
- $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
- $parameters = $organizationFactory->makeParameters($organizationCreationRequest);
- $this->assertInstanceOf(Parameters::class, $parameters);
- }
- public function testMakeSettings(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('makeSettings');
- $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
- $organizationCreationRequest->method('getProduct')->willReturn(SettingsProductEnum::ARTIST_PREMIUM);
- $settings = $organizationFactory->makeSettings($organizationCreationRequest);
- $this->assertEquals(
- SettingsProductEnum::ARTIST_PREMIUM,
- $settings->getProduct()
- );
- }
- public function testMakePostalAddress(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('makePostalAddress');
- $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
- $organizationCreationRequest->method('getStreetAddress1')->willReturn('address1');
- $organizationCreationRequest->method('getStreetAddress2')->willReturn('address2');
- $organizationCreationRequest->method('getStreetAddress3')->willReturn('address3');
- $organizationCreationRequest->method('getPostalCode')->willReturn('00000');
- $organizationCreationRequest->method('getCity')->willReturn('city');
- $organizationCreationRequest->method('getCountryId')->willReturn(1);
- $country = $this->getMockBuilder(Country::class)->getMock();
- $this->countryRepository->expects(self::once())->method('find')->with(1)->willReturn($country);
- $organizationAddressPostal = $organizationFactory->makePostalAddress($organizationCreationRequest);
- $this->assertEquals(
- AddressPostalOrganizationTypeEnum::ADDRESS_HEAD_OFFICE,
- $organizationAddressPostal->getType()
- );
- $addressPostal = $organizationAddressPostal->getAddressPostal();
- $this->assertEquals(
- 'address1',
- $addressPostal->getStreetAddress()
- );
- $this->assertEquals(
- 'address2',
- $addressPostal->getStreetAddressSecond()
- );
- $this->assertEquals(
- 'address3',
- $addressPostal->getStreetAddressThird()
- );
- $this->assertEquals(
- '00000',
- $addressPostal->getPostalCode()
- );
- $this->assertEquals(
- 'city',
- $addressPostal->getAddressCity()
- );
- $this->assertEquals(
- $country,
- $addressPostal->getAddressCountry()
- );
- }
- public function testMakePostalAddressNonExistingCountry(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('makePostalAddress');
- $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
- $organizationCreationRequest->method('getCountryId')->willReturn(1);
- $this->countryRepository->expects(self::once())->method('find')->with(1)->willReturn(null);
- $this->expectException(\RuntimeException::class);
- $this->expectExceptionMessage('No country found for id 1');
- $organizationFactory->makePostalAddress($organizationCreationRequest);
- }
- public function testMakeContactPoint(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('makeContactPoint');
- $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
- $organizationCreationRequest->method('getPhoneNumber')->willReturn('+33102030405');
- $organizationCreationRequest->method('getEmail')->willReturn('contact@domain.net');
- $this->phoneNumberUtil
- ->method('isPossibleNumber')
- ->with('+33102030405')
- ->willReturn(true);
- $phoneNumber = $this->getMockBuilder(PhoneNumber::class)->getMock();
- $this->phoneNumberUtil
- ->expects(self::once())
- ->method('parse')
- ->with('+33102030405')
- ->willReturn($phoneNumber);
- $contactPoint = $organizationFactory->makeContactPoint($organizationCreationRequest);
- $this->assertEquals(
- 'contact@domain.net',
- $contactPoint->getEmail()
- );
- $this->assertEquals(
- $phoneNumber,
- $contactPoint->getTelphone()
- );
- }
- public function testMakeContactPointInvalidPhoneNumber(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('makeContactPoint');
- $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
- $organizationCreationRequest->method('getPhoneNumber')->willReturn('invalid');
- $organizationCreationRequest->method('getEmail')->willReturn('contact@domain.net');
- $this->phoneNumberUtil
- ->method('isPossibleNumber')
- ->with('invalid')
- ->willReturn(false);
- $phoneNumber = $this->getMockBuilder(PhoneNumber::class)->getMock();
- $this->phoneNumberUtil
- ->expects(self::never())
- ->method('parse');
- $this->expectException(\RuntimeException::class);
- $this->expectExceptionMessage('Phone number is invalid or missing');
- $organizationFactory->makeContactPoint($organizationCreationRequest);
- }
- public function testMakeNetworkOrganization(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('makeNetworkOrganization');
- DatesUtils::setFakeDatetime('2024-01-01');
- $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
- $organizationCreationRequest->method('getParentId')->willReturn(123);
- $parent = $this->getMockBuilder(Organization::class)->getMock();
- $settings = $this->getMockBuilder(Settings::class)->getMock();
- $settings->method('getProduct')->willReturn(SettingsProductEnum::MANAGER);
- $parent->method('getSettings')->willReturn($settings);
- $this->organizationRepository->expects(self::once())->method('find')->with(123)->willReturn($parent);
- $network = $this->getMockBuilder(Network::class)->getMock();
- $networkOrganization = $this->getMockBuilder(NetworkOrganization::class)->getMock();
- $networkOrganization->method('getNetwork')->willReturn($network);
- $this->organizationUtils
- ->expects(self::once())
- ->method('getActiveNetworkOrganization')
- ->with($parent)
- ->willReturn($networkOrganization);
- $networkOrganization = $organizationFactory->makeNetworkOrganization($organizationCreationRequest);
- $this->assertEquals(
- $parent,
- $networkOrganization->getParent()
- );
- $this->assertEquals(
- $network,
- $networkOrganization->getNetwork()
- );
- $this->assertEquals(
- '2024-01-01',
- $networkOrganization->getStartDate()->format('Y-m-d')
- );
- }
- public function testMakeNetworkOrganizationMissingParent(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('makeNetworkOrganization');
- $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
- $organizationCreationRequest->method('getParentId')->willReturn(123);
- $this->expectException(\RuntimeException::class);
- $this->expectExceptionMessage('No parent organization found for id 123');
- $this->organizationRepository->expects(self::once())->method('find')->with(123)->willReturn(null);
- $organizationFactory->makeNetworkOrganization($organizationCreationRequest);
- }
- public function testMakeNetworkOrganizationParentIsNotManager(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('makeNetworkOrganization');
- DatesUtils::setFakeDatetime('2024-01-01');
- $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
- $organizationCreationRequest->method('getParentId')->willReturn(123);
- $parent = $this->getMockBuilder(Organization::class)->getMock();
- $settings = $this->getMockBuilder(Settings::class)->getMock();
- $settings->method('getProduct')->willReturn(SettingsProductEnum::SCHOOL);
- $parent->method('getSettings')->willReturn($settings);
- $this->organizationRepository->expects(self::once())->method('find')->with(123)->willReturn($parent);
- $this->expectException(\RuntimeException::class);
- $this->expectExceptionMessage("Parent organization must have the product 'manager' (actual product: 'school')");
- $organizationFactory->makeNetworkOrganization($organizationCreationRequest);
- }
- public function testMakeNetworkOrganizationMissingNetwork(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('makeNetworkOrganization');
- $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
- $organizationCreationRequest->method('getParentId')->willReturn(123);
- $parent = $this->getMockBuilder(Organization::class)->getMock();
- $settings = $this->getMockBuilder(Settings::class)->getMock();
- $settings->method('getProduct')->willReturn(SettingsProductEnum::MANAGER);
- $parent->method('getSettings')->willReturn($settings);
- $this->organizationRepository->expects(self::once())->method('find')->with(123)->willReturn($parent);
- $this->expectException(\RuntimeException::class);
- $this->expectExceptionMessage('No network found for parent 123');
- $this->organizationRepository->expects(self::once())->method('find')->with(123)->willReturn($parent);
- $organizationFactory->makeNetworkOrganization($organizationCreationRequest);
- }
- public function testMakeNetworkOrganizationIsCMFInvalidIdentifier(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('makeNetworkOrganization');
- $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
- $organizationCreationRequest->method('getParentId')->willReturn(123);
- $organizationCreationRequest->method('getIdentifier')->willReturn('invalid');
- $parent = $this->getMockBuilder(Organization::class)->getMock();
- $settings = $this->getMockBuilder(Settings::class)->getMock();
- $settings->method('getProduct')->willReturn(SettingsProductEnum::MANAGER);
- $parent->method('getSettings')->willReturn($settings);
- $this->organizationRepository->method('find')->with(123)->willReturn($parent);
- $network = $this->getMockBuilder(Network::class)->getMock();
- $network->method('getId')->willReturn(NetworkEnum::CMF->value);
- $networkOrganization = $this->getMockBuilder(NetworkOrganization::class)->getMock();
- $networkOrganization->method('getNetwork')->willReturn($network);
- $this->organizationUtils
- ->method('getActiveNetworkOrganization')
- ->with($parent)
- ->willReturn($networkOrganization);
- $this->expectException(\RuntimeException::class);
- $this->expectExceptionMessage('CMF identifier is missing or invalid.');
- $organizationFactory->makeNetworkOrganization($organizationCreationRequest);
- }
- public function testMakeNetworkOrganizationIsNotCMFInvalidIdentifier(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('makeNetworkOrganization');
- $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
- $organizationCreationRequest->method('getParentId')->willReturn(123);
- $organizationCreationRequest->method('getIdentifier')->willReturn('invalid');
- $parent = $this->getMockBuilder(Organization::class)->getMock();
- $settings = $this->getMockBuilder(Settings::class)->getMock();
- $settings->method('getProduct')->willReturn(SettingsProductEnum::MANAGER);
- $parent->method('getSettings')->willReturn($settings);
- $this->organizationRepository->method('find')->with(123)->willReturn($parent);
- $network = $this->getMockBuilder(Network::class)->getMock();
- $network->method('getId')->willReturn(NetworkEnum::OUTOFNET->value);
- $networkOrganization = $this->getMockBuilder(NetworkOrganization::class)->getMock();
- $networkOrganization->method('getNetwork')->willReturn($network);
- $this->organizationUtils
- ->method('getActiveNetworkOrganization')
- ->with($parent)
- ->willReturn($networkOrganization);
- $result = $organizationFactory->makeNetworkOrganization($organizationCreationRequest);
- $this->assertEquals(
- $network,
- $result->getNetwork()
- );
- }
- public function testMakeAdminAccess(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('makeAdminAccess');
- $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
- $organizationCreationRequest->method('getSubdomain')->willReturn('foo');
- $adminAccess = $organizationFactory->makeAdminAccess($organizationCreationRequest);
- $this->assertTrue(
- $adminAccess->getAdminAccess()
- );
- $this->assertEquals(
- 'adminfoo',
- $adminAccess->getPerson()->getUsername()
- );
- $this->assertEquals(
- 32,
- strlen($adminAccess->getPerson()->getPassword())
- );
- }
- public function testMakeCycles(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('makeCycles');
- $cycles = $organizationFactory->makeCycles();
- $cyclesExpectedData = [
- ['Cycle initiation', 10, CycleEnum::INITIATION_CYCLE],
- ['Cycle 1', 20, CycleEnum::CYCLE_1],
- ['Cycle 2', 30, CycleEnum::CYCLE_2],
- ['Cycle 3', 40, CycleEnum::CYCLE_3],
- ['Cycle 4', 50, CycleEnum::CYCLE_4],
- ['Hors cycle', 60, CycleEnum::OUT_CYCLE],
- ];
- $i = 0;
- foreach ($cycles as $cycle) {
- $this->assertEquals(
- $cyclesExpectedData[$i][0],
- $cycle->getLabel()
- );
- $this->assertEquals(
- $cyclesExpectedData[$i][1],
- $cycle->getOrder()
- );
- $this->assertEquals(
- $cyclesExpectedData[$i][2],
- $cycle->getCycleEnum()
- );
- $this->assertFalse(
- $cycle->getIsSystem()
- );
- ++$i;
- }
- }
- public function testMakeAccessNewPerson(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('makeAccess');
- $organizationMemberCreationRequest = $this->getMockBuilder(OrganizationMemberCreationRequest::class)->getMock();
- $organizationMemberCreationRequest->method('getUsername')->willReturn('bob');
- $organizationMemberCreationRequest->method('getName')->willReturn('Bob');
- $organizationMemberCreationRequest->method('getGivenName')->willReturn('bOBBy');
- $organizationMemberCreationRequest->method('getGender')->willReturn(GenderEnum::MISTER);
- $this->personRepository
- ->method('findOneBy')
- ->with(['username' => 'bob'], null)
- ->willReturn(null);
- $personAddressPostal = $this->getMockBuilder(PersonAddressPostal::class)->getMock();
- $organizationFactory
- ->expects(self::once())
- ->method('makePersonPostalAddress')
- ->with($organizationMemberCreationRequest)
- ->willReturn($personAddressPostal);
- $contactPoint = $this->getMockBuilder(ContactPoint::class)->getMock();
- $organizationFactory
- ->expects(self::once())
- ->method('makePersonContactPoint')
- ->with($organizationMemberCreationRequest)
- ->willReturn($contactPoint);
- $functionType = $this->getMockBuilder(FunctionType::class)->getMock();
- $this->functionTypeRepository
- ->expects(self::once())
- ->method('findOneBy')
- ->with(['mission' => FunctionEnum::ACCOUNTANT])
- ->willReturn($functionType);
- $creationDate = $this->getMockBuilder(\DateTime::class)->getMock();
- $access = $organizationFactory->makeAccess(
- $organizationMemberCreationRequest,
- FunctionEnum::ACCOUNTANT,
- $creationDate,
- 1
- );
- $this->assertInstanceOf(Access::class, $access);
- $this->assertEquals(
- 'bob',
- $access->getPerson()->getUsername()
- );
- $this->assertTrue(
- strlen($access->getPerson()->getPassword()) === 32
- );
- $this->assertEquals(
- 'Bob',
- $access->getPerson()->getName()
- );
- $this->assertEquals(
- 'Bobby',
- $access->getPerson()->getGivenName()
- );
- $this->assertEquals(
- [$personAddressPostal],
- $access->getPerson()->getPersonAddressPostal()->toArray()
- );
- $this->assertEquals(
- [$contactPoint],
- $access->getPerson()->getContactPoints()->toArray()
- );
- $this->assertEquals(
- $functionType,
- $access->getOrganizationFunction()->first()->getFunctionType()
- );
- $this->assertEquals(
- $creationDate,
- $access->getOrganizationFunction()->first()->getStartDate()
- );
- }
- public function testMakeAccessNewPersonUsernameAlreadyInUse(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('makeAccess');
- $organizationMemberCreationRequest = $this->getMockBuilder(OrganizationMemberCreationRequest::class)->getMock();
- $organizationMemberCreationRequest->method('getUsername')->willReturn('bob');
- $organizationMemberCreationRequest->method('getName')->willReturn('Bob');
- $organizationMemberCreationRequest->method('getGivenName')->willReturn('bOBBy');
- $organizationMemberCreationRequest->method('getGender')->willReturn(GenderEnum::MISTER);
- $person = $this->getMockBuilder(Person::class)->getMock();
- $this->personRepository
- ->method('findOneBy')
- ->with(['username' => 'bob'], null)
- ->willReturn($person);
- $organizationFactory
- ->expects(self::never())
- ->method('makePersonPostalAddress');
- $organizationFactory
- ->expects(self::never())
- ->method('makePersonContactPoint');
- $this->expectException(\RuntimeException::class);
- $this->expectExceptionMessage('Username already in use : bob');
- $creationDate = $this->getMockBuilder(\DateTime::class)->getMock();
- $organizationFactory->makeAccess(
- $organizationMemberCreationRequest,
- FunctionEnum::ADHERENT,
- $creationDate,
- 1
- );
- }
- public function testMakeAccessExistingPerson(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('makeAccess');
- $person = $this->getMockBuilder(Person::class)->getMock();
- $this->personRepository
- ->expects(self::once())
- ->method('find')
- ->with(123)
- ->willReturn($person);
- $creationDate = $this->getMockBuilder(\DateTime::class)->getMock();
- $functionType = $this->getMockBuilder(FunctionType::class)->getMock();
- $this->functionTypeRepository
- ->expects(self::once())
- ->method('findOneBy')
- ->with(['mission' => FunctionEnum::ACCOUNTANT])
- ->willReturn($functionType);
- $creationDate = $this->getMockBuilder(\DateTime::class)->getMock();
- $access = $organizationFactory->makeAccess(
- 123,
- FunctionEnum::ACCOUNTANT,
- $creationDate,
- 1
- );
- $this->assertInstanceOf(Access::class, $access);
- $this->assertEquals(
- $person,
- $access->getPerson()
- );
- $this->assertEquals(
- $functionType,
- $access->getOrganizationFunction()->first()->getFunctionType()
- );
- $this->assertEquals(
- $creationDate,
- $access->getOrganizationFunction()->first()->getStartDate()
- );
- }
- public function testMakeAccessPostalAddress(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('makePersonPostalAddress');
- $organizationMemberCreationRequest = $this->getMockBuilder(OrganizationMemberCreationRequest::class)->getMock();
- $organizationMemberCreationRequest->method('getStreetAddress1')->willReturn('Aaa');
- $organizationMemberCreationRequest->method('getStreetAddress2')->willReturn('Bbb');
- $organizationMemberCreationRequest->method('getStreetAddress3')->willReturn(null);
- $organizationMemberCreationRequest->method('getPostalCode')->willReturn('00000');
- $organizationMemberCreationRequest->method('getCity')->willReturn('city');
- $organizationMemberCreationRequest->method('getCountryId')->willReturn(123);
- $country = $this->getMockBuilder(Country::class)->getMock();
- $this->countryRepository
- ->expects(self::once())
- ->method('find')
- ->with(123)
- ->willReturn($country);
- $creationDate = $this->getMockBuilder(\DateTime::class)->getMock();
- $personPostalAddress = $organizationFactory->makePersonPostalAddress(
- $organizationMemberCreationRequest,
- $creationDate,
- 1
- );
- $this->assertEquals(
- AddressPostalPersonTypeEnum::ADDRESS_PRINCIPAL,
- $personPostalAddress->getType()
- );
- $postalAddress = $personPostalAddress->getAddressPostal();
- $this->assertEquals('Aaa', $postalAddress->getStreetAddress());
- $this->assertEquals('Bbb', $postalAddress->getStreetAddressSecond());
- $this->assertEquals(null, $postalAddress->getStreetAddressThird());
- $this->assertEquals('00000', $postalAddress->getPostalCode());
- $this->assertEquals('city', $postalAddress->getAddressCity());
- $this->assertEquals($country, $postalAddress->getAddressCountry());
- }
- public function testMakeAccessContactPoint(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('makePersonContactPoint');
- $organizationMemberCreationRequest = $this->getMockBuilder(OrganizationMemberCreationRequest::class)->getMock();
- $organizationMemberCreationRequest->method('getPhone')->willReturn('+33102030405');
- $organizationMemberCreationRequest->method('getEmail')->willReturn('email@domain.com');
- $organizationMemberCreationRequest->method('getMobile')->willReturn('+33607080910');
- $this->phoneNumberUtil
- ->expects(self::exactly(2))
- ->method('isPossibleNumber')
- ->willReturnMap([
- ['+33102030405', null, true],
- ['+33607080910', null, true],
- ]);
- $phoneNumber = $this->getMockBuilder(PhoneNumber::class)->getMock();
- $mobilePhoneNumber = $this->getMockBuilder(PhoneNumber::class)->getMock();
- $this->phoneNumberUtil
- ->expects(self::exactly(2))
- ->method('parse')
- ->willReturnMap([
- ['+33102030405', null, null, false, $phoneNumber],
- ['+33607080910', null, null, false, $mobilePhoneNumber],
- ]);
- $creationDate = $this->getMockBuilder(\DateTime::class)->getMock();
- $contactPoint = $organizationFactory->makePersonContactPoint(
- $organizationMemberCreationRequest,
- $creationDate,
- 1
- );
- $this->assertEquals(
- ContactPointTypeEnum::PRINCIPAL,
- $contactPoint->getContactType()
- );
- $this->assertEquals(
- 'email@domain.com',
- $contactPoint->getEmail()
- );
- $this->assertEquals(
- $phoneNumber,
- $contactPoint->getTelphone()
- );
- $this->assertEquals(
- $mobilePhoneNumber,
- $contactPoint->getMobilPhone()
- );
- }
- public function testMakeAccessContactPointInvalidPhone(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('makePersonContactPoint');
- $organizationMemberCreationRequest = $this->getMockBuilder(OrganizationMemberCreationRequest::class)->getMock();
- $organizationMemberCreationRequest->method('getUsername')->willReturn('bob');
- $organizationMemberCreationRequest->method('getPhone')->willReturn('invalid');
- $organizationMemberCreationRequest->method('getEmail')->willReturn('email@domain.com');
- $organizationMemberCreationRequest->method('getMobile')->willReturn('+33607080910');
- $this->phoneNumberUtil
- ->expects(self::once())
- ->method('isPossibleNumber')
- ->with('invalid')
- ->willReturn(false);
- $this->phoneNumberUtil
- ->expects(self::never())
- ->method('parse');
- $this->expectException(\RuntimeException::class);
- $this->expectExceptionMessage('Phone number is invalid or missing (person: bob)');
- $creationDate = $this->getMockBuilder(\DateTime::class)->getMock();
- $organizationFactory->makePersonContactPoint(
- $organizationMemberCreationRequest,
- $creationDate,
- 1
- );
- }
- public function testMakeAccessContactPointInvalidMobilePhone(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('makePersonContactPoint');
- $organizationMemberCreationRequest = $this->getMockBuilder(OrganizationMemberCreationRequest::class)->getMock();
- $organizationMemberCreationRequest->method('getUsername')->willReturn('bob');
- $organizationMemberCreationRequest->method('getPhone')->willReturn('+33102030405');
- $organizationMemberCreationRequest->method('getEmail')->willReturn('email@domain.com');
- $organizationMemberCreationRequest->method('getMobile')->willReturn('invalid');
- $this->phoneNumberUtil
- ->expects(self::exactly(2))
- ->method('isPossibleNumber')
- ->willReturnMap([
- ['+33102030405', null, true],
- ['invalid', null, false],
- ]);
- $this->phoneNumberUtil
- ->expects(self::never())
- ->method('parse');
- $this->expectException(\RuntimeException::class);
- $this->expectExceptionMessage('Mobile phone number is invalid (person: bob)');
- $creationDate = $this->getMockBuilder(\DateTime::class)->getMock();
- $organizationFactory->makePersonContactPoint(
- $organizationMemberCreationRequest,
- $creationDate,
- 1
- );
- }
- public function testMakePersonContactPointNoMobile(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('makePersonContactPoint');
- $organizationMemberCreationRequest = $this->getMockBuilder(OrganizationMemberCreationRequest::class)->getMock();
- $organizationMemberCreationRequest->method('getPhone')->willReturn('+33102030405');
- $organizationMemberCreationRequest->method('getMobile')->willReturn(null);
- $this->phoneNumberUtil
- ->expects(self::once())
- ->method('isPossibleNumber')
- ->with('+33102030405')
- ->willReturn(true);
- $creationDate = $this->getMockBuilder(\DateTime::class)->getMock();
- $contactPoint = $organizationFactory->makePersonContactPoint(
- $organizationMemberCreationRequest,
- $creationDate,
- 1
- );
- $this->assertEquals(
- null,
- $contactPoint->getMobilPhone()
- );
- }
- public function testMakeSubdomain(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('makeSubdomain');
- $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
- $organizationCreationRequest->method('getSubdomain')->willReturn('subdomain');
- $subdomain = $organizationFactory->makeSubdomain($organizationCreationRequest);
- $this->assertEquals(
- 'subdomain',
- $subdomain->getSubdomain()
- );
- $this->assertTrue(
- $subdomain->isActive()
- );
- }
- public function testCreateTypo3Website(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('createTypo3Website');
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $organization->method('getId')->willReturn(123);
- $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
- $response->method('getStatusCode')->willReturn(Response::HTTP_OK);
- $response->method('getContent')->willReturn('{"root_uid": 456}');
- $this->typo3Service->expects(self::once())->method('createSite')->with(123)->willReturn($response);
- $organization->expects(self::once())->method('setCmsId')->with(456);
- $this->entityManager->expects(self::once())->method('persist')->with($organization);
- $this->entityManager->expects(self::once())->method('flush');
- $result = $organizationFactory->createTypo3Website($organization);
- $this->assertEquals(
- 456,
- $result
- );
- }
- public function testCreateTypo3WebsiteWithError(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('createTypo3Website');
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $organization->method('getId')->willReturn(123);
- $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
- $response->method('getStatusCode')->willReturn(Response::HTTP_FORBIDDEN);
- $response->method('getContent')->willReturn('');
- $this->typo3Service->expects(self::once())->method('createSite')->with(123)->willReturn($response);
- $this->logger
- ->expects(self::once())
- ->method('critical')
- ->with('/!\ A critical error happened while creating the Typo3 website');
- $result = $organizationFactory->createTypo3Website($organization);
- $this->assertNull($result);
- }
- public function testCreateTypo3WebsiteWithInvalidResponse(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('createTypo3Website');
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $organization->method('getId')->willReturn(123);
- $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
- $response->method('getStatusCode')->willReturn(Response::HTTP_OK);
- $response->method('getContent')->willReturn('<html lang="fr">Login page</html>');
- $this->typo3Service->expects(self::once())->method('createSite')->with(123)->willReturn($response);
- $this->logger
- ->expects(self::once())
- ->method('critical')
- ->with('/!\ A critical error happened while creating the Typo3 website');
- $result = $organizationFactory->createTypo3Website($organization);
- $this->assertNull($result);
- }
- public function testUpdateAdminassosDb(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('updateAdminassosDb');
- $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
- $response->method('getStatusCode')->willReturn(Response::HTTP_OK);
- $this->apiLegacyRequestService
- ->expects(self::once())
- ->method('get')
- ->with('/_internal/request/adminassos/create/organization/123')
- ->willReturn($response);
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $organization->method('getId')->willReturn(123);
- $organizationFactory->updateAdminassosDb($organization);
- }
- public function testUpdateAdminassosDbError(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('updateAdminassosDb');
- $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
- $response->method('getStatusCode')->willReturn(Response::HTTP_BAD_REQUEST);
- $response->method('getContent')->willReturn('some error');
- $this->apiLegacyRequestService
- ->expects(self::once())
- ->method('get')
- ->with('/_internal/request/adminassos/create/organization/123')
- ->willReturn($response);
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $organization->method('getId')->willReturn(123);
- $this->expectException(\RuntimeException::class);
- $this->expectExceptionMessage('An error happened while updating the adminassos database: some error');
- $organizationFactory->updateAdminassosDb($organization);
- }
- public function testNormalizeIdentificationField(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('normalizeIdentificationField');
- $this->assertEquals(
- 'c+est+une+phrase+normalisee+',
- $organizationFactory->normalizeIdentificationField("C'est une phrase normalisée.")
- );
- }
- public function testDelete(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('delete');
- $organizationDeletionRequest = $this->getMockBuilder(OrganizationDeletionRequest::class)->getMock();
- $organizationDeletionRequest->method('getOrganizationId')->willReturn(123);
- $parameters = $this->getMockBuilder(Parameters::class)->getMock();
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $organization->method('getId')->willReturn(123);
- $organization->method('getParameters')->willReturn($parameters);
- $this->organizationRepository
- ->expects(self::once())
- ->method('find')
- ->with(123)
- ->willReturn($organization);
- $this->entityManager->expects(self::once())->method('beginTransaction');
- $this->entityManager->expects(self::once())->method('flush');
- $this->entityManager->expects(self::once())->method('commit');
- $this->entityManager->expects(self::never())->method('rollback');
- $person1 = $this->getMockBuilder(Person::class)->getMock();
- $person1->method('getId')->willReturn(1);
- $person2 = $this->getMockBuilder(Person::class)->getMock();
- $person2->method('getId')->willReturn(2);
- $person3 = $this->getMockBuilder(Person::class)->getMock();
- $person3->method('getId')->willReturn(3);
- $organizationFactory
- ->method('getFutureOrphanPersons')
- ->with($organization)
- ->willReturn([$person1, $person2, $person3]);
- $this->entityManager->expects(self::exactly(5))->method('remove')->withConsecutive(
- [$parameters],
- [$organization],
- [$person1],
- [$person2],
- [$person3],
- );
- $organizationFactory->expects(self::once())->method('deleteTypo3Website')->with(123);
- $organizationFactory->expects(self::once())->method('switchDolibarrSocietyToProspect')->with(123);
- $this->fileManager->expects(self::once())->method('deleteOrganizationFiles')->with(123);
- $this->fileManager
- ->expects(self::exactly(3))
- ->method('deletePersonFiles')
- ->withConsecutive([1], [2], [3]);
- $organizationDeletionRequest
- ->expects(self::once())
- ->method('setStatus')
- ->with(OrganizationDeletionRequest::STATUS_OK);
- $result = $organizationFactory->delete($organizationDeletionRequest);
- $this->assertEquals(
- $organizationDeletionRequest,
- $result
- );
- }
- public function testDeleteWithRollback(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('delete');
- $organizationDeletionRequest = $this->getMockBuilder(OrganizationDeletionRequest::class)->getMock();
- $organizationDeletionRequest->method('getOrganizationId')->willReturn(123);
- $parameters = $this->getMockBuilder(Parameters::class)->getMock();
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $organization->method('getParameters')->willReturn($parameters);
- $person1 = $this->getMockBuilder(Person::class)->getMock();
- $person1->method('getId')->willReturn(1);
- $person2 = $this->getMockBuilder(Person::class)->getMock();
- $person2->method('getId')->willReturn(2);
- $person3 = $this->getMockBuilder(Person::class)->getMock();
- $person3->method('getId')->willReturn(3);
- $organizationFactory
- ->method('getFutureOrphanPersons')
- ->with($organization)
- ->willReturn([$person1, $person2, $person3]);
- $this->organizationRepository
- ->expects(self::once())
- ->method('find')
- ->with(123)
- ->willReturn($organization);
- $this->entityManager->expects(self::once())->method('beginTransaction');
- $this->entityManager->expects(self::never())->method('flush');
- $this->entityManager->expects(self::never())->method('commit');
- $this->entityManager->expects(self::once())->method('rollback');
- $this->entityManager->method('remove')->willThrowException(new \Exception('some error'));
- $organizationFactory->expects(self::never())->method('deleteTypo3Website');
- $organizationFactory->expects(self::never())->method('switchDolibarrSocietyToProspect');
- $organizationDeletionRequest
- ->expects(self::never())
- ->method('setStatus');
- $this->logger
- ->expects(self::once())
- ->method('critical')
- ->with($this->callback(function ($arg) {
- return is_string($arg) && str_contains($arg, 'An error happened, operation cancelled') && str_contains($arg, 'some error');
- }));
- $this->expectException(\Exception::class);
- $organizationFactory->delete($organizationDeletionRequest);
- }
- public function testDeleteWithNonBlockingErrors(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('delete');
- $organizationDeletionRequest = $this->getMockBuilder(OrganizationDeletionRequest::class)->getMock();
- $organizationDeletionRequest->method('getOrganizationId')->willReturn(123);
- $parameters = $this->getMockBuilder(Parameters::class)->getMock();
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $organization->method('getId')->willReturn(123);
- $organization->method('getParameters')->willReturn($parameters);
- $person1 = $this->getMockBuilder(Person::class)->getMock();
- $person1->method('getId')->willReturn(1);
- $person2 = $this->getMockBuilder(Person::class)->getMock();
- $person2->method('getId')->willReturn(2);
- $person3 = $this->getMockBuilder(Person::class)->getMock();
- $person3->method('getId')->willReturn(3);
- $organizationFactory
- ->method('getFutureOrphanPersons')
- ->with($organization)
- ->willReturn([$person1, $person2, $person3]);
- $this->organizationRepository
- ->expects(self::once())
- ->method('find')
- ->with(123)
- ->willReturn($organization);
- $this->entityManager->expects(self::once())->method('beginTransaction');
- $this->entityManager->expects(self::once())->method('flush');
- $this->entityManager->expects(self::once())->method('commit');
- $this->entityManager->expects(self::never())->method('rollback');
- $this->entityManager->expects(self::exactly(5))->method('remove')->withConsecutive(
- [$parameters],
- [$organization],
- [$person1],
- [$person2],
- [$person3],
- );
- $organizationFactory->expects(self::once())->method('deleteTypo3Website')->willThrowException(new \Exception('some error'));
- $organizationFactory->expects(self::once())->method('switchDolibarrSocietyToProspect')->willThrowException(new \Exception('some error'));
- $organizationDeletionRequest
- ->expects(self::once())
- ->method('setStatus')
- ->with(OrganizationDeletionRequest::STATUS_OK_WITH_ERRORS);
- $this->fileManager
- ->expects(self::once())
- ->method('deleteOrganizationFiles')
- ->with(123)
- ->willThrowException(new \Exception('some error'));
- $this->fileManager
- ->expects(self::exactly(3))
- ->method('deletePersonFiles')
- ->withConsecutive([1], [2], [3])
- ->willThrowException(new \Exception('some error'));
- $this->logger
- ->expects(self::exactly(6))
- ->method('critical')
- ->withConsecutive(
- ['An error happened while deleting the Typo3 website, please proceed manually.'],
- ['An error happened while updating the Dolibarr society, please proceed manually.'],
- ["An error happened while deleting the organization's files, please proceed manually."],
- ["An error happened while deleting the person's files, please proceed manually (id=1)."],
- ["An error happened while deleting the person's files, please proceed manually (id=2)."],
- ["An error happened while deleting the person's files, please proceed manually (id=3)."],
- );
- $result = $organizationFactory->delete($organizationDeletionRequest);
- $this->assertEquals(
- $organizationDeletionRequest,
- $result
- );
- }
- public function testGetFutureOrphanPersons(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('getFutureOrphanPersons');
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $access1 = $this->getMockBuilder(Access::class)->getMock();
- $person1 = $this->getMockBuilder(Person::class)->getMock();
- $access1->method('getPerson')->willReturn($person1);
- $access2 = $this->getMockBuilder(Access::class)->getMock();
- $person2 = $this->getMockBuilder(Person::class)->getMock();
- $access2->method('getPerson')->willReturn($person2);
- $access3 = $this->getMockBuilder(Access::class)->getMock();
- $person3 = $this->getMockBuilder(Person::class)->getMock();
- $access3->method('getPerson')->willReturn($person3);
- $otherAccess1 = $this->getMockBuilder(Access::class)->getMock();
- $otherAccess2 = $this->getMockBuilder(Access::class)->getMock();
- $person1->method('getAccesses')->willReturn(new ArrayCollection([$access1, $otherAccess1]));
- $person2->method('getAccesses')->willReturn(new ArrayCollection([$access2, $otherAccess2]));
- $person3->method('getAccesses')->willReturn(new ArrayCollection([$access3]));
- $organization->method('getAccesses')->willReturn(new ArrayCollection([$access1, $access2, $access3]));
- $this->assertEquals(
- [$person3],
- $organizationFactory->getFutureOrphanPersons($organization)
- );
- }
- public function testDeleteTypo3Website(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('deleteTypo3Website');
- $this->typo3Service->expects(self::once())->method('hardDeleteSite')->with(123);
- $organizationFactory->deleteTypo3Website(123);
- }
- public function testSwitchDolibarrSocietyToProspect(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('switchDolibarrSocietyToProspect');
- $this->dolibarrApiService
- ->expects(self::once())
- ->method('switchSocietyToProspect')
- ->with(123);
- $organizationFactory->switchDolibarrSocietyToProspect(123);
- }
- public function testSetAdminAccountPassword(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('setAdminAccountPassword');
- // Create mock organization
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- // Create mock access with adminAccess=true
- $access = $this->getMockBuilder(Access::class)->getMock();
- // Create mock person
- $person = $this->getMockBuilder(Person::class)->getMock();
- // Set up the access to return the person
- $access->method('getPerson')->willReturn($person);
- // Set up the AccessRepository to return the access when findAdminAccess is called
- $this->accessRepository->method('findAdminAccess')->with($organization)->willReturn($access);
- // Expect the person's setPassword method to be called with the password
- $person->expects(self::once())->method('setPassword')->with('Password123!');
- // Expect the EntityManager's persist and flush methods to be called
- $this->entityManager->expects(self::once())->method('persist')->with($person);
- $this->entityManager->expects(self::once())->method('flush');
- // Call the method with a valid password
- $organizationFactory->setAdminAccountPassword($organization, 'Password123!');
- }
- public function testSetAdminAccountPasswordInvalidPassword(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('setAdminAccountPassword');
- // Create mock organization
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- // Expect an exception to be thrown for an invalid password
- $this->expectException(\RuntimeException::class);
- $this->expectExceptionMessage('Password must be at least 8 characters long and include at least one uppercase letter, one lowercase letter, one digit, and one special character.');
- // Call the method with an invalid password
- $organizationFactory->setAdminAccountPassword($organization, 'password');
- }
- public function testSetAdminAccountPasswordNoAdminAccount(): void
- {
- $organizationFactory = $this->getOrganizationFactoryMockFor('setAdminAccountPassword');
- // Create mock organization
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- // Set up the AccessRepository to return null when findAdminAccess is called
- $this->accessRepository->method('findAdminAccess')->with($organization)->willReturn(null);
- // Expect an exception to be thrown when no admin account is found
- $this->expectException(\RuntimeException::class);
- $this->expectExceptionMessage('No admin account found for this organization.');
- // Call the method with a valid password
- $organizationFactory->setAdminAccountPassword($organization, 'Password123!');
- }
- }
|