Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]
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 * External mod_book functions unit tests 19 * 20 * @package mod_book 21 * @category external 22 * @copyright 2015 Juan Leyva <juan@moodle.com> 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 * @since Moodle 3.0 25 */ 26 27 defined('MOODLE_INTERNAL') || die(); 28 29 global $CFG; 30 31 require_once($CFG->dirroot . '/webservice/tests/helpers.php'); 32 33 /** 34 * External mod_book functions unit tests 35 * 36 * @package mod_book 37 * @category external 38 * @copyright 2015 Juan Leyva <juan@moodle.com> 39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 40 * @since Moodle 3.0 41 */ 42 class mod_book_external_testcase extends externallib_advanced_testcase { 43 44 /** 45 * Test view_book 46 */ 47 public function test_view_book() { 48 global $DB; 49 50 $this->resetAfterTest(true); 51 52 $this->setAdminUser(); 53 // Setup test data. 54 $course = $this->getDataGenerator()->create_course(); 55 $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id)); 56 $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book'); 57 $chapter = $bookgenerator->create_chapter(array('bookid' => $book->id)); 58 $chapterhidden = $bookgenerator->create_chapter(array('bookid' => $book->id, 'hidden' => 1)); 59 60 $context = context_module::instance($book->cmid); 61 $cm = get_coursemodule_from_instance('book', $book->id); 62 63 // Test invalid instance id. 64 try { 65 mod_book_external::view_book(0); 66 $this->fail('Exception expected due to invalid mod_book instance id.'); 67 } catch (moodle_exception $e) { 68 $this->assertEquals('invalidrecord', $e->errorcode); 69 } 70 71 // Test not-enrolled user. 72 $user = self::getDataGenerator()->create_user(); 73 $this->setUser($user); 74 try { 75 mod_book_external::view_book($book->id, 0); 76 $this->fail('Exception expected due to not enrolled user.'); 77 } catch (moodle_exception $e) { 78 $this->assertEquals('requireloginerror', $e->errorcode); 79 } 80 81 // Test user with full capabilities. 82 $studentrole = $DB->get_record('role', array('shortname' => 'student')); 83 $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id); 84 85 // Trigger and capture the event. 86 $sink = $this->redirectEvents(); 87 88 $result = mod_book_external::view_book($book->id, 0); 89 $result = external_api::clean_returnvalue(mod_book_external::view_book_returns(), $result); 90 91 $events = $sink->get_events(); 92 $this->assertCount(2, $events); 93 $event = array_shift($events); 94 95 // Checking that the event contains the expected values. 96 $this->assertInstanceOf('\mod_book\event\course_module_viewed', $event); 97 $this->assertEquals($context, $event->get_context()); 98 $moodleurl = new \moodle_url('/mod/book/view.php', array('id' => $cm->id)); 99 $this->assertEquals($moodleurl, $event->get_url()); 100 $this->assertEventContextNotUsed($event); 101 $this->assertNotEmpty($event->get_name()); 102 103 $event = array_shift($events); 104 $this->assertInstanceOf('\mod_book\event\chapter_viewed', $event); 105 $this->assertEquals($chapter->id, $event->objectid); 106 107 $result = mod_book_external::view_book($book->id, $chapter->id); 108 $result = external_api::clean_returnvalue(mod_book_external::view_book_returns(), $result); 109 110 $events = $sink->get_events(); 111 // We expect a total of 3 events. 112 $this->assertCount(3, $events); 113 114 // Try to view a hidden chapter. 115 try { 116 mod_book_external::view_book($book->id, $chapterhidden->id); 117 $this->fail('Exception expected due to missing capability.'); 118 } catch (moodle_exception $e) { 119 $this->assertEquals('errorchapter', $e->errorcode); 120 } 121 122 // Test user with no capabilities. 123 // We need a explicit prohibit since this capability is only defined in authenticated user and guest roles. 124 assign_capability('mod/book:read', CAP_PROHIBIT, $studentrole->id, $context->id); 125 accesslib_clear_all_caches_for_unit_testing(); 126 127 try { 128 mod_book_external::view_book($book->id, 0); 129 $this->fail('Exception expected due to missing capability.'); 130 } catch (moodle_exception $e) { 131 $this->assertEquals('nopermissions', $e->errorcode); 132 } 133 134 } 135 136 /** 137 * Test get_books_by_courses 138 */ 139 public function test_get_books_by_courses() { 140 global $DB, $USER; 141 $this->resetAfterTest(true); 142 $this->setAdminUser(); 143 $course1 = self::getDataGenerator()->create_course(); 144 $bookoptions1 = array( 145 'course' => $course1->id, 146 'name' => 'First Book' 147 ); 148 $book1 = self::getDataGenerator()->create_module('book', $bookoptions1); 149 $course2 = self::getDataGenerator()->create_course(); 150 $bookoptions2 = array( 151 'course' => $course2->id, 152 'name' => 'Second Book' 153 ); 154 $book2 = self::getDataGenerator()->create_module('book', $bookoptions2); 155 $student1 = $this->getDataGenerator()->create_user(); 156 $studentrole = $DB->get_record('role', array('shortname' => 'student')); 157 158 // Enroll Student1 in Course1. 159 self::getDataGenerator()->enrol_user($student1->id, $course1->id, $studentrole->id); 160 $this->setUser($student1); 161 162 $books = mod_book_external::get_books_by_courses(); 163 // We need to execute the return values cleaning process to simulate the web service server. 164 $books = external_api::clean_returnvalue(mod_book_external::get_books_by_courses_returns(), $books); 165 $this->assertCount(1, $books['books']); 166 $this->assertEquals('First Book', $books['books'][0]['name']); 167 // We see 10 fields. 168 $this->assertCount(10, $books['books'][0]); 169 170 // As Student you cannot see some book properties like 'section'. 171 $this->assertFalse(isset($books['books'][0]['section'])); 172 173 // Student1 is not enrolled in course2. The webservice will return a warning! 174 $books = mod_book_external::get_books_by_courses(array($course2->id)); 175 // We need to execute the return values cleaning process to simulate the web service server. 176 $books = external_api::clean_returnvalue(mod_book_external::get_books_by_courses_returns(), $books); 177 $this->assertCount(0, $books['books']); 178 $this->assertEquals(1, $books['warnings'][0]['warningcode']); 179 180 // Now as admin. 181 $this->setAdminUser(); 182 // As Admin we can see this book. 183 $books = mod_book_external::get_books_by_courses(array($course2->id)); 184 // We need to execute the return values cleaning process to simulate the web service server. 185 $books = external_api::clean_returnvalue(mod_book_external::get_books_by_courses_returns(), $books); 186 187 $this->assertCount(1, $books['books']); 188 $this->assertEquals('Second Book', $books['books'][0]['name']); 189 // We see 17 fields. 190 $this->assertCount(17, $books['books'][0]); 191 // As an Admin you can see some book properties like 'section'. 192 $this->assertEquals(0, $books['books'][0]['section']); 193 194 // Enrol student in the second course. 195 self::getDataGenerator()->enrol_user($student1->id, $course2->id, $studentrole->id); 196 $this->setUser($student1); 197 $books = mod_book_external::get_books_by_courses(); 198 $books = external_api::clean_returnvalue(mod_book_external::get_books_by_courses_returns(), $books); 199 $this->assertCount(2, $books['books']); 200 201 } 202 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body