MDL-13114 tool_uploadcourse: Helper uses MUC for caching
[moodle.git] / admin / tool / uploadcourse / tests / processor_test.php
CommitLineData
509dd695
FM
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 * File containing tests for the processor.
19 *
20 * @package tool_uploadcourse
21 * @copyright 2013 Frédéric Massart
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27global $CFG;
3f57c87e 28require_once($CFG->libdir . '/csvlib.class.php');
509dd695
FM
29
30/**
31 * Processor test case.
3f57c87e
FM
32 *
33 * @package tool_uploadcourse
34 * @copyright 2013 Frédéric Massart
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
509dd695
FM
36 */
37class tool_uploadcourse_processor_testcase extends advanced_testcase {
38
39 public function test_basic() {
40 global $DB;
41 $this->resetAfterTest(true);
42
43 $content = array(
44 "shortname,fullname,summary",
45 "c1,Course 1,Course 1 summary",
46 "c2,Course 2,Course 2 summary",
47 );
48 $content = implode("\n", $content);
49 $iid = csv_import_reader::get_new_iid('uploadcourse');
50 $cir = new csv_import_reader($iid, 'uploadcourse');
51 $cir->load_csv_content($content, 'utf-8', 'comma');
52 $cir->init();
53
54 $options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_ALL);
55 $defaults = array('category' => '1');
56
57 $p = new tool_uploadcourse_processor($cir, $options, $defaults);
58 $this->assertFalse($DB->record_exists('course', array('shortname' => 'c1')));
59 $this->assertFalse($DB->record_exists('course', array('shortname' => 'c2')));
60 $p->execute();
61 $this->assertTrue($DB->record_exists('course', array('shortname' => 'c1')));
62 $this->assertTrue($DB->record_exists('course', array('shortname' => 'c2')));
63 }
64
65 public function test_restore_template_course() {
66 global $DB;
67 $this->resetAfterTest(true);
68 $this->setAdminUser();
69
70 $c1 = $this->getDataGenerator()->create_course();
71 $c1f1 = $this->getDataGenerator()->create_module('forum', array('course' => $c1->id));
72
73 $content = array(
74 "shortname,fullname,summary",
75 "c2,Course 2,Course 2 summary",
76 );
77 $content = implode("\n", $content);
78 $iid = csv_import_reader::get_new_iid('uploadcourse');
79 $cir = new csv_import_reader($iid, 'uploadcourse');
80 $cir->load_csv_content($content, 'utf-8', 'comma');
81 $cir->init();
82
83 $options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_NEW, 'templatecourse' => $c1->shortname);
84 $defaults = array('category' => '1');
85
86 $p = new tool_uploadcourse_processor($cir, $options, $defaults);
87 $this->assertFalse($DB->record_exists('course', array('shortname' => 'c2')));
88 $p->execute();
89 $c2 = $DB->get_record('course', array('shortname' => 'c2'));
90 $modinfo = get_fast_modinfo($c2);
91 $found = false;
92 foreach ($modinfo->get_cms() as $cmid => $cm) {
93 if ($cm->modname == 'forum' && $cm->name == $c1f1->name) {
94 $found = true;
95 break;
96 }
97 }
98 $this->assertTrue($found);
99
100 // Restore the time limit to prevent warning.
101 set_time_limit(0);
102 }
103
104 public function test_restore_restore_file() {
105 global $DB;
106 $this->resetAfterTest(true);
107 $this->setAdminUser();
108
109 $content = array(
110 "shortname,fullname,summary",
111 "c1,Course 1,Course 1 summary",
112 );
113 $content = implode("\n", $content);
114 $iid = csv_import_reader::get_new_iid('uploadcourse');
115 $cir = new csv_import_reader($iid, 'uploadcourse');
116 $cir->load_csv_content($content, 'utf-8', 'comma');
117 $cir->init();
118
119 $options = array(
120 'mode' => tool_uploadcourse_processor::MODE_CREATE_NEW,
121 'restorefile' => __DIR__ . '/fixtures/backup.mbz',
122 'templatecourse' => 'DoesNotExist' // Restorefile takes priority.
123 );
124 $defaults = array('category' => '1');
125
126 $p = new tool_uploadcourse_processor($cir, $options, $defaults);
127 $this->assertFalse($DB->record_exists('course', array('shortname' => 'c1')));
128 $p->execute();
129 $c1 = $DB->get_record('course', array('shortname' => 'c1'));
130 $modinfo = get_fast_modinfo($c1);
131 $found = false;
132 foreach ($modinfo->get_cms() as $cmid => $cm) {
133 if ($cm->modname == 'glossary' && $cm->name == 'Imported Glossary') {
134 $found = true;
135 break;
136 }
137 }
138 $this->assertTrue($found);
139
140 // Restore the time limit to prevent warning.
141 set_time_limit(0);
142 }
143
144 public function test_shortname_template() {
145 global $DB;
146 $this->resetAfterTest(true);
147
148 $content = array(
149 "shortname,fullname,summary,idnumber",
150 ",Course 1,C1 Summary,ID123",
151 );
152 $content = implode("\n", $content);
153 $iid = csv_import_reader::get_new_iid('uploadcourse');
154 $cir = new csv_import_reader($iid, 'uploadcourse');
155 $cir->load_csv_content($content, 'utf-8', 'comma');
156 $cir->init();
157
158 $options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_NEW, 'shortnametemplate' => '%i: %f');
159 $defaults = array('category' => '1');
160
161 $p = new tool_uploadcourse_processor($cir, $options, $defaults);
162 $this->assertFalse($DB->record_exists('course', array('idnumber' => 'ID123')));
163 $p->execute();
164 $this->assertTrue($DB->record_exists('course', array('idnumber' => 'ID123')));
165 $c = $DB->get_record('course', array('idnumber' => 'ID123'));
166 $this->assertEquals('ID123: Course 1', $c->shortname);
167 }
168
169}