Differences Between: [Versions 310 and 402] [Versions 39 and 402]
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 tool_uploadcourse; 18 19 use tool_uploadcourse_processor; 20 21 defined('MOODLE_INTERNAL') || die(); 22 23 global $CFG; 24 require_once($CFG->libdir . '/csvlib.class.php'); 25 26 /** 27 * Processor test case. 28 * 29 * @package tool_uploadcourse 30 * @copyright 2013 Frédéric Massart 31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 32 */ 33 class processor_test extends \advanced_testcase { 34 35 public function test_basic() { 36 global $DB; 37 $this->resetAfterTest(true); 38 39 $content = array( 40 "shortname,fullname,summary", 41 "c1,Course 1,Course 1 summary", 42 "c2,Course 2,Course 2 summary", 43 ); 44 $content = implode("\n", $content); 45 $iid = \csv_import_reader::get_new_iid('uploadcourse'); 46 $cir = new \csv_import_reader($iid, 'uploadcourse'); 47 $cir->load_csv_content($content, 'utf-8', 'comma'); 48 $cir->init(); 49 50 $options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_ALL); 51 $defaults = array('category' => '1'); 52 53 $p = new tool_uploadcourse_processor($cir, $options, $defaults); 54 $this->assertFalse($DB->record_exists('course', array('shortname' => 'c1'))); 55 $this->assertFalse($DB->record_exists('course', array('shortname' => 'c2'))); 56 $p->execute(); 57 $this->assertTrue($DB->record_exists('course', array('shortname' => 'c1'))); 58 $this->assertTrue($DB->record_exists('course', array('shortname' => 'c2'))); 59 } 60 61 public function test_restore_template_course() { 62 global $DB; 63 $this->resetAfterTest(true); 64 $this->setAdminUser(); 65 66 $c1 = $this->getDataGenerator()->create_course(); 67 $c1f1 = $this->getDataGenerator()->create_module('forum', array('course' => $c1->id)); 68 69 $content = array( 70 "shortname,fullname,summary", 71 "c2,Course 2,Course 2 summary", 72 ); 73 $content = implode("\n", $content); 74 $iid = \csv_import_reader::get_new_iid('uploadcourse'); 75 $cir = new \csv_import_reader($iid, 'uploadcourse'); 76 $cir->load_csv_content($content, 'utf-8', 'comma'); 77 $cir->init(); 78 79 $options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_NEW, 'templatecourse' => $c1->shortname); 80 $defaults = array('category' => '1'); 81 82 $p = new tool_uploadcourse_processor($cir, $options, $defaults); 83 $this->assertFalse($DB->record_exists('course', array('shortname' => 'c2'))); 84 $p->execute(); 85 $c2 = $DB->get_record('course', array('shortname' => 'c2')); 86 $modinfo = get_fast_modinfo($c2); 87 $found = false; 88 foreach ($modinfo->get_cms() as $cmid => $cm) { 89 if ($cm->modname == 'forum' && $cm->name == $c1f1->name) { 90 $found = true; 91 break; 92 } 93 } 94 $this->assertTrue($found); 95 } 96 97 public function test_restore_restore_file() { 98 global $DB; 99 $this->resetAfterTest(true); 100 $this->setAdminUser(); 101 102 $content = array( 103 "shortname,fullname,summary", 104 "c1,Course 1,Course 1 summary", 105 ); 106 $content = implode("\n", $content); 107 $iid = \csv_import_reader::get_new_iid('uploadcourse'); 108 $cir = new \csv_import_reader($iid, 'uploadcourse'); 109 $cir->load_csv_content($content, 'utf-8', 'comma'); 110 $cir->init(); 111 112 $options = array( 113 'mode' => tool_uploadcourse_processor::MODE_CREATE_NEW, 114 'restorefile' => __DIR__ . '/fixtures/backup.mbz', 115 'templatecourse' => 'DoesNotExist' // Restorefile takes priority. 116 ); 117 $defaults = array('category' => '1'); 118 119 $p = new tool_uploadcourse_processor($cir, $options, $defaults); 120 $this->assertFalse($DB->record_exists('course', array('shortname' => 'c1'))); 121 $p->execute(); 122 $c1 = $DB->get_record('course', array('shortname' => 'c1')); 123 $modinfo = get_fast_modinfo($c1); 124 $found = false; 125 foreach ($modinfo->get_cms() as $cmid => $cm) { 126 if ($cm->modname == 'glossary' && $cm->name == 'Imported Glossary') { 127 $found = true; 128 break; 129 } 130 } 131 $this->assertTrue($found); 132 } 133 134 public function test_shortname_template() { 135 global $DB; 136 $this->resetAfterTest(true); 137 138 $content = array( 139 "shortname,fullname,summary,idnumber", 140 ",Course 1,C1 Summary,ID123", 141 ); 142 $content = implode("\n", $content); 143 $iid = \csv_import_reader::get_new_iid('uploadcourse'); 144 $cir = new \csv_import_reader($iid, 'uploadcourse'); 145 $cir->load_csv_content($content, 'utf-8', 'comma'); 146 $cir->init(); 147 148 $options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_NEW, 'shortnametemplate' => '%i: %f'); 149 $defaults = array('category' => '1'); 150 151 $p = new tool_uploadcourse_processor($cir, $options, $defaults); 152 $this->assertFalse($DB->record_exists('course', array('idnumber' => 'ID123'))); 153 $p->execute(); 154 $this->assertTrue($DB->record_exists('course', array('idnumber' => 'ID123'))); 155 $c = $DB->get_record('course', array('idnumber' => 'ID123')); 156 $this->assertEquals('ID123: Course 1', $c->shortname); 157 } 158 159 public function test_empty_csv() { 160 $this->resetAfterTest(true); 161 162 $content = array(); 163 $content = implode("\n", $content); 164 $iid = \csv_import_reader::get_new_iid('uploadcourse'); 165 $cir = new \csv_import_reader($iid, 'uploadcourse'); 166 $cir->load_csv_content($content, 'utf-8', 'comma'); 167 $cir->init(); 168 169 $options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_NEW); 170 $this->expectException(\moodle_exception::class); 171 $p = new tool_uploadcourse_processor($cir, $options, array()); 172 } 173 174 public function test_not_enough_columns() { 175 $this->resetAfterTest(true); 176 177 $content = array( 178 "shortname", 179 "c1", 180 ); 181 $content = implode("\n", $content); 182 $iid = \csv_import_reader::get_new_iid('uploadcourse'); 183 $cir = new \csv_import_reader($iid, 'uploadcourse'); 184 $cir->load_csv_content($content, 'utf-8', 'comma'); 185 $cir->init(); 186 187 $options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_NEW); 188 $this->expectException(\moodle_exception::class); 189 $p = new tool_uploadcourse_processor($cir, $options, array()); 190 } 191 192 public function test_preview() { 193 global $DB; 194 $this->resetAfterTest(true); 195 196 $content = array( 197 "shortname,fullname,summary", 198 "c1,Course 1,Course 1 summary", 199 "c2,Course 2,Course 2 summary", 200 ); 201 $content = implode("\n", $content); 202 $iid = \csv_import_reader::get_new_iid('uploadcourse'); 203 $cir = new \csv_import_reader($iid, 'uploadcourse'); 204 $cir->load_csv_content($content, 'utf-8', 'comma'); 205 $cir->init(); 206 207 $options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_ALL); 208 $defaults = array('category' => '1'); 209 210 $p = new tool_uploadcourse_processor($cir, $options, $defaults); 211 // Nothing special to expect here, just make sure no exceptions are thrown. 212 $p->preview(); 213 } 214 215 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body