OnParametersChangeTest.php 17 KB

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