ModuleTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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\HttpKernel\Exception\AccessDeniedHttpException;
  15. class TestableModule extends Module {
  16. public function getModuleByConditionsConfig(): array { return parent::getModuleByConditionsConfig(); }
  17. }
  18. class ModuleTest extends TestCase
  19. {
  20. private const OPENTALENT_CONFIG = __DIR__.'/../../../config/opentalent';
  21. private MockObject | Reflection $reflection;
  22. private MockObject | YamlParser $parser;
  23. public function setUp():void
  24. {
  25. $this->reflection = $this->getMockBuilder(Reflection::class)->disableOriginalConstructor()->getMock();
  26. $this->configUtils = $this->getMockBuilder(ConfigUtils::class)->disableOriginalConstructor()->getMock();
  27. }
  28. public function getMockForMethod(string $methodName): MockObject | TestableModule {
  29. return $this->getMockBuilder(TestableModule::class)
  30. ->setConstructorArgs([$this->reflection, $this->configUtils])
  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->configUtils->method('loadFromFile')->with('modulesbyconditions')->willReturn(
  77. ['opentalent' =>
  78. ['modulesbyconditions' =>
  79. ['CotisationCall' => [
  80. 'roles' => ['ROLE_COTISATION'],
  81. 'conditions' => [
  82. 'service' => [
  83. 'name' => CotisationUtils::class,
  84. 'function' => 'isLastParentAndCMF'
  85. ]
  86. ]
  87. ]]
  88. ]
  89. ]
  90. );
  91. $organization = $this->getMockBuilder(Organization::class)->getMock();
  92. $this->reflection
  93. ->method('dynamicInvokeServiceWithArgsAndMethod')
  94. ->with(CotisationUtils::class, 'isLastParentAndCMF', array($organization))
  95. ->willReturn(true);
  96. $this->assertContains('CotisationCall', $module->getModulesByConditions($organization)) ;
  97. }
  98. /**
  99. * @see Module::getModulesByConditions()
  100. */
  101. public function testGetModulesByConditionsLogicError(): void
  102. {
  103. $module = $this->getMockForMethod('getModulesByConditions');
  104. $this->configUtils->method('loadFromFile')->with('modulesbyconditions')->willReturn(
  105. ['opentalent' =>
  106. ['modulesbyconditions' =>
  107. ['CotisationCall' => []]
  108. ]
  109. ]
  110. );
  111. $organization = $this->getMockBuilder(Organization::class)->getMock();
  112. $this->reflection
  113. ->method('dynamicInvokeServiceWithArgsAndMethod')
  114. ->with(CotisationUtils::class, 'isLastParentAndCMF', array($organization))
  115. ->willThrowException(new \Exception());
  116. $this->expectException(\LogicException::class);
  117. $module->getModulesByConditions($organization);
  118. }
  119. /**
  120. * @see Module::getModulesByProductConfiguration()
  121. */
  122. public function testGetModulesByProductConfiguration(): void
  123. {
  124. $module = $this->getMockForMethod('getModulesByProductConfiguration');
  125. $this->configUtils->method('loadFromFile')->with('products')->willReturn(
  126. ['opentalent' => ['products' => ['artist' => ['modules' => ['foo']]]]]
  127. );
  128. $this->assertEquals(['foo'], $module->getModulesByProductConfiguration('artist')) ;
  129. }
  130. /**
  131. * @see Module::getModulesByProductConfiguration()
  132. */
  133. public function testGetModulesByProductConfigurationExtend(): void
  134. {
  135. $module = $this->getMockForMethod('getModulesByProductConfiguration');
  136. $this->configUtils->method('loadFromFile')->with('products')->willReturn(
  137. ['opentalent' =>
  138. ['products' =>
  139. [
  140. 'artist' => ['modules' => ['foo']],
  141. 'artist_premium' => ['extend' => 'artist', 'modules' => ['bar']]
  142. ]
  143. ]
  144. ]
  145. );
  146. $this->assertEqualsCanonicalizing(
  147. ['foo', 'bar'],
  148. $module->getModulesByProductConfiguration('artist-premium')
  149. ) ;
  150. }
  151. /**
  152. * @see Module::getModulesByProductConfiguration()
  153. */
  154. public function testGetModulesByProductConfigurationAccessDenied(): void
  155. {
  156. $module = $this->getMockBuilder(TestableModule::class);
  157. $module = $this->getMockForMethod('getModulesByProductConfiguration');
  158. $this->configUtils->method('loadFromFile')->with('products')->willReturn(
  159. ['opentalent' => ['products' => ['artist' => ['modules' => ['foo']]]]]
  160. );
  161. $this->expectException(AccessDeniedHttpException::class);
  162. $this->expectExceptionMessage('The product artist_premium does not exist !');
  163. $module->getModulesByProductConfiguration('artist-premium');
  164. }
  165. /**
  166. * @see Module::getModuleByResourceName()
  167. */
  168. public function testGetModuleByResourceName(): void {
  169. $module = $this->getMockForMethod('getModuleByResourceName');
  170. $this->configUtils->method('loadFromFile')->with('products')->willReturn(
  171. ['opentalent' =>
  172. ['modules' =>
  173. ['Core' => ['entities' => ['foo', 'bar']]]
  174. ]
  175. ]
  176. );
  177. $this->assertEquals('Core', $module->getModuleByResourceName('foo'));
  178. }
  179. /**
  180. * @see Module::getModuleByResourceName()
  181. */
  182. public function testGetModuleByResourceNameNotFound(): void {
  183. $module = $this->getMockForMethod('getModuleByResourceName');
  184. $this->configUtils->method('loadFromFile')->with('products')->willReturn(
  185. ['opentalent' =>
  186. ['modules' =>
  187. ['Core' => ['entities' => ['bar']]]
  188. ]
  189. ]
  190. );
  191. $this->assertNull($module->getModuleByResourceName('foo'));
  192. }
  193. }