AccessProfileCreatorTest.php 17 KB

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