[ 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 * Choice module library functions tests 19 * 20 * @package mod_choice 21 * @category test 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 require_once($CFG->dirroot . '/mod/choice/lib.php'); 33 34 /** 35 * Choice module library functions tests 36 * 37 * @package mod_choice 38 * @category test 39 * @copyright 2015 Juan Leyva <juan@moodle.com> 40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 41 * @since Moodle 3.0 42 */ 43 class mod_choice_lib_testcase extends externallib_advanced_testcase { 44 45 /** 46 * Test choice_view 47 * @return void 48 */ 49 public function test_choice_view() { 50 global $CFG; 51 52 $this->resetAfterTest(); 53 54 $this->setAdminUser(); 55 // Setup test data. 56 $course = $this->getDataGenerator()->create_course(); 57 $choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id)); 58 $context = context_module::instance($choice->cmid); 59 $cm = get_coursemodule_from_instance('choice', $choice->id); 60 61 // Trigger and capture the event. 62 $sink = $this->redirectEvents(); 63 64 choice_view($choice, $course, $cm, $context); 65 66 $events = $sink->get_events(); 67 $this->assertCount(1, $events); 68 $event = array_shift($events); 69 70 // Checking that the event contains the expected values. 71 $this->assertInstanceOf('\mod_choice\event\course_module_viewed', $event); 72 $this->assertEquals($context, $event->get_context()); 73 $url = new \moodle_url('/mod/choice/view.php', array('id' => $cm->id)); 74 $this->assertEquals($url, $event->get_url()); 75 $this->assertEventContextNotUsed($event); 76 $this->assertNotEmpty($event->get_name()); 77 } 78 79 /** 80 * Test choice_can_view_results 81 * @return void 82 */ 83 public function test_choice_can_view_results() { 84 global $DB, $USER; 85 86 $this->resetAfterTest(); 87 88 $this->setAdminUser(); 89 // Setup test data. 90 $course = $this->getDataGenerator()->create_course(); 91 $choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id)); 92 $context = context_module::instance($choice->cmid); 93 $cm = get_coursemodule_from_instance('choice', $choice->id); 94 95 // Default values are false, user cannot view results. 96 $canview = choice_can_view_results($choice); 97 $this->assertFalse($canview); 98 99 // Show results forced. 100 $choice->showresults = CHOICE_SHOWRESULTS_ALWAYS; 101 $DB->update_record('choice', $choice); 102 $canview = choice_can_view_results($choice); 103 $this->assertTrue($canview); 104 105 // Show results after closing. 106 $choice->showresults = CHOICE_SHOWRESULTS_AFTER_CLOSE; 107 $DB->update_record('choice', $choice); 108 $canview = choice_can_view_results($choice); 109 $this->assertFalse($canview); 110 111 $choice->timeclose = time() - HOURSECS; 112 $DB->update_record('choice', $choice); 113 $canview = choice_can_view_results($choice); 114 $this->assertTrue($canview); 115 116 // Show results after answering. 117 $choice->timeclose = 0; 118 $choice->showresults = CHOICE_SHOWRESULTS_AFTER_ANSWER; 119 $DB->update_record('choice', $choice); 120 $canview = choice_can_view_results($choice); 121 $this->assertFalse($canview); 122 123 // Get the first option. 124 $choicewithoptions = choice_get_choice($choice->id); 125 $optionids = array_keys($choicewithoptions->option); 126 127 choice_user_submit_response($optionids[0], $choice, $USER->id, $course, $cm); 128 129 $canview = choice_can_view_results($choice); 130 $this->assertTrue($canview); 131 132 } 133 134 /** 135 * @expectedException moodle_exception 136 */ 137 public function test_choice_user_submit_response_validation() { 138 global $USER; 139 140 $this->resetAfterTest(); 141 142 $this->setAdminUser(); 143 // Setup test data. 144 $course = $this->getDataGenerator()->create_course(); 145 $choice1 = $this->getDataGenerator()->create_module('choice', array('course' => $course->id)); 146 $choice2 = $this->getDataGenerator()->create_module('choice', array('course' => $course->id)); 147 $cm = get_coursemodule_from_instance('choice', $choice1->id); 148 149 $choicewithoptions1 = choice_get_choice($choice1->id); 150 $choicewithoptions2 = choice_get_choice($choice2->id); 151 $optionids1 = array_keys($choicewithoptions1->option); 152 $optionids2 = array_keys($choicewithoptions2->option); 153 154 // Make sure we cannot submit options from a different choice instance. 155 choice_user_submit_response($optionids2[0], $choice1, $USER->id, $course, $cm); 156 } 157 158 /** 159 * Test choice_get_my_response 160 * @return void 161 */ 162 public function test_choice_get_my_response() { 163 global $USER; 164 165 $this->resetAfterTest(); 166 167 $this->setAdminUser(); 168 // Setup test data. 169 $course = $this->getDataGenerator()->create_course(); 170 $choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id)); 171 $context = context_module::instance($choice->cmid); 172 $cm = get_coursemodule_from_instance('choice', $choice->id); 173 174 $choicewithoptions = choice_get_choice($choice->id); 175 $optionids = array_keys($choicewithoptions->option); 176 177 choice_user_submit_response($optionids[0], $choice, $USER->id, $course, $cm); 178 $responses = choice_get_my_response($choice, $course, $cm, $context); 179 $this->assertCount(1, $responses); 180 $response = array_shift($responses); 181 $this->assertEquals($optionids[0], $response->optionid); 182 183 // Multiple responses. 184 $choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id, 'allowmultiple' => 1)); 185 $context = context_module::instance($choice->cmid); 186 $cm = get_coursemodule_from_instance('choice', $choice->id); 187 188 $choicewithoptions = choice_get_choice($choice->id); 189 $optionids = array_keys($choicewithoptions->option); 190 191 choice_user_submit_response($optionids, $choice, $USER->id, $course, $cm); 192 $responses = choice_get_my_response($choice, $course, $cm, $context); 193 $this->assertCount(count($optionids), $responses); 194 foreach ($responses as $resp) { 195 $this->assertContains($resp->optionid, $optionids); 196 } 197 } 198 199 /** 200 * Test choice_get_availability_status 201 * @return void 202 */ 203 public function test_choice_get_availability_status() { 204 global $USER; 205 206 $this->resetAfterTest(); 207 208 $this->setAdminUser(); 209 // Setup test data. 210 $course = $this->getDataGenerator()->create_course(); 211 $choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id)); 212 213 // No time restrictions and updates allowed. 214 list($status, $warnings) = choice_get_availability_status($choice, false); 215 $this->assertEquals(true, $status); 216 $this->assertCount(0, $warnings); 217 218 // No updates allowed, but haven't answered yet. 219 $choice->allowupdate = false; 220 list($status, $warnings) = choice_get_availability_status($choice, false); 221 $this->assertEquals(true, $status); 222 $this->assertCount(0, $warnings); 223 224 // No updates allowed and have answered. 225 $cm = get_coursemodule_from_instance('choice', $choice->id); 226 $choicewithoptions = choice_get_choice($choice->id); 227 $optionids = array_keys($choicewithoptions->option); 228 choice_user_submit_response($optionids[0], $choice, $USER->id, $course, $cm); 229 list($status, $warnings) = choice_get_availability_status($choice, false); 230 $this->assertEquals(false, $status); 231 $this->assertCount(1, $warnings); 232 $this->assertEquals('choicesaved', array_keys($warnings)[0]); 233 234 $choice->allowupdate = true; 235 236 // With time restrictions, still open. 237 $choice->timeopen = time() - DAYSECS; 238 $choice->timeclose = time() + DAYSECS; 239 list($status, $warnings) = choice_get_availability_status($choice, false); 240 $this->assertEquals(true, $status); 241 $this->assertCount(0, $warnings); 242 243 // Choice not open yet. 244 $choice->timeopen = time() + DAYSECS; 245 $choice->timeclose = $choice->timeopen + DAYSECS; 246 list($status, $warnings) = choice_get_availability_status($choice, false); 247 $this->assertEquals(false, $status); 248 $this->assertCount(1, $warnings); 249 $this->assertEquals('notopenyet', array_keys($warnings)[0]); 250 251 // Choice closed. 252 $choice->timeopen = time() - DAYSECS; 253 $choice->timeclose = time() - 1; 254 list($status, $warnings) = choice_get_availability_status($choice, false); 255 $this->assertEquals(false, $status); 256 $this->assertCount(1, $warnings); 257 $this->assertEquals('expired', array_keys($warnings)[0]); 258 259 } 260 261 }
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 |