Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

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