OnParametersChangeTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. <?php
  2. /** @noinspection PhpUnhandledExceptionInspection */
  3. namespace App\Tests\Unit\Service\OnChange\Organization;
  4. use App\Entity\Access\Access;
  5. use App\Entity\Booking\Course;
  6. use App\Entity\Education\EducationCurriculum;
  7. use App\Entity\Education\EducationNotationConfig;
  8. use App\Entity\Organization\Organization;
  9. use App\Entity\Organization\Parameters;
  10. use App\Enum\Education\AdvancedEducationNotationTypeEnum;
  11. use App\Message\Message\Typo3\Typo3Delete;
  12. use App\Message\Message\Typo3\Typo3Undelete;
  13. use App\Message\Message\Typo3\Typo3Update;
  14. use App\Repository\Booking\CourseRepository;
  15. use App\Service\Network\Utils as NetworkUtils;
  16. use App\Service\OnChange\OnChangeContext;
  17. use App\Service\OnChange\Organization\OnParametersChange;
  18. use App\Service\Organization\Utils as OrganizationUtils;
  19. use Doctrine\Common\Collections\ArrayCollection;
  20. use PHPUnit\Framework\TestCase;
  21. use Symfony\Component\Messenger\Envelope;
  22. use Symfony\Component\Messenger\MessageBusInterface;
  23. class OnParametersChangeTest extends TestCase
  24. {
  25. private CourseRepository $courseRepository;
  26. private NetworkUtils $networkUtils;
  27. private MessageBusInterface $messageBus;
  28. private OrganizationUtils $organizationUtils;
  29. public function setUp(): void
  30. {
  31. $this->courseRepository = $this->getMockBuilder(CourseRepository::class)->disableOriginalConstructor()->getMock();
  32. $this->networkUtils = $this->getMockBuilder(NetworkUtils::class)->disableOriginalConstructor()->getMock();
  33. $this->organizationUtils = $this->getMockBuilder(OrganizationUtils::class)->disableOriginalConstructor()->getMock();
  34. $this->messageBus = $this->getMockBuilder(MessageBusInterface::class)->disableOriginalConstructor()->getMock();
  35. }
  36. /**
  37. * @see OnParametersChange::validate()
  38. */
  39. public function testValidateValid(): void
  40. {
  41. $onParametersChange = $this->getMockBuilder(OnParametersChange::class)
  42. ->setConstructorArgs([$this->courseRepository, $this->networkUtils, $this->organizationUtils, $this->messageBus])
  43. ->setMethodsExcept(['validate'])
  44. ->getMock();
  45. $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
  46. $consecutiveParams = [
  47. [false, true], // Is CMF and site web enabled
  48. [true, false], // Is not CMF and site web disabled
  49. [false, false], // Is not CMF and site web enabled
  50. ];
  51. foreach ($consecutiveParams as $params) {
  52. $organization = $this->getMockBuilder(Organization::class)->getMock();
  53. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  54. $parameters->method('getOrganization')->willReturn($organization);
  55. $parameters->expects(self::once())->method('getDesactivateOpentalentSiteWeb')->willReturn($params[0]);
  56. $this->networkUtils = $this->getMockBuilder(NetworkUtils::class)->getMock();
  57. $this->networkUtils->method('isCMFAndActiveNow')->with($organization)->willReturn($params[1]);
  58. $onParametersChange->validate($parameters, $context);
  59. }
  60. }
  61. /**
  62. * @see OnParametersChange::validate()
  63. */
  64. public function testValidateInvalid(): void
  65. {
  66. $onParametersChange = $this->getMockBuilder(OnParametersChange::class)
  67. ->setConstructorArgs([$this->courseRepository, $this->networkUtils, $this->organizationUtils, $this->messageBus])
  68. ->setMethodsExcept(['validate'])
  69. ->getMock();
  70. $organization = $this->getMockBuilder(Organization::class)->getMock();
  71. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  72. $parameters->method('getOrganization')->willReturn($organization);
  73. $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
  74. // Is CMF and site web disabled
  75. $parameters->expects(self::once())->method('getDesactivateOpentalentSiteWeb')->willReturn(true);
  76. $this->networkUtils->expects(self::once())->method('isCMFAndActiveNow')->with($organization)->willReturn(true);
  77. $this->expectException(\RuntimeException::class);
  78. $onParametersChange->validate($parameters, $context);
  79. }
  80. /**
  81. * @see OnParametersChange::beforeChange()
  82. */
  83. public function testBeforeChange(): void
  84. {
  85. $onParametersChange = $this
  86. ->getMockBuilder(OnParametersChange::class)
  87. ->setConstructorArgs([$this->courseRepository, $this->networkUtils, $this->organizationUtils, $this->messageBus])
  88. ->setMethodsExcept(['beforeChange'])
  89. ->getMock();
  90. $onParametersChange->expects(self::once())->method('onAdvancedEducationNotationTypeChange');
  91. $onParametersChange->expects(self::once())->method('onMusicalDateChange');
  92. $musicalDate = new \DateTime('2022-01-01');
  93. $previousParameters = $this->getMockBuilder(Parameters::class)->getMock();
  94. $previousParameters->method('getAdvancedEducationNotationType')->willReturn(AdvancedEducationNotationTypeEnum::BY_EDUCATION);
  95. $previousParameters->method('getMusicalDate')->willReturn($musicalDate);
  96. $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
  97. $context->method('previousData')->willReturn($previousParameters);
  98. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  99. $parameters->method('getAdvancedEducationNotationType')->willReturn(AdvancedEducationNotationTypeEnum::BY_TEACHER);
  100. $parameters->method('getMusicalDate')->willReturn(new \DateTime('2023-01-01'));
  101. // Both mocked methods should be called once here
  102. $onParametersChange->beforeChange($parameters, $context);
  103. }
  104. /**
  105. * @see OnParametersChange::beforeChange()
  106. */
  107. public function testBeforeChangeNoChange(): void
  108. {
  109. $onParametersChange = $this
  110. ->getMockBuilder(OnParametersChange::class)
  111. ->setConstructorArgs([$this->courseRepository, $this->networkUtils, $this->organizationUtils, $this->messageBus])
  112. ->setMethodsExcept(['beforeChange'])
  113. ->getMock();
  114. $onParametersChange->expects(self::never())->method('onAdvancedEducationNotationTypeChange');
  115. $onParametersChange->expects(self::never())->method('onMusicalDateChange');
  116. $musicalDate = new \DateTime('2022-01-01');
  117. $previousParameters = $this->getMockBuilder(Parameters::class)->getMock();
  118. $previousParameters->method('getAdvancedEducationNotationType')->willReturn(AdvancedEducationNotationTypeEnum::BY_EDUCATION);
  119. $previousParameters->method('getMusicalDate')->willReturn($musicalDate);
  120. $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
  121. $context->method('previousData')->willReturn($previousParameters);
  122. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  123. $parameters->method('getId')->willReturn(1);
  124. $parameters->method('getAdvancedEducationNotationType')->willReturn(AdvancedEducationNotationTypeEnum::BY_EDUCATION);
  125. $parameters->method('getMusicalDate')->willReturn($musicalDate);
  126. // None of the mocked methods should be called again here
  127. $onParametersChange->beforeChange($parameters, $context);
  128. }
  129. /**
  130. * @see OnParametersChange::onChange()
  131. */
  132. public function testOnChangeNoChange(): void
  133. {
  134. $onParametersChange = $this
  135. ->getMockBuilder(OnParametersChange::class)
  136. ->setConstructorArgs([$this->courseRepository, $this->networkUtils, $this->organizationUtils, $this->messageBus])
  137. ->setMethodsExcept(['onChange'])
  138. ->getMock();
  139. $this->messageBus->expects($this->never())->method('dispatch');
  140. $previousParameters = $this->getMockBuilder(Parameters::class)->getMock();
  141. $previousParameters->method('getId')->willReturn(1);
  142. $previousParameters->method('getAverage')->willReturn(20);
  143. $previousParameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  144. $previousParameters->method('getCustomDomain')->willReturn(null);
  145. $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
  146. $context->method('previousData')->willReturn($previousParameters);
  147. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  148. $parameters->method('getId')->willReturn(1);
  149. $parameters->method('getAverage')->willReturn(20);
  150. $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  151. $parameters->method('getCustomDomain')->willReturn(null);
  152. $onParametersChange->onChange($parameters, $context);
  153. }
  154. /**
  155. * @see OnParametersChange::onChange()
  156. */
  157. public function testOnChangeCustomDomainChanged(): void
  158. {
  159. $onParametersChange = $this
  160. ->getMockBuilder(OnParametersChange::class)
  161. ->setConstructorArgs([$this->courseRepository, $this->networkUtils, $this->organizationUtils, $this->messageBus])
  162. ->setMethodsExcept(['onChange'])
  163. ->getMock();
  164. $this->messageBus
  165. ->expects(self::once())
  166. ->method('dispatch')
  167. ->with(self::isInstanceOf(Typo3Update::class))
  168. ->willReturn(new Envelope(new Typo3Update(1)));
  169. $previousParameters = $this->getMockBuilder(Parameters::class)->getMock();
  170. $previousParameters->method('getId')->willReturn(1);
  171. $previousParameters->method('getAverage')->willReturn(20);
  172. $previousParameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  173. $previousParameters->expects(self::once())->method('getCustomDomain')->willReturn(null);
  174. $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
  175. $context->method('previousData')->willReturn($previousParameters);
  176. $organization = $this->getMockBuilder(Organization::class)->getMock();
  177. $organization->method('getId')->willReturn(1);
  178. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  179. $parameters->method('getId')->willReturn(1);
  180. $parameters->method('getOrganization')->willReturn($organization);
  181. $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  182. $parameters->expects(self::once())->method('getCustomDomain')->willReturn('custom');
  183. $parameters->method('getAverage')->willReturn(20);
  184. $onParametersChange->onChange($parameters, $context);
  185. }
  186. /**
  187. * @see OnParametersChange::onChange()
  188. */
  189. public function testOnChangeWebsiteDisabled(): void
  190. {
  191. $onParametersChange = $this
  192. ->getMockBuilder(OnParametersChange::class)
  193. ->setConstructorArgs([$this->courseRepository, $this->networkUtils, $this->organizationUtils, $this->messageBus])
  194. ->setMethodsExcept(['onChange'])
  195. ->getMock();
  196. $this->messageBus
  197. ->expects(self::once())
  198. ->method('dispatch')
  199. ->with(self::isInstanceOf(Typo3Delete::class))
  200. ->willReturn(new Envelope(new Typo3Delete(1)));
  201. $previousParameters = $this->getMockBuilder(Parameters::class)->getMock();
  202. $previousParameters->method('getId')->willReturn(1);
  203. $previousParameters->method('getAverage')->willReturn(20);
  204. $previousParameters->expects(self::once())->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  205. $previousParameters->method('getCustomDomain')->willReturn(null);
  206. $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
  207. $context->method('previousData')->willReturn($previousParameters);
  208. $organization = $this->getMockBuilder(Organization::class)->getMock();
  209. $organization->method('getId')->willReturn(1);
  210. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  211. $parameters->method('getId')->willReturn(1);
  212. $parameters->method('getOrganization')->willReturn($organization);
  213. $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(true);
  214. $parameters->method('getCustomDomain')->willReturn(null);
  215. $parameters->method('getAverage')->willReturn(20);
  216. $onParametersChange->onChange($parameters, $context);
  217. }
  218. /**
  219. * @see OnParametersChange::onChange()
  220. */
  221. public function testOnChangeWebsiteEnabled(): void
  222. {
  223. $onParametersChange = $this
  224. ->getMockBuilder(OnParametersChange::class)
  225. ->setConstructorArgs([$this->courseRepository, $this->networkUtils, $this->organizationUtils, $this->messageBus])
  226. ->setMethodsExcept(['onChange'])
  227. ->getMock();
  228. $this->messageBus
  229. ->expects(self::exactly(2))
  230. ->method('dispatch')
  231. ->willReturnCallback(function ($message) {
  232. if ($message instanceof Typo3Undelete) {
  233. return new Envelope(new Typo3Undelete(1));
  234. }
  235. if ($message instanceof Typo3Update) {
  236. return new Envelope(new Typo3Update(1));
  237. }
  238. throw new \AssertionError('unexpected message : '.$message::class);
  239. });
  240. $previousParameters = $this->getMockBuilder(Parameters::class)->getMock();
  241. $previousParameters->method('getId')->willReturn(1);
  242. $previousParameters->method('getAverage')->willReturn(20);
  243. $previousParameters->expects(self::once())->method('getDesactivateOpentalentSiteWeb')->willReturn(true);
  244. $previousParameters->method('getCustomDomain')->willReturn(null);
  245. $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
  246. $context->method('previousData')->willReturn($previousParameters);
  247. $organization = $this->getMockBuilder(Organization::class)->getMock();
  248. $organization->method('getId')->willReturn(1);
  249. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  250. $parameters->method('getId')->willReturn(1);
  251. $parameters->method('getOrganization')->willReturn($organization);
  252. $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  253. $parameters->method('getCustomDomain')->willReturn(null);
  254. $parameters->method('getAverage')->willReturn(20);
  255. $onParametersChange->onChange($parameters, $context);
  256. }
  257. /**
  258. * @see OnParametersChange::onAdvancedEducationNotationTypeChange()
  259. */
  260. public function testOnAdvancedEducationNotationTypeByTeachersChange(): void
  261. {
  262. $onParametersChange = $this
  263. ->getMockBuilder(OnParametersChange::class)
  264. ->setConstructorArgs([$this->courseRepository, $this->networkUtils, $this->organizationUtils, $this->messageBus])
  265. ->setMethodsExcept(['onAdvancedEducationNotationTypeChange'])
  266. ->getMock();
  267. $educationCurriculum = $this->getMockBuilder(EducationCurriculum::class)->getMock();
  268. $educationNotationConfig = $this->getMockBuilder(EducationNotationConfig::class)->getMock();
  269. $educationNotationConfig->method('getEducationCurriculums')->willReturn(new ArrayCollection([$educationCurriculum]));
  270. $organization = $this->getMockBuilder(Organization::class)->getMock();
  271. $organization->method('getEducationNotationConfigs')->willReturn(new ArrayCollection([$educationNotationConfig]));
  272. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  273. $parameters->method('getOrganization')->willReturn($organization);
  274. $parameters->method('getAdvancedEducationNotationType')->willReturn(AdvancedEducationNotationTypeEnum::BY_TEACHER);
  275. $educationCurriculum->expects(self::once())->method('setEducationNotationConfig')->with(null);
  276. $onParametersChange->onAdvancedEducationNotationTypeChange($parameters);
  277. }
  278. /**
  279. * @see OnParametersChange::onAdvancedEducationNotationTypeChange()
  280. */
  281. public function testOnAdvancedEducationNotationTypeByEducationChange(): void
  282. {
  283. $onParametersChange = $this
  284. ->getMockBuilder(OnParametersChange::class)
  285. ->setConstructorArgs([$this->courseRepository, $this->networkUtils, $this->organizationUtils, $this->messageBus])
  286. ->setMethodsExcept(['onAdvancedEducationNotationTypeChange'])
  287. ->getMock();
  288. $teacher = $this->getMockBuilder(Access::class)->getMock();
  289. $educationNotationConfig = $this->getMockBuilder(EducationNotationConfig::class)->getMock();
  290. $educationNotationConfig->method('getTeachers')->willReturn(new ArrayCollection([$teacher]));
  291. $organization = $this->getMockBuilder(Organization::class)->getMock();
  292. $organization->method('getEducationNotationConfigs')->willReturn(new ArrayCollection([$educationNotationConfig]));
  293. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  294. $parameters->method('getOrganization')->willReturn($organization);
  295. $parameters->method('getAdvancedEducationNotationType')->willReturn(AdvancedEducationNotationTypeEnum::BY_EDUCATION);
  296. $teacher->expects(self::once())->method('setEducationNotationConfig')->with(null);
  297. $onParametersChange->onAdvancedEducationNotationTypeChange($parameters);
  298. }
  299. /**
  300. * 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.
  301. *
  302. * @throws \Exception
  303. *
  304. * @see OnParametersChange::onMusicalDateChange()
  305. */
  306. public function testOnMusicalDateChangeToPast(): void
  307. {
  308. $onParametersChange = $this
  309. ->getMockBuilder(OnParametersChange::class)
  310. ->setConstructorArgs([$this->courseRepository, $this->networkUtils, $this->organizationUtils, $this->messageBus])
  311. ->setMethodsExcept(['onMusicalDateChange'])
  312. ->getMock();
  313. $organization = $this->getMockBuilder(Organization::class)->getMock();
  314. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  315. $organization->method('getParameters')->willReturn($parameters);
  316. $parameters->method('getOrganization')->willReturn($organization);
  317. $parameters->method('getMusicalDate')->willReturn(new \DateTime('2022-09-01'));
  318. $startDate = new \DateTime('2022-09-02');
  319. $this->organizationUtils
  320. ->expects(self::once())
  321. ->method('getActivityYearSwitchDate')
  322. ->with($organization, $startDate)
  323. ->willReturn(2022);
  324. $course = $this->getMockBuilder(Course::class)->getMock();
  325. $course->method('getStartYear')->willReturn(2021);
  326. $course->method('getEndYear')->willReturn(2022);
  327. $course->method('getDatetimeStart')->willReturn($startDate);
  328. $this->courseRepository->method('getCoursesToFrom')->willReturn([$course]);
  329. $course->expects(self::once())->method('setStartYear')->with(2022);
  330. $course->expects(self::once())->method('setEndYear')->with(2023);
  331. $onParametersChange->onMusicalDateChange($parameters, new \DateTime('2022-09-05'));
  332. }
  333. /**
  334. * 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.
  335. *
  336. * @throws \Exception
  337. *
  338. * @see OnParametersChange::onMusicalDateChange()
  339. */
  340. public function testOnMusicalDateChangeToFuture(): void
  341. {
  342. $onParametersChange = $this
  343. ->getMockBuilder(OnParametersChange::class)
  344. ->setConstructorArgs([$this->courseRepository, $this->networkUtils, $this->organizationUtils, $this->messageBus])
  345. ->setMethodsExcept(['onMusicalDateChange'])
  346. ->getMock();
  347. $organization = $this->getMockBuilder(Organization::class)->getMock();
  348. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  349. $organization->method('getParameters')->willReturn($parameters);
  350. $parameters->method('getOrganization')->willReturn($organization);
  351. $parameters->method('getMusicalDate')->willReturn(new \DateTime('2022-09-05'));
  352. $startDate = new \DateTime('2022-09-02');
  353. $this->organizationUtils
  354. ->expects(self::once())
  355. ->method('getActivityYearSwitchDate')
  356. ->with($organization, $startDate)
  357. ->willReturn(2021);
  358. $course = $this->getMockBuilder(Course::class)->getMock();
  359. $course->method('getStartYear')->willReturn(2022);
  360. $course->method('getEndYear')->willReturn(2023);
  361. $course->method('getDatetimeStart')->willReturn($startDate);
  362. $this->courseRepository
  363. ->method('getCoursesToFrom')
  364. ->willReturn([$course])
  365. ;
  366. $course->expects(self::once())->method('setStartYear')->with(2021);
  367. $course->expects(self::once())->method('setEndYear')->with(2022);
  368. $onParametersChange->onMusicalDateChange($parameters, new \DateTime('2022-09-01'));
  369. }
  370. }