Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   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          $users = get_enrolled_users($context);
  72          $this->assertEquals(1, count($users));
  73          $this->assertEquals('tool_generator_000001', reset($users)->username);
  74  
  75          // Check there's a page on the course.
  76          $pages = $modinfo->get_instances_of('page');
  77          $this->assertEquals(1, count($pages));
  78  
  79          // Check there are small files.
  80          $resources = $modinfo->get_instances_of('resource');
  81          $ok = false;
  82          foreach ($resources as $resource) {
  83              if ($resource->sectionnum == 0) {
  84                  // The one in section 0 is the 'small files' resource.
  85                  $ok = true;
  86                  break;
  87              }
  88          }
  89          $this->assertTrue($ok);
  90  
  91          // Check it contains 2 files (the default txt and a dat file).
  92          $fs = get_file_storage();
  93          $resourcecontext = \context_module::instance($resource->id);
  94          $files = $fs->get_area_files($resourcecontext->id, 'mod_resource', 'content', false, 'filename', false);
  95          $files = array_values($files);
  96          $this->assertEquals(2, count($files));
  97          $this->assertEquals('resource1.txt', $files[0]->get_filename());
  98          $this->assertEquals('smallfile0.dat', $files[1]->get_filename());
  99  
 100          // Check there's a single 'big' file (it's actually only 8KB).
 101          $ok = false;
 102          foreach ($resources as $resource) {
 103              if ($resource->sectionnum == 1) {
 104                  $ok = true;
 105                  break;
 106              }
 107          }
 108          $this->assertTrue($ok);
 109  
 110          // Check it contains 2 files.
 111          $resourcecontext = \context_module::instance($resource->id);
 112          $files = $fs->get_area_files($resourcecontext->id, 'mod_resource', 'content', false, 'filename', false);
 113          $files = array_values($files);
 114          $this->assertEquals(2, count($files));
 115          $this->assertEquals('bigfile0.dat', $files[0]->get_filename());
 116          $this->assertEquals('resource2.txt', $files[1]->get_filename());
 117  
 118          // Get forum and count the number of posts on it.
 119          $forums = $modinfo->get_instances_of('forum');
 120          $forum = reset($forums);
 121          $posts = $DB->count_records_sql("
 122                  SELECT
 123                      COUNT(1)
 124                  FROM
 125                      {forum_posts} fp
 126                      JOIN {forum_discussions} fd ON fd.id = fp.discussion
 127                  WHERE
 128                      fd.forum = ?", array($forum->instance));
 129          $this->assertEquals(2, $posts);
 130      }
 131  
 132      /**
 133       * Creates an small test course with fixed data set and checks the used sections and users.
 134       */
 135      public function test_fixed_data_set() {
 136  
 137          $this->resetAfterTest();
 138          $this->setAdminUser();
 139  
 140          // Create the S course (more sections and activities than XS).
 141          $backend = new tool_generator_course_backend('TOOL_S_COURSE_1', 1, true, false, false);
 142          $courseid = $backend->make();
 143  
 144          // Get course details.
 145          $course = get_course($courseid);
 146          $modinfo = get_fast_modinfo($course);
 147  
 148          // Check module instances belongs to section 1.
 149          $instances = $modinfo->get_instances_of('page');
 150          foreach ($instances as $instance) {
 151              $this->assertEquals(1, $instance->sectionnum);
 152          }
 153  
 154          // Users that started discussions are the same.
 155          $forums = $modinfo->get_instances_of('forum');
 156          $discussions = forum_get_discussions(reset($forums), 'd.timemodified ASC');
 157          $lastusernumber = 0;
 158          $discussionstarters = array();
 159          foreach ($discussions as $discussion) {
 160              $usernumber = \core_user::get_user($discussion->userid, 'id, idnumber')->idnumber;
 161  
 162              // Checks that the users are odd numbers.
 163              $this->assertEquals(1, $usernumber % 2);
 164  
 165              // Checks that the users follows an increasing order.
 166              $this->assertGreaterThan($lastusernumber, $usernumber);
 167              $lastusernumber = $usernumber;
 168              $discussionstarters[$discussion->userid] = $discussion->subject;
 169          }
 170  
 171      }
 172  
 173      /**
 174       * Creates a small test course specifying a maximum size and checks the generated files size is limited.
 175       */
 176      public function test_filesize_limit() {
 177  
 178          $this->resetAfterTest();
 179          $this->setAdminUser();
 180  
 181          // Limit.
 182          $filesizelimit = 100;
 183  
 184          // Create a limited XS course.
 185          $backend = new tool_generator_course_backend('TOOL_XS_LIMITED', 0, false, $filesizelimit, false);
 186          $courseid = $backend->make();
 187  
 188          $course = get_course($courseid);
 189          $modinfo = get_fast_modinfo($course);
 190  
 191          // Check there are small files.
 192          $fs = get_file_storage();
 193          $resources = $modinfo->get_instances_of('resource');
 194          foreach ($resources as $resource) {
 195              $resourcecontext = \context_module::instance($resource->id);
 196              $files = $fs->get_area_files($resourcecontext->id, 'mod_resource', 'content', false, 'filename', false);
 197              foreach ($files as $file) {
 198                  if ($file->get_mimetype() == 'application/octet-stream') {
 199                      $this->assertLessThanOrEqual($filesizelimit, $file->get_filesize());
 200                  }
 201              }
 202          }
 203  
 204          // Create a non-limited XS course.
 205          $backend = new tool_generator_course_backend('TOOL_XS_NOLIMITS', 0, false, false, false);
 206          $courseid = $backend->make();
 207  
 208          $course = get_course($courseid);
 209          $modinfo = get_fast_modinfo($course);
 210  
 211          // Check there are small files.
 212          $fs = get_file_storage();
 213          $resources = $modinfo->get_instances_of('resource');
 214          foreach ($resources as $resource) {
 215              $resourcecontext = \context_module::instance($resource->id);
 216              $files = $fs->get_area_files($resourcecontext->id, 'mod_resource', 'content', false, 'filename', false);
 217              foreach ($files as $file) {
 218                  if ($file->get_mimetype() == 'application/octet-stream') {
 219                      $this->assertGreaterThan($filesizelimit, (int)$file->get_filesize());
 220                  }
 221              }
 222          }
 223  
 224      }
 225  }