[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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 * format_topics related unit tests 19 * 20 * @package format_topics 21 * @copyright 2015 Marina Glancy 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 defined('MOODLE_INTERNAL') || die(); 26 27 global $CFG; 28 require_once($CFG->dirroot . '/course/lib.php'); 29 30 /** 31 * format_topics related unit tests 32 * 33 * @package format_topics 34 * @copyright 2015 Marina Glancy 35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 36 */ 37 class format_topics_testcase extends advanced_testcase { 38 39 public function test_update_course_numsections() { 40 global $DB; 41 $this->resetAfterTest(true); 42 43 $generator = $this->getDataGenerator(); 44 45 $course = $generator->create_course(array('numsections' => 10, 'format' => 'topics'), 46 array('createsections' => true)); 47 $generator->create_module('assign', array('course' => $course, 'section' => 7)); 48 49 $this->setAdminUser(); 50 51 $this->assertEquals(11, $DB->count_records('course_sections', array('course' => $course->id))); 52 53 // Change the numsections to 8, last two sections did not have any activities, they should be deleted. 54 update_course((object)array('id' => $course->id, 'numsections' => 8)); 55 $this->assertEquals(9, $DB->count_records('course_sections', array('course' => $course->id))); 56 $this->assertEquals(9, count(get_fast_modinfo($course)->get_section_info_all())); 57 58 // Change the numsections to 5, section 8 should be deleted but section 7 should remain as it has activities. 59 update_course((object)array('id' => $course->id, 'numsections' => 6)); 60 $this->assertEquals(8, $DB->count_records('course_sections', array('course' => $course->id))); 61 $this->assertEquals(8, count(get_fast_modinfo($course)->get_section_info_all())); 62 $this->assertEquals(6, course_get_format($course)->get_course()->numsections); 63 } 64 65 /** 66 * Tests for format_topics::get_section_name method with default section names. 67 */ 68 public function test_get_section_name() { 69 global $DB; 70 $this->resetAfterTest(true); 71 72 // Generate a course with 5 sections. 73 $generator = $this->getDataGenerator(); 74 $numsections = 5; 75 $course = $generator->create_course(array('numsections' => $numsections, 'format' => 'topics'), 76 array('createsections' => true)); 77 78 // Get section names for course. 79 $coursesections = $DB->get_records('course_sections', array('course' => $course->id)); 80 81 // Test get_section_name with default section names. 82 $courseformat = course_get_format($course); 83 foreach ($coursesections as $section) { 84 // Assert that with unmodified section names, get_section_name returns the same result as get_default_section_name. 85 $this->assertEquals($courseformat->get_default_section_name($section), $courseformat->get_section_name($section)); 86 } 87 } 88 89 /** 90 * Tests for format_topics::get_section_name method with modified section names. 91 */ 92 public function test_get_section_name_customised() { 93 global $DB; 94 $this->resetAfterTest(true); 95 96 // Generate a course with 5 sections. 97 $generator = $this->getDataGenerator(); 98 $numsections = 5; 99 $course = $generator->create_course(array('numsections' => $numsections, 'format' => 'topics'), 100 array('createsections' => true)); 101 102 // Get section names for course. 103 $coursesections = $DB->get_records('course_sections', array('course' => $course->id)); 104 105 // Modify section names. 106 $customname = "Custom Section"; 107 foreach ($coursesections as $section) { 108 $section->name = "$customname $section->section"; 109 $DB->update_record('course_sections', $section); 110 } 111 112 // Requery updated section names then test get_section_name. 113 $coursesections = $DB->get_records('course_sections', array('course' => $course->id)); 114 $courseformat = course_get_format($course); 115 foreach ($coursesections as $section) { 116 // Assert that with modified section names, get_section_name returns the modified section name. 117 $this->assertEquals($section->name, $courseformat->get_section_name($section)); 118 } 119 } 120 121 /** 122 * Tests for format_topics::get_default_section_name. 123 */ 124 public function test_get_default_section_name() { 125 global $DB; 126 $this->resetAfterTest(true); 127 128 // Generate a course with 5 sections. 129 $generator = $this->getDataGenerator(); 130 $numsections = 5; 131 $course = $generator->create_course(array('numsections' => $numsections, 'format' => 'topics'), 132 array('createsections' => true)); 133 134 // Get section names for course. 135 $coursesections = $DB->get_records('course_sections', array('course' => $course->id)); 136 137 // Test get_default_section_name with default section names. 138 $courseformat = course_get_format($course); 139 foreach ($coursesections as $section) { 140 if ($section->section == 0) { 141 $sectionname = get_string('section0name', 'format_topics'); 142 $this->assertEquals($sectionname, $courseformat->get_default_section_name($section)); 143 } else { 144 $sectionname = get_string('sectionname', 'format_topics') . ' ' . $section->section; 145 $this->assertEquals($sectionname, $courseformat->get_default_section_name($section)); 146 } 147 } 148 } 149 150 /** 151 * Test web service updating section name 152 */ 153 public function test_update_inplace_editable() { 154 global $CFG, $DB, $PAGE; 155 require_once($CFG->dirroot . '/lib/external/externallib.php'); 156 157 $this->resetAfterTest(); 158 $user = $this->getDataGenerator()->create_user(); 159 $this->setUser($user); 160 $course = $this->getDataGenerator()->create_course(array('numsections' => 5, 'format' => 'topics'), 161 array('createsections' => true)); 162 $section = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 2)); 163 164 // Call webservice without necessary permissions. 165 try { 166 core_external::update_inplace_editable('format_topics', 'sectionname', $section->id, 'New section name'); 167 $this->fail('Exception expected'); 168 } catch (moodle_exception $e) { 169 $this->assertEquals('Course or activity not accessible. (Not enrolled)', 170 $e->getMessage()); 171 } 172 173 // Change to teacher and make sure that section name can be updated using web service update_inplace_editable(). 174 $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher')); 175 $this->getDataGenerator()->enrol_user($user->id, $course->id, $teacherrole->id); 176 177 $res = core_external::update_inplace_editable('format_topics', 'sectionname', $section->id, 'New section name'); 178 $res = external_api::clean_returnvalue(core_external::update_inplace_editable_returns(), $res); 179 $this->assertEquals('New section name', $res['value']); 180 $this->assertEquals('New section name', $DB->get_field('course_sections', 'name', array('id' => $section->id))); 181 } 182 183 /** 184 * Test callback updating section name 185 */ 186 public function test_inplace_editable() { 187 global $DB, $PAGE; 188 189 $this->resetAfterTest(); 190 $user = $this->getDataGenerator()->create_user(); 191 $course = $this->getDataGenerator()->create_course(array('numsections' => 5, 'format' => 'topics'), 192 array('createsections' => true)); 193 $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher')); 194 $this->getDataGenerator()->enrol_user($user->id, $course->id, $teacherrole->id); 195 $this->setUser($user); 196 197 $section = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 2)); 198 199 // Call callback format_topics_inplace_editable() directly. 200 $tmpl = component_callback('format_topics', 'inplace_editable', array('sectionname', $section->id, 'Rename me again')); 201 $this->assertInstanceOf('core\output\inplace_editable', $tmpl); 202 $res = $tmpl->export_for_template($PAGE->get_renderer('core')); 203 $this->assertEquals('Rename me again', $res['value']); 204 $this->assertEquals('Rename me again', $DB->get_field('course_sections', 'name', array('id' => $section->id))); 205 206 // Try updating using callback from mismatching course format. 207 try { 208 $tmpl = component_callback('format_weeks', 'inplace_editable', array('sectionname', $section->id, 'New name')); 209 $this->fail('Exception expected'); 210 } catch (moodle_exception $e) { 211 $this->assertEquals(1, preg_match('/^Can not find data record in database/', $e->getMessage())); 212 } 213 } 214 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |