ModuleTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. namespace App\Tests\Unit\Service\Security;
  3. use App\Entity\Organization\Organization;
  4. use App\Entity\Organization\Settings;
  5. use App\Service\Cotisation\Utils as CotisationUtils;
  6. use App\Service\Security\Module;
  7. use App\Service\Utils\ConfigUtils;
  8. use App\Service\Utils\Parser\YamlParser;
  9. use App\Service\Utils\Reflection;
  10. use Hoa\Iterator\Mock;
  11. use PHPUnit\Framework\MockObject\MockObject;
  12. use PHPUnit\Framework\TestCase;
  13. use PHPUnit\Util\Test;
  14. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  15. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  16. class TestableModule extends Module {
  17. public function getModuleByConditionsConfig(): array { return parent::getModuleByConditionsConfig(); }
  18. }
  19. class ModuleTest extends TestCase
  20. {
  21. private MockObject | Reflection $reflection;
  22. private MockObject | ParameterBagInterface $parameterBag;
  23. public function setUp():void
  24. {
  25. $this->reflection = $this->getMockBuilder(Reflection::class)->disableOriginalConstructor()->getMock();
  26. $this->parameterBag = $this->getMockBuilder(ParameterBagInterface::class)->disableOriginalConstructor()->getMock();
  27. }
  28. public function getMockForMethod(string $methodName): MockObject | TestableModule {
  29. return $this->getMockBuilder(TestableModule::class)
  30. ->setConstructorArgs([$this->reflection, $this->parameterBag])
  31. ->setMethodsExcept([$methodName])
  32. ->getMock();
  33. }
  34. /**
  35. * @see Module::getOrganizationModules()
  36. */
  37. public function testGetOrganizationModules(): void
  38. {
  39. $module = $this->getMockForMethod('getOrganizationModules');
  40. $module->expects(self::once())->method('getModuleBySettings')->willReturn(['Sms']);
  41. $module->expects(self::once())->method('getModulesByConditions')->willReturn(['CotisationCall']);
  42. $settings = $this->getMockBuilder(Settings::class)->disableOriginalConstructor()->getMock();
  43. $settings->expects(self::once())->method('getProduct')->willReturn('school');
  44. $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
  45. $organization->expects(self::once())->method('getSettings')->willReturn($settings);
  46. $module->expects(self::once())->method('getModulesByProductConfiguration')->willReturn(['Notification']);
  47. $this->assertEqualsCanonicalizing(
  48. ['Sms', 'CotisationCall', 'Notification'],
  49. $module->getOrganizationModules($organization)
  50. );
  51. }
  52. /**
  53. * @see Module::getModuleBySettings()
  54. */
  55. public function testGetModuleBySettings(): void
  56. {
  57. $module = $this->getMockForMethod('getModuleBySettings');
  58. $settings = $this->getMockBuilder(Settings::class)->getMock();
  59. $settings
  60. ->expects($this->once())
  61. ->method('getModules')
  62. ->willReturn(["Sms" => true]);
  63. $organization = $this->getMockBuilder(Organization::class)->getMock();
  64. $organization
  65. ->expects($this->once())
  66. ->method('getSettings')
  67. ->willReturn($settings);
  68. $this->assertContains('Sms', $module->getModuleBySettings($organization)) ;
  69. }
  70. /**
  71. * @see Module::getModulesByConditions()
  72. */
  73. public function testGetModulesByConditions(): void
  74. {
  75. $module = $this->getMockForMethod('getModulesByConditions');
  76. $this->parameterBag->method('get')->with('opentalent.modulesbyconditions')->willReturn(
  77. ['CotisationCall' => [
  78. 'roles' => ['ROLE_COTISATION'],
  79. 'conditions' => [
  80. 'service' => [
  81. 'name' => CotisationUtils::class,
  82. 'function' => 'isLastParentAndCMF'
  83. ]
  84. ]
  85. ]]
  86. );
  87. $organization = $this->getMockBuilder(Organization::class)->getMock();
  88. $this->reflection
  89. ->method('dynamicInvokeServiceWithArgsAndMethod')
  90. ->with(CotisationUtils::class, 'isLastParentAndCMF', array($organization))
  91. ->willReturn(true);
  92. $this->assertContains('CotisationCall', $module->getModulesByConditions($organization)) ;
  93. }
  94. /**
  95. * @see Module::getModulesByConditions()
  96. */
  97. public function testGetModulesByConditionsLogicError(): void
  98. {
  99. $module = $this->getMockForMethod('getModulesByConditions');
  100. $this->parameterBag->method('get')->with('opentalent.modulesbyconditions')->willReturn(
  101. ['CotisationCall' => []]
  102. );
  103. $organization = $this->getMockBuilder(Organization::class)->getMock();
  104. $this->reflection
  105. ->method('dynamicInvokeServiceWithArgsAndMethod')
  106. ->with(CotisationUtils::class, 'isLastParentAndCMF', array($organization))
  107. ->willThrowException(new \Exception());
  108. $this->expectException(\LogicException::class);
  109. $module->getModulesByConditions($organization);
  110. }
  111. /**
  112. * @see Module::getModulesByProductConfiguration()
  113. */
  114. public function testGetModulesByProductConfiguration(): void
  115. {
  116. $module = $this->getMockForMethod('getModulesByProductConfiguration');
  117. $this->parameterBag->method('get')->with('opentalent.products')->willReturn(
  118. ['artist' => ['modules' => ['foo']]]
  119. );
  120. $this->assertEquals(['foo'], $module->getModulesByProductConfiguration('artist')) ;
  121. }
  122. /**
  123. * @see Module::getModulesByProductConfiguration()
  124. */
  125. public function testGetModulesByProductConfigurationExtend(): void
  126. {
  127. $module = $this->getMockForMethod('getModulesByProductConfiguration');
  128. $this->parameterBag->method('get')->with('opentalent.products')->willReturn(
  129. [
  130. 'artist' => ['modules' => ['foo']],
  131. 'artist_premium' => ['extend' => 'artist', 'modules' => ['bar']]
  132. ]
  133. );
  134. $this->assertEqualsCanonicalizing(
  135. ['foo', 'bar'],
  136. $module->getModulesByProductConfiguration('artist-premium')
  137. ) ;
  138. }
  139. /**
  140. * @see Module::getModulesByProductConfiguration()
  141. */
  142. public function testGetModulesByProductConfigurationAccessDenied(): void
  143. {
  144. $module = $this->getMockBuilder(TestableModule::class);
  145. $module = $this->getMockForMethod('getModulesByProductConfiguration');
  146. $this->parameterBag->method('get')->with('opentalent.products')->willReturn(
  147. ['artist' => ['modules' => ['foo']]]
  148. );
  149. $this->expectException(AccessDeniedHttpException::class);
  150. $this->expectExceptionMessage('The product artist_premium does not exist !');
  151. $module->getModulesByProductConfiguration('artist-premium');
  152. }
  153. /**
  154. * @see Module::getModuleByResourceName()
  155. */
  156. public function testGetModuleByResourceName(): void {
  157. $module = $this->getMockForMethod('getModuleByResourceName');
  158. $this->parameterBag->method('get')->with('opentalent.products')->willReturn(
  159. ['Core' => ['entities' => ['foo', 'bar']]]
  160. );
  161. $this->assertEquals('Core', $module->getModuleByResourceName('foo'));
  162. }
  163. /**
  164. * @see Module::getModuleByResourceName()
  165. */
  166. public function testGetModuleByResourceNameNotFound(): void {
  167. $module = $this->getMockForMethod('getModuleByResourceName');
  168. $this->parameterBag->method('get')->with('opentalent.products')->willReturn(
  169. ['Core' => ['entities' => ['bar']]]
  170. );
  171. $this->assertNull($module->getModuleByResourceName('foo'));
  172. }
  173. }