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