AccessProfileCreatorTest.php 19 KB

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