AccessProfileCreatorTest.php 17 KB

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