ModuleTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. namespace App\Tests\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\Parser;
  8. use App\Service\Utils\Reflection;
  9. use PHPUnit\Framework\MockObject\MockObject;
  10. use PHPUnit\Framework\TestCase;
  11. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  12. class TestableModule extends Module {
  13. public function loadModuleConfig(): array { return parent::loadModuleConfig(); }
  14. public function getModuleConfig(): array { return parent::getModuleConfig(); }
  15. public function loadModuleByConditionsConfig(): array { return parent::loadModuleByConditionsConfig(); }
  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 | Parser $parser;
  23. public function setUp():void
  24. {
  25. $this->reflection = $this->getMockBuilder(Reflection::class)->disableOriginalConstructor()->getMock();
  26. $this->parser = $this->getMockBuilder(Parser::class)->disableOriginalConstructor()->getMock();
  27. }
  28. /**
  29. * @see Module::getOrganizationModules()
  30. */
  31. public function testGetOrganizationModules(): void
  32. {
  33. $module = $this->getMockBuilder(Module::class)
  34. ->setConstructorArgs([$this->reflection, $this->parser, self::OPENTALENT_CONFIG])
  35. ->setMethodsExcept(['getOrganizationModules'])
  36. ->getMock();
  37. $module->expects(self::once())->method('getModuleBySettings')->willReturn(['Sms']);
  38. $module->expects(self::once())->method('getModulesByConditions')->willReturn(['CotisationCall']);
  39. $settings = $this->getMockBuilder(Settings::class)->disableOriginalConstructor()->getMock();
  40. $settings->expects(self::once())->method('getProduct')->willReturn('school');
  41. $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
  42. $organization->expects(self::once())->method('getSettings')->willReturn($settings);
  43. $module->expects(self::once())->method('getModulesByProductConfiguration')->willReturn(['Notification']);
  44. $this->assertEqualsCanonicalizing(
  45. ['Sms', 'CotisationCall', 'Notification'],
  46. $module->getOrganizationModules($organization)
  47. );
  48. }
  49. /**
  50. * @see Module::getModuleBySettings()
  51. */
  52. public function testGetModuleBySettings(): void
  53. {
  54. $module = $this->getMockBuilder(TestableModule::class)
  55. ->setConstructorArgs([$this->reflection, $this->parser, self::OPENTALENT_CONFIG])
  56. ->setMethodsExcept(['getModuleBySettings'])
  57. ->getMock();
  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->getMockBuilder(TestableModule::class)
  76. ->setConstructorArgs([$this->reflection, $this->parser, self::OPENTALENT_CONFIG])
  77. ->setMethodsExcept(['getModulesByConditions'])
  78. ->getMock();
  79. $module->method('getModuleByConditionsConfig')->willReturn(
  80. ['opentalent' =>
  81. ['modulesbyconditions' =>
  82. ['CotisationCall' => [
  83. 'roles' => ['ROLE_COTISATION'],
  84. 'conditions' => [
  85. 'service' => [
  86. 'name' => CotisationUtils::class,
  87. 'function' => 'isLastParentAndCMF'
  88. ]
  89. ]
  90. ]]
  91. ]
  92. ]
  93. );
  94. $organization = $this->getMockBuilder(Organization::class)->getMock();
  95. $this->reflection
  96. ->method('dynamicInvokeServiceWithArgsAndMethod')
  97. ->with(CotisationUtils::class, 'isLastParentAndCMF', array($organization))
  98. ->willReturn(true);
  99. $this->assertContains('CotisationCall', $module->getModulesByConditions($organization)) ;
  100. }
  101. /**
  102. * @see Module::getModulesByConditions()
  103. */
  104. public function testGetModulesByConditionsLogicError(): void
  105. {
  106. $module = $this->getMockBuilder(TestableModule::class)
  107. ->setConstructorArgs([$this->reflection, $this->parser, self::OPENTALENT_CONFIG])
  108. ->setMethodsExcept(['getModulesByConditions'])
  109. ->getMock();
  110. $module->method('getModuleByConditionsConfig')->willReturn(
  111. ['opentalent' =>
  112. ['modulesbyconditions' =>
  113. ['CotisationCall' => []]
  114. ]
  115. ]
  116. );
  117. $organization = $this->getMockBuilder(Organization::class)->getMock();
  118. $this->reflection
  119. ->method('dynamicInvokeServiceWithArgsAndMethod')
  120. ->with(CotisationUtils::class, 'isLastParentAndCMF', array($organization))
  121. ->willThrowException(new \Exception());
  122. $this->expectException(\LogicException::class);
  123. $module->getModulesByConditions($organization);
  124. }
  125. /**
  126. * @see Module::getModulesByProductConfiguration()
  127. */
  128. public function testGetModulesByProductConfiguration(): void
  129. {
  130. $module = $this->getMockBuilder(TestableModule::class)
  131. ->setConstructorArgs([$this->reflection, $this->parser, self::OPENTALENT_CONFIG])
  132. ->setMethodsExcept(['getModulesByProductConfiguration'])
  133. ->getMock();
  134. $module->method('getModuleConfig')->willReturn(['opentalent' => ['products' => ['artist' => ['modules' => ['foo']]]]]);
  135. $this->assertEquals(['foo'], $module->getModulesByProductConfiguration('artist')) ;
  136. }
  137. /**
  138. * @see Module::getModulesByProductConfiguration()
  139. */
  140. public function testGetModulesByProductConfigurationExtend(): void
  141. {
  142. $module = $this->getMockBuilder(TestableModule::class)
  143. ->setConstructorArgs([$this->reflection, $this->parser, self::OPENTALENT_CONFIG])
  144. ->setMethodsExcept(['getModulesByProductConfiguration'])
  145. ->getMock();
  146. $module->method('getModuleConfig')->willReturn(
  147. ['opentalent' =>
  148. ['products' =>
  149. [
  150. 'artist' => ['modules' => ['foo']],
  151. 'artist_premium' => ['extend' => 'artist', 'modules' => ['bar']]
  152. ]
  153. ]
  154. ]
  155. );
  156. $this->assertEqualsCanonicalizing(
  157. ['foo', 'bar'],
  158. $module->getModulesByProductConfiguration('artist-premium')
  159. ) ;
  160. }
  161. /**
  162. * @see Module::getModulesByProductConfiguration()
  163. */
  164. public function testGetModulesByProductConfigurationAccessDenied(): void: void
  165. {
  166. $module = $this->getMockBuilder(TestableModule::class)
  167. ->setConstructorArgs([$this->reflection, $this->parser, self::OPENTALENT_CONFIG])
  168. ->setMethodsExcept(['getModulesByProductConfiguration'])
  169. ->getMock();
  170. $module->method('getModuleConfig')->willReturn(['opentalent' => ['products' => ['artist' => ['modules' => ['foo']]]]]);
  171. $this->expectException(AccessDeniedHttpException::class);
  172. $this->expectExceptionMessage('The product artist_premium does not exist !');
  173. $module->getModulesByProductConfiguration('artist-premium');
  174. }
  175. /**
  176. * @see Module::getModuleConfig()
  177. */
  178. public function testGetModuleConfig(): void {
  179. $module = $this->getMockBuilder(TestableModule::class)
  180. ->setConstructorArgs([$this->reflection, $this->parser, self::OPENTALENT_CONFIG])
  181. ->setMethodsExcept(['getModuleConfig'])
  182. ->getMock();
  183. $module->expects(self::once())->method('loadModuleConfig')->willReturn(['foo']);
  184. $this->assertEquals(['foo'], $module->getModuleConfig());
  185. $this->assertEquals(['foo'], $module->getModuleConfig());
  186. }
  187. /**
  188. * @see Module::loadModuleConfig()
  189. */
  190. public function testLoadModuleConfig(): void {
  191. $module = $this->getMockBuilder(TestableModule::class)
  192. ->setConstructorArgs([$this->reflection, $this->parser, self::OPENTALENT_CONFIG])
  193. ->setMethodsExcept(['loadModuleConfig'])
  194. ->getMock();
  195. $this->parser
  196. ->expects(self::once())
  197. ->method('yamlParser')
  198. ->with(self::OPENTALENT_CONFIG, 'products.yaml')
  199. ->willReturn(['foo']);
  200. $this->assertEquals(
  201. ['foo'],
  202. $module->loadModuleConfig()
  203. );
  204. }
  205. /**
  206. * @see Module::getModuleByConditionsConfig()
  207. */
  208. public function testGetModuleByConditionsConfig(): void {
  209. $module = $this->getMockBuilder(TestableModule::class)
  210. ->setConstructorArgs([$this->reflection, $this->parser, self::OPENTALENT_CONFIG])
  211. ->setMethodsExcept(['getModuleByConditionsConfig'])
  212. ->getMock();
  213. $module->expects(self::once())->method('loadModuleByConditionsConfig')->willReturn(['foo']);
  214. $this->assertEquals(['foo'], $module->getModuleByConditionsConfig());
  215. $this->assertEquals(['foo'], $module->getModuleByConditionsConfig());
  216. }
  217. /**
  218. * @see Module::loadModuleByConditionsConfig()
  219. */
  220. public function testLoadModuleByConditionsConfig(): void {
  221. $module = $this->getMockBuilder(TestableModule::class)
  222. ->setConstructorArgs([$this->reflection, $this->parser, self::OPENTALENT_CONFIG])
  223. ->setMethodsExcept(['loadModuleByConditionsConfig'])
  224. ->getMock();
  225. $this->parser->expects(self::once())
  226. ->method('yamlParser')
  227. ->with(self::OPENTALENT_CONFIG, 'modulesbyconditions.yaml')
  228. ->willReturn(['foo']);
  229. $this->assertEquals(
  230. ['foo'],
  231. $module->loadModuleByConditionsConfig()
  232. );
  233. }
  234. /**
  235. * @see Module::getModuleByResourceName()
  236. */
  237. public function testGetModuleByResourceName(): void {
  238. $module = $this->getMockBuilder(TestableModule::class)
  239. ->setConstructorArgs([$this->reflection, $this->parser, self::OPENTALENT_CONFIG])
  240. ->setMethodsExcept(['getModuleByResourceName'])
  241. ->getMock();
  242. $module->method('getModuleConfig')->willReturn(
  243. ['opentalent' =>
  244. ['modules' =>
  245. ['Core' => ['entities' => ['foo', 'bar']]]
  246. ]
  247. ]
  248. );
  249. $this->assertEquals('Core', $module->getModuleByResourceName('foo'));
  250. }
  251. /**
  252. * @see Module::getModuleByResourceName()
  253. */
  254. public function testGetModuleByResourceNameNotFound(): void {
  255. $module = $this->getMockBuilder(TestableModule::class)
  256. ->setConstructorArgs([$this->reflection, $this->parser, self::OPENTALENT_CONFIG])
  257. ->setMethodsExcept(['getModuleByResourceName'])
  258. ->getMock();
  259. $module->method('getModuleConfig')->willReturn(
  260. ['opentalent' =>
  261. ['modules' =>
  262. ['Core' => ['entities' => ['bar']]]
  263. ]
  264. ]
  265. );
  266. $this->assertNull($module->getModuleByResourceName('foo'));
  267. }
  268. }