[ 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 * SCORM module library functions tests 19 * 20 * @package mod_scorm 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/scorm/lib.php'); 33 34 /** 35 * SCORM module library functions tests 36 * 37 * @package mod_scorm 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_scorm_lib_testcase extends externallib_advanced_testcase { 44 45 /** 46 * Set up for every test 47 */ 48 public function setUp() { 49 global $DB; 50 $this->resetAfterTest(); 51 $this->setAdminUser(); 52 53 // Setup test data. 54 $this->course = $this->getDataGenerator()->create_course(); 55 $this->scorm = $this->getDataGenerator()->create_module('scorm', array('course' => $this->course->id)); 56 $this->context = context_module::instance($this->scorm->cmid); 57 $this->cm = get_coursemodule_from_instance('scorm', $this->scorm->id); 58 59 // Create users. 60 $this->student = self::getDataGenerator()->create_user(); 61 $this->teacher = self::getDataGenerator()->create_user(); 62 63 // Users enrolments. 64 $this->studentrole = $DB->get_record('role', array('shortname' => 'student')); 65 $this->teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher')); 66 $this->getDataGenerator()->enrol_user($this->student->id, $this->course->id, $this->studentrole->id, 'manual'); 67 $this->getDataGenerator()->enrol_user($this->teacher->id, $this->course->id, $this->teacherrole->id, 'manual'); 68 } 69 70 /** 71 * Test scorm_view 72 * @return void 73 */ 74 public function test_scorm_view() { 75 global $CFG; 76 77 // Trigger and capture the event. 78 $sink = $this->redirectEvents(); 79 80 scorm_view($this->scorm, $this->course, $this->cm, $this->context); 81 82 $events = $sink->get_events(); 83 $this->assertCount(1, $events); 84 $event = array_shift($events); 85 86 // Checking that the event contains the expected values. 87 $this->assertInstanceOf('\mod_scorm\event\course_module_viewed', $event); 88 $this->assertEquals($this->context, $event->get_context()); 89 $url = new \moodle_url('/mod/scorm/view.php', array('id' => $this->cm->id)); 90 $this->assertEquals($url, $event->get_url()); 91 $this->assertEventContextNotUsed($event); 92 $this->assertNotEmpty($event->get_name()); 93 } 94 95 /** 96 * Test scorm_get_availability_status and scorm_require_available 97 * @return void 98 */ 99 public function test_scorm_check_and_require_available() { 100 global $DB; 101 102 // Set to the student user. 103 self::setUser($this->student); 104 105 // Usual case. 106 list($status, $warnings) = scorm_get_availability_status($this->scorm, false); 107 $this->assertEquals(true, $status); 108 $this->assertCount(0, $warnings); 109 110 // SCORM not open. 111 $this->scorm->timeopen = time() + DAYSECS; 112 list($status, $warnings) = scorm_get_availability_status($this->scorm, false); 113 $this->assertEquals(false, $status); 114 $this->assertCount(1, $warnings); 115 116 // SCORM closed. 117 $this->scorm->timeopen = 0; 118 $this->scorm->timeclose = time() - DAYSECS; 119 list($status, $warnings) = scorm_get_availability_status($this->scorm, false); 120 $this->assertEquals(false, $status); 121 $this->assertCount(1, $warnings); 122 123 // SCORM not open and closed. 124 $this->scorm->timeopen = time() + DAYSECS; 125 list($status, $warnings) = scorm_get_availability_status($this->scorm, false); 126 $this->assertEquals(false, $status); 127 $this->assertCount(2, $warnings); 128 129 // Now additional checkings with different parameters values. 130 list($status, $warnings) = scorm_get_availability_status($this->scorm, true, $this->context); 131 $this->assertEquals(false, $status); 132 $this->assertCount(2, $warnings); 133 134 // SCORM not open. 135 $this->scorm->timeopen = time() + DAYSECS; 136 $this->scorm->timeclose = 0; 137 list($status, $warnings) = scorm_get_availability_status($this->scorm, true, $this->context); 138 $this->assertEquals(false, $status); 139 $this->assertCount(1, $warnings); 140 141 // SCORM closed. 142 $this->scorm->timeopen = 0; 143 $this->scorm->timeclose = time() - DAYSECS; 144 list($status, $warnings) = scorm_get_availability_status($this->scorm, true, $this->context); 145 $this->assertEquals(false, $status); 146 $this->assertCount(1, $warnings); 147 148 // SCORM not open and closed. 149 $this->scorm->timeopen = time() + DAYSECS; 150 list($status, $warnings) = scorm_get_availability_status($this->scorm, true, $this->context); 151 $this->assertEquals(false, $status); 152 $this->assertCount(2, $warnings); 153 154 // As teacher now. 155 self::setUser($this->teacher); 156 157 // SCORM not open and closed. 158 $this->scorm->timeopen = time() + DAYSECS; 159 list($status, $warnings) = scorm_get_availability_status($this->scorm, false); 160 $this->assertEquals(false, $status); 161 $this->assertCount(2, $warnings); 162 163 // Now, we use the special capability. 164 // SCORM not open and closed. 165 $this->scorm->timeopen = time() + DAYSECS; 166 list($status, $warnings) = scorm_get_availability_status($this->scorm, true, $this->context); 167 $this->assertEquals(true, $status); 168 $this->assertCount(0, $warnings); 169 170 // Check exceptions does not broke anything. 171 scorm_require_available($this->scorm, true, $this->context); 172 // Now, expect exceptions. 173 $this->expectException('moodle_exception'); 174 $this->expectExceptionMessage(get_string("notopenyet", "scorm", userdate($this->scorm->timeopen))); 175 176 // Now as student other condition. 177 self::setUser($this->student); 178 $this->scorm->timeopen = 0; 179 $this->scorm->timeclose = time() - DAYSECS; 180 181 $this->expectException('moodle_exception'); 182 $this->expectExceptionMessage(get_string("expired", "scorm", userdate($this->scorm->timeclose))); 183 scorm_require_available($this->scorm, false); 184 } 185 186 /** 187 * Test scorm_get_last_completed_attempt 188 * 189 * @return void 190 */ 191 public function test_scorm_get_last_completed_attempt() { 192 $this->assertEquals(1, scorm_get_last_completed_attempt($this->scorm->id, $this->student->id)); 193 } 194 }
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 |