MDL-57447 block_myoverview: build paging bar data structure
[moodle.git] / calendar / tests / event_factory_test.php
CommitLineData
5ca71c2d
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 factory test.
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\factories\event_factory;
28use core_calendar\local\interfaces\event_interface;
29
30/**
31 * Event factory 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 */
36class core_calendar_event_factory_testcase extends advanced_testcase {
37 /**
38 * Test event class getters.
39 *
40 * @dataProvider create_instance_testcases()
41 * @param array $instanceparams Associative array of instance parameters.
42 */
43 public function test_create_instance(
44 $dbrow,
45 callable $callbackapplier,
46 $expectedattributevalue
47 ) {
48 $this->resetAfterTest(true);
49 $this->setAdminUser();
50 $event = $this->create_event();
51
52 $factory = new event_factory($callbackapplier);
53 $dbrow->id = $event->id;
54 $instance = $factory->create_instance($dbrow);
55
56 $this->assertEquals($instance->testattribute, $expectedattributevalue);
57 }
58
59 /**
60 * Test invalid callback exception.
61 *
62 * @expectedException \core_calendar\local\event\exceptions\invalid_callback_exception
63 */
64 public function test_invalid_callback() {
65 $this->resetAfterTest(true);
66 $this->setAdminUser();
67 $event = $this->create_event();
68 $factory = new event_factory(function () {
69 return 'hello';
70 });
71
72 $factory->create_instance(
73 (object)[
74 'id' => $event->id,
75 'name' => 'test',
76 'description' => 'Test description',
77 'format' => 2,
78 'courseid' => 1,
79 'groupid' => 1,
80 'userid' => 1,
81 'repeatid' => 1,
82 'modulename' => 'assign',
83 'instance' => 1,
84 'eventtype' => 'due',
85 'timestart' => 123456789,
86 'timeduration' => 12,
87 'timemodified' => 123456789,
88 'timesort' => 123456789,
89 'visible' => 1,
90 'subscriptionid' => 1
91 ]
92 );
93 }
94
95 public function create_instance_testcases() {
96 return [
97 'Sample event record' => [
98 'dbrow' => (object)[
99 'name' => 'Test event',
100 'description' => 'Hello',
101 'format' => 1,
102 'courseid' => 1,
103 'groupid' => 1,
104 'userid' => 1,
105 'repeatid' => null,
106 'modulename' => 'Test module',
107 'instance' => 1,
108 'eventtype' => 'Due',
109 'timestart' => 123456789,
110 'timeduration' => 123456789,
111 'timemodified' => 123456789,
112 'timesort' => 123456789,
113 'visible' => true,
114 'subscriptionid' => 1
115 ],
116 'callbackapplier' => function(event_interface $event) {
117 $event->testattribute = 'Hello';
118 return $event;
119 },
120 'Hello'
121 ]
122 ];
123 }
124
125 /**
126 * Helper function to create calendar events using the old code.
127 *
128 * @param array $properties A list of calendar event properties to set
129 * @return event
130 */
131 protected function create_event($properties = []) {
132 $record = new \stdClass();
133 $record->name = 'event name';
134 $record->eventtype = 'global';
135 $record->timestart = time();
136 $record->timeduration = 0;
137 $record->timesort = 0;
138 $record->type = 1;
139 $record->courseid = 0;
140
141 foreach ($properties as $name => $value) {
142 $record->$name = $value;
143 }
144
145 $event = new \core_calendar\event($record);
146 return $event->create($record, false);
147 }
148}