Merge branch 'MDL-64284-36_get_component_classes_cache' of https://github.com/tomdick...
[moodle.git] / mod / book / 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  * Unit tests for (some of) mod/book/lib.php.
19  *
20  * @package    mod_book
21  * @category   phpunit
22  * @copyright  2015 Juan Leyva <juan@moodle.com>
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
27 defined('MOODLE_INTERNAL') || die();
29 global $CFG;
30 require_once($CFG->dirroot . '/mod/book/lib.php');
32 /**
33  * Unit tests for (some of) mod/book/lib.php.
34  *
35  * @package    mod_book
36  * @category   phpunit
37  * @copyright  2015 Juan Leyva <juan@moodle.com>
38  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39  */
40 class mod_book_lib_testcase extends advanced_testcase {
42     public function setUp() {
43         $this->resetAfterTest();
44         $this->setAdminUser();
45     }
47     public function test_export_contents() {
48         global $DB;
50         $user = $this->getDataGenerator()->create_user();
51         $course = $this->getDataGenerator()->create_course(array('enablecomment' => 1));
52         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
53         $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
55         // Test book with 3 chapters.
56         $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
57         $cm = get_coursemodule_from_id('book', $book->cmid);
59         $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book');
60         $chapter1 = $bookgenerator->create_chapter(array('bookid' => $book->id, "pagenum" => 1));
61         $chapter2 = $bookgenerator->create_chapter(array('bookid' => $book->id, "pagenum" => 2));
62         $subchapter = $bookgenerator->create_chapter(array('bookid' => $book->id, "pagenum" => 3, "subchapter" => 1));
63         $chapter3 = $bookgenerator->create_chapter(array('bookid' => $book->id, "pagenum" => 4, "hidden" => 1));
65         $this->setUser($user);
67         $contents = book_export_contents($cm, '');
68         // The hidden chapter must not be included, and additional page with the structure must be included.
69         $this->assertCount(4, $contents);
71         $this->assertEquals('structure', $contents[0]['filename']);
72         $this->assertEquals('index.html', $contents[1]['filename']);
73         $this->assertEquals('Chapter 1', $contents[1]['content']);
74         $this->assertEquals('index.html', $contents[2]['filename']);
75         $this->assertEquals('Chapter 2', $contents[2]['content']);
76         $this->assertEquals('index.html', $contents[3]['filename']);
77         $this->assertEquals('Chapter 3', $contents[3]['content']);
79         // Test empty book.
80         $emptybook = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
81         $cm = get_coursemodule_from_id('book', $emptybook->cmid);
82         $contents = book_export_contents($cm, '');
84         $this->assertCount(1, $contents);
85         $this->assertEquals('structure', $contents[0]['filename']);
86         $this->assertEquals(json_encode(array()), $contents[0]['content']);
88     }
90     /**
91      * Test book_view
92      * @return void
93      */
94     public function test_book_view() {
95         global $CFG, $DB;
97         $CFG->enablecompletion = 1;
99         // Setup test data.
100         $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
101         $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id),
102                                                             array('completion' => 2, 'completionview' => 1));
103         $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book');
104         $chapter = $bookgenerator->create_chapter(array('bookid' => $book->id));
106         $context = context_module::instance($book->cmid);
107         $cm = get_coursemodule_from_instance('book', $book->id);
109         // Trigger and capture the event.
110         $sink = $this->redirectEvents();
112         // Check just opening the book.
113         book_view($book, 0, false, $course, $cm, $context);
115         $events = $sink->get_events();
116         $this->assertCount(1, $events);
117         $event = array_shift($events);
119         // Checking that the event contains the expected values.
120         $this->assertInstanceOf('\mod_book\event\course_module_viewed', $event);
121         $this->assertEquals($context, $event->get_context());
122         $moodleurl = new \moodle_url('/mod/book/view.php', array('id' => $cm->id));
123         $this->assertEquals($moodleurl, $event->get_url());
124         $this->assertEventContextNotUsed($event);
125         $this->assertNotEmpty($event->get_name());
127         // Check viewing one book chapter (the only one so it will be the first and last).
128         book_view($book, $chapter, true, $course, $cm, $context);
130         $events = $sink->get_events();
131         // We expect a total of 4 events. One for module viewed, one for chapter viewed and two belonging to completion.
132         $this->assertCount(4, $events);
134         // Check completion status.
135         $completion = new completion_info($course);
136         $completiondata = $completion->get_data($cm);
137         $this->assertEquals(1, $completiondata->completionstate);
138     }
140     public function test_book_core_calendar_provide_event_action() {
141         // Create the activity.
142         $course = $this->getDataGenerator()->create_course();
143         $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
145         // Create a calendar event.
146         $event = $this->create_action_event($course->id, $book->id,
147             \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
149         // Create an action factory.
150         $factory = new \core_calendar\action_factory();
152         // Decorate action event.
153         $actionevent = mod_book_core_calendar_provide_event_action($event, $factory);
155         // Confirm the event was decorated.
156         $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
157         $this->assertEquals(get_string('view'), $actionevent->get_name());
158         $this->assertInstanceOf('moodle_url', $actionevent->get_url());
159         $this->assertEquals(1, $actionevent->get_item_count());
160         $this->assertTrue($actionevent->is_actionable());
161     }
163     public function test_book_core_calendar_provide_event_action_in_hidden_section() {
164         // Create the activity.
165         $course = $this->getDataGenerator()->create_course();
166         $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
168         // Enrol a student in the course.
169         $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
171         // Create a calendar event.
172         $event = $this->create_action_event($course->id, $book->id,
173                 \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
175         // Set sections 0 as hidden.
176         set_section_visible($course->id, 0, 0);
178         // Now, log out.
179         $this->setUser();
181         // Create an action factory.
182         $factory = new \core_calendar\action_factory();
184         // Decorate action event for the student.
185         $actionevent = mod_book_core_calendar_provide_event_action($event, $factory, $student->id);
187         // Confirm the event is not shown at all.
188         $this->assertNull($actionevent);
189     }
191     public function test_book_core_calendar_provide_event_action_for_user() {
192         // Create the activity.
193         $course = $this->getDataGenerator()->create_course();
194         $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
196         // Enrol a student in the course.
197         $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
199         // Create a calendar event.
200         $event = $this->create_action_event($course->id, $book->id,
201             \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
203         // Now, log out.
204         $this->setUser();
206         // Create an action factory.
207         $factory = new \core_calendar\action_factory();
209         // Decorate action event for the student.
210         $actionevent = mod_book_core_calendar_provide_event_action($event, $factory, $student->id);
212         // Confirm the event was decorated.
213         $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
214         $this->assertEquals(get_string('view'), $actionevent->get_name());
215         $this->assertInstanceOf('moodle_url', $actionevent->get_url());
216         $this->assertEquals(1, $actionevent->get_item_count());
217         $this->assertTrue($actionevent->is_actionable());
218     }
220     public function test_book_core_calendar_provide_event_action_as_non_user() {
221         global $CFG;
223         // Create the activity.
224         $course = $this->getDataGenerator()->create_course();
225         $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
227         // Create a calendar event.
228         $event = $this->create_action_event($course->id, $book->id,
229             \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
231         // Log out the user and set force login to true.
232         \core\session\manager::init_empty_session();
233         $CFG->forcelogin = true;
235         // Create an action factory.
236         $factory = new \core_calendar\action_factory();
238         // Decorate action event.
239         $actionevent = mod_book_core_calendar_provide_event_action($event, $factory);
241         // Ensure result was null.
242         $this->assertNull($actionevent);
243     }
245     public function test_book_core_calendar_provide_event_action_already_completed() {
246         global $CFG;
248         $CFG->enablecompletion = 1;
250         // Create the activity.
251         $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
252         $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id),
253             array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
255         // Get some additional data.
256         $cm = get_coursemodule_from_instance('book', $book->id);
258         // Create a calendar event.
259         $event = $this->create_action_event($course->id, $book->id,
260             \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
262         // Mark the activity as completed.
263         $completion = new completion_info($course);
264         $completion->set_module_viewed($cm);
266         // Create an action factory.
267         $factory = new \core_calendar\action_factory();
269         // Decorate action event.
270         $actionevent = mod_book_core_calendar_provide_event_action($event, $factory);
272         // Ensure result was null.
273         $this->assertNull($actionevent);
274     }
276     public function test_book_core_calendar_provide_event_action_already_completed_for_user() {
277         global $CFG;
279         $CFG->enablecompletion = 1;
281         // Create the activity.
282         $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
283         $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id),
284             array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
286         // Enrol a student in the course.
287         $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
289         // Get some additional data.
290         $cm = get_coursemodule_from_instance('book', $book->id);
292         // Create a calendar event.
293         $event = $this->create_action_event($course->id, $book->id,
294             \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
296         // Mark the activity as completed for the student.
297         $completion = new completion_info($course);
298         $completion->set_module_viewed($cm, $student->id);
300         // Create an action factory.
301         $factory = new \core_calendar\action_factory();
303         // Decorate action event for the student.
304         $actionevent = mod_book_core_calendar_provide_event_action($event, $factory, $student->id);
306         // Ensure result was null.
307         $this->assertNull($actionevent);
308     }
310     /**
311      * Creates an action event.
312      *
313      * @param int $courseid The course id.
314      * @param int $instanceid The instance id.
315      * @param string $eventtype The event type.
316      * @return bool|calendar_event
317      */
318     private function create_action_event($courseid, $instanceid, $eventtype) {
319         $event = new stdClass();
320         $event->name = 'Calendar event';
321         $event->modulename  = 'book';
322         $event->courseid = $courseid;
323         $event->instance = $instanceid;
324         $event->type = CALENDAR_EVENT_TYPE_ACTION;
325         $event->eventtype = $eventtype;
326         $event->timestart = time();
328         return calendar_event::create($event);
329     }
331     public function test_mod_book_get_tagged_chapters() {
332         global $DB;
334         $this->resetAfterTest();
335         $this->setAdminUser();
337         // Setup test data.
338         $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book');
339         $course3 = $this->getDataGenerator()->create_course();
340         $course2 = $this->getDataGenerator()->create_course();
341         $course1 = $this->getDataGenerator()->create_course();
342         $book1 = $this->getDataGenerator()->create_module('book', array('course' => $course1->id));
343         $book2 = $this->getDataGenerator()->create_module('book', array('course' => $course2->id));
344         $book3 = $this->getDataGenerator()->create_module('book', array('course' => $course3->id));
345         $chapter11 = $bookgenerator->create_content($book1, array('tags' => array('Cats', 'Dogs')));
346         $chapter12 = $bookgenerator->create_content($book1, array('tags' => array('Cats', 'mice')));
347         $chapter13 = $bookgenerator->create_content($book1, array('tags' => array('Cats')));
348         $chapter14 = $bookgenerator->create_content($book1);
349         $chapter15 = $bookgenerator->create_content($book1, array('tags' => array('Cats')));
350         $chapter16 = $bookgenerator->create_content($book1, array('tags' => array('Cats'), 'hidden' => true));
351         $chapter21 = $bookgenerator->create_content($book2, array('tags' => array('Cats')));
352         $chapter22 = $bookgenerator->create_content($book2, array('tags' => array('Cats', 'Dogs')));
353         $chapter23 = $bookgenerator->create_content($book2, array('tags' => array('mice', 'Cats')));
354         $chapter31 = $bookgenerator->create_content($book3, array('tags' => array('mice', 'Cats')));
356         $tag = core_tag_tag::get_by_name(0, 'Cats');
358         // Admin can see everything.
359         $res = mod_book_get_tagged_chapters($tag, /*$exclusivemode = */false,
360             /*$fromctx = */0, /*$ctx = */0, /*$rec = */1, /*$chapter = */0);
361         $this->assertRegExp('/'.$chapter11->title.'</', $res->content);
362         $this->assertRegExp('/'.$chapter12->title.'</', $res->content);
363         $this->assertRegExp('/'.$chapter13->title.'</', $res->content);
364         $this->assertNotRegExp('/'.$chapter14->title.'</', $res->content);
365         $this->assertRegExp('/'.$chapter15->title.'</', $res->content);
366         $this->assertRegExp('/'.$chapter16->title.'</', $res->content);
367         $this->assertNotRegExp('/'.$chapter21->title.'</', $res->content);
368         $this->assertNotRegExp('/'.$chapter22->title.'</', $res->content);
369         $this->assertNotRegExp('/'.$chapter23->title.'</', $res->content);
370         $this->assertNotRegExp('/'.$chapter31->title.'</', $res->content);
371         $this->assertEmpty($res->prevpageurl);
372         $this->assertNotEmpty($res->nextpageurl);
373         $res = mod_book_get_tagged_chapters($tag, /*$exclusivemode = */false,
374             /*$fromctx = */0, /*$ctx = */0, /*$rec = */1, /*$chapter = */1);
375         $this->assertNotRegExp('/'.$chapter11->title.'</', $res->content);
376         $this->assertNotRegExp('/'.$chapter12->title.'</', $res->content);
377         $this->assertNotRegExp('/'.$chapter13->title.'</', $res->content);
378         $this->assertNotRegExp('/'.$chapter14->title.'</', $res->content);
379         $this->assertNotRegExp('/'.$chapter15->title.'</', $res->content);
380         $this->assertNotRegExp('/'.$chapter16->title.'</', $res->content);
381         $this->assertRegExp('/'.$chapter21->title.'</', $res->content);
382         $this->assertRegExp('/'.$chapter22->title.'</', $res->content);
383         $this->assertRegExp('/'.$chapter23->title.'</', $res->content);
384         $this->assertRegExp('/'.$chapter31->title.'</', $res->content);
385         $this->assertNotEmpty($res->prevpageurl);
386         $this->assertEmpty($res->nextpageurl);
388         // Create and enrol a user.
389         $student = self::getDataGenerator()->create_user();
390         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
391         $this->getDataGenerator()->enrol_user($student->id, $course1->id, $studentrole->id, 'manual');
392         $this->getDataGenerator()->enrol_user($student->id, $course2->id, $studentrole->id, 'manual');
393         $this->setUser($student);
394         core_tag_index_builder::reset_caches();
396         // User can not see chapters in course 3 because he is not enrolled.
397         $res = mod_book_get_tagged_chapters($tag, /*$exclusivemode = */false,
398             /*$fromctx = */0, /*$ctx = */0, /*$rec = */1, /*$chapter = */1);
399         $this->assertRegExp('/'.$chapter22->title.'/', $res->content);
400         $this->assertRegExp('/'.$chapter23->title.'/', $res->content);
401         $this->assertNotRegExp('/'.$chapter31->title.'/', $res->content);
403         // User can search book chapters inside a course.
404         $coursecontext = context_course::instance($course1->id);
405         $res = mod_book_get_tagged_chapters($tag, /*$exclusivemode = */false,
406             /*$fromctx = */0, /*$ctx = */$coursecontext->id, /*$rec = */1, /*$chapter = */0);
407         $this->assertRegExp('/'.$chapter11->title.'/', $res->content);
408         $this->assertRegExp('/'.$chapter12->title.'/', $res->content);
409         $this->assertRegExp('/'.$chapter13->title.'/', $res->content);
410         $this->assertNotRegExp('/'.$chapter14->title.'/', $res->content);
411         $this->assertRegExp('/'.$chapter15->title.'/', $res->content);
412         $this->assertNotRegExp('/'.$chapter21->title.'/', $res->content);
413         $this->assertNotRegExp('/'.$chapter22->title.'/', $res->content);
414         $this->assertNotRegExp('/'.$chapter23->title.'/', $res->content);
415         $this->assertEmpty($res->nextpageurl);
417         // User cannot see hidden chapters.
418         $this->assertNotRegExp('/'.$chapter16->title.'/', $res->content);
419     }