Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * 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
 */
25
namespace mod_book;
26
 
27
use core_external\external_api;
28
 
29
defined('MOODLE_INTERNAL') || die();
30
 
31
global $CFG;
32
require_once($CFG->dirroot . '/mod/book/lib.php');
33
 
34
/**
35
 * Unit tests for (some of) mod/book/lib.php.
36
 *
37
 * @package    mod_book
38
 * @category   phpunit
39
 * @copyright  2015 Juan Leyva <juan@moodle.com>
40
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41
 */
1441 ariadna 42
final class lib_test extends \advanced_testcase {
1 efrain 43
 
44
    public function setUp(): void {
1441 ariadna 45
        parent::setUp();
1 efrain 46
        $this->resetAfterTest();
47
        $this->setAdminUser();
48
    }
49
 
11 efrain 50
    public function test_export_contents(): void {
1 efrain 51
        global $DB, $CFG;
52
        require_once($CFG->dirroot . '/course/externallib.php');
53
 
54
        $user = $this->getDataGenerator()->create_user();
55
        $teacher = $this->getDataGenerator()->create_user();
56
        $course = $this->getDataGenerator()->create_course(array('enablecomment' => 1));
57
        $studentrole = $DB->get_record('role', array('shortname' => 'student'));
58
        $teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
59
 
60
        $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
61
        $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id);
62
 
63
        // Test book with 3 chapters.
64
        $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
65
        $cm = get_coursemodule_from_id('book', $book->cmid);
66
 
67
        $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book');
68
        $chapter1 = $bookgenerator->create_chapter(array('bookid' => $book->id, "pagenum" => 1,
69
            'tags' => array('Cats', 'Dogs')));
70
        $tag = \core_tag_tag::get_by_name(0, 'Cats');
71
 
72
        $chapter2 = $bookgenerator->create_chapter(array('bookid' => $book->id, "pagenum" => 2));
73
        $subchapter = $bookgenerator->create_chapter(array('bookid' => $book->id, "pagenum" => 3, "subchapter" => 1));
74
        $chapter3 = $bookgenerator->create_chapter(array('bookid' => $book->id, "pagenum" => 4, "hidden" => 1));
75
 
76
        $this->setUser($user);
77
 
78
        $contents = book_export_contents($cm, '');
79
        // The hidden chapter must not be included, and additional page with the structure must be included.
80
        $this->assertCount(4, $contents);
81
 
82
        $this->assertEquals('structure', $contents[0]['filename']);
83
        $this->assertEquals('index.html', $contents[1]['filename']);
84
        $this->assertEquals('Chapter 1', $contents[1]['content']);
85
        $this->assertCount(2, $contents[1]['tags']);
86
        $this->assertEquals('Cats', $contents[1]['tags'][0]['rawname']);
87
        $this->assertEquals($tag->id, $contents[1]['tags'][0]['id']);
88
        $this->assertEquals('Dogs', $contents[1]['tags'][1]['rawname']);
89
        $this->assertEquals('index.html', $contents[2]['filename']);
90
        $this->assertEquals('Chapter 2', $contents[2]['content']);
91
        $this->assertEquals('index.html', $contents[3]['filename']);
92
        $this->assertEquals('Chapter 3', $contents[3]['content']);
93
 
94
        // Now, test the function via the external API.
95
        $contents = \core_course_external::get_course_contents($course->id, array());
96
        $contents = external_api::clean_returnvalue(\core_course_external::get_course_contents_returns(), $contents);
97
 
98
        $this->assertCount(4, $contents[0]['modules'][0]['contents']);
99
 
100
        $this->assertEquals('content', $contents[0]['modules'][0]['contents'][0]['type']);
101
        $this->assertEquals('structure', $contents[0]['modules'][0]['contents'][0]['filename']);
102
 
103
        $this->assertEquals('file', $contents[0]['modules'][0]['contents'][1]['type']);
104
        $this->assertEquals('Chapter 1', $contents[0]['modules'][0]['contents'][1]['content']);
105
 
106
        $this->assertEquals('file', $contents[0]['modules'][0]['contents'][2]['type']);
107
        $this->assertEquals('Chapter 2', $contents[0]['modules'][0]['contents'][2]['content']);
108
 
109
        $this->assertEquals('file', $contents[0]['modules'][0]['contents'][3]['type']);
110
        $this->assertEquals('Chapter 3', $contents[0]['modules'][0]['contents'][3]['content']);
111
 
112
        $this->assertEquals('book', $contents[0]['modules'][0]['modname']);
113
        $this->assertEquals($cm->id, $contents[0]['modules'][0]['id']);
114
        $this->assertCount(2, $contents[0]['modules'][0]['contents'][1]['tags']);
115
        $this->assertEquals('Cats', $contents[0]['modules'][0]['contents'][1]['tags'][0]['rawname']);
116
        $this->assertEquals('Dogs', $contents[0]['modules'][0]['contents'][1]['tags'][1]['rawname']);
117
 
118
        // As a teacher.
119
        $this->setUser($teacher);
120
 
121
        $contents = book_export_contents($cm, '');
122
        // As a teacher, the hidden chapter must be included in the structure.
123
        $this->assertCount(5, $contents);
124
 
125
        $this->assertEquals('structure', $contents[0]['filename']);
126
        // Check structure is correct.
127
        $foundhiddenchapter = false;
128
        $chapters = json_decode($contents[0]['content']);
129
        foreach ($chapters as $chapter) {
130
            if ($chapter->title == 'Chapter 4' && $chapter->hidden == 1) {
131
                $foundhiddenchapter = true;
132
            }
133
        }
134
        $this->assertTrue($foundhiddenchapter);
135
 
136
        $this->assertEquals('index.html', $contents[1]['filename']);
137
        $this->assertEquals('Chapter 1', $contents[1]['content']);
138
        $this->assertCount(2, $contents[1]['tags']);
139
        $this->assertEquals('Cats', $contents[1]['tags'][0]['rawname']);
140
        $this->assertEquals($tag->id, $contents[1]['tags'][0]['id']);
141
        $this->assertEquals('Dogs', $contents[1]['tags'][1]['rawname']);
142
        $this->assertEquals('index.html', $contents[2]['filename']);
143
        $this->assertEquals('Chapter 2', $contents[2]['content']);
144
        $this->assertEquals('index.html', $contents[3]['filename']);
145
        $this->assertEquals('Chapter 3', $contents[3]['content']);
146
        $this->assertEquals('index.html', $contents[4]['filename']);
147
        $this->assertEquals('Chapter 4', $contents[4]['content']);
148
 
149
        // Now, test the function via the external API.
150
        $contents = \core_course_external::get_course_contents($course->id, array());
151
        $contents = external_api::clean_returnvalue(\core_course_external::get_course_contents_returns(), $contents);
152
 
153
        $this->assertCount(5, $contents[0]['modules'][0]['contents']);
154
 
155
        $this->assertEquals('content', $contents[0]['modules'][0]['contents'][0]['type']);
156
        $this->assertEquals('structure', $contents[0]['modules'][0]['contents'][0]['filename']);
157
        // Check structure is correct.
158
        $foundhiddenchapter = false;
159
        $chapters = json_decode($contents[0]['modules'][0]['contents'][0]['content']);
160
        foreach ($chapters as $chapter) {
161
            if ($chapter->title == 'Chapter 4' && $chapter->hidden == 1) {
162
                $foundhiddenchapter = true;
163
            }
164
        }
165
        $this->assertTrue($foundhiddenchapter);
166
 
167
        $this->assertEquals('file', $contents[0]['modules'][0]['contents'][1]['type']);
168
        $this->assertEquals('Chapter 1', $contents[0]['modules'][0]['contents'][1]['content']);
169
 
170
        $this->assertEquals('file', $contents[0]['modules'][0]['contents'][2]['type']);
171
        $this->assertEquals('Chapter 2', $contents[0]['modules'][0]['contents'][2]['content']);
172
 
173
        $this->assertEquals('file', $contents[0]['modules'][0]['contents'][3]['type']);
174
        $this->assertEquals('Chapter 3', $contents[0]['modules'][0]['contents'][3]['content']);
175
 
176
        $this->assertEquals('file', $contents[0]['modules'][0]['contents'][4]['type']);
177
        $this->assertEquals('Chapter 4', $contents[0]['modules'][0]['contents'][4]['content']);
178
 
179
        $this->assertEquals('book', $contents[0]['modules'][0]['modname']);
180
        $this->assertEquals($cm->id, $contents[0]['modules'][0]['id']);
181
        $this->assertCount(2, $contents[0]['modules'][0]['contents'][1]['tags']);
182
        $this->assertEquals('Cats', $contents[0]['modules'][0]['contents'][1]['tags'][0]['rawname']);
183
        $this->assertEquals('Dogs', $contents[0]['modules'][0]['contents'][1]['tags'][1]['rawname']);
184
 
185
        // Test empty book.
186
        $emptybook = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
187
        $cm = get_coursemodule_from_id('book', $emptybook->cmid);
188
        $contents = book_export_contents($cm, '');
189
 
190
        $this->assertCount(1, $contents);
191
        $this->assertEquals('structure', $contents[0]['filename']);
192
        $this->assertEquals(json_encode(array()), $contents[0]['content']);
193
 
194
    }
195
 
196
    /**
197
     * Test book_view
198
     * @return void
199
     */
11 efrain 200
    public function test_book_view(): void {
1 efrain 201
        global $CFG, $DB;
202
 
203
        $CFG->enablecompletion = 1;
204
 
205
        // Setup test data.
206
        $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
207
        $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id),
208
                                                            array('completion' => 2, 'completionview' => 1));
209
        $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book');
210
        $chapter = $bookgenerator->create_chapter(array('bookid' => $book->id));
211
 
212
        $context = \context_module::instance($book->cmid);
213
        $cm = get_coursemodule_from_instance('book', $book->id);
214
 
215
        // Trigger and capture the event.
216
        $sink = $this->redirectEvents();
217
 
218
        // Check just opening the book.
219
        book_view($book, 0, false, $course, $cm, $context);
220
 
221
        $events = $sink->get_events();
222
        $this->assertCount(1, $events);
223
        $event = array_shift($events);
224
 
225
        // Checking that the event contains the expected values.
226
        $this->assertInstanceOf('\mod_book\event\course_module_viewed', $event);
227
        $this->assertEquals($context, $event->get_context());
228
        $moodleurl = new \moodle_url('/mod/book/view.php', array('id' => $cm->id));
229
        $this->assertEquals($moodleurl, $event->get_url());
230
        $this->assertEventContextNotUsed($event);
231
        $this->assertNotEmpty($event->get_name());
232
 
233
        // Check viewing one book chapter (the only one so it will be the first and last).
234
        book_view($book, $chapter, true, $course, $cm, $context);
235
 
236
        $events = $sink->get_events();
237
        // We expect a total of 4 events. One for module viewed, one for chapter viewed and two belonging to completion.
238
        $this->assertCount(4, $events);
239
 
240
        // Check completion status.
241
        $completion = new \completion_info($course);
242
        $completiondata = $completion->get_data($cm);
243
        $this->assertEquals(1, $completiondata->completionstate);
244
    }
245
 
11 efrain 246
    public function test_book_core_calendar_provide_event_action(): void {
1 efrain 247
        // Create the activity.
248
        $course = $this->getDataGenerator()->create_course();
249
        $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
250
 
251
        // Create a calendar event.
252
        $event = $this->create_action_event($course->id, $book->id,
253
            \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
254
 
255
        // Create an action factory.
256
        $factory = new \core_calendar\action_factory();
257
 
258
        // Decorate action event.
259
        $actionevent = mod_book_core_calendar_provide_event_action($event, $factory);
260
 
261
        // Confirm the event was decorated.
262
        $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
263
        $this->assertEquals(get_string('view'), $actionevent->get_name());
264
        $this->assertInstanceOf('moodle_url', $actionevent->get_url());
265
        $this->assertEquals(1, $actionevent->get_item_count());
266
        $this->assertTrue($actionevent->is_actionable());
267
    }
268
 
11 efrain 269
    public function test_book_core_calendar_provide_event_action_in_hidden_section(): void {
1 efrain 270
        // Create the activity.
271
        $course = $this->getDataGenerator()->create_course();
272
        $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
273
 
274
        // Enrol a student in the course.
275
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
276
 
277
        // Create a calendar event.
278
        $event = $this->create_action_event($course->id, $book->id,
279
                \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
280
 
281
        // Set sections 0 as hidden.
282
        set_section_visible($course->id, 0, 0);
283
 
284
        // Now, log out.
285
        $this->setUser();
286
 
287
        // Create an action factory.
288
        $factory = new \core_calendar\action_factory();
289
 
290
        // Decorate action event for the student.
291
        $actionevent = mod_book_core_calendar_provide_event_action($event, $factory, $student->id);
292
 
293
        // Confirm the event is not shown at all.
294
        $this->assertNull($actionevent);
295
    }
296
 
11 efrain 297
    public function test_book_core_calendar_provide_event_action_for_user(): void {
1 efrain 298
        // Create the activity.
299
        $course = $this->getDataGenerator()->create_course();
300
        $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
301
 
302
        // Enrol a student in the course.
303
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
304
 
305
        // Create a calendar event.
306
        $event = $this->create_action_event($course->id, $book->id,
307
            \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
308
 
309
        // Now, log out.
310
        $this->setUser();
311
 
312
        // Create an action factory.
313
        $factory = new \core_calendar\action_factory();
314
 
315
        // Decorate action event for the student.
316
        $actionevent = mod_book_core_calendar_provide_event_action($event, $factory, $student->id);
317
 
318
        // Confirm the event was decorated.
319
        $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
320
        $this->assertEquals(get_string('view'), $actionevent->get_name());
321
        $this->assertInstanceOf('moodle_url', $actionevent->get_url());
322
        $this->assertEquals(1, $actionevent->get_item_count());
323
        $this->assertTrue($actionevent->is_actionable());
324
    }
325
 
11 efrain 326
    public function test_book_core_calendar_provide_event_action_as_non_user(): void {
1 efrain 327
        global $CFG;
328
 
329
        // Create the activity.
330
        $course = $this->getDataGenerator()->create_course();
331
        $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
332
 
333
        // Create a calendar event.
334
        $event = $this->create_action_event($course->id, $book->id,
335
            \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
336
 
337
        // Log out the user and set force login to true.
338
        \core\session\manager::init_empty_session();
339
        $CFG->forcelogin = true;
340
 
341
        // Create an action factory.
342
        $factory = new \core_calendar\action_factory();
343
 
344
        // Decorate action event.
345
        $actionevent = mod_book_core_calendar_provide_event_action($event, $factory);
346
 
347
        // Ensure result was null.
348
        $this->assertNull($actionevent);
349
    }
350
 
11 efrain 351
    public function test_book_core_calendar_provide_event_action_already_completed(): void {
1 efrain 352
        global $CFG;
353
 
354
        $CFG->enablecompletion = 1;
355
 
356
        // Create the activity.
357
        $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
358
        $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id),
359
            array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
360
 
361
        // Get some additional data.
362
        $cm = get_coursemodule_from_instance('book', $book->id);
363
 
364
        // Create a calendar event.
365
        $event = $this->create_action_event($course->id, $book->id,
366
            \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
367
 
368
        // Mark the activity as completed.
369
        $completion = new \completion_info($course);
370
        $completion->set_module_viewed($cm);
371
 
372
        // Create an action factory.
373
        $factory = new \core_calendar\action_factory();
374
 
375
        // Decorate action event.
376
        $actionevent = mod_book_core_calendar_provide_event_action($event, $factory);
377
 
378
        // Ensure result was null.
379
        $this->assertNull($actionevent);
380
    }
381
 
11 efrain 382
    public function test_book_core_calendar_provide_event_action_already_completed_for_user(): void {
1 efrain 383
        global $CFG;
384
 
385
        $CFG->enablecompletion = 1;
386
 
387
        // Create the activity.
388
        $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
389
        $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id),
390
            array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
391
 
392
        // Enrol a student in the course.
393
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
394
 
395
        // Get some additional data.
396
        $cm = get_coursemodule_from_instance('book', $book->id);
397
 
398
        // Create a calendar event.
399
        $event = $this->create_action_event($course->id, $book->id,
400
            \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
401
 
402
        // Mark the activity as completed for the student.
403
        $completion = new \completion_info($course);
404
        $completion->set_module_viewed($cm, $student->id);
405
 
406
        // Create an action factory.
407
        $factory = new \core_calendar\action_factory();
408
 
409
        // Decorate action event for the student.
410
        $actionevent = mod_book_core_calendar_provide_event_action($event, $factory, $student->id);
411
 
412
        // Ensure result was null.
413
        $this->assertNull($actionevent);
414
    }
415
 
416
    /**
417
     * Creates an action event.
418
     *
419
     * @param int $courseid The course id.
420
     * @param int $instanceid The instance id.
421
     * @param string $eventtype The event type.
422
     * @return bool|calendar_event
423
     */
424
    private function create_action_event($courseid, $instanceid, $eventtype) {
425
        $event = new \stdClass();
426
        $event->name = 'Calendar event';
427
        $event->modulename  = 'book';
428
        $event->courseid = $courseid;
429
        $event->instance = $instanceid;
430
        $event->type = CALENDAR_EVENT_TYPE_ACTION;
431
        $event->eventtype = $eventtype;
432
        $event->timestart = time();
433
 
434
        return \calendar_event::create($event);
435
    }
436
 
11 efrain 437
    public function test_mod_book_get_tagged_chapters(): void {
1 efrain 438
        global $DB;
439
 
440
        $this->resetAfterTest();
441
        $this->setAdminUser();
442
 
443
        // Setup test data.
444
        $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book');
445
        $course3 = $this->getDataGenerator()->create_course();
446
        $course2 = $this->getDataGenerator()->create_course();
447
        $course1 = $this->getDataGenerator()->create_course();
448
        $book1 = $this->getDataGenerator()->create_module('book', array('course' => $course1->id));
449
        $book2 = $this->getDataGenerator()->create_module('book', array('course' => $course2->id));
450
        $book3 = $this->getDataGenerator()->create_module('book', array('course' => $course3->id));
451
        $chapter11 = $bookgenerator->create_content($book1, array('tags' => array('Cats', 'Dogs')));
452
        $chapter12 = $bookgenerator->create_content($book1, array('tags' => array('Cats', 'mice')));
453
        $chapter13 = $bookgenerator->create_content($book1, array('tags' => array('Cats')));
454
        $chapter14 = $bookgenerator->create_content($book1);
455
        $chapter15 = $bookgenerator->create_content($book1, array('tags' => array('Cats')));
456
        $chapter16 = $bookgenerator->create_content($book1, array('tags' => array('Cats'), 'hidden' => true));
457
        $chapter21 = $bookgenerator->create_content($book2, array('tags' => array('Cats')));
458
        $chapter22 = $bookgenerator->create_content($book2, array('tags' => array('Cats', 'Dogs')));
459
        $chapter23 = $bookgenerator->create_content($book2, array('tags' => array('mice', 'Cats')));
460
        $chapter31 = $bookgenerator->create_content($book3, array('tags' => array('mice', 'Cats')));
461
 
462
        $tag = \core_tag_tag::get_by_name(0, 'Cats');
463
 
464
        // Admin can see everything.
465
        $res = mod_book_get_tagged_chapters($tag, /*$exclusivemode = */false,
466
            /*$fromctx = */0, /*$ctx = */0, /*$rec = */1, /*$chapter = */0);
467
        $this->assertMatchesRegularExpression('/'.$chapter11->title.'</', $res->content);
468
        $this->assertMatchesRegularExpression('/'.$chapter12->title.'</', $res->content);
469
        $this->assertMatchesRegularExpression('/'.$chapter13->title.'</', $res->content);
470
        $this->assertDoesNotMatchRegularExpression('/'.$chapter14->title.'</', $res->content);
471
        $this->assertMatchesRegularExpression('/'.$chapter15->title.'</', $res->content);
472
        $this->assertMatchesRegularExpression('/'.$chapter16->title.'</', $res->content);
473
        $this->assertDoesNotMatchRegularExpression('/'.$chapter21->title.'</', $res->content);
474
        $this->assertDoesNotMatchRegularExpression('/'.$chapter22->title.'</', $res->content);
475
        $this->assertDoesNotMatchRegularExpression('/'.$chapter23->title.'</', $res->content);
476
        $this->assertDoesNotMatchRegularExpression('/'.$chapter31->title.'</', $res->content);
477
        $this->assertEmpty($res->prevpageurl);
478
        $this->assertNotEmpty($res->nextpageurl);
479
        $res = mod_book_get_tagged_chapters($tag, /*$exclusivemode = */false,
480
            /*$fromctx = */0, /*$ctx = */0, /*$rec = */1, /*$chapter = */1);
481
        $this->assertDoesNotMatchRegularExpression('/'.$chapter11->title.'</', $res->content);
482
        $this->assertDoesNotMatchRegularExpression('/'.$chapter12->title.'</', $res->content);
483
        $this->assertDoesNotMatchRegularExpression('/'.$chapter13->title.'</', $res->content);
484
        $this->assertDoesNotMatchRegularExpression('/'.$chapter14->title.'</', $res->content);
485
        $this->assertDoesNotMatchRegularExpression('/'.$chapter15->title.'</', $res->content);
486
        $this->assertDoesNotMatchRegularExpression('/'.$chapter16->title.'</', $res->content);
487
        $this->assertMatchesRegularExpression('/'.$chapter21->title.'</', $res->content);
488
        $this->assertMatchesRegularExpression('/'.$chapter22->title.'</', $res->content);
489
        $this->assertMatchesRegularExpression('/'.$chapter23->title.'</', $res->content);
490
        $this->assertMatchesRegularExpression('/'.$chapter31->title.'</', $res->content);
491
        $this->assertNotEmpty($res->prevpageurl);
492
        $this->assertEmpty($res->nextpageurl);
493
 
494
        // Create and enrol a user.
495
        $student = self::getDataGenerator()->create_user();
496
        $studentrole = $DB->get_record('role', array('shortname' => 'student'));
497
        $this->getDataGenerator()->enrol_user($student->id, $course1->id, $studentrole->id, 'manual');
498
        $this->getDataGenerator()->enrol_user($student->id, $course2->id, $studentrole->id, 'manual');
499
        $this->setUser($student);
500
        \core_tag_index_builder::reset_caches();
501
 
502
        // User can not see chapters in course 3 because he is not enrolled.
503
        $res = mod_book_get_tagged_chapters($tag, /*$exclusivemode = */false,
504
            /*$fromctx = */0, /*$ctx = */0, /*$rec = */1, /*$chapter = */1);
505
        $this->assertMatchesRegularExpression('/'.$chapter22->title.'/', $res->content);
506
        $this->assertMatchesRegularExpression('/'.$chapter23->title.'/', $res->content);
507
        $this->assertDoesNotMatchRegularExpression('/'.$chapter31->title.'/', $res->content);
508
 
509
        // User can search book chapters inside a course.
510
        $coursecontext = \context_course::instance($course1->id);
511
        $res = mod_book_get_tagged_chapters($tag, /*$exclusivemode = */false,
512
            /*$fromctx = */0, /*$ctx = */$coursecontext->id, /*$rec = */1, /*$chapter = */0);
513
        $this->assertMatchesRegularExpression('/'.$chapter11->title.'/', $res->content);
514
        $this->assertMatchesRegularExpression('/'.$chapter12->title.'/', $res->content);
515
        $this->assertMatchesRegularExpression('/'.$chapter13->title.'/', $res->content);
516
        $this->assertDoesNotMatchRegularExpression('/'.$chapter14->title.'/', $res->content);
517
        $this->assertMatchesRegularExpression('/'.$chapter15->title.'/', $res->content);
518
        $this->assertDoesNotMatchRegularExpression('/'.$chapter21->title.'/', $res->content);
519
        $this->assertDoesNotMatchRegularExpression('/'.$chapter22->title.'/', $res->content);
520
        $this->assertDoesNotMatchRegularExpression('/'.$chapter23->title.'/', $res->content);
521
        $this->assertEmpty($res->nextpageurl);
522
 
523
        // User cannot see hidden chapters.
524
        $this->assertDoesNotMatchRegularExpression('/'.$chapter16->title.'/', $res->content);
525
    }
526
}