[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/admin/tool/uploadcourse/tests/ -> processor_test.php (source)

   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      /**
  40       * Tidy up open files that may be left open.
  41       */
  42      protected function tearDown() {
  43          gc_collect_cycles();
  44      }
  45  
  46      public function test_basic() {
  47          global $DB;
  48          $this->resetAfterTest(true);
  49  
  50          $content = array(
  51              "shortname,fullname,summary",
  52              "c1,Course 1,Course 1 summary",
  53              "c2,Course 2,Course 2 summary",
  54          );
  55          $content = implode("\n", $content);
  56          $iid = csv_import_reader::get_new_iid('uploadcourse');
  57          $cir = new csv_import_reader($iid, 'uploadcourse');
  58          $cir->load_csv_content($content, 'utf-8', 'comma');
  59          $cir->init();
  60  
  61          $options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_ALL);
  62          $defaults = array('category' => '1');
  63  
  64          $p = new tool_uploadcourse_processor($cir, $options, $defaults);
  65          $this->assertFalse($DB->record_exists('course', array('shortname' => 'c1')));
  66          $this->assertFalse($DB->record_exists('course', array('shortname' => 'c2')));
  67          $p->execute();
  68          $this->assertTrue($DB->record_exists('course', array('shortname' => 'c1')));
  69          $this->assertTrue($DB->record_exists('course', array('shortname' => 'c2')));
  70      }
  71  
  72      public function test_restore_template_course() {
  73          global $DB;
  74          $this->resetAfterTest(true);
  75          $this->setAdminUser();
  76  
  77          $c1 = $this->getDataGenerator()->create_course();
  78          $c1f1 = $this->getDataGenerator()->create_module('forum', array('course' => $c1->id));
  79  
  80          $content = array(
  81              "shortname,fullname,summary",
  82              "c2,Course 2,Course 2 summary",
  83          );
  84          $content = implode("\n", $content);
  85          $iid = csv_import_reader::get_new_iid('uploadcourse');
  86          $cir = new csv_import_reader($iid, 'uploadcourse');
  87          $cir->load_csv_content($content, 'utf-8', 'comma');
  88          $cir->init();
  89  
  90          $options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_NEW, 'templatecourse' => $c1->shortname);
  91          $defaults = array('category' => '1');
  92  
  93          $p = new tool_uploadcourse_processor($cir, $options, $defaults);
  94          $this->assertFalse($DB->record_exists('course', array('shortname' => 'c2')));
  95          $p->execute();
  96          $c2 = $DB->get_record('course', array('shortname' => 'c2'));
  97          $modinfo = get_fast_modinfo($c2);
  98          $found = false;
  99          foreach ($modinfo->get_cms() as $cmid => $cm) {
 100              if ($cm->modname == 'forum' && $cm->name == $c1f1->name) {
 101                  $found = true;
 102                  break;
 103              }
 104          }
 105          $this->assertTrue($found);
 106      }
 107  
 108      public function test_restore_restore_file() {
 109          global $DB;
 110          $this->resetAfterTest(true);
 111          $this->setAdminUser();
 112  
 113          $content = array(
 114              "shortname,fullname,summary",
 115              "c1,Course 1,Course 1 summary",
 116          );
 117          $content = implode("\n", $content);
 118          $iid = csv_import_reader::get_new_iid('uploadcourse');
 119          $cir = new csv_import_reader($iid, 'uploadcourse');
 120          $cir->load_csv_content($content, 'utf-8', 'comma');
 121          $cir->init();
 122  
 123          $options = array(
 124              'mode' => tool_uploadcourse_processor::MODE_CREATE_NEW,
 125              'restorefile' => __DIR__ . '/fixtures/backup.mbz',
 126              'templatecourse' => 'DoesNotExist'  // Restorefile takes priority.
 127          );
 128          $defaults = array('category' => '1');
 129  
 130          $p = new tool_uploadcourse_processor($cir, $options, $defaults);
 131          $this->assertFalse($DB->record_exists('course', array('shortname' => 'c1')));
 132          $p->execute();
 133          $c1 = $DB->get_record('course', array('shortname' => 'c1'));
 134          $modinfo = get_fast_modinfo($c1);
 135          $found = false;
 136          foreach ($modinfo->get_cms() as $cmid => $cm) {
 137              if ($cm->modname == 'glossary' && $cm->name == 'Imported Glossary') {
 138                  $found = true;
 139                  break;
 140              }
 141          }
 142          $this->assertTrue($found);
 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      public function test_empty_csv() {
 171          $this->resetAfterTest(true);
 172  
 173          $content = array();
 174          $content = implode("\n", $content);
 175          $iid = csv_import_reader::get_new_iid('uploadcourse');
 176          $cir = new csv_import_reader($iid, 'uploadcourse');
 177          $cir->load_csv_content($content, 'utf-8', 'comma');
 178          $cir->init();
 179  
 180          $options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_NEW);
 181          $this->setExpectedException('moodle_exception');
 182          $p = new tool_uploadcourse_processor($cir, $options, array());
 183      }
 184  
 185      public function test_not_enough_columns() {
 186          $this->resetAfterTest(true);
 187  
 188          $content = array(
 189              "shortname",
 190              "c1",
 191          );
 192          $content = implode("\n", $content);
 193          $iid = csv_import_reader::get_new_iid('uploadcourse');
 194          $cir = new csv_import_reader($iid, 'uploadcourse');
 195          $cir->load_csv_content($content, 'utf-8', 'comma');
 196          $cir->init();
 197  
 198          $options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_NEW);
 199          $this->setExpectedException('moodle_exception');
 200          $p = new tool_uploadcourse_processor($cir, $options, array());
 201      }
 202  
 203      public function test_preview() {
 204          global $DB;
 205          $this->resetAfterTest(true);
 206  
 207          $content = array(
 208              "shortname,fullname,summary",
 209              "c1,Course 1,Course 1 summary",
 210              "c2,Course 2,Course 2 summary",
 211          );
 212          $content = implode("\n", $content);
 213          $iid = csv_import_reader::get_new_iid('uploadcourse');
 214          $cir = new csv_import_reader($iid, 'uploadcourse');
 215          $cir->load_csv_content($content, 'utf-8', 'comma');
 216          $cir->init();
 217  
 218          $options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_ALL);
 219          $defaults = array('category' => '1');
 220  
 221          $p = new tool_uploadcourse_processor($cir, $options, $defaults);
 222          // Nothing special to expect here, just make sure no exceptions are thrown.
 223          $p->preview();
 224      }
 225  
 226  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1