Merge branch 'MDL-58536-master' of git://github.com/ryanwyllie/moodle
[moodle.git] / mod / glossary / tests / lib_test.php
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/>.
17 /**
18  * Glossary lib tests.
19  *
20  * @package    mod_glossary
21  * @copyright  2015 Frédéric Massart - FMCorz.net
22  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 defined('MOODLE_INTERNAL') || die();
27 global $CFG;
28 require_once($CFG->dirroot . '/mod/glossary/lib.php');
30 /**
31  * Glossary lib testcase.
32  *
33  * @package    mod_glossary
34  * @copyright  2015 Frédéric Massart - FMCorz.net
35  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36  */
37 class mod_glossary_lib_testcase extends advanced_testcase {
39     public function test_glossary_view() {
40         global $CFG;
41         $origcompletion = $CFG->enablecompletion;
42         $CFG->enablecompletion = true;
43         $this->resetAfterTest(true);
45         // Generate all the things.
46         $c1 = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
47         $g1 = $this->getDataGenerator()->create_module('glossary', array(
48             'course' => $c1->id,
49             'completion' => COMPLETION_TRACKING_AUTOMATIC,
50             'completionview' => 1
51         ));
52         $g2 = $this->getDataGenerator()->create_module('glossary', array(
53             'course' => $c1->id,
54             'completion' => COMPLETION_TRACKING_AUTOMATIC,
55             'completionview' => 1
56         ));
57         $u1 = $this->getDataGenerator()->create_user();
58         $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
59         $modinfo = course_modinfo::instance($c1->id);
60         $cm1 = $modinfo->get_cm($g1->cmid);
61         $cm2 = $modinfo->get_cm($g2->cmid);
62         $ctx1 = $cm1->context;
63         $completion = new completion_info($c1);
65         $this->setUser($u1);
67         // Confirm what we've set up.
68         $this->assertEquals(COMPLETION_NOT_VIEWED, $completion->get_data($cm1, false, $u1->id)->viewed);
69         $this->assertEquals(COMPLETION_INCOMPLETE, $completion->get_data($cm1, false, $u1->id)->completionstate);
70         $this->assertEquals(COMPLETION_NOT_VIEWED, $completion->get_data($cm2, false, $u1->id)->viewed);
71         $this->assertEquals(COMPLETION_INCOMPLETE, $completion->get_data($cm2, false, $u1->id)->completionstate);
73         // Simulate the view call.
74         $sink = $this->redirectEvents();
75         glossary_view($g1, $c1, $cm1, $ctx1, 'letter');
76         $events = $sink->get_events();
78         // Assertions.
79         $this->assertCount(3, $events);
80         $this->assertEquals('\core\event\course_module_completion_updated', $events[0]->eventname);
81         $this->assertEquals('\core\event\course_module_completion_updated', $events[1]->eventname);
82         $this->assertEquals('\mod_glossary\event\course_module_viewed', $events[2]->eventname);
83         $this->assertEquals($g1->id, $events[2]->objectid);
84         $this->assertEquals('letter', $events[2]->other['mode']);
85         $this->assertEquals(COMPLETION_VIEWED, $completion->get_data($cm1, false, $u1->id)->viewed);
86         $this->assertEquals(COMPLETION_COMPLETE, $completion->get_data($cm1, false, $u1->id)->completionstate);
87         $this->assertEquals(COMPLETION_NOT_VIEWED, $completion->get_data($cm2, false, $u1->id)->viewed);
88         $this->assertEquals(COMPLETION_INCOMPLETE, $completion->get_data($cm2, false, $u1->id)->completionstate);
90         // Tear down.
91         $sink->close();
92         $CFG->enablecompletion = $origcompletion;
93     }
95     public function test_glossary_entry_view() {
96         $this->resetAfterTest(true);
98         // Generate all the things.
99         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
100         $c1 = $this->getDataGenerator()->create_course();
101         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
102         $e1 = $gg->create_content($g1);
103         $u1 = $this->getDataGenerator()->create_user();
104         $ctx = context_module::instance($g1->cmid);
105         $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
107         // Assertions.
108         $sink = $this->redirectEvents();
109         glossary_entry_view($e1, $ctx);
110         $events = $sink->get_events();
111         $this->assertCount(1, $events);
112         $this->assertEquals('\mod_glossary\event\entry_viewed', $events[0]->eventname);
113         $this->assertEquals($e1->id, $events[0]->objectid);
114         $sink->close();
115     }
117     public function test_glossary_core_calendar_provide_event_action() {
118         $this->resetAfterTest();
119         $this->setAdminUser();
121         // Create the activity.
122         $course = $this->getDataGenerator()->create_course();
123         $glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course->id));
125         // Create a calendar event.
126         $event = $this->create_action_event($course->id, $glossary->id,
127             \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
129         // Create an action factory.
130         $factory = new \core_calendar\action_factory();
132         // Decorate action event.
133         $actionevent = mod_glossary_core_calendar_provide_event_action($event, $factory);
135         // Confirm the event was decorated.
136         $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
137         $this->assertEquals(get_string('view'), $actionevent->get_name());
138         $this->assertInstanceOf('moodle_url', $actionevent->get_url());
139         $this->assertEquals(1, $actionevent->get_item_count());
140         $this->assertTrue($actionevent->is_actionable());
141     }
143     public function test_glossary_core_calendar_provide_event_action_already_completed() {
144         global $CFG;
146         $this->resetAfterTest();
147         $this->setAdminUser();
149         $CFG->enablecompletion = 1;
151         // Create the activity.
152         $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
153         $glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course->id),
154             array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
156         // Get some additional data.
157         $cm = get_coursemodule_from_instance('glossary', $glossary->id);
159         // Create a calendar event.
160         $event = $this->create_action_event($course->id, $glossary->id,
161             \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
163         // Mark the activity as completed.
164         $completion = new completion_info($course);
165         $completion->set_module_viewed($cm);
167         // Create an action factory.
168         $factory = new \core_calendar\action_factory();
170         // Decorate action event.
171         $actionevent = mod_glossary_core_calendar_provide_event_action($event, $factory);
173         // Ensure result was null.
174         $this->assertNull($actionevent);
175     }
177     /**
178      * Creates an action event.
179      *
180      * @param int $courseid The course id.
181      * @param int $instanceid The instance id.
182      * @param string $eventtype The event type.
183      * @return bool|calendar_event
184      */
185     private function create_action_event($courseid, $instanceid, $eventtype) {
186         $event = new stdClass();
187         $event->name = 'Calendar event';
188         $event->modulename  = 'glossary';
189         $event->courseid = $courseid;
190         $event->instance = $instanceid;
191         $event->type = CALENDAR_EVENT_TYPE_ACTION;
192         $event->eventtype = $eventtype;
193         $event->timestart = time();
195         return calendar_event::create($event);
196     }
198     /**
199      * Test the callback responsible for returning the completion rule descriptions.
200      * This function should work given either an instance of the module (cm_info), such as when checking the active rules,
201      * or if passed a stdClass of similar structure, such as when checking the the default completion settings for a mod type.
202      */
203     public function test_mod_glossary_completion_get_active_rule_descriptions() {
204         $this->resetAfterTest();
205         $this->setAdminUser();
207         // Two activities, both with automatic completion. One has the 'completionsubmit' rule, one doesn't.
208         $course = $this->getDataGenerator()->create_course(['enablecompletion' => 2]);
209         $glossary1 = $this->getDataGenerator()->create_module('glossary', [
210             'course' => $course->id,
211             'completion' => 2,
212             'completionentries' => 3
213         ]);
214         $glossary2 = $this->getDataGenerator()->create_module('glossary', [
215             'course' => $course->id,
216             'completion' => 2,
217             'completionentries' => 0
218         ]);
219         $cm1 = cm_info::create(get_coursemodule_from_instance('glossary', $glossary1->id));
220         $cm2 = cm_info::create(get_coursemodule_from_instance('glossary', $glossary2->id));
222         // Data for the stdClass input type.
223         // This type of input would occur when checking the default completion rules for an activity type, where we don't have
224         // any access to cm_info, rather the input is a stdClass containing completion and customdata attributes, just like cm_info.
225         $moddefaults = new stdClass();
226         $moddefaults->customdata = ['customcompletionrules' => ['completionentries' => 3]];
227         $moddefaults->completion = 2;
229         $activeruledescriptions = [get_string('completionentriesdesc', 'glossary', $glossary1->completionentries)];
230         $this->assertEquals(mod_glossary_get_completion_active_rule_descriptions($cm1), $activeruledescriptions);
231         $this->assertEquals(mod_glossary_get_completion_active_rule_descriptions($cm2), []);
232         $this->assertEquals(mod_glossary_get_completion_active_rule_descriptions($moddefaults), $activeruledescriptions);
233         $this->assertEquals(mod_glossary_get_completion_active_rule_descriptions(new stdClass()), []);
234     }