Differences Between: [Versions 310 and 311] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]
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 namespace mod_page; 18 19 use externallib_advanced_testcase; 20 use mod_page_external; 21 22 defined('MOODLE_INTERNAL') || die(); 23 24 global $CFG; 25 26 require_once($CFG->dirroot . '/webservice/tests/helpers.php'); 27 28 /** 29 * External mod_page functions unit tests 30 * 31 * @package mod_page 32 * @category external 33 * @copyright 2015 Juan Leyva <juan@moodle.com> 34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 35 * @since Moodle 3.0 36 */ 37 class externallib_test extends externallib_advanced_testcase { 38 39 /** 40 * Test view_page 41 */ 42 public function test_view_page() { 43 global $DB; 44 45 $this->resetAfterTest(true); 46 47 // Setup test data. 48 $course = $this->getDataGenerator()->create_course(); 49 $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id)); 50 $context = \context_module::instance($page->cmid); 51 $cm = get_coursemodule_from_instance('page', $page->id); 52 53 // Test invalid instance id. 54 try { 55 mod_page_external::view_page(0); 56 $this->fail('Exception expected due to invalid mod_page instance id.'); 57 } catch (\moodle_exception $e) { 58 $this->assertEquals('invalidrecord', $e->errorcode); 59 } 60 61 // Test not-enrolled user. 62 $user = self::getDataGenerator()->create_user(); 63 $this->setUser($user); 64 try { 65 mod_page_external::view_page($page->id); 66 $this->fail('Exception expected due to not enrolled user.'); 67 } catch (\moodle_exception $e) { 68 $this->assertEquals('requireloginerror', $e->errorcode); 69 } 70 71 // Test user with full capabilities. 72 $studentrole = $DB->get_record('role', array('shortname' => 'student')); 73 $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id); 74 75 // Trigger and capture the event. 76 $sink = $this->redirectEvents(); 77 78 $result = mod_page_external::view_page($page->id); 79 $result = \external_api::clean_returnvalue(mod_page_external::view_page_returns(), $result); 80 81 $events = $sink->get_events(); 82 $this->assertCount(1, $events); 83 $event = array_shift($events); 84 85 // Checking that the event contains the expected values. 86 $this->assertInstanceOf('\mod_page\event\course_module_viewed', $event); 87 $this->assertEquals($context, $event->get_context()); 88 $moodlepage = new \moodle_url('/mod/page/view.php', array('id' => $cm->id)); 89 $this->assertEquals($moodlepage, $event->get_url()); 90 $this->assertEventContextNotUsed($event); 91 $this->assertNotEmpty($event->get_name()); 92 93 // Test user with no capabilities. 94 // We need a explicit prohibit since this capability is only defined in authenticated user and guest roles. 95 assign_capability('mod/page:view', CAP_PROHIBIT, $studentrole->id, $context->id); 96 // Empty all the caches that may be affected by this change. 97 accesslib_clear_all_caches_for_unit_testing(); 98 \course_modinfo::clear_instance_cache(); 99 100 try { 101 mod_page_external::view_page($page->id); 102 $this->fail('Exception expected due to missing capability.'); 103 } catch (\moodle_exception $e) { 104 $this->assertEquals('requireloginerror', $e->errorcode); 105 } 106 107 } 108 109 /** 110 * Test test_mod_page_get_pages_by_courses 111 */ 112 public function test_mod_page_get_pages_by_courses() { 113 global $DB; 114 115 $this->resetAfterTest(true); 116 117 $course1 = self::getDataGenerator()->create_course(); 118 $course2 = self::getDataGenerator()->create_course(); 119 120 $student = self::getDataGenerator()->create_user(); 121 $studentrole = $DB->get_record('role', array('shortname' => 'student')); 122 $this->getDataGenerator()->enrol_user($student->id, $course1->id, $studentrole->id); 123 124 // First page. 125 $record = new \stdClass(); 126 $record->course = $course1->id; 127 $page1 = self::getDataGenerator()->create_module('page', $record); 128 129 // Second page. 130 $record = new \stdClass(); 131 $record->course = $course2->id; 132 $page2 = self::getDataGenerator()->create_module('page', $record); 133 134 // Execute real Moodle enrolment as we'll call unenrol() method on the instance later. 135 $enrol = enrol_get_plugin('manual'); 136 $enrolinstances = enrol_get_instances($course2->id, true); 137 foreach ($enrolinstances as $courseenrolinstance) { 138 if ($courseenrolinstance->enrol == "manual") { 139 $instance2 = $courseenrolinstance; 140 break; 141 } 142 } 143 $enrol->enrol_user($instance2, $student->id, $studentrole->id); 144 145 self::setUser($student); 146 147 $returndescription = mod_page_external::get_pages_by_courses_returns(); 148 149 // Create what we expect to be returned when querying the two courses. 150 $expectedfields = array('id', 'coursemodule', 'course', 'name', 'intro', 'introformat', 'introfiles', 151 'content', 'contentformat', 'contentfiles', 'legacyfiles', 'legacyfileslast', 'display', 152 'displayoptions', 'revision', 'timemodified', 'section', 'visible', 'groupmode', 'groupingid'); 153 154 // Add expected coursemodule and data. 155 $page1->coursemodule = $page1->cmid; 156 $page1->introformat = 1; 157 $page1->contentformat = 1; 158 $page1->section = 0; 159 $page1->visible = true; 160 $page1->groupmode = 0; 161 $page1->groupingid = 0; 162 $page1->introfiles = []; 163 $page1->contentfiles = []; 164 165 $page2->coursemodule = $page2->cmid; 166 $page2->introformat = 1; 167 $page2->contentformat = 1; 168 $page2->section = 0; 169 $page2->visible = true; 170 $page2->groupmode = 0; 171 $page2->groupingid = 0; 172 $page2->introfiles = []; 173 $page2->contentfiles = []; 174 175 foreach ($expectedfields as $field) { 176 $expected1[$field] = $page1->{$field}; 177 $expected2[$field] = $page2->{$field}; 178 } 179 180 $expectedpages = array($expected2, $expected1); 181 182 // Call the external function passing course ids. 183 $result = mod_page_external::get_pages_by_courses(array($course2->id, $course1->id)); 184 $result = \external_api::clean_returnvalue($returndescription, $result); 185 186 $this->assertEquals($expectedpages, $result['pages']); 187 $this->assertCount(0, $result['warnings']); 188 189 // Call the external function without passing course id. 190 $result = mod_page_external::get_pages_by_courses(); 191 $result = \external_api::clean_returnvalue($returndescription, $result); 192 $this->assertEquals($expectedpages, $result['pages']); 193 $this->assertCount(0, $result['warnings']); 194 195 // Add a file to the intro. 196 $filename = "file.txt"; 197 $filerecordinline = array( 198 'contextid' => \context_module::instance($page2->cmid)->id, 199 'component' => 'mod_page', 200 'filearea' => 'intro', 201 'itemid' => 0, 202 'filepath' => '/', 203 'filename' => $filename, 204 ); 205 $fs = get_file_storage(); 206 $timepost = time(); 207 $fs->create_file_from_string($filerecordinline, 'image contents (not really)'); 208 209 $result = mod_page_external::get_pages_by_courses(array($course2->id, $course1->id)); 210 $result = \external_api::clean_returnvalue($returndescription, $result); 211 212 $this->assertCount(1, $result['pages'][0]['introfiles']); 213 $this->assertEquals($filename, $result['pages'][0]['introfiles'][0]['filename']); 214 215 // Unenrol user from second course. 216 $enrol->unenrol_user($instance2, $student->id); 217 array_shift($expectedpages); 218 219 // Call the external function without passing course id. 220 $result = mod_page_external::get_pages_by_courses(); 221 $result = \external_api::clean_returnvalue($returndescription, $result); 222 $this->assertEquals($expectedpages, $result['pages']); 223 224 // Call for the second course we unenrolled the user from, expected warning. 225 $result = mod_page_external::get_pages_by_courses(array($course2->id)); 226 $this->assertCount(1, $result['warnings']); 227 $this->assertEquals('1', $result['warnings'][0]['warningcode']); 228 $this->assertEquals($course2->id, $result['warnings'][0]['itemid']); 229 } 230 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body