Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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      /**
 164       * @expectedException moodle_exception
 165       */
 166      public function test_empty_csv() {
 167          $this->resetAfterTest(true);
 168  
 169          $content = array();
 170          $content = implode("\n", $content);
 171          $iid = csv_import_reader::get_new_iid('uploadcourse');
 172          $cir = new csv_import_reader($iid, 'uploadcourse');
 173          $cir->load_csv_content($content, 'utf-8', 'comma');
 174          $cir->init();
 175  
 176          $options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_NEW);
 177          $p = new tool_uploadcourse_processor($cir, $options, array());
 178      }
 179  
 180      /**
 181       * @expectedException moodle_exception
 182       */
 183      public function test_not_enough_columns() {
 184          $this->resetAfterTest(true);
 185  
 186          $content = array(
 187              "shortname",
 188              "c1",
 189          );
 190          $content = implode("\n", $content);
 191          $iid = csv_import_reader::get_new_iid('uploadcourse');
 192          $cir = new csv_import_reader($iid, 'uploadcourse');
 193          $cir->load_csv_content($content, 'utf-8', 'comma');
 194          $cir->init();
 195  
 196          $options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_NEW);
 197          $p = new tool_uploadcourse_processor($cir, $options, array());
 198      }
 199  
 200      public function test_preview() {
 201          global $DB;
 202          $this->resetAfterTest(true);
 203  
 204          $content = array(
 205              "shortname,fullname,summary",
 206              "c1,Course 1,Course 1 summary",
 207              "c2,Course 2,Course 2 summary",
 208          );
 209          $content = implode("\n", $content);
 210          $iid = csv_import_reader::get_new_iid('uploadcourse');
 211          $cir = new csv_import_reader($iid, 'uploadcourse');
 212          $cir->load_csv_content($content, 'utf-8', 'comma');
 213          $cir->init();
 214  
 215          $options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_ALL);
 216          $defaults = array('category' => '1');
 217  
 218          $p = new tool_uploadcourse_processor($cir, $options, $defaults);
 219          // Nothing special to expect here, just make sure no exceptions are thrown.
 220          $p->preview();
 221      }
 222  
 223  }