OnParametersChangeTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <?php
  2. namespace App\Test\Service\OnChange\Organization;
  3. use App\Entity\Access\Access;
  4. use App\Entity\Booking\Course;
  5. use App\Entity\Education\EducationCurriculum;
  6. use App\Entity\Education\EducationNotationConfig;
  7. use App\Entity\Organization\Organization;
  8. use App\Entity\Organization\Parameters;
  9. use App\Enum\Education\AdvancedEducationNotationTypeEnum;
  10. use App\Message\Command\Parameters\AverageChange;
  11. use App\Message\Command\Typo3\Typo3DeleteCommand;
  12. use App\Message\Command\Typo3\Typo3UndeleteCommand;
  13. use App\Message\Command\Typo3\Typo3UpdateCommand;
  14. use App\Repository\Booking\CourseRepository;
  15. use App\Service\OnChange\OnChangeContext;
  16. use App\Service\OnChange\Organization\OnParametersChange;
  17. use AssertionError;
  18. use PHPUnit\Framework\TestCase;
  19. use Symfony\Component\Messenger\Envelope;
  20. use Symfony\Component\Messenger\MessageBusInterface;
  21. class OnParametersChangeTest extends TestCase
  22. {
  23. private Parameters $parameters;
  24. private OnParametersChange $onParametersChange;
  25. private CourseRepository $courseRepositoryMock;
  26. private \App\Service\Network\Utils $networkUtils;
  27. private MessageBusInterface $messageBus;
  28. public function setUp():void
  29. {
  30. $this->courseRepositoryMock = $this->getMockBuilder(CourseRepository::class)->disableOriginalConstructor()->getMock();
  31. $this->networkUtils = $this->getMockBuilder(\App\Service\Network\Utils::class)->disableOriginalConstructor()->getMock();
  32. $this->messageBus = $this->getMockBuilder(MessageBusInterface::class)->disableOriginalConstructor()->getMock();
  33. $this->parameters = new Parameters();
  34. $this->onParametersChange = new OnParametersChange(
  35. $this->courseRepositoryMock,
  36. $this->networkUtils,
  37. $this->messageBus
  38. );
  39. }
  40. public function testValidate(): void
  41. {
  42. $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
  43. // 1. Is CMF and site web enabled ; 2. Is not CMF and site web disabled ; 3. Is not CMF and site web enabled
  44. foreach ([[false, true], [true, false], [false, false]] as $params) {
  45. $parameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
  46. $parameters->expects(self::once())->method('getDesactivateOpentalentSiteWeb')->willReturn($params[0]);
  47. $this->networkUtils = $this->getMockBuilder(\App\Service\Network\Utils::class)->disableOriginalConstructor()->getMock();
  48. $this->networkUtils->method('isCMFAndActiveNow')->willReturn($params[1]);
  49. $this->onParametersChange->validate($parameters, $context);
  50. }
  51. }
  52. public function testValidateInvalid(): void
  53. {
  54. $parameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
  55. $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
  56. // Is CMF and site web disabled
  57. $parameters->expects(self::once())->method('getDesactivateOpentalentSiteWeb')->willReturn(true);
  58. $this->networkUtils->expects(self::once())->method('isCMFAndActiveNow')->willReturn(true);
  59. try {
  60. $this->onParametersChange->validate($parameters, $context);
  61. throw new AssertionError('OnParametersChange::validate should have thrown an error');
  62. } catch (\RuntimeException) {}
  63. }
  64. public function testBeforeChange(): void
  65. {
  66. $onParametersChange = $this
  67. ->getMockBuilder(OnParametersChange::class)
  68. ->onlyMethods(['onAdvancedEducationNotationTypeChange', 'onMusicalDateChange'])
  69. ->disableOriginalConstructor()
  70. ->getMock();
  71. $onParametersChange
  72. ->expects(self::once())
  73. ->method('onAdvancedEducationNotationTypeChange')
  74. ->willReturnSelf();
  75. $onParametersChange
  76. ->expects(self::once())
  77. ->method('onMusicalDateChange')
  78. ->willReturnSelf();
  79. $previousParameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
  80. $previousParameters->method('getAdvancedEducationNotationType')->willReturn('BY_EDUCATION');
  81. $musicalDate = new \DateTime('2022-01-01');
  82. $previousParameters->method('getMusicalDate')->willReturn($musicalDate);
  83. $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
  84. $context->method('previousData')->willReturn($previousParameters);
  85. $parameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
  86. $parameters->method('getAdvancedEducationNotationType')->willReturn('SOMETHING_ELSE');
  87. $parameters->method('getMusicalDate')->willReturn(new \DateTime('2023-01-01'));
  88. // Both mocked methods should be called once here
  89. $onParametersChange->beforeChange($parameters, $context);
  90. $parameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
  91. $parameters->method('getId')->willReturn(1);
  92. $parameters->method('getAdvancedEducationNotationType')->willReturn('BY_EDUCATION');
  93. $parameters->method('getMusicalDate')->willReturn($musicalDate);
  94. // None of the mocked methods should be called again here
  95. $onParametersChange->beforeChange($parameters, $context);
  96. }
  97. public function testOnChangeNoChange(): void
  98. {
  99. $this->messageBus->expects($this->never())->method('dispatch');
  100. $previousParameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
  101. $previousParameters->method('getId')->willReturn(1);
  102. $previousParameters->expects(self::once())->method('getAverage')->willReturn(20);
  103. $previousParameters->expects(self::once())->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  104. $previousParameters->expects(self::once())->method('getCustomDomain')->willReturn(null);
  105. $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
  106. $context->method('previousData')->willReturn($previousParameters);
  107. $parameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
  108. $parameters->method('getId')->willReturn(1);
  109. $parameters->method('getAverage')->willReturn(20);
  110. $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  111. $parameters->method('getCustomDomain')->willReturn(null);
  112. $this->onParametersChange->onChange($parameters, $context);
  113. }
  114. public function testOnChangeAverageChanged(): void
  115. {
  116. $this->messageBus
  117. ->expects(self::once())
  118. ->method('dispatch')
  119. ->with(self::isInstanceOf(AverageChange::class))
  120. ->willReturn(new Envelope(new AverageChange(1)));
  121. $previousParameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
  122. $previousParameters->method('getId')->willReturn(1);
  123. $previousParameters->expects(self::once())->method('getAverage')->willReturn(20);
  124. $previousParameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  125. $previousParameters->method('getCustomDomain')->willReturn(null);
  126. $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
  127. $context->method('previousData')->willReturn($previousParameters);
  128. $parameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
  129. $parameters->method('getId')->willReturn(1);
  130. $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  131. $parameters->method('getCustomDomain')->willReturn(null);
  132. $parameters->expects(self::once())->method('getAverage')->willReturn(30);
  133. $this->onParametersChange->onChange($parameters, $context);
  134. }
  135. public function testOnChangeCustomDomainChanged(): void
  136. {
  137. $this->messageBus
  138. ->expects(self::once())
  139. ->method('dispatch')
  140. ->with(self::isInstanceOf(Typo3UpdateCommand::class))
  141. ->willReturn(new Envelope(new Typo3UpdateCommand(1)));
  142. $previousParameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
  143. $previousParameters->method('getId')->willReturn(1);
  144. $previousParameters->method('getAverage')->willReturn(20);
  145. $previousParameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  146. $previousParameters->expects(self::once())->method('getCustomDomain')->willReturn(null);
  147. $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
  148. $context->method('previousData')->willReturn($previousParameters);
  149. $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
  150. $organization->method('getId')->willReturn(1);
  151. $parameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
  152. $parameters->method('getId')->willReturn(1);
  153. $parameters->method('getOrganization')->willReturn($organization);
  154. $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  155. $parameters->expects(self::once())->method('getCustomDomain')->willReturn('custom');
  156. $parameters->method('getAverage')->willReturn(20);
  157. $this->onParametersChange->onChange($parameters, $context);
  158. }
  159. public function testOnChangeWebsiteDisabled(): void
  160. {
  161. $this->messageBus
  162. ->expects(self::once())
  163. ->method('dispatch')
  164. ->with(self::isInstanceOf(Typo3DeleteCommand::class))
  165. ->willReturn(new Envelope(new Typo3DeleteCommand(1)));
  166. $previousParameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
  167. $previousParameters->method('getId')->willReturn(1);
  168. $previousParameters->method('getAverage')->willReturn(20);
  169. $previousParameters->expects(self::once())->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  170. $previousParameters->method('getCustomDomain')->willReturn(null);
  171. $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
  172. $context->method('previousData')->willReturn($previousParameters);
  173. $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
  174. $organization->method('getId')->willReturn(1);
  175. $parameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
  176. $parameters->method('getId')->willReturn(1);
  177. $parameters->method('getOrganization')->willReturn($organization);
  178. $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(true);
  179. $parameters->method('getCustomDomain')->willReturn(null);
  180. $parameters->method('getAverage')->willReturn(20);
  181. $this->onParametersChange->onChange($parameters, $context);
  182. }
  183. public function testOnChangeWebsiteEnabled(): void
  184. {
  185. $this->messageBus
  186. ->expects(self::exactly(2))
  187. ->method('dispatch')
  188. ->willReturnCallback(function ($message, $stamps = []) {
  189. if ($message instanceof Typo3UndeleteCommand) {
  190. return new Envelope(new Typo3UndeleteCommand(1));
  191. }
  192. if($message instanceof Typo3UpdateCommand) {
  193. return new Envelope(new Typo3UpdateCommand(1));
  194. }
  195. throw new AssertionError('unexpected message : ' . $message::class);
  196. });
  197. $previousParameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
  198. $previousParameters->method('getId')->willReturn(1);
  199. $previousParameters->method('getAverage')->willReturn(20);
  200. $previousParameters->expects(self::once())->method('getDesactivateOpentalentSiteWeb')->willReturn(true);
  201. $previousParameters->method('getCustomDomain')->willReturn(null);
  202. $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
  203. $context->method('previousData')->willReturn($previousParameters);
  204. $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
  205. $organization->method('getId')->willReturn(1);
  206. $parameters = $this->getMockBuilder(Parameters::class)->disableOriginalConstructor()->getMock();
  207. $parameters->method('getId')->willReturn(1);
  208. $parameters->method('getOrganization')->willReturn($organization);
  209. $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  210. $parameters->method('getCustomDomain')->willReturn(null);
  211. $parameters->method('getAverage')->willReturn(20);
  212. $this->onParametersChange->onChange($parameters, $context);
  213. }
  214. /**
  215. * @see OnParametersChange::onAdvancedEducationNotationTypeChange()
  216. */
  217. public function testOnAdvancedEducationNotationTypeByTeachersChange(): void
  218. {
  219. $educationNotationConfig = new EducationNotationConfig();
  220. $educationCurriculum = new EducationCurriculum();
  221. $educationNotationConfig->addEducationCurriculum($educationCurriculum);
  222. $organization = new Organization();
  223. $organization->addEducationNotationConfig($educationNotationConfig);
  224. $this->parameters->setAdvancedEducationNotationType(AdvancedEducationNotationTypeEnum::BY_TEACHER()->getValue());
  225. $this->parameters->setOrganization($organization);
  226. $this->assertCount(1, $educationNotationConfig->getEducationCurriculums());
  227. $this->onParametersChange->onAdvancedEducationNotationTypeChange($this->parameters);
  228. $this->assertNull($educationNotationConfig->getEducationCurriculums()->first()->getEducationNotationConfig());
  229. }
  230. /**
  231. * @see OnParametersChange::onAdvancedEducationNotationTypeChange()
  232. */
  233. public function testOnAdvancedEducationNotationTypeByEducationChange(): void
  234. {
  235. $educationNotationConfig = new EducationNotationConfig();
  236. $teacher = new Access();
  237. $educationNotationConfig->addTeacher($teacher);
  238. $organization = new Organization();
  239. $organization->addEducationNotationConfig($educationNotationConfig);
  240. $this->parameters->setAdvancedEducationNotationType(AdvancedEducationNotationTypeEnum::BY_EDUCATION()->getValue());
  241. $this->parameters->setOrganization($organization);
  242. $this->assertCount(1, $educationNotationConfig->getTeachers());
  243. $this->onParametersChange->onAdvancedEducationNotationTypeChange($this->parameters);
  244. $this->assertNull($educationNotationConfig->getTeachers()->first()->getEducationNotationConfig());
  245. }
  246. /**
  247. * Un cours qui débute le 02/09/2022, si l'année musical passe du 05/09 au 01/09 alors le cours passe de l'année 2021/2022 à 2022/2023
  248. * @throws \Exception
  249. * @see OnParametersChange::onMusicalDateChange()
  250. */
  251. public function testOnMusicalDateChangeToPast(): void
  252. {
  253. $this->parameters->setMusicalDate(new \DateTime('2022-09-01'));
  254. $organization = new Organization();
  255. $this->parameters->setOrganization($organization);
  256. $organization->setParameters($this->parameters);
  257. $course = new Course();
  258. $course->setStartYear(2021);
  259. $course->setEndYear(2022);
  260. $course->setDatetimeStart(new \DateTime('2022-09-02'));
  261. $this->courseRepositoryMock
  262. ->method('getCoursesToFrom')
  263. ->willReturn([$course])
  264. ;
  265. $this->onParametersChange->onMusicalDateChange($this->parameters, new \DateTime('2022-09-05'));
  266. $this->assertEquals(2022, $course->getStartYear());
  267. $this->assertEquals(2023, $course->getEndYear());
  268. }
  269. /**
  270. * Un cours qui débute le 02/09/2022, si l'année musical passe du 01/09 au 05/09 alors le cours passe de l'année 2022/2023 à 2021/2022
  271. *
  272. * @throws \Exception
  273. * @see OnParametersChange::onMusicalDateChange()
  274. */
  275. public function testOnMusicalDateChangeToFuture(): void
  276. {
  277. $this->parameters->setMusicalDate(new \DateTime('2022-09-05'));
  278. $organization = new Organization();
  279. $this->parameters->setOrganization($organization);
  280. $organization->setParameters($this->parameters);
  281. $course = new Course();
  282. $course->setStartYear(2022);
  283. $course->setEndYear(2023);
  284. $course->setDatetimeStart(new \DateTime('2022-09-02'));
  285. $this->courseRepositoryMock
  286. ->method('getCoursesToFrom')
  287. ->willReturn([$course])
  288. ;
  289. $this->onParametersChange->onMusicalDateChange($this->parameters, new \DateTime('2022-09-01'));
  290. $this->assertEquals(2021, $course->getStartYear());
  291. $this->assertEquals(2022, $course->getEndYear());
  292. }
  293. }