MDL-37624 calendar: Added location support
[moodle.git] / calendar / tests / event_test.php
CommitLineData
e46efe3a
CB
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/**
18 * Event tests.
19 *
20 * @package core_calendar
21 * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27use core_calendar\local\event\entities\event;
28use core_calendar\local\event\proxies\std_proxy;
0085b0ea 29use core_calendar\local\event\proxies\coursecat_proxy;
e46efe3a
CB
30use core_calendar\local\event\value_objects\event_description;
31use core_calendar\local\event\value_objects\event_times;
42e76c3f 32use core_calendar\local\event\entities\event_collection_interface;
e46efe3a
CB
33
34/**
35 * Event testcase.
36 *
37 * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
40class core_calendar_event_testcase extends advanced_testcase {
41 /**
42 * Test event class getters.
43 *
44 * @dataProvider getters_testcases()
45 * @param array $constructorparams Associative array of constructor parameters.
46 */
47 public function test_getters($constructorparams) {
48 $event = new event(
49 $constructorparams['id'],
50 $constructorparams['name'],
51 $constructorparams['description'],
0085b0ea 52 $constructorparams['category'],
e46efe3a
CB
53 $constructorparams['course'],
54 $constructorparams['group'],
55 $constructorparams['user'],
56 $constructorparams['repeats'],
57 $constructorparams['course_module'],
58 $constructorparams['type'],
59 $constructorparams['times'],
60 $constructorparams['visible'],
ef4e04ee
MH
61 $constructorparams['subscription'],
62 $constructorparams['location']
e46efe3a
CB
63 );
64
65 foreach ($constructorparams as $name => $value) {
66 if ($name !== 'visible') {
67 $this->assertEquals($event->{'get_' . $name}(), $value);
68 }
69 }
70
71 $this->assertEquals($event->is_visible(), $constructorparams['visible']);
72 }
73
74 /**
75 * Test cases for getters test.
76 */
77 public function getters_testcases() {
78 $lamecallable = function($id) {
79 return (object)['id' => $id];
80 };
81
82 return [
83 'Dataset 1' => [
84 'constructorparams' => [
85 'id' => 1,
86 'name' => 'Test event 1',
87 'description' => new event_description('asdf', 1),
0085b0ea 88 'category' => new coursecat_proxy(0),
e46efe3a
CB
89 'course' => new std_proxy(1, $lamecallable),
90 'group' => new std_proxy(1, $lamecallable),
91 'user' => new std_proxy(1, $lamecallable),
92 'repeats' => new core_calendar_event_test_event_collection(),
93 'course_module' => new std_proxy(1, $lamecallable),
94 'type' => 'dunno what this actually is meant to be',
95 'times' => new event_times(
a25116c0
EL
96 (new \DateTimeImmutable())->setTimestamp(-386380800),
97 (new \DateTimeImmutable())->setTimestamp(115776000),
98 (new \DateTimeImmutable())->setTimestamp(115776000),
e46efe3a
CB
99 (new \DateTimeImmutable())->setTimestamp(time())
100 ),
101 'visible' => true,
ef4e04ee
MH
102 'subscription' => new std_proxy(1, $lamecallable),
103 'location' => 'Test',
e46efe3a
CB
104 ]
105 ],
106 ];
107 }
108}
109
110/**
111 * Test event class.
112 *
113 * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
114 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
115 */
116class core_calendar_event_test_event_collection implements event_collection_interface {
117 /**
118 * @var array $events Array of events.
119 */
120 protected $events;
121
122 /**
123 * Constructor.
124 */
125 public function __construct() {
126 $this->events = [
127 'not really an event hahaha',
128 'also not really. gottem.'
129 ];
130 }
131
132 public function get_id() {
133 return 1729;
134 }
135
136 public function get_num() {
137 return 2;
138 }
139
140 public function getIterator() {
141 foreach ($this->events as $event) {
142 yield $event;
143 }
144 }
145}