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]

   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  defined('MOODLE_INTERNAL') || die();
  18  
  19  /**
  20   * Automated unit testing. This tests the 'make large course' backend,
  21   * using the 'XS' option so that it completes quickly.
  22   *
  23   * @package tool_generator
  24   * @copyright 2013 The Open University
  25   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  class tool_generator_maketestcourse_testcase extends advanced_testcase {
  28      /**
  29       * Creates a small test course and checks all the components have been put in place.
  30       */
  31      public function test_make_xs_course() {
  32          global $DB;
  33  
  34          $this->resetAfterTest();
  35          $this->setAdminUser();
  36  
  37          $expectedshortname = 'TOOL_MAKELARGECOURSE_XS';
  38          $expectedfullname = 'Ridiculous fullname';
  39          $expectedsummary = 'who even knows what this is about';
  40  
  41          // Create the XS course.
  42          $backend = new tool_generator_course_backend(
  43              $expectedshortname,
  44              0,
  45              false,
  46              false,
  47              false,
  48              $expectedfullname,
  49              $expectedsummary
  50          );
  51          $courseid = $backend->make();
  52  
  53          // Get course details.
  54          $course = get_course($courseid);
  55          $context = context_course::instance($courseid);
  56          $modinfo = get_fast_modinfo($course);
  57  
  58          // Check course names.
  59          $this->assertEquals($expectedshortname, $course->shortname);
  60          $this->assertEquals($expectedfullname, $course->fullname);
  61  
  62          // Check course summary.
  63          $this->assertEquals($expectedsummary, $course->summary);
  64  
  65          // Check sections (just section 0 plus one other).
  66          $this->assertEquals(2, count($modinfo->get_section_info_all()));
  67  
  68          // Check user is enrolled.
  69          $users = get_enrolled_users($context);
  70          $this->assertEquals(1, count($users));
  71          $this->assertEquals('tool_generator_000001', reset($users)->username);
  72  
  73          // Check there's a page on the course.
  74          $pages = $modinfo->get_instances_of('page');
  75          $this->assertEquals(1, count($pages));
  76  
  77          // Check there are small files.
  78          $resources = $modinfo->get_instances_of('resource');
  79          $ok = false;
  80          foreach ($resources as $resource) {
  81              if ($resource->sectionnum == 0) {
  82                  // The one in section 0 is the 'small files' resource.
  83                  $ok = true;
  84                  break;
  85              }
  86          }
  87          $this->assertTrue($ok);
  88  
  89          // Check it contains 2 files (the default txt and a dat file).
  90          $fs = get_file_storage();
  91          $resourcecontext = context_module::instance($resource->id);
  92          $files = $fs->get_area_files($resourcecontext->id, 'mod_resource', 'content', false, 'filename', false);
  93          $files = array_values($files);
  94          $this->assertEquals(2, count($files));
  95          $this->assertEquals('resource1.txt', $files[0]->get_filename());
  96          $this->assertEquals('smallfile0.dat', $files[1]->get_filename());
  97  
  98          // Check there's a single 'big' file (it's actually only 8KB).
  99          $ok = false;
 100          foreach ($resources as $resource) {
 101              if ($resource->sectionnum == 1) {
 102                  $ok = true;
 103                  break;
 104              }
 105          }
 106          $this->assertTrue($ok);
 107  
 108          // Check it contains 2 files.
 109          $resourcecontext = context_module::instance($resource->id);
 110          $files = $fs->get_area_files($resourcecontext->id, 'mod_resource', 'content', false, 'filename', false);
 111          $files = array_values($files);
 112          $this->assertEquals(2, count($files));
 113          $this->assertEquals('bigfile0.dat', $files[0]->get_filename());
 114          $this->assertEquals('resource2.txt', $files[1]->get_filename());
 115  
 116          // Get forum and count the number of posts on it.
 117          $forums = $modinfo->get_instances_of('forum');
 118          $forum = reset($forums);
 119          $posts = $DB->count_records_sql("
 120                  SELECT
 121                      COUNT(1)
 122                  FROM
 123                      {forum_posts} fp
 124                      JOIN {forum_discussions} fd ON fd.id = fp.discussion
 125                  WHERE
 126                      fd.forum = ?", array($forum->instance));
 127          $this->assertEquals(2, $posts);
 128      }
 129  
 130      /**
 131       * Creates an small test course with fixed data set and checks the used sections and users.
 132       */
 133      public function test_fixed_data_set() {
 134  
 135          $this->resetAfterTest();
 136          $this->setAdminUser();
 137  
 138          // Create the S course (more sections and activities than XS).
 139          $backend = new tool_generator_course_backend('TOOL_S_COURSE_1', 1, true, false, false);
 140          $courseid = $backend->make();
 141  
 142          // Get course details.
 143          $course = get_course($courseid);
 144          $modinfo = get_fast_modinfo($course);
 145  
 146          // Check module instances belongs to section 1.
 147          $instances = $modinfo->get_instances_of('page');
 148          foreach ($instances as $instance) {
 149              $this->assertEquals(1, $instance->sectionnum);
 150          }
 151  
 152          // Users that started discussions are the same.
 153          $forums = $modinfo->get_instances_of('forum');
 154          $discussions = forum_get_discussions(reset($forums), 'd.timemodified ASC');
 155          $lastusernumber = 0;
 156          $discussionstarters = array();
 157          foreach ($discussions as $discussion) {
 158              $usernumber = core_user::get_user($discussion->userid, 'id, idnumber')->idnumber;
 159  
 160              // Checks that the users are odd numbers.
 161              $this->assertEquals(1, $usernumber % 2);
 162  
 163              // Checks that the users follows an increasing order.
 164              $this->assertGreaterThan($lastusernumber, $usernumber);
 165              $lastusernumber = $usernumber;
 166              $discussionstarters[$discussion->userid] = $discussion->subject;
 167          }
 168  
 169      }
 170  
 171      /**
 172       * Creates a small test course specifying a maximum size and checks the generated files size is limited.
 173       */
 174      public function test_filesize_limit() {
 175  
 176          $this->resetAfterTest();
 177          $this->setAdminUser();
 178  
 179          // Limit.
 180          $filesizelimit = 100;
 181  
 182          // Create a limited XS course.
 183          $backend = new tool_generator_course_backend('TOOL_XS_LIMITED', 0, false, $filesizelimit, false);
 184          $courseid = $backend->make();
 185  
 186          $course = get_course($courseid);
 187          $modinfo = get_fast_modinfo($course);
 188  
 189          // Check there are small files.
 190          $fs = get_file_storage();
 191          $resources = $modinfo->get_instances_of('resource');
 192          foreach ($resources as $resource) {
 193              $resourcecontext = context_module::instance($resource->id);
 194              $files = $fs->get_area_files($resourcecontext->id, 'mod_resource', 'content', false, 'filename', false);
 195              foreach ($files as $file) {
 196                  if ($file->get_mimetype() == 'application/octet-stream') {
 197                      $this->assertLessThanOrEqual($filesizelimit, $file->get_filesize());
 198                  }
 199              }
 200          }
 201  
 202          // Create a non-limited XS course.
 203          $backend = new tool_generator_course_backend('TOOL_XS_NOLIMITS', 0, false, false, false);
 204          $courseid = $backend->make();
 205  
 206          $course = get_course($courseid);
 207          $modinfo = get_fast_modinfo($course);
 208  
 209          // Check there are small files.
 210          $fs = get_file_storage();
 211          $resources = $modinfo->get_instances_of('resource');
 212          foreach ($resources as $resource) {
 213              $resourcecontext = context_module::instance($resource->id);
 214              $files = $fs->get_area_files($resourcecontext->id, 'mod_resource', 'content', false, 'filename', false);
 215              foreach ($files as $file) {
 216                  if ($file->get_mimetype() == 'application/octet-stream') {
 217                      $this->assertGreaterThan($filesizelimit, (int)$file->get_filesize());
 218                  }
 219              }
 220          }
 221  
 222      }
 223  }