Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  global $CFG;
  28  require_once($CFG->libdir . '/csvlib.class.php');
  29  
  30  /**
  31   * Processor test case.
  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
  36   */
  37  class 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  
 101      public function test_restore_restore_file() {
 102          global $DB;
 103          $this->resetAfterTest(true);
 104          $this->setAdminUser();
 105  
 106          $content = array(
 107              "shortname,fullname,summary",
 108              "c1,Course 1,Course 1 summary",
 109          );
 110          $content = implode("\n", $content);
 111          $iid = csv_import_reader::get_new_iid('uploadcourse');
 112          $cir = new csv_import_reader($iid, 'uploadcourse');
 113          $cir->load_csv_content($content, 'utf-8', 'comma');
 114          $cir->init();
 115  
 116          $options = array(
 117              'mode' => tool_uploadcourse_processor::MODE_CREATE_NEW,
 118              'restorefile' => __DIR__ . '/fixtures/backup.mbz',
 119              'templatecourse' => 'DoesNotExist'  // Restorefile takes priority.
 120          );
 121          $defaults = array('category' => '1');
 122  
 123          $p = new tool_uploadcourse_processor($cir, $options, $defaults);
 124          $this->assertFalse($DB->record_exists('course', array('shortname' => 'c1')));
 125          $p->execute();
 126          $c1 = $DB->get_record('course', array('shortname' => 'c1'));
 127          $modinfo = get_fast_modinfo($c1);
 128          $found = false;
 129          foreach ($modinfo->get_cms() as $cmid => $cm) {
 130              if ($cm->modname == 'glossary' && $cm->name == 'Imported Glossary') {
 131                  $found = true;
 132                  break;
 133              }
 134          }
 135          $this->assertTrue($found);
 136      }
 137  
 138      public function test_shortname_template() {
 139          global $DB;
 140          $this->resetAfterTest(true);
 141  
 142          $content = array(
 143              "shortname,fullname,summary,idnumber",
 144              ",Course 1,C1 Summary,ID123",
 145          );
 146          $content = implode("\n", $content);
 147          $iid = csv_import_reader::get_new_iid('uploadcourse');
 148          $cir = new csv_import_reader($iid, 'uploadcourse');
 149          $cir->load_csv_content($content, 'utf-8', 'comma');
 150          $cir->init();
 151  
 152          $options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_NEW, 'shortnametemplate' => '%i: %f');
 153          $defaults = array('category' => '1');
 154  
 155          $p = new tool_uploadcourse_processor($cir, $options, $defaults);
 156          $this->assertFalse($DB->record_exists('course', array('idnumber' => 'ID123')));
 157          $p->execute();
 158          $this->assertTrue($DB->record_exists('course', array('idnumber' => 'ID123')));
 159          $c = $DB->get_record('course', array('idnumber' => 'ID123'));
 160          $this->assertEquals('ID123: Course 1', $c->shortname);
 161      }
 162  
 163      public function test_empty_csv() {
 164          $this->resetAfterTest(true);
 165  
 166          $content = array();
 167          $content = implode("\n", $content);
 168          $iid = csv_import_reader::get_new_iid('uploadcourse');
 169          $cir = new csv_import_reader($iid, 'uploadcourse');
 170          $cir->load_csv_content($content, 'utf-8', 'comma');
 171          $cir->init();
 172  
 173          $options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_NEW);
 174          $this->expectException(moodle_exception::class);
 175          $p = new tool_uploadcourse_processor($cir, $options, array());
 176      }
 177  
 178      public function test_not_enough_columns() {
 179          $this->resetAfterTest(true);
 180  
 181          $content = array(
 182              "shortname",
 183              "c1",
 184          );
 185          $content = implode("\n", $content);
 186          $iid = csv_import_reader::get_new_iid('uploadcourse');
 187          $cir = new csv_import_reader($iid, 'uploadcourse');
 188          $cir->load_csv_content($content, 'utf-8', 'comma');
 189          $cir->init();
 190  
 191          $options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_NEW);
 192          $this->expectException(moodle_exception::class);
 193          $p = new tool_uploadcourse_processor($cir, $options, array());
 194      }
 195  
 196      public function test_preview() {
 197          global $DB;
 198          $this->resetAfterTest(true);
 199  
 200          $content = array(
 201              "shortname,fullname,summary",
 202              "c1,Course 1,Course 1 summary",
 203              "c2,Course 2,Course 2 summary",
 204          );
 205          $content = implode("\n", $content);
 206          $iid = csv_import_reader::get_new_iid('uploadcourse');
 207          $cir = new csv_import_reader($iid, 'uploadcourse');
 208          $cir->load_csv_content($content, 'utf-8', 'comma');
 209          $cir->init();
 210  
 211          $options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_ALL);
 212          $defaults = array('category' => '1');
 213  
 214          $p = new tool_uploadcourse_processor($cir, $options, $defaults);
 215          // Nothing special to expect here, just make sure no exceptions are thrown.
 216          $p->preview();
 217      }
 218  
 219  }