MDL-57456 mod_glossary: performance improvement for tags backup/restore
[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');
29 require_once($CFG->dirroot . '/mod/glossary/locallib.php');
31 /**
32  * Glossary lib testcase.
33  *
34  * @package    mod_glossary
35  * @copyright  2015 Frédéric Massart - FMCorz.net
36  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37  */
38 class mod_glossary_lib_testcase extends advanced_testcase {
40     public function test_glossary_view() {
41         global $CFG;
42         $origcompletion = $CFG->enablecompletion;
43         $CFG->enablecompletion = true;
44         $this->resetAfterTest(true);
46         // Generate all the things.
47         $c1 = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
48         $g1 = $this->getDataGenerator()->create_module('glossary', array(
49             'course' => $c1->id,
50             'completion' => COMPLETION_TRACKING_AUTOMATIC,
51             'completionview' => 1
52         ));
53         $g2 = $this->getDataGenerator()->create_module('glossary', array(
54             'course' => $c1->id,
55             'completion' => COMPLETION_TRACKING_AUTOMATIC,
56             'completionview' => 1
57         ));
58         $u1 = $this->getDataGenerator()->create_user();
59         $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
60         $modinfo = course_modinfo::instance($c1->id);
61         $cm1 = $modinfo->get_cm($g1->cmid);
62         $cm2 = $modinfo->get_cm($g2->cmid);
63         $ctx1 = $cm1->context;
64         $completion = new completion_info($c1);
66         $this->setUser($u1);
68         // Confirm what we've set up.
69         $this->assertEquals(COMPLETION_NOT_VIEWED, $completion->get_data($cm1, false, $u1->id)->viewed);
70         $this->assertEquals(COMPLETION_INCOMPLETE, $completion->get_data($cm1, false, $u1->id)->completionstate);
71         $this->assertEquals(COMPLETION_NOT_VIEWED, $completion->get_data($cm2, false, $u1->id)->viewed);
72         $this->assertEquals(COMPLETION_INCOMPLETE, $completion->get_data($cm2, false, $u1->id)->completionstate);
74         // Simulate the view call.
75         $sink = $this->redirectEvents();
76         glossary_view($g1, $c1, $cm1, $ctx1, 'letter');
77         $events = $sink->get_events();
79         // Assertions.
80         $this->assertCount(3, $events);
81         $this->assertEquals('\core\event\course_module_completion_updated', $events[0]->eventname);
82         $this->assertEquals('\core\event\course_module_completion_updated', $events[1]->eventname);
83         $this->assertEquals('\mod_glossary\event\course_module_viewed', $events[2]->eventname);
84         $this->assertEquals($g1->id, $events[2]->objectid);
85         $this->assertEquals('letter', $events[2]->other['mode']);
86         $this->assertEquals(COMPLETION_VIEWED, $completion->get_data($cm1, false, $u1->id)->viewed);
87         $this->assertEquals(COMPLETION_COMPLETE, $completion->get_data($cm1, false, $u1->id)->completionstate);
88         $this->assertEquals(COMPLETION_NOT_VIEWED, $completion->get_data($cm2, false, $u1->id)->viewed);
89         $this->assertEquals(COMPLETION_INCOMPLETE, $completion->get_data($cm2, false, $u1->id)->completionstate);
91         // Tear down.
92         $sink->close();
93         $CFG->enablecompletion = $origcompletion;
94     }
96     public function test_glossary_entry_view() {
97         $this->resetAfterTest(true);
99         // Generate all the things.
100         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
101         $c1 = $this->getDataGenerator()->create_course();
102         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
103         $e1 = $gg->create_content($g1);
104         $u1 = $this->getDataGenerator()->create_user();
105         $ctx = context_module::instance($g1->cmid);
106         $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
108         // Assertions.
109         $sink = $this->redirectEvents();
110         glossary_entry_view($e1, $ctx);
111         $events = $sink->get_events();
112         $this->assertCount(1, $events);
113         $this->assertEquals('\mod_glossary\event\entry_viewed', $events[0]->eventname);
114         $this->assertEquals($e1->id, $events[0]->objectid);
115         $sink->close();
116     }
118     public function test_glossary_core_calendar_provide_event_action() {
119         $this->resetAfterTest();
120         $this->setAdminUser();
122         // Create the activity.
123         $course = $this->getDataGenerator()->create_course();
124         $glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course->id));
126         // Create a calendar event.
127         $event = $this->create_action_event($course->id, $glossary->id,
128             \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
130         // Create an action factory.
131         $factory = new \core_calendar\action_factory();
133         // Decorate action event.
134         $actionevent = mod_glossary_core_calendar_provide_event_action($event, $factory);
136         // Confirm the event was decorated.
137         $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
138         $this->assertEquals(get_string('view'), $actionevent->get_name());
139         $this->assertInstanceOf('moodle_url', $actionevent->get_url());
140         $this->assertEquals(1, $actionevent->get_item_count());
141         $this->assertTrue($actionevent->is_actionable());
142     }
144     public function test_glossary_core_calendar_provide_event_action_already_completed() {
145         global $CFG;
147         $this->resetAfterTest();
148         $this->setAdminUser();
150         $CFG->enablecompletion = 1;
152         // Create the activity.
153         $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
154         $glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course->id),
155             array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
157         // Get some additional data.
158         $cm = get_coursemodule_from_instance('glossary', $glossary->id);
160         // Create a calendar event.
161         $event = $this->create_action_event($course->id, $glossary->id,
162             \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
164         // Mark the activity as completed.
165         $completion = new completion_info($course);
166         $completion->set_module_viewed($cm);
168         // Create an action factory.
169         $factory = new \core_calendar\action_factory();
171         // Decorate action event.
172         $actionevent = mod_glossary_core_calendar_provide_event_action($event, $factory);
174         // Ensure result was null.
175         $this->assertNull($actionevent);
176     }
178     /**
179      * Creates an action event.
180      *
181      * @param int $courseid The course id.
182      * @param int $instanceid The instance id.
183      * @param string $eventtype The event type.
184      * @return bool|calendar_event
185      */
186     private function create_action_event($courseid, $instanceid, $eventtype) {
187         $event = new stdClass();
188         $event->name = 'Calendar event';
189         $event->modulename  = 'glossary';
190         $event->courseid = $courseid;
191         $event->instance = $instanceid;
192         $event->type = CALENDAR_EVENT_TYPE_ACTION;
193         $event->eventtype = $eventtype;
194         $event->timestart = time();
196         return calendar_event::create($event);
197     }
199     public function test_mod_glossary_get_tagged_entries() {
200         global $DB;
202         $this->resetAfterTest();
203         $this->setAdminUser();
205         // Setup test data.
206         $glossarygenerator = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
207         $course3 = $this->getDataGenerator()->create_course();
208         $course2 = $this->getDataGenerator()->create_course();
209         $course1 = $this->getDataGenerator()->create_course();
210         $glossary1 = $this->getDataGenerator()->create_module('glossary', array('course' => $course1->id));
211         $glossary2 = $this->getDataGenerator()->create_module('glossary', array('course' => $course2->id));
212         $glossary3 = $this->getDataGenerator()->create_module('glossary', array('course' => $course3->id));
213         $entry11 = $glossarygenerator->create_content($glossary1, array('tags' => array('Cats', 'Dogs')));
214         $entry12 = $glossarygenerator->create_content($glossary1, array('tags' => array('Cats', 'mice')));
215         $entry13 = $glossarygenerator->create_content($glossary1, array('tags' => array('Cats')));
216         $entry14 = $glossarygenerator->create_content($glossary1);
217         $entry15 = $glossarygenerator->create_content($glossary1, array('tags' => array('Cats')));
218         $entry16 = $glossarygenerator->create_content($glossary1, array('tags' => array('Cats'), 'approved' => false));
219         $entry21 = $glossarygenerator->create_content($glossary2, array('tags' => array('Cats')));
220         $entry22 = $glossarygenerator->create_content($glossary2, array('tags' => array('Cats', 'Dogs')));
221         $entry23 = $glossarygenerator->create_content($glossary2, array('tags' => array('mice', 'Cats')));
222         $entry31 = $glossarygenerator->create_content($glossary3, array('tags' => array('mice', 'Cats')));
224         $tag = core_tag_tag::get_by_name(0, 'Cats');
226         // Admin can see everything.
227         $res = mod_glossary_get_tagged_entries($tag, /*$exclusivemode = */false,
228             /*$fromctx = */0, /*$ctx = */0, /*$rec = */1, /*$entry = */0);
229         $this->assertRegExp('/'.$entry11->concept.'</', $res->content);
230         $this->assertRegExp('/'.$entry12->concept.'</', $res->content);
231         $this->assertRegExp('/'.$entry13->concept.'</', $res->content);
232         $this->assertNotRegExp('/'.$entry14->concept.'</', $res->content);
233         $this->assertRegExp('/'.$entry15->concept.'</', $res->content);
234         $this->assertRegExp('/'.$entry16->concept.'</', $res->content);
235         $this->assertNotRegExp('/'.$entry21->concept.'</', $res->content);
236         $this->assertNotRegExp('/'.$entry22->concept.'</', $res->content);
237         $this->assertNotRegExp('/'.$entry23->concept.'</', $res->content);
238         $this->assertNotRegExp('/'.$entry31->concept.'</', $res->content);
239         $this->assertEmpty($res->prevpageurl);
240         $this->assertNotEmpty($res->nextpageurl);
241         $res = mod_glossary_get_tagged_entries($tag, /*$exclusivemode = */false,
242             /*$fromctx = */0, /*$ctx = */0, /*$rec = */1, /*$entry = */1);
243         $this->assertNotRegExp('/'.$entry11->concept.'</', $res->content);
244         $this->assertNotRegExp('/'.$entry12->concept.'</', $res->content);
245         $this->assertNotRegExp('/'.$entry13->concept.'</', $res->content);
246         $this->assertNotRegExp('/'.$entry14->concept.'</', $res->content);
247         $this->assertNotRegExp('/'.$entry15->concept.'</', $res->content);
248         $this->assertNotRegExp('/'.$entry16->concept.'</', $res->content);
249         $this->assertRegExp('/'.$entry21->concept.'</', $res->content);
250         $this->assertRegExp('/'.$entry22->concept.'</', $res->content);
251         $this->assertRegExp('/'.$entry23->concept.'</', $res->content);
252         $this->assertRegExp('/'.$entry31->concept.'</', $res->content);
253         $this->assertNotEmpty($res->prevpageurl);
254         $this->assertEmpty($res->nextpageurl);
256         // Create and enrol a user.
257         $student = self::getDataGenerator()->create_user();
258         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
259         $this->getDataGenerator()->enrol_user($student->id, $course1->id, $studentrole->id, 'manual');
260         $this->getDataGenerator()->enrol_user($student->id, $course2->id, $studentrole->id, 'manual');
261         $this->setUser($student);
262         core_tag_index_builder::reset_caches();
264         // User can not see entries in course 3 because he is not enrolled.
265         $res = mod_glossary_get_tagged_entries($tag, /*$exclusivemode = */false,
266             /*$fromctx = */0, /*$ctx = */0, /*$rec = */1, /*$entry = */1);
267         $this->assertRegExp('/'.$entry22->concept.'/', $res->content);
268         $this->assertRegExp('/'.$entry23->concept.'/', $res->content);
269         $this->assertNotRegExp('/'.$entry31->concept.'/', $res->content);
271         // User can search glossary entries inside a course.
272         $coursecontext = context_course::instance($course1->id);
273         $res = mod_glossary_get_tagged_entries($tag, /*$exclusivemode = */false,
274             /*$fromctx = */0, /*$ctx = */$coursecontext->id, /*$rec = */1, /*$entry = */0);
275         $this->assertRegExp('/'.$entry11->concept.'/', $res->content);
276         $this->assertRegExp('/'.$entry12->concept.'/', $res->content);
277         $this->assertRegExp('/'.$entry13->concept.'/', $res->content);
278         $this->assertNotRegExp('/'.$entry14->concept.'/', $res->content);
279         $this->assertRegExp('/'.$entry15->concept.'/', $res->content);
280         $this->assertNotRegExp('/'.$entry21->concept.'/', $res->content);
281         $this->assertNotRegExp('/'.$entry22->concept.'/', $res->content);
282         $this->assertNotRegExp('/'.$entry23->concept.'/', $res->content);
283         $this->assertEmpty($res->nextpageurl);
285         // User cannot see hidden entries.
286         $this->assertNotRegExp('/'.$entry16->concept.'/', $res->content);
287     }