Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 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 namespace format_topics; 18 19 use core_external\external_api; 20 21 defined('MOODLE_INTERNAL') || die(); 22 23 global $CFG; 24 require_once($CFG->dirroot . '/course/lib.php'); 25 26 /** 27 * Topics course format related unit tests. 28 * 29 * @package format_topics 30 * @copyright 2015 Marina Glancy 31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 32 */ 33 class format_topics_test extends \advanced_testcase { 34 35 /** 36 * Tests for format_topics::get_section_name method with default section names. 37 * 38 * @return void 39 */ 40 public function test_get_section_name() { 41 global $DB; 42 $this->resetAfterTest(true); 43 44 // Generate a course with 5 sections. 45 $generator = $this->getDataGenerator(); 46 $numsections = 5; 47 $course = $generator->create_course(['numsections' => $numsections, 'format' => 'topics'], 48 ['createsections' => true]); 49 50 // Get section names for course. 51 $coursesections = $DB->get_records('course_sections', ['course' => $course->id]); 52 53 // Test get_section_name with default section names. 54 $courseformat = course_get_format($course); 55 foreach ($coursesections as $section) { 56 // Assert that with unmodified section names, get_section_name returns the same result as get_default_section_name. 57 $this->assertEquals($courseformat->get_default_section_name($section), $courseformat->get_section_name($section)); 58 } 59 } 60 61 /** 62 * Tests for format_topics::get_section_name method with modified section names. 63 * 64 * @return void 65 */ 66 public function test_get_section_name_customised() { 67 global $DB; 68 $this->resetAfterTest(true); 69 70 // Generate a course with 5 sections. 71 $generator = $this->getDataGenerator(); 72 $numsections = 5; 73 $course = $generator->create_course(['numsections' => $numsections, 'format' => 'topics'], 74 ['createsections' => true]); 75 76 // Get section names for course. 77 $coursesections = $DB->get_records('course_sections', ['course' => $course->id]); 78 79 // Modify section names. 80 $customname = "Custom Section"; 81 foreach ($coursesections as $section) { 82 $section->name = "$customname $section->section"; 83 $DB->update_record('course_sections', $section); 84 } 85 86 // Requery updated section names then test get_section_name. 87 $coursesections = $DB->get_records('course_sections', ['course' => $course->id]); 88 $courseformat = course_get_format($course); 89 foreach ($coursesections as $section) { 90 // Assert that with modified section names, get_section_name returns the modified section name. 91 $this->assertEquals($section->name, $courseformat->get_section_name($section)); 92 } 93 } 94 95 /** 96 * Tests for format_topics::get_default_section_name. 97 * 98 * @return void 99 */ 100 public function test_get_default_section_name() { 101 global $DB; 102 $this->resetAfterTest(true); 103 104 // Generate a course with 5 sections. 105 $generator = $this->getDataGenerator(); 106 $numsections = 5; 107 $course = $generator->create_course(['numsections' => $numsections, 'format' => 'topics'], 108 ['createsections' => true]); 109 110 // Get section names for course. 111 $coursesections = $DB->get_records('course_sections', ['course' => $course->id]); 112 113 // Test get_default_section_name with default section names. 114 $courseformat = course_get_format($course); 115 foreach ($coursesections as $section) { 116 if ($section->section == 0) { 117 $sectionname = get_string('section0name', 'format_topics'); 118 $this->assertEquals($sectionname, $courseformat->get_default_section_name($section)); 119 } else { 120 $sectionname = get_string('sectionname', 'format_topics') . ' ' . $section->section; 121 $this->assertEquals($sectionname, $courseformat->get_default_section_name($section)); 122 } 123 } 124 } 125 126 /** 127 * Test web service updating section name. 128 * 129 * @return void 130 */ 131 public function test_update_inplace_editable() { 132 global $CFG, $DB, $PAGE; 133 require_once($CFG->dirroot . '/lib/external/externallib.php'); 134 135 $this->resetAfterTest(); 136 $user = $this->getDataGenerator()->create_user(); 137 $this->setUser($user); 138 $course = $this->getDataGenerator()->create_course(['numsections' => 5, 'format' => 'topics'], 139 ['createsections' => true]); 140 $section = $DB->get_record('course_sections', ['course' => $course->id, 'section' => 2]); 141 142 // Call webservice without necessary permissions. 143 try { 144 \core_external::update_inplace_editable('format_topics', 'sectionname', $section->id, 'New section name'); 145 $this->fail('Exception expected'); 146 } catch (\moodle_exception $e) { 147 $this->assertEquals('Course or activity not accessible. (Not enrolled)', 148 $e->getMessage()); 149 } 150 151 // Change to teacher and make sure that section name can be updated using web service update_inplace_editable(). 152 $teacherrole = $DB->get_record('role', ['shortname' => 'editingteacher']); 153 $this->getDataGenerator()->enrol_user($user->id, $course->id, $teacherrole->id); 154 155 $res = \core_external::update_inplace_editable('format_topics', 'sectionname', $section->id, 'New section name'); 156 $res = external_api::clean_returnvalue(\core_external::update_inplace_editable_returns(), $res); 157 $this->assertEquals('New section name', $res['value']); 158 $this->assertEquals('New section name', $DB->get_field('course_sections', 'name', ['id' => $section->id])); 159 } 160 161 /** 162 * Test callback updating section name. 163 * 164 * @return void 165 */ 166 public function test_inplace_editable() { 167 global $DB, $PAGE; 168 169 $this->resetAfterTest(); 170 $user = $this->getDataGenerator()->create_user(); 171 $course = $this->getDataGenerator()->create_course(['numsections' => 5, 'format' => 'topics'], 172 ['createsections' => true]); 173 $teacherrole = $DB->get_record('role', ['shortname' => 'editingteacher']); 174 $this->getDataGenerator()->enrol_user($user->id, $course->id, $teacherrole->id); 175 $this->setUser($user); 176 177 $section = $DB->get_record('course_sections', ['course' => $course->id, 'section' => 2]); 178 179 // Call callback format_topics_inplace_editable() directly. 180 $tmpl = component_callback('format_topics', 'inplace_editable', ['sectionname', $section->id, 'Rename me again']); 181 $this->assertInstanceOf('core\output\inplace_editable', $tmpl); 182 $res = $tmpl->export_for_template($PAGE->get_renderer('core')); 183 $this->assertEquals('Rename me again', $res['value']); 184 $this->assertEquals('Rename me again', $DB->get_field('course_sections', 'name', ['id' => $section->id])); 185 186 // Try updating using callback from mismatching course format. 187 try { 188 component_callback('format_weeks', 'inplace_editable', ['sectionname', $section->id, 'New name']); 189 $this->fail('Exception expected'); 190 } catch (\moodle_exception $e) { 191 $this->assertEquals(1, preg_match('/^Can\'t find data record in database/', $e->getMessage())); 192 } 193 } 194 195 /** 196 * Test get_default_course_enddate. 197 * 198 * @return void 199 */ 200 public function test_default_course_enddate() { 201 global $CFG, $DB; 202 203 $this->resetAfterTest(true); 204 205 require_once($CFG->dirroot . '/course/tests/fixtures/testable_course_edit_form.php'); 206 207 $this->setTimezone('UTC'); 208 209 $params = ['format' => 'topics', 'numsections' => 5, 'startdate' => 1445644800]; 210 $course = $this->getDataGenerator()->create_course($params); 211 $category = $DB->get_record('course_categories', ['id' => $course->category]); 212 213 $args = [ 214 'course' => $course, 215 'category' => $category, 216 'editoroptions' => [ 217 'context' => \context_course::instance($course->id), 218 'subdirs' => 0 219 ], 220 'returnto' => new \moodle_url('/'), 221 'returnurl' => new \moodle_url('/'), 222 ]; 223 224 $courseform = new \testable_course_edit_form(null, $args); 225 $courseform->definition_after_data(); 226 227 $enddate = $params['startdate'] + get_config('moodlecourse', 'courseduration'); 228 229 $weeksformat = course_get_format($course->id); 230 $this->assertEquals($enddate, $weeksformat->get_default_course_enddate($courseform->get_quick_form())); 231 232 } 233 234 /** 235 * Test for get_view_url() to ensure that the url is only given for the correct cases. 236 * 237 * @return void 238 */ 239 public function test_get_view_url() { 240 global $CFG; 241 $this->resetAfterTest(); 242 243 $linkcoursesections = $CFG->linkcoursesections; 244 245 // Generate a course with two sections (0 and 1) and two modules. 246 $generator = $this->getDataGenerator(); 247 $course1 = $generator->create_course(['format' => 'topics']); 248 course_create_sections_if_missing($course1, [0, 1]); 249 250 $data = (object)['id' => $course1->id]; 251 $format = course_get_format($course1); 252 $format->update_course_format_options($data); 253 254 // In page. 255 $CFG->linkcoursesections = 0; 256 $this->assertNotEmpty($format->get_view_url(null)); 257 $this->assertNotEmpty($format->get_view_url(0)); 258 $this->assertNotEmpty($format->get_view_url(1)); 259 $CFG->linkcoursesections = 1; 260 $this->assertNotEmpty($format->get_view_url(null)); 261 $this->assertNotEmpty($format->get_view_url(0)); 262 $this->assertNotEmpty($format->get_view_url(1)); 263 264 // Navigation. 265 $CFG->linkcoursesections = 0; 266 $this->assertNull($format->get_view_url(1, ['navigation' => 1])); 267 $this->assertNull($format->get_view_url(0, ['navigation' => 1])); 268 $CFG->linkcoursesections = 1; 269 $this->assertNotEmpty($format->get_view_url(1, ['navigation' => 1])); 270 $this->assertNotEmpty($format->get_view_url(0, ['navigation' => 1])); 271 } 272 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body