/calendar/tests/action_event_test.php

https://github.com/markn86/moodle · PHP · 204 lines · 119 code · 27 blank · 58 comment · 2 complexity · 3b30e2af2b99901d925e9dde951254ed MD5 · raw file

  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Action event tests.
  18. *
  19. * @package core_calendar
  20. * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. defined('MOODLE_INTERNAL') || die();
  24. use core_calendar\local\event\entities\action_event;
  25. use core_calendar\local\event\value_objects\action;
  26. use core_calendar\local\event\value_objects\event_description;
  27. use core_calendar\local\event\value_objects\event_times;
  28. use core_calendar\local\event\entities\event_collection_interface;
  29. use core_calendar\local\event\entities\event_interface;
  30. /**
  31. * Action event testcase.
  32. *
  33. * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
  34. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35. */
  36. class core_calendar_action_event_testcase extends advanced_testcase {
  37. /**
  38. * Test event class getters.
  39. *
  40. * @dataProvider getters_testcases()
  41. * @param array $constructorparams Associative array of constructor parameters.
  42. */
  43. public function test_getters($constructorparams) {
  44. $event = new action_event(
  45. $constructorparams['event'],
  46. $constructorparams['action']
  47. );
  48. foreach ($constructorparams as $name => $value) {
  49. if ($name !== 'event') {
  50. $this->assertEquals($event->{'get_' . $name}(), $value);
  51. }
  52. }
  53. }
  54. /**
  55. * Test cases for getters test.
  56. */
  57. public function getters_testcases() {
  58. return [
  59. 'Dataset 1' => [
  60. 'constructorparams' => [
  61. 'event' => new core_calendar_action_event_test_event(),
  62. 'action' => new action(
  63. 'action 1',
  64. new moodle_url('http://example.com'),
  65. 2,
  66. true
  67. )
  68. ]
  69. ],
  70. 'Dataset 2' => [
  71. 'constructorparams' => [
  72. 'event' => new core_calendar_action_event_test_event(),
  73. 'action' => new action(
  74. 'action 2',
  75. new moodle_url('http://example.com'),
  76. 5,
  77. false
  78. )
  79. ]
  80. ],
  81. ];
  82. }
  83. }
  84. /**
  85. * Test event.
  86. *
  87. * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
  88. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  89. */
  90. class core_calendar_action_event_test_event implements event_interface {
  91. public function get_id() {
  92. return 1729;
  93. }
  94. public function get_name() {
  95. return 'Jeff';
  96. }
  97. public function get_description() {
  98. return new event_description('asdf', 1);
  99. }
  100. public function get_location() {
  101. return 'Cube office';
  102. }
  103. public function get_category() {
  104. return new \stdClass();
  105. }
  106. public function get_course() {
  107. return new \stdClass();
  108. }
  109. public function get_course_module() {
  110. return new \stdClass();
  111. }
  112. public function get_group() {
  113. return new \stdClass();
  114. }
  115. public function get_user() {
  116. return new \stdClass();
  117. }
  118. public function get_type() {
  119. return 'asdf';
  120. }
  121. public function get_times() {
  122. return new event_times(
  123. (new \DateTimeImmutable())->setTimestamp(-386380800),
  124. (new \DateTimeImmutable())->setTimestamp(115776000),
  125. (new \DateTimeImmutable())->setTimestamp(115776000),
  126. (new \DateTimeImmutable())->setTimestamp(time())
  127. );
  128. }
  129. public function get_repeats() {
  130. return new core_calendar_action_event_test_event_collection();
  131. }
  132. public function get_subscription() {
  133. return new \stdClass();
  134. }
  135. public function is_visible() {
  136. return true;
  137. }
  138. /**
  139. * Component
  140. * @return string|null
  141. */
  142. public function get_component() {
  143. return null;
  144. }
  145. }
  146. /**
  147. * Test event collection.
  148. *
  149. * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
  150. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  151. */
  152. class core_calendar_action_event_test_event_collection implements event_collection_interface {
  153. /**
  154. * @var array
  155. */
  156. protected $events;
  157. /**
  158. * core_calendar_action_event_test_event_collection constructor.
  159. */
  160. public function __construct() {
  161. $this->events = [
  162. 'not really an event hahaha',
  163. 'also not really. gottem.'
  164. ];
  165. }
  166. public function get_id() {
  167. return 1729;
  168. }
  169. public function get_num() {
  170. return 2;
  171. }
  172. public function getIterator() {
  173. foreach ($this->events as $event) {
  174. yield $event;
  175. }
  176. }
  177. }