AccessProfileCreatorTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. {
  144. $accessProfileCreator = $this
  145. ->getMockBuilder(AccessProfileCreator::class)
  146. ->setConstructorArgs([$this->organizationProfileCreator, $this->accessRepository, $this->accessUtils])
  147. ->setMethodsExcept(['getAccessProfile'])
  148. ->getMock();
  149. $this->accessRepository
  150. ->method('findAllValidAccesses')
  151. ->with($this->access)
  152. ->willReturn([$this->access]);
  153. $accessProfileCreator
  154. ->expects(self::once())
  155. ->method('createCompleteAccessProfile')
  156. ->with($this->access)
  157. ->willReturn($this->accessProfile);
  158. $this->accessUtils
  159. ->expects(self::once())
  160. ->method('filterAccesses')
  161. ->with([$this->access], $this->access)
  162. ->willReturn([]);
  163. $this->access->method('getChildren')->willReturn(new ArrayCollection([]));
  164. $originalAccess = $this->getMockBuilder(Access::class)->getMock();
  165. $originalAccessProfile = $this->getMockBuilder(AccessProfile::class)->getMock();
  166. $accessProfileCreator
  167. ->expects(self::once())
  168. ->method('createLightAccessProfile')
  169. ->with($originalAccess)
  170. ->willReturn($originalAccessProfile);
  171. $this->accessProfile->expects(self::once())->method('setOriginalAccess')->with($originalAccessProfile);
  172. $accessProfileCreator->getAccessProfile($this->access, $originalAccess);
  173. }
  174. /**
  175. * Setup mocks for the tests on createCompleteAccessProfile.
  176. *
  177. * @see AccessProfileCreator::createCompleteAccessProfile()
  178. */
  179. private function setupCreateCompleteAccessProfile(): void
  180. {
  181. $this->accessProfileCreator = $this
  182. ->getMockBuilder(AccessProfileCreator::class)
  183. ->setConstructorArgs([$this->organizationProfileCreator, $this->accessRepository, $this->accessUtils])
  184. ->setMethodsExcept(['createCompleteAccessProfile'])
  185. ->getMock();
  186. $this->access->expects(self::once())->method('getAdminAccess')->willReturn(true);
  187. $this->access->expects(self::once())->method('getHistorical')->willReturn(['present' => true]);
  188. $this->access->expects(self::once())->method('getOrganization')->willReturn($this->organization);
  189. $this->accessUtils->expects(self::once())->method('getAllRoles')->with($this->access)->willReturn(['FOO']);
  190. $this->organizationProfileCreator
  191. ->expects(self::once())
  192. ->method('createCompleteOrganizationProfile')
  193. ->with($this->organization)
  194. ->willReturn($this->organizationProfile);
  195. $this->accessProfile->method('setIsAdminAccess')->willReturnSelf();
  196. $this->accessProfile->method('setRoles')->willReturnSelf();
  197. $this->accessProfile->method('setHistorical')->willReturnSelf();
  198. $this->accessProfile->method('setOrganization')->willReturnSelf();
  199. $this->accessProfile->method('setIsGuardian')->willReturnSelf();
  200. $this->accessProfile->method('setIsPayor')->willReturnSelf();
  201. $this->accessProfileCreator
  202. ->expects(self::once())
  203. ->method('createLightAccessProfile')
  204. ->with($this->access)
  205. ->willReturn($this->accessProfile);
  206. }
  207. /**
  208. * Default situation: the access is neither guardian nor payer or admin.
  209. *
  210. * @see AccessProfileCreator::createCompleteAccessProfile()
  211. */
  212. public function testCreateCompleteAccessProfile(): void
  213. {
  214. $this->setupCreateCompleteAccessProfile();
  215. $this->access->method('getChildren')->willReturn($this->emptyCollection);
  216. $this->access->method('getBillingPayers')->willReturn($this->emptyCollection);
  217. $this->access->method('getBillingReceivers')->willReturn($this->emptyCollection);
  218. $this->access->method('getAccessIntangibles')->willReturn($this->emptyCollection);
  219. $this->accessProfile->expects(self::once())->method('setIsAdminAccess')->with(true)->willReturnSelf();
  220. $this->accessProfile->expects(self::once())->method('setRoles')->with(['FOO'])->willReturnSelf();
  221. $this->accessProfile->expects(self::once())->method('setHistorical')->with(['present' => true])->willReturnSelf();
  222. $this->accessProfile->expects(self::once())->method('setOrganization')->with($this->organizationProfile)->willReturnSelf();
  223. $this->accessProfile->expects(self::once())->method('setIsGuardian')->with(false)->willReturnSelf();
  224. $this->accessProfile->expects(self::once())->method('setIsPayor')->with(false)->willReturnSelf();
  225. $this->accessProfileCreator->createCompleteAccessProfile($this->access);
  226. }
  227. /**
  228. * If the access has children, isGuardian shall be set to true.
  229. *
  230. * @see AccessProfileCreator::createCompleteAccessProfile()
  231. */
  232. public function testCreateCompleteAccessProfileIsGuardian(): void
  233. {
  234. $this->setupCreateCompleteAccessProfile();
  235. $this->access->method('getChildren')->willReturn($this->nonEmptyCollection);
  236. $this->access->method('getBillingPayers')->willReturn($this->emptyCollection);
  237. $this->access->method('getBillingReceivers')->willReturn($this->emptyCollection);
  238. $this->access->method('getAccessIntangibles')->willReturn($this->emptyCollection);
  239. $this->accessProfile->expects(self::once())->method('setIsGuardian')->with(true)->willReturnSelf();
  240. $this->accessProfileCreator->createCompleteAccessProfile($this->access);
  241. }
  242. /**
  243. * If the access has billingPayers, isPayor shall be set to true.
  244. *
  245. * @see AccessProfileCreator::createCompleteAccessProfile()
  246. */
  247. public function testCreateCompleteAccessProfileIsPayorWithBillingPayer(): void
  248. {
  249. $this->setupCreateCompleteAccessProfile();
  250. $this->access->method('getChildren')->willReturn($this->emptyCollection);
  251. $this->access->method('getBillingPayers')->willReturn($this->nonEmptyCollection);
  252. $this->access->method('getBillingReceivers')->willReturn($this->emptyCollection);
  253. $this->access->method('getAccessIntangibles')->willReturn($this->emptyCollection);
  254. $this->accessProfile->expects(self::once())->method('setIsPayor')->with(true)->willReturnSelf();
  255. $this->accessProfileCreator->createCompleteAccessProfile($this->access);
  256. }
  257. /**
  258. * If the access has no billingPayers but has AccessIntangible, isPayor shall be set to true.
  259. *
  260. * @see AccessProfileCreator::createCompleteAccessProfile()
  261. */
  262. public function testCreateCompleteAccessProfileIsPayorWithAccessIntangible(): void
  263. {
  264. $this->setupCreateCompleteAccessProfile();
  265. $this->access->method('getChildren')->willReturn($this->emptyCollection);
  266. $this->access->method('getBillingPayers')->willReturn($this->emptyCollection);
  267. $this->access->method('getBillingReceivers')->willReturn($this->emptyCollection);
  268. $this->access->method('getAccessIntangibles')->willReturn($this->nonEmptyCollection);
  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, has AccessIntangible but also have children, isPayor shall be set to false.
  274. *
  275. * @see AccessProfileCreator::createCompleteAccessProfile()
  276. */
  277. public function testCreateCompleteAccessProfileNotPayorWithAccessIntangibleBecauseChildren(): void
  278. {
  279. $this->setupCreateCompleteAccessProfile();
  280. $this->access->method('getChildren')->willReturn($this->nonEmptyCollection);
  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(false)->willReturnSelf();
  285. $this->accessProfileCreator->createCompleteAccessProfile($this->access);
  286. }
  287. /**
  288. * @see AccessProfileCreator::createLightAccessProfile()
  289. */
  290. public function testCreateLightAccessProfile(): void
  291. {
  292. $accessProfileCreator = $this
  293. ->getMockBuilder(AccessProfileCreator::class)
  294. ->setConstructorArgs([$this->organizationProfileCreator, $this->accessRepository, $this->accessUtils])
  295. ->setMethodsExcept(['createLightAccessProfile'])
  296. ->getMock();
  297. $image = $this->getMockBuilder(File::class)->getMock();
  298. $image->expects(self::once())->method('getId')->willReturn(123);
  299. $person = $this->getMockBuilder(Person::class)->getMock();
  300. $person->expects(self::once())->method('getName')->willReturn('Foo');
  301. $person->expects(self::once())->method('getGivenName')->willReturn('Bar');
  302. $person->expects(self::once())->method('getGender')->willReturn(GenderEnum::MISTER);
  303. $person->expects(self::once())->method('getImage')->willReturn($image);
  304. $this->access->expects(self::once())->method('getId')->willReturn(1);
  305. $this->access->method('getOrganization')->willReturn($this->organization);
  306. $this->access->method('getPerson')->willReturn($person);
  307. $this->access->expects(self::once())->method('getActivityYear')->willReturn(2020);
  308. $this->access->expects(self::once())->method('getSuperAdminAccess')->willReturn(false);
  309. $this->organizationProfileCreator
  310. ->expects(self::once())
  311. ->method('createLightOrganizationProfile')
  312. ->with($this->organization)
  313. ->willReturn($this->organizationProfile);
  314. $accessProfile = $accessProfileCreator->createLightAccessProfile($this->access);
  315. $this->assertEquals(1, $accessProfile->getId());
  316. $this->assertEquals('Foo', $accessProfile->getName());
  317. $this->assertEquals('Bar', $accessProfile->getGivenName());
  318. $this->assertEquals(GenderEnum::MISTER, $accessProfile->getGender());
  319. $this->assertEquals(2020, $accessProfile->getActivityYear());
  320. $this->assertEquals(123, $accessProfile->getAvatarId());
  321. $this->assertFalse($accessProfile->getIsSuperAdminAccess());
  322. $this->assertEquals($this->organizationProfile, $accessProfile->getOrganization());
  323. }
  324. }