OnParametersChangeTest.php 17 KB

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