AccessProfileCreatorTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. <?php
  2. namespace App\Tests\Unit\Service\Access;
  3. use App\ApiResources\Profile\AccessProfile;
  4. use App\ApiResources\Profile\OrganizationProfile;
  5. use App\Entity\Access\Access;
  6. use App\Entity\Access\AccessPreference;
  7. use App\Entity\Core\File;
  8. use App\Entity\Organization\Organization;
  9. use App\Entity\Person\Person;
  10. use App\Enum\Person\GenderEnum;
  11. use App\Repository\Access\AccessRepository;
  12. use App\Service\Access\AccessProfileCreator;
  13. use App\Service\Access\Utils as AccessUtils;
  14. use App\Service\Organization\OrganizationProfileCreator;
  15. use Doctrine\Common\Collections\ArrayCollection;
  16. use Doctrine\Common\Collections\Collection;
  17. use Doctrine\ORM\EntityManagerInterface;
  18. use PHPUnit\Framework\MockObject\MockObject;
  19. use PHPUnit\Framework\TestCase;
  20. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  21. class AccessProfileCreatorTest extends TestCase
  22. {
  23. private MockObject|OrganizationProfileCreator $organizationProfileCreator;
  24. private MockObject|AccessRepository $accessRepository;
  25. private MockObject|AccessUtils $accessUtils;
  26. private MockObject|AccessProfileCreator $accessProfileCreator;
  27. private MockObject|AccessProfile $accessProfile;
  28. private MockObject|AccessPreference $accessPreference;
  29. private MockObject|Collection $emptyCollection;
  30. private MockObject|Collection $nonEmptyCollection;
  31. private MockObject|Organization $organization;
  32. private MockObject|Access $access;
  33. private MockObject|OrganizationProfile $organizationProfile;
  34. private MockObject|EntityManagerInterface $entityManager;
  35. public function setUp(): void
  36. {
  37. $this->organizationProfileCreator = $this->getMockBuilder(OrganizationProfileCreator::class)->disableOriginalConstructor()->getMock();
  38. $this->accessRepository = $this->getMockBuilder(AccessRepository::class)->disableOriginalConstructor()->getMock();
  39. $this->accessUtils = $this->getMockBuilder(AccessUtils::class)->disableOriginalConstructor()->getMock();
  40. $this->entityManager = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock();
  41. $this->emptyCollection = $this->getMockBuilder(Collection::class)->getMock();
  42. $this->emptyCollection->method('isEmpty')->willReturn(true);
  43. $this->nonEmptyCollection = $this->getMockBuilder(Collection::class)->getMock();
  44. $this->nonEmptyCollection->method('isEmpty')->willReturn(false);
  45. $this->organization = $this->getMockBuilder(Organization::class)->getMock();
  46. $this->access = $this->getMockBuilder(Access::class)->getMock();
  47. $this->organizationProfile = $this->getMockBuilder(OrganizationProfile::class)->getMock();
  48. $this->accessProfile = $this->getMockBuilder(AccessProfile::class)->getMock();
  49. $this->accessPreference = $this->getMockBuilder(AccessPreference::class)->getMock();
  50. }
  51. /**
  52. * @see AccessProfileCreator::getAccessProfile()
  53. */
  54. public function testGetAccessProfileFailed(): void
  55. {
  56. $this->accessProfileCreator = $this
  57. ->getMockBuilder(AccessProfileCreator::class)
  58. ->setConstructorArgs([$this->organizationProfileCreator, $this->accessRepository, $this->accessUtils, $this->entityManager])
  59. ->setMethodsExcept(['getAccessProfile'])
  60. ->getMock();
  61. $this->accessRepository
  62. ->method('findAllValidAccesses')
  63. ->with($this->access)
  64. ->willReturn([]);
  65. $this->expectException(AuthenticationException::class);
  66. $this->accessProfileCreator->getAccessProfile($this->access);
  67. }
  68. /**
  69. * Access valide avec multicompte et famille.
  70. *
  71. * @see AccessProfileCreator::getAccessProfile()
  72. */
  73. public function testGetAccessProfile(): void
  74. {
  75. $accessProfileCreator = $this
  76. ->getMockBuilder(AccessProfileCreator::class)
  77. ->setConstructorArgs([$this->organizationProfileCreator, $this->accessRepository, $this->accessUtils, $this->entityManager])
  78. ->setMethodsExcept(['getAccessProfile'])
  79. ->getMock();
  80. $otherAccess1 = $this->getMockBuilder(Access::class)->getMock();
  81. $otherAccess2 = $this->getMockBuilder(Access::class)->getMock();
  82. // Find valid accesses
  83. $this->accessRepository
  84. ->method('findAllValidAccesses')
  85. ->with($this->access)
  86. ->willReturn([$this->access, $otherAccess1, $otherAccess2]);
  87. // Find valid accesses
  88. $this->access
  89. ->method('getPreferences')
  90. ->willReturn(null);
  91. // create the profile
  92. $accessProfileCreator
  93. ->expects(self::once())
  94. ->method('createAccessPrefence')
  95. ->with($this->access);
  96. // create the profile
  97. $accessProfileCreator
  98. ->expects(self::once())
  99. ->method('createCompleteAccessProfile')
  100. ->with($this->access)
  101. ->willReturn($this->accessProfile);
  102. // Multi compte
  103. $otherOrganization1 = $this->getMockBuilder(Organization::class)->getMock();
  104. $otherOrganization2 = $this->getMockBuilder(Organization::class)->getMock();
  105. $otherAccess1->method('getOrganization')->willReturn($otherOrganization1);
  106. $otherAccess2->method('getOrganization')->willReturn($otherOrganization2);
  107. $otherOrganizationProfile1 = $this->getMockBuilder(OrganizationProfile::class)->getMock();
  108. $otherOrganizationProfile2 = $this->getMockBuilder(OrganizationProfile::class)->getMock();
  109. $this->organizationProfileCreator
  110. ->expects(self::exactly(2))
  111. ->method('createLightOrganizationProfile')
  112. ->willReturnMap(
  113. [
  114. [$otherOrganization1, $otherOrganizationProfile1],
  115. [$otherOrganization2, $otherOrganizationProfile2],
  116. ]
  117. );
  118. $this->accessUtils
  119. ->expects(self::once())
  120. ->method('filterAccesses')
  121. ->with([$this->access, $otherAccess1, $otherAccess2], $this->access)
  122. ->willReturn([$otherAccess1, $otherAccess2]);
  123. $this->accessProfile
  124. ->expects(self::exactly(2))
  125. ->method('addMultiAccess')
  126. ->withConsecutive(
  127. [$otherOrganizationProfile1],
  128. [$otherOrganizationProfile2]
  129. );
  130. // Accesses famille
  131. $children1 = $this->getMockBuilder(Access::class)->getMock();
  132. $children2 = $this->getMockBuilder(Access::class)->getMock();
  133. $childrenProfile1 = $this->getMockBuilder(AccessProfile::class)->getMock();
  134. $childrenProfile2 = $this->getMockBuilder(AccessProfile::class)->getMock();
  135. $this->access->expects(self::once())->method('getChildren')->willReturn(new ArrayCollection([$children1, $children2]));
  136. $accessProfileCreator
  137. ->expects(self::exactly(2))
  138. ->method('createLightAccessProfile')
  139. ->willReturnMap([
  140. [$children1, $childrenProfile1],
  141. [$children2, $childrenProfile2],
  142. ]);
  143. $this->accessProfile
  144. ->expects(self::exactly(2))
  145. ->method('addFamilyAccess')
  146. ->withConsecutive(
  147. [$childrenProfile1],
  148. [$childrenProfile2]
  149. );
  150. $accessProfileCreator->getAccessProfile($this->access);
  151. }
  152. /**
  153. * Test d'un access en compte switch.
  154. *
  155. * @see AccessProfileCreator::getAccessProfile()
  156. */
  157. public function testGetAccessProfileSwitch(): void
  158. {
  159. $accessProfileCreator = $this
  160. ->getMockBuilder(AccessProfileCreator::class)
  161. ->setConstructorArgs([$this->organizationProfileCreator, $this->accessRepository, $this->accessUtils, $this->entityManager])
  162. ->setMethodsExcept(['getAccessProfile'])
  163. ->getMock();
  164. $this->accessRepository
  165. ->method('findAllValidAccesses')
  166. ->with($this->access)
  167. ->willReturn([$this->access]);
  168. $accessProfileCreator
  169. ->expects(self::once())
  170. ->method('createCompleteAccessProfile')
  171. ->with($this->access)
  172. ->willReturn($this->accessProfile);
  173. $this->accessUtils
  174. ->expects(self::once())
  175. ->method('filterAccesses')
  176. ->with([$this->access], $this->access)
  177. ->willReturn([]);
  178. $this->access->method('getChildren')->willReturn(new ArrayCollection([]));
  179. $originalAccess = $this->getMockBuilder(Access::class)->getMock();
  180. $originalAccessProfile = $this->getMockBuilder(AccessProfile::class)->getMock();
  181. $accessProfileCreator
  182. ->expects(self::once())
  183. ->method('createLightAccessProfile')
  184. ->with($originalAccess)
  185. ->willReturn($originalAccessProfile);
  186. $this->accessProfile->expects(self::once())->method('setOriginalAccess')->with($originalAccessProfile);
  187. $accessProfileCreator->getAccessProfile($this->access, $originalAccess);
  188. }
  189. /**
  190. * Setup mocks for the tests on createCompleteAccessProfile.
  191. *
  192. * @see AccessProfileCreator::createCompleteAccessProfile()
  193. */
  194. private function setupCreateCompleteAccessProfile(): void
  195. {
  196. $this->accessProfileCreator = $this
  197. ->getMockBuilder(AccessProfileCreator::class)
  198. ->setConstructorArgs([$this->organizationProfileCreator, $this->accessRepository, $this->accessUtils, $this->entityManager])
  199. ->setMethodsExcept(['createCompleteAccessProfile'])
  200. ->getMock();
  201. $this->access->expects(self::once())->method('getAdminAccess')->willReturn(true);
  202. $this->access->expects(self::once())->method('getHistorical')->willReturn(['present' => true]);
  203. $this->access->expects(self::once())->method('getOrganization')->willReturn($this->organization);
  204. $this->accessUtils->expects(self::once())->method('getAllRoles')->with($this->access)->willReturn(['FOO']);
  205. $this->organizationProfileCreator
  206. ->expects(self::once())
  207. ->method('createCompleteOrganizationProfile')
  208. ->with($this->organization)
  209. ->willReturn($this->organizationProfile);
  210. $this->accessProfile->method('setIsAdminAccess')->willReturnSelf();
  211. $this->accessProfile->method('setRoles')->willReturnSelf();
  212. $this->accessProfile->method('setHistorical')->willReturnSelf();
  213. $this->accessProfile->method('setOrganization')->willReturnSelf();
  214. $this->accessProfile->method('setIsGuardian')->willReturnSelf();
  215. $this->accessProfile->method('setIsPayor')->willReturnSelf();
  216. $this->accessProfileCreator
  217. ->expects(self::once())
  218. ->method('createLightAccessProfile')
  219. ->with($this->access)
  220. ->willReturn($this->accessProfile);
  221. }
  222. /**
  223. * Default situation: the access is neither guardian nor payer or admin.
  224. *
  225. * @see AccessProfileCreator::createCompleteAccessProfile()
  226. */
  227. public function testCreateCompleteAccessProfile(): void
  228. {
  229. $this->setupCreateCompleteAccessProfile();
  230. $this->access->method('getChildren')->willReturn($this->emptyCollection);
  231. $this->access->method('getBillingPayers')->willReturn($this->emptyCollection);
  232. $this->access->method('getBillingReceivers')->willReturn($this->emptyCollection);
  233. $this->access->method('getAccessIntangibles')->willReturn($this->emptyCollection);
  234. $this->accessProfile->expects(self::once())->method('setIsAdminAccess')->with(true)->willReturnSelf();
  235. $this->accessProfile->expects(self::once())->method('setRoles')->with(['FOO'])->willReturnSelf();
  236. $this->accessProfile->expects(self::once())->method('setHistorical')->with(['present' => true])->willReturnSelf();
  237. $this->accessProfile->expects(self::once())->method('setOrganization')->with($this->organizationProfile)->willReturnSelf();
  238. $this->accessProfile->expects(self::once())->method('setIsGuardian')->with(false)->willReturnSelf();
  239. $this->accessProfile->expects(self::once())->method('setIsPayor')->with(false)->willReturnSelf();
  240. $this->accessProfileCreator->createCompleteAccessProfile($this->access);
  241. }
  242. /**
  243. * If the access has children, isGuardian shall be set to true.
  244. *
  245. * @see AccessProfileCreator::createCompleteAccessProfile()
  246. */
  247. public function testCreateCompleteAccessProfileIsGuardian(): void
  248. {
  249. $this->setupCreateCompleteAccessProfile();
  250. $this->access->method('getChildren')->willReturn($this->nonEmptyCollection);
  251. $this->access->method('getBillingPayers')->willReturn($this->emptyCollection);
  252. $this->access->method('getBillingReceivers')->willReturn($this->emptyCollection);
  253. $this->access->method('getAccessIntangibles')->willReturn($this->emptyCollection);
  254. $this->accessProfile->expects(self::once())->method('setIsGuardian')->with(true)->willReturnSelf();
  255. $this->accessProfileCreator->createCompleteAccessProfile($this->access);
  256. }
  257. /**
  258. * If the access has billingPayers, isPayor shall be set to true.
  259. *
  260. * @see AccessProfileCreator::createCompleteAccessProfile()
  261. */
  262. public function testCreateCompleteAccessProfileIsPayorWithBillingPayer(): void
  263. {
  264. $this->setupCreateCompleteAccessProfile();
  265. $this->access->method('getChildren')->willReturn($this->emptyCollection);
  266. $this->access->method('getBillingPayers')->willReturn($this->nonEmptyCollection);
  267. $this->access->method('getBillingReceivers')->willReturn($this->emptyCollection);
  268. $this->access->method('getAccessIntangibles')->willReturn($this->emptyCollection);
  269. $this->accessProfile->expects(self::once())->method('setIsPayor')->with(true)->willReturnSelf();
  270. $this->accessProfileCreator->createCompleteAccessProfile($this->access);
  271. }
  272. /**
  273. * If the access has no billingPayers but has AccessIntangible, isPayor shall be set to true.
  274. *
  275. * @see AccessProfileCreator::createCompleteAccessProfile()
  276. */
  277. public function testCreateCompleteAccessProfileIsPayorWithAccessIntangible(): void
  278. {
  279. $this->setupCreateCompleteAccessProfile();
  280. $this->access->method('getChildren')->willReturn($this->emptyCollection);
  281. $this->access->method('getBillingPayers')->willReturn($this->emptyCollection);
  282. $this->access->method('getBillingReceivers')->willReturn($this->emptyCollection);
  283. $this->access->method('getAccessIntangibles')->willReturn($this->nonEmptyCollection);
  284. $this->accessProfile->expects(self::once())->method('setIsPayor')->with(true)->willReturnSelf();
  285. $this->accessProfileCreator->createCompleteAccessProfile($this->access);
  286. }
  287. /**
  288. * If the access has no billingPayers, has AccessIntangible but also have children, isPayor shall be set to false.
  289. *
  290. * @see AccessProfileCreator::createCompleteAccessProfile()
  291. */
  292. public function testCreateCompleteAccessProfileNotPayorWithAccessIntangibleBecauseChildren(): void
  293. {
  294. $this->setupCreateCompleteAccessProfile();
  295. $this->access->method('getChildren')->willReturn($this->nonEmptyCollection);
  296. $this->access->method('getBillingPayers')->willReturn($this->emptyCollection);
  297. $this->access->method('getBillingReceivers')->willReturn($this->emptyCollection);
  298. $this->access->method('getAccessIntangibles')->willReturn($this->nonEmptyCollection);
  299. $this->accessProfile->expects(self::once())->method('setIsPayor')->with(false)->willReturnSelf();
  300. $this->accessProfileCreator->createCompleteAccessProfile($this->access);
  301. }
  302. /**
  303. * @see AccessProfileCreator::createLightAccessProfile()
  304. */
  305. public function testCreateLightAccessProfile(): void
  306. {
  307. $accessProfileCreator = $this
  308. ->getMockBuilder(AccessProfileCreator::class)
  309. ->setConstructorArgs([$this->organizationProfileCreator, $this->accessRepository, $this->accessUtils, $this->entityManager])
  310. ->setMethodsExcept(['createLightAccessProfile'])
  311. ->getMock();
  312. $image = $this->getMockBuilder(File::class)->getMock();
  313. $image->expects(self::once())->method('getId')->willReturn(123);
  314. $person = $this->getMockBuilder(Person::class)->getMock();
  315. $person->expects(self::once())->method('getName')->willReturn('Foo');
  316. $person->expects(self::once())->method('getGivenName')->willReturn('Bar');
  317. $person->expects(self::once())->method('getGender')->willReturn(GenderEnum::MISTER);
  318. $person->expects(self::once())->method('getImage')->willReturn($image);
  319. $accessPreference = $this->getMockBuilder(AccessPreference::class)->getMock();
  320. $accessPreference->expects(self::once())->method('getId')->willReturn(1);
  321. $this->access->expects(self::once())->method('getId')->willReturn(1);
  322. $this->access->method('getOrganization')->willReturn($this->organization);
  323. $this->access->method('getPerson')->willReturn($person);
  324. $this->access->expects(self::once())->method('getPreferences')->willReturn($accessPreference);
  325. $this->access->expects(self::once())->method('getActivityYear')->willReturn(2020);
  326. $this->access->expects(self::once())->method('getSuperAdminAccess')->willReturn(false);
  327. $this->organizationProfileCreator
  328. ->expects(self::once())
  329. ->method('createLightOrganizationProfile')
  330. ->with($this->organization)
  331. ->willReturn($this->organizationProfile);
  332. $accessProfile = $accessProfileCreator->createLightAccessProfile($this->access);
  333. $this->assertEquals(1, $accessProfile->getId());
  334. $this->assertEquals('Foo', $accessProfile->getName());
  335. $this->assertEquals('Bar', $accessProfile->getGivenName());
  336. $this->assertEquals(GenderEnum::MISTER, $accessProfile->getGender());
  337. $this->assertEquals(2020, $accessProfile->getActivityYear());
  338. $this->assertEquals(123, $accessProfile->getAvatarId());
  339. $this->assertFalse($accessProfile->getIsSuperAdminAccess());
  340. $this->assertEquals($this->organizationProfile, $accessProfile->getOrganization());
  341. $this->assertEquals(1, $accessProfile->getAccessPreferenceId());
  342. }
  343. /**
  344. * If the access has no billingPayers, has AccessIntangible but also have children, isPayor shall be set to false.
  345. *
  346. * @see AccessProfileCreator::createAccessPrefence()
  347. */
  348. public function testCreateAccessPreference(): void
  349. {
  350. $accessProfileCreator = $this
  351. ->getMockBuilder(AccessProfileCreator::class)
  352. ->setConstructorArgs([$this->organizationProfileCreator, $this->accessRepository, $this->accessUtils, $this->entityManager])
  353. ->setMethodsExcept(['createAccessPrefence'])
  354. ->getMock();
  355. $this->access->expects(self::once())->method('setPreferences');
  356. $this->entityManager->expects(self::once())->method('flush')->with($this->access);
  357. $accessProfileCreator->createAccessPrefence($this->access);
  358. }
  359. }