MDL-50015 mod_imscp: New WS mod_imscp_view_imscp
[moodle.git] / mod / book / tests / lib_test.php
CommitLineData
e6d63934
JL
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
26
27defined('MOODLE_INTERNAL') || die();
28
29global $CFG;
30require_once($CFG->dirroot . '/mod/book/lib.php');
31
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 */
40class mod_book_lib_testcase extends advanced_testcase {
41
42 public function test_export_contents() {
43 global $DB;
44
45 $this->resetAfterTest(true);
46
47 $user = $this->getDataGenerator()->create_user();
48 $course = $this->getDataGenerator()->create_course(array('enablecomment' => 1));
49 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
50 $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
51
52 // Test book with 3 chapters.
53 $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
54 $cm = get_coursemodule_from_id('book', $book->cmid);
55
56 $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book');
57 $chapter1 = $bookgenerator->create_chapter(array('bookid' => $book->id, "pagenum" => 1));
58 $chapter2 = $bookgenerator->create_chapter(array('bookid' => $book->id, "pagenum" => 2));
59 $subchapter = $bookgenerator->create_chapter(array('bookid' => $book->id, "pagenum" => 3, "subchapter" => 1));
60 $chapter3 = $bookgenerator->create_chapter(array('bookid' => $book->id, "pagenum" => 4, "hidden" => 1));
61
62 $this->setUser($user);
63
64 $contents = book_export_contents($cm, '');
65 // The hidden chapter must not be included, and additional page with the structure must be included.
66 $this->assertCount(4, $contents);
67
68 $this->assertEquals('structure', $contents[0]['filename']);
69 $this->assertEquals('index.html', $contents[1]['filename']);
70 $this->assertEquals('Chapter 1', $contents[1]['content']);
71 $this->assertEquals('index.html', $contents[2]['filename']);
72 $this->assertEquals('Chapter 2', $contents[2]['content']);
73 $this->assertEquals('index.html', $contents[3]['filename']);
74 $this->assertEquals('Chapter 3', $contents[3]['content']);
75
76 // Test empty book.
77 $emptybook = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
78 $cm = get_coursemodule_from_id('book', $emptybook->cmid);
79 $contents = book_export_contents($cm, '');
80
81 $this->assertCount(1, $contents);
82 $this->assertEquals('structure', $contents[0]['filename']);
83 $this->assertEquals(json_encode(array()), $contents[0]['content']);
84
85 }
86}