Merge branch 'MDL-64656-master' of git://github.com/jleyva/moodle
[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, $CFG;
49         require_once($CFG->dirroot . '/course/externallib.php');
51         $user = $this->getDataGenerator()->create_user();
52         $course = $this->getDataGenerator()->create_course(array('enablecomment' => 1));
53         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
54         $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
56         // Test book with 3 chapters.
57         $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
58         $cm = get_coursemodule_from_id('book', $book->cmid);
60         $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book');
61         $chapter1 = $bookgenerator->create_chapter(array('bookid' => $book->id, "pagenum" => 1,
62             'tags' => array('Cats', 'Dogs')));
63         $tag = core_tag_tag::get_by_name(0, 'Cats');
65         $chapter2 = $bookgenerator->create_chapter(array('bookid' => $book->id, "pagenum" => 2));
66         $subchapter = $bookgenerator->create_chapter(array('bookid' => $book->id, "pagenum" => 3, "subchapter" => 1));
67         $chapter3 = $bookgenerator->create_chapter(array('bookid' => $book->id, "pagenum" => 4, "hidden" => 1));
69         $this->setUser($user);
71         $contents = book_export_contents($cm, '');
72         // The hidden chapter must not be included, and additional page with the structure must be included.
73         $this->assertCount(4, $contents);
75         $this->assertEquals('structure', $contents[0]['filename']);
76         $this->assertEquals('index.html', $contents[1]['filename']);
77         $this->assertEquals('Chapter 1', $contents[1]['content']);
78         $this->assertCount(2, $contents[1]['tags']);
79         $this->assertEquals('Cats', $contents[1]['tags'][0]['rawname']);
80         $this->assertEquals($tag->id, $contents[1]['tags'][0]['id']);
81         $this->assertEquals('Dogs', $contents[1]['tags'][1]['rawname']);
82         $this->assertEquals('index.html', $contents[2]['filename']);
83         $this->assertEquals('Chapter 2', $contents[2]['content']);
84         $this->assertEquals('index.html', $contents[3]['filename']);
85         $this->assertEquals('Chapter 3', $contents[3]['content']);
87         // Now, test the function via the external API.
88         $contents = core_course_external::get_course_contents($course->id, array());
89         $contents = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $contents);
90         $this->assertEquals('book', $contents[0]['modules'][0]['modname']);
91         $this->assertEquals($cm->id, $contents[0]['modules'][0]['id']);
92         $this->assertCount(2, $contents[0]['modules'][0]['contents'][1]['tags']);
93         $this->assertEquals('Cats', $contents[0]['modules'][0]['contents'][1]['tags'][0]['rawname']);
94         $this->assertEquals('Dogs', $contents[0]['modules'][0]['contents'][1]['tags'][1]['rawname']);
96         // Test empty book.
97         $emptybook = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
98         $cm = get_coursemodule_from_id('book', $emptybook->cmid);
99         $contents = book_export_contents($cm, '');
101         $this->assertCount(1, $contents);
102         $this->assertEquals('structure', $contents[0]['filename']);
103         $this->assertEquals(json_encode(array()), $contents[0]['content']);
105     }
107     /**
108      * Test book_view
109      * @return void
110      */
111     public function test_book_view() {
112         global $CFG, $DB;
114         $CFG->enablecompletion = 1;
116         // Setup test data.
117         $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
118         $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id),
119                                                             array('completion' => 2, 'completionview' => 1));
120         $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book');
121         $chapter = $bookgenerator->create_chapter(array('bookid' => $book->id));
123         $context = context_module::instance($book->cmid);
124         $cm = get_coursemodule_from_instance('book', $book->id);
126         // Trigger and capture the event.
127         $sink = $this->redirectEvents();
129         // Check just opening the book.
130         book_view($book, 0, false, $course, $cm, $context);
132         $events = $sink->get_events();
133         $this->assertCount(1, $events);
134         $event = array_shift($events);
136         // Checking that the event contains the expected values.
137         $this->assertInstanceOf('\mod_book\event\course_module_viewed', $event);
138         $this->assertEquals($context, $event->get_context());
139         $moodleurl = new \moodle_url('/mod/book/view.php', array('id' => $cm->id));
140         $this->assertEquals($moodleurl, $event->get_url());
141         $this->assertEventContextNotUsed($event);
142         $this->assertNotEmpty($event->get_name());
144         // Check viewing one book chapter (the only one so it will be the first and last).
145         book_view($book, $chapter, true, $course, $cm, $context);
147         $events = $sink->get_events();
148         // We expect a total of 4 events. One for module viewed, one for chapter viewed and two belonging to completion.
149         $this->assertCount(4, $events);
151         // Check completion status.
152         $completion = new completion_info($course);
153         $completiondata = $completion->get_data($cm);
154         $this->assertEquals(1, $completiondata->completionstate);
155     }
157     public function test_book_core_calendar_provide_event_action() {
158         // Create the activity.
159         $course = $this->getDataGenerator()->create_course();
160         $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
162         // Create a calendar event.
163         $event = $this->create_action_event($course->id, $book->id,
164             \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
166         // Create an action factory.
167         $factory = new \core_calendar\action_factory();
169         // Decorate action event.
170         $actionevent = mod_book_core_calendar_provide_event_action($event, $factory);
172         // Confirm the event was decorated.
173         $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
174         $this->assertEquals(get_string('view'), $actionevent->get_name());
175         $this->assertInstanceOf('moodle_url', $actionevent->get_url());
176         $this->assertEquals(1, $actionevent->get_item_count());
177         $this->assertTrue($actionevent->is_actionable());
178     }
180     public function test_book_core_calendar_provide_event_action_in_hidden_section() {
181         // Create the activity.
182         $course = $this->getDataGenerator()->create_course();
183         $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
185         // Enrol a student in the course.
186         $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
188         // Create a calendar event.
189         $event = $this->create_action_event($course->id, $book->id,
190                 \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
192         // Set sections 0 as hidden.
193         set_section_visible($course->id, 0, 0);
195         // Now, log out.
196         $this->setUser();
198         // Create an action factory.
199         $factory = new \core_calendar\action_factory();
201         // Decorate action event for the student.
202         $actionevent = mod_book_core_calendar_provide_event_action($event, $factory, $student->id);
204         // Confirm the event is not shown at all.
205         $this->assertNull($actionevent);
206     }
208     public function test_book_core_calendar_provide_event_action_for_user() {
209         // Create the activity.
210         $course = $this->getDataGenerator()->create_course();
211         $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
213         // Enrol a student in the course.
214         $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
216         // Create a calendar event.
217         $event = $this->create_action_event($course->id, $book->id,
218             \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
220         // Now, log out.
221         $this->setUser();
223         // Create an action factory.
224         $factory = new \core_calendar\action_factory();
226         // Decorate action event for the student.
227         $actionevent = mod_book_core_calendar_provide_event_action($event, $factory, $student->id);
229         // Confirm the event was decorated.
230         $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
231         $this->assertEquals(get_string('view'), $actionevent->get_name());
232         $this->assertInstanceOf('moodle_url', $actionevent->get_url());
233         $this->assertEquals(1, $actionevent->get_item_count());
234         $this->assertTrue($actionevent->is_actionable());
235     }
237     public function test_book_core_calendar_provide_event_action_as_non_user() {
238         global $CFG;
240         // Create the activity.
241         $course = $this->getDataGenerator()->create_course();
242         $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
244         // Create a calendar event.
245         $event = $this->create_action_event($course->id, $book->id,
246             \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
248         // Log out the user and set force login to true.
249         \core\session\manager::init_empty_session();
250         $CFG->forcelogin = true;
252         // Create an action factory.
253         $factory = new \core_calendar\action_factory();
255         // Decorate action event.
256         $actionevent = mod_book_core_calendar_provide_event_action($event, $factory);
258         // Ensure result was null.
259         $this->assertNull($actionevent);
260     }
262     public function test_book_core_calendar_provide_event_action_already_completed() {
263         global $CFG;
265         $CFG->enablecompletion = 1;
267         // Create the activity.
268         $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
269         $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id),
270             array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
272         // Get some additional data.
273         $cm = get_coursemodule_from_instance('book', $book->id);
275         // Create a calendar event.
276         $event = $this->create_action_event($course->id, $book->id,
277             \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
279         // Mark the activity as completed.
280         $completion = new completion_info($course);
281         $completion->set_module_viewed($cm);
283         // Create an action factory.
284         $factory = new \core_calendar\action_factory();
286         // Decorate action event.
287         $actionevent = mod_book_core_calendar_provide_event_action($event, $factory);
289         // Ensure result was null.
290         $this->assertNull($actionevent);
291     }
293     public function test_book_core_calendar_provide_event_action_already_completed_for_user() {
294         global $CFG;
296         $CFG->enablecompletion = 1;
298         // Create the activity.
299         $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
300         $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id),
301             array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
303         // Enrol a student in the course.
304         $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
306         // Get some additional data.
307         $cm = get_coursemodule_from_instance('book', $book->id);
309         // Create a calendar event.
310         $event = $this->create_action_event($course->id, $book->id,
311             \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
313         // Mark the activity as completed for the student.
314         $completion = new completion_info($course);
315         $completion->set_module_viewed($cm, $student->id);
317         // Create an action factory.
318         $factory = new \core_calendar\action_factory();
320         // Decorate action event for the student.
321         $actionevent = mod_book_core_calendar_provide_event_action($event, $factory, $student->id);
323         // Ensure result was null.
324         $this->assertNull($actionevent);
325     }
327     /**
328      * Creates an action event.
329      *
330      * @param int $courseid The course id.
331      * @param int $instanceid The instance id.
332      * @param string $eventtype The event type.
333      * @return bool|calendar_event
334      */
335     private function create_action_event($courseid, $instanceid, $eventtype) {
336         $event = new stdClass();
337         $event->name = 'Calendar event';
338         $event->modulename  = 'book';
339         $event->courseid = $courseid;
340         $event->instance = $instanceid;
341         $event->type = CALENDAR_EVENT_TYPE_ACTION;
342         $event->eventtype = $eventtype;
343         $event->timestart = time();
345         return calendar_event::create($event);
346     }
348     public function test_mod_book_get_tagged_chapters() {
349         global $DB;
351         $this->resetAfterTest();
352         $this->setAdminUser();
354         // Setup test data.
355         $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book');
356         $course3 = $this->getDataGenerator()->create_course();
357         $course2 = $this->getDataGenerator()->create_course();
358         $course1 = $this->getDataGenerator()->create_course();
359         $book1 = $this->getDataGenerator()->create_module('book', array('course' => $course1->id));
360         $book2 = $this->getDataGenerator()->create_module('book', array('course' => $course2->id));
361         $book3 = $this->getDataGenerator()->create_module('book', array('course' => $course3->id));
362         $chapter11 = $bookgenerator->create_content($book1, array('tags' => array('Cats', 'Dogs')));
363         $chapter12 = $bookgenerator->create_content($book1, array('tags' => array('Cats', 'mice')));
364         $chapter13 = $bookgenerator->create_content($book1, array('tags' => array('Cats')));
365         $chapter14 = $bookgenerator->create_content($book1);
366         $chapter15 = $bookgenerator->create_content($book1, array('tags' => array('Cats')));
367         $chapter16 = $bookgenerator->create_content($book1, array('tags' => array('Cats'), 'hidden' => true));
368         $chapter21 = $bookgenerator->create_content($book2, array('tags' => array('Cats')));
369         $chapter22 = $bookgenerator->create_content($book2, array('tags' => array('Cats', 'Dogs')));
370         $chapter23 = $bookgenerator->create_content($book2, array('tags' => array('mice', 'Cats')));
371         $chapter31 = $bookgenerator->create_content($book3, array('tags' => array('mice', 'Cats')));
373         $tag = core_tag_tag::get_by_name(0, 'Cats');
375         // Admin can see everything.
376         $res = mod_book_get_tagged_chapters($tag, /*$exclusivemode = */false,
377             /*$fromctx = */0, /*$ctx = */0, /*$rec = */1, /*$chapter = */0);
378         $this->assertRegExp('/'.$chapter11->title.'</', $res->content);
379         $this->assertRegExp('/'.$chapter12->title.'</', $res->content);
380         $this->assertRegExp('/'.$chapter13->title.'</', $res->content);
381         $this->assertNotRegExp('/'.$chapter14->title.'</', $res->content);
382         $this->assertRegExp('/'.$chapter15->title.'</', $res->content);
383         $this->assertRegExp('/'.$chapter16->title.'</', $res->content);
384         $this->assertNotRegExp('/'.$chapter21->title.'</', $res->content);
385         $this->assertNotRegExp('/'.$chapter22->title.'</', $res->content);
386         $this->assertNotRegExp('/'.$chapter23->title.'</', $res->content);
387         $this->assertNotRegExp('/'.$chapter31->title.'</', $res->content);
388         $this->assertEmpty($res->prevpageurl);
389         $this->assertNotEmpty($res->nextpageurl);
390         $res = mod_book_get_tagged_chapters($tag, /*$exclusivemode = */false,
391             /*$fromctx = */0, /*$ctx = */0, /*$rec = */1, /*$chapter = */1);
392         $this->assertNotRegExp('/'.$chapter11->title.'</', $res->content);
393         $this->assertNotRegExp('/'.$chapter12->title.'</', $res->content);
394         $this->assertNotRegExp('/'.$chapter13->title.'</', $res->content);
395         $this->assertNotRegExp('/'.$chapter14->title.'</', $res->content);
396         $this->assertNotRegExp('/'.$chapter15->title.'</', $res->content);
397         $this->assertNotRegExp('/'.$chapter16->title.'</', $res->content);
398         $this->assertRegExp('/'.$chapter21->title.'</', $res->content);
399         $this->assertRegExp('/'.$chapter22->title.'</', $res->content);
400         $this->assertRegExp('/'.$chapter23->title.'</', $res->content);
401         $this->assertRegExp('/'.$chapter31->title.'</', $res->content);
402         $this->assertNotEmpty($res->prevpageurl);
403         $this->assertEmpty($res->nextpageurl);
405         // Create and enrol a user.
406         $student = self::getDataGenerator()->create_user();
407         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
408         $this->getDataGenerator()->enrol_user($student->id, $course1->id, $studentrole->id, 'manual');
409         $this->getDataGenerator()->enrol_user($student->id, $course2->id, $studentrole->id, 'manual');
410         $this->setUser($student);
411         core_tag_index_builder::reset_caches();
413         // User can not see chapters in course 3 because he is not enrolled.
414         $res = mod_book_get_tagged_chapters($tag, /*$exclusivemode = */false,
415             /*$fromctx = */0, /*$ctx = */0, /*$rec = */1, /*$chapter = */1);
416         $this->assertRegExp('/'.$chapter22->title.'/', $res->content);
417         $this->assertRegExp('/'.$chapter23->title.'/', $res->content);
418         $this->assertNotRegExp('/'.$chapter31->title.'/', $res->content);
420         // User can search book chapters inside a course.
421         $coursecontext = context_course::instance($course1->id);
422         $res = mod_book_get_tagged_chapters($tag, /*$exclusivemode = */false,
423             /*$fromctx = */0, /*$ctx = */$coursecontext->id, /*$rec = */1, /*$chapter = */0);
424         $this->assertRegExp('/'.$chapter11->title.'/', $res->content);
425         $this->assertRegExp('/'.$chapter12->title.'/', $res->content);
426         $this->assertRegExp('/'.$chapter13->title.'/', $res->content);
427         $this->assertNotRegExp('/'.$chapter14->title.'/', $res->content);
428         $this->assertRegExp('/'.$chapter15->title.'/', $res->content);
429         $this->assertNotRegExp('/'.$chapter21->title.'/', $res->content);
430         $this->assertNotRegExp('/'.$chapter22->title.'/', $res->content);
431         $this->assertNotRegExp('/'.$chapter23->title.'/', $res->content);
432         $this->assertEmpty($res->nextpageurl);
434         // User cannot see hidden chapters.
435         $this->assertNotRegExp('/'.$chapter16->title.'/', $res->content);
436     }