Commit 89d8dec3 authored by Mihail Geshoski's avatar Mihail Geshoski Committed by Juan Leyva
Browse files

MDL-65653 book: Update unit tests

parent 794604db
......@@ -49,9 +49,13 @@ class mod_book_lib_testcase extends advanced_testcase {
require_once($CFG->dirroot . '/course/externallib.php');
$user = $this->getDataGenerator()->create_user();
$teacher = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course(array('enablecomment' => 1));
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
$this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
$this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id);
// Test book with 3 chapters.
$book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
......@@ -75,18 +79,95 @@ class mod_book_lib_testcase extends advanced_testcase {
$this->assertEquals('structure', $contents[0]['filename']);
$this->assertEquals('index.html', $contents[1]['filename']);
$this->assertEquals('Chapter 1', $contents[1]['content']);
$this->assertFalse($contents[1]['ishidden']);
$this->assertCount(2, $contents[1]['tags']);
$this->assertEquals('Cats', $contents[1]['tags'][0]['rawname']);
$this->assertEquals($tag->id, $contents[1]['tags'][0]['id']);
$this->assertEquals('Dogs', $contents[1]['tags'][1]['rawname']);
$this->assertEquals('index.html', $contents[2]['filename']);
$this->assertEquals('Chapter 2', $contents[2]['content']);
$this->assertFalse($contents[2]['ishidden']);
$this->assertEquals('index.html', $contents[3]['filename']);
$this->assertEquals('Chapter 3', $contents[3]['content']);
$this->assertFalse($contents[3]['ishidden']);
// Now, test the function via the external API.
$contents = core_course_external::get_course_contents($course->id, array());
$contents = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $contents);
$this->assertCount(4, $contents[0]['modules'][0]['contents']);
$this->assertEquals('content', $contents[0]['modules'][0]['contents'][0]['type']);
$this->assertEquals('structure', $contents[0]['modules'][0]['contents'][0]['filename']);
$this->assertEquals('file', $contents[0]['modules'][0]['contents'][1]['type']);
$this->assertEquals('Chapter 1', $contents[0]['modules'][0]['contents'][1]['content']);
$this->assertFalse($contents[0]['modules'][0]['contents'][1]['ishidden']);
$this->assertEquals('file', $contents[0]['modules'][0]['contents'][2]['type']);
$this->assertEquals('Chapter 2', $contents[0]['modules'][0]['contents'][2]['content']);
$this->assertFalse($contents[0]['modules'][0]['contents'][2]['ishidden']);
$this->assertEquals('file', $contents[0]['modules'][0]['contents'][3]['type']);
$this->assertEquals('Chapter 3', $contents[0]['modules'][0]['contents'][3]['content']);
$this->assertFalse($contents[0]['modules'][0]['contents'][3]['ishidden']);
$this->assertEquals('book', $contents[0]['modules'][0]['modname']);
$this->assertEquals($cm->id, $contents[0]['modules'][0]['id']);
$this->assertCount(2, $contents[0]['modules'][0]['contents'][1]['tags']);
$this->assertEquals('Cats', $contents[0]['modules'][0]['contents'][1]['tags'][0]['rawname']);
$this->assertEquals('Dogs', $contents[0]['modules'][0]['contents'][1]['tags'][1]['rawname']);
// As a teacher.
$this->setUser($teacher);
$contents = book_export_contents($cm, '');
// As a teacher, the hidden chapter must be included, and additional page with the structure must be included.
$this->assertCount(5, $contents);
$this->assertEquals('structure', $contents[0]['filename']);
$this->assertEquals('index.html', $contents[1]['filename']);
$this->assertEquals('Chapter 1', $contents[1]['content']);
$this->assertFalse($contents[1]['ishidden']);
$this->assertCount(2, $contents[1]['tags']);
$this->assertEquals('Cats', $contents[1]['tags'][0]['rawname']);
$this->assertEquals($tag->id, $contents[1]['tags'][0]['id']);
$this->assertEquals('Dogs', $contents[1]['tags'][1]['rawname']);
$this->assertEquals('index.html', $contents[2]['filename']);
$this->assertEquals('Chapter 2', $contents[2]['content']);
$this->assertFalse($contents[2]['ishidden']);
$this->assertEquals('index.html', $contents[3]['filename']);
$this->assertEquals('Chapter 3', $contents[3]['content']);
$this->assertFalse($contents[3]['ishidden']);
$this->assertEquals('index.html', $contents[4]['filename']);
$this->assertEquals('Chapter 4', $contents[4]['content']);
$this->assertTrue($contents[4]['ishidden']);
// Now, test the function via the external API.
$contents = core_course_external::get_course_contents($course->id, array());
$contents = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $contents);
$this->assertCount(5, $contents[0]['modules'][0]['contents']);
$this->assertEquals('content', $contents[0]['modules'][0]['contents'][0]['type']);
$this->assertEquals('structure', $contents[0]['modules'][0]['contents'][0]['filename']);
$this->assertEquals('file', $contents[0]['modules'][0]['contents'][1]['type']);
$this->assertEquals('Chapter 1', $contents[0]['modules'][0]['contents'][1]['content']);
$this->assertFalse($contents[0]['modules'][0]['contents'][1]['ishidden']);
$this->assertEquals('file', $contents[0]['modules'][0]['contents'][2]['type']);
$this->assertEquals('Chapter 2', $contents[0]['modules'][0]['contents'][2]['content']);
$this->assertFalse($contents[0]['modules'][0]['contents'][2]['ishidden']);
$this->assertEquals('file', $contents[0]['modules'][0]['contents'][3]['type']);
$this->assertEquals('Chapter 3', $contents[0]['modules'][0]['contents'][3]['content']);
$this->assertFalse($contents[0]['modules'][0]['contents'][3]['ishidden']);
$this->assertEquals('file', $contents[0]['modules'][0]['contents'][4]['type']);
$this->assertEquals('Chapter 4', $contents[0]['modules'][0]['contents'][4]['content']);
$this->assertTrue($contents[0]['modules'][0]['contents'][4]['ishidden']);
$this->assertEquals('book', $contents[0]['modules'][0]['modname']);
$this->assertEquals($cm->id, $contents[0]['modules'][0]['id']);
$this->assertCount(2, $contents[0]['modules'][0]['contents'][1]['tags']);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment