AccessProfileCreatorTest.php 17 KB

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