[ Index ] |
PHP Cross Reference of moodle-2.8 |
[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 defined('MOODLE_INTERNAL') || die(); 18 19 /** 20 * Automated unit testing. This tests the 'make large course' backend, 21 * using the 'XS' option so that it completes quickly. 22 * 23 * @package tool_generator 24 * @copyright 2013 The Open University 25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 26 */ 27 class tool_generator_maketestcourse_testcase extends advanced_testcase { 28 /** 29 * Creates a small test course and checks all the components have been put in place. 30 */ 31 public function test_make_xs_course() { 32 global $DB; 33 34 $this->resetAfterTest(); 35 $this->setAdminUser(); 36 37 // Create the XS course. 38 $backend = new tool_generator_course_backend('TOOL_MAKELARGECOURSE_XS', 0, false, false, false); 39 $courseid = $backend->make(); 40 41 // Get course details. 42 $course = get_course($courseid); 43 $context = context_course::instance($courseid); 44 $modinfo = get_fast_modinfo($course); 45 46 // Check sections (just section 0 plus one other). 47 $this->assertEquals(2, count($modinfo->get_section_info_all())); 48 49 // Check user is enrolled. 50 $users = get_enrolled_users($context); 51 $this->assertEquals(1, count($users)); 52 $this->assertEquals('tool_generator_000001', reset($users)->username); 53 54 // Check there's a page on the course. 55 $pages = $modinfo->get_instances_of('page'); 56 $this->assertEquals(1, count($pages)); 57 58 // Check there are small files. 59 $resources = $modinfo->get_instances_of('resource'); 60 $ok = false; 61 foreach ($resources as $resource) { 62 if ($resource->sectionnum == 0) { 63 // The one in section 0 is the 'small files' resource. 64 $ok = true; 65 break; 66 } 67 } 68 $this->assertTrue($ok); 69 70 // Check it contains 2 files (the default txt and a dat file). 71 $fs = get_file_storage(); 72 $resourcecontext = context_module::instance($resource->id); 73 $files = $fs->get_area_files($resourcecontext->id, 'mod_resource', 'content', false, 'filename', false); 74 $files = array_values($files); 75 $this->assertEquals(2, count($files)); 76 $this->assertEquals('resource1.txt', $files[0]->get_filename()); 77 $this->assertEquals('smallfile0.dat', $files[1]->get_filename()); 78 79 // Check there's a single 'big' file (it's actually only 8KB). 80 $ok = false; 81 foreach ($resources as $resource) { 82 if ($resource->sectionnum == 1) { 83 $ok = true; 84 break; 85 } 86 } 87 $this->assertTrue($ok); 88 89 // Check it contains 2 files. 90 $resourcecontext = context_module::instance($resource->id); 91 $files = $fs->get_area_files($resourcecontext->id, 'mod_resource', 'content', false, 'filename', false); 92 $files = array_values($files); 93 $this->assertEquals(2, count($files)); 94 $this->assertEquals('bigfile0.dat', $files[0]->get_filename()); 95 $this->assertEquals('resource2.txt', $files[1]->get_filename()); 96 97 // Get forum and count the number of posts on it. 98 $forums = $modinfo->get_instances_of('forum'); 99 $forum = reset($forums); 100 $posts = $DB->count_records_sql(" 101 SELECT 102 COUNT(1) 103 FROM 104 {forum_posts} fp 105 JOIN {forum_discussions} fd ON fd.id = fp.discussion 106 WHERE 107 fd.forum = ?", array($forum->instance)); 108 $this->assertEquals(2, $posts); 109 } 110 111 /** 112 * Creates an small test course with fixed data set and checks the used sections and users. 113 */ 114 public function test_fixed_data_set() { 115 116 $this->resetAfterTest(); 117 $this->setAdminUser(); 118 119 // Create the S course (more sections and activities than XS). 120 $backend = new tool_generator_course_backend('TOOL_S_COURSE_1', 1, true, false, false); 121 $courseid = $backend->make(); 122 123 // Get course details. 124 $course = get_course($courseid); 125 $modinfo = get_fast_modinfo($course); 126 127 // Check module instances belongs to section 1. 128 $instances = $modinfo->get_instances_of('page'); 129 foreach ($instances as $instance) { 130 $this->assertEquals(1, $instance->sectionnum); 131 } 132 133 // Users that started discussions are the same. 134 $forums = $modinfo->get_instances_of('forum'); 135 $discussions = forum_get_discussions(reset($forums), 'd.timemodified ASC'); 136 $lastusernumber = 0; 137 $discussionstarters = array(); 138 foreach ($discussions as $discussion) { 139 $usernumber = intval($discussion->lastname); 140 141 // Checks that the users are odd numbers. 142 $this->assertEquals(1, $usernumber % 2); 143 144 // Checks that the users follows an increasing order. 145 $this->assertGreaterThan($lastusernumber, $usernumber); 146 $lastusernumber = $usernumber; 147 $discussionstarters[$discussion->userid] = $discussion->subject; 148 } 149 150 } 151 152 /** 153 * Creates a small test course specifying a maximum size and checks the generated files size is limited. 154 */ 155 public function test_filesize_limit() { 156 157 $this->resetAfterTest(); 158 $this->setAdminUser(); 159 160 // Limit. 161 $filesizelimit = 100; 162 163 // Create a limited XS course. 164 $backend = new tool_generator_course_backend('TOOL_XS_LIMITED', 0, false, $filesizelimit, false); 165 $courseid = $backend->make(); 166 167 $course = get_course($courseid); 168 $modinfo = get_fast_modinfo($course); 169 170 // Check there are small files. 171 $fs = get_file_storage(); 172 $resources = $modinfo->get_instances_of('resource'); 173 foreach ($resources as $resource) { 174 $resourcecontext = context_module::instance($resource->id); 175 $files = $fs->get_area_files($resourcecontext->id, 'mod_resource', 'content', false, 'filename', false); 176 foreach ($files as $file) { 177 if ($file->get_mimetype() == 'application/octet-stream') { 178 $this->assertLessThanOrEqual($filesizelimit, $file->get_filesize()); 179 } 180 } 181 } 182 183 // Create a non-limited XS course. 184 $backend = new tool_generator_course_backend('TOOL_XS_NOLIMITS', 0, false, false, false); 185 $courseid = $backend->make(); 186 187 $course = get_course($courseid); 188 $modinfo = get_fast_modinfo($course); 189 190 // Check there are small files. 191 $fs = get_file_storage(); 192 $resources = $modinfo->get_instances_of('resource'); 193 foreach ($resources as $resource) { 194 $resourcecontext = context_module::instance($resource->id); 195 $files = $fs->get_area_files($resourcecontext->id, 'mod_resource', 'content', false, 'filename', false); 196 foreach ($files as $file) { 197 if ($file->get_mimetype() == 'application/octet-stream') { 198 $this->assertGreaterThan($filesizelimit, (int)$file->get_filesize()); 199 } 200 } 201 } 202 203 } 204 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |