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] [Versions 401 and 402] [Versions 401 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  namespace core_course;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  global $CFG;
  22  require_once($CFG->dirroot . '/course/lib.php');
  23  require_once($CFG->dirroot . '/course/modlib.php');
  24  
  25  /**
  26   * Module lib related unit tests
  27   *
  28   * @package    core_course
  29   * @category   test
  30   * @copyright  2016 Juan Leyva
  31   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class modlib_test extends \advanced_testcase {
  34  
  35      /**
  36       * Test prepare_new_moduleinfo_data
  37       */
  38      public function test_prepare_new_moduleinfo_data() {
  39          global $DB;
  40          $this->resetAfterTest(true);
  41  
  42          $this->setAdminUser();
  43          $course = self::getDataGenerator()->create_course();
  44          $coursecontext = \context_course::instance($course->id);
  45          // Test with a complex module, like assign.
  46          $assignmodule = $DB->get_record('modules', array('name' => 'assign'), '*', MUST_EXIST);
  47          $sectionnumber = 1;
  48  
  49          list($module, $context, $cw, $cm, $data) = prepare_new_moduleinfo_data($course, $assignmodule->name, $sectionnumber);
  50          $this->assertEquals($assignmodule, $module);
  51          $this->assertEquals($coursecontext, $context);
  52          $this->assertNull($cm); // Not cm yet.
  53  
  54          $expecteddata = new \stdClass();
  55          $expecteddata->section          = $sectionnumber;
  56          $expecteddata->visible          = 1;
  57          $expecteddata->course           = $course->id;
  58          $expecteddata->module           = $module->id;
  59          $expecteddata->modulename       = $module->name;
  60          $expecteddata->groupmode        = $course->groupmode;
  61          $expecteddata->groupingid       = $course->defaultgroupingid;
  62          $expecteddata->id               = '';
  63          $expecteddata->instance         = '';
  64          $expecteddata->coursemodule     = '';
  65          $expecteddata->advancedgradingmethod_submissions = ''; // Not grading methods enabled by default.
  66          $expecteddata->completion       = 0;
  67          $expecteddata->downloadcontent  = DOWNLOAD_COURSE_CONTENT_ENABLED;
  68  
  69          // Unset untestable.
  70          unset($data->introeditor);
  71          unset($data->_advancedgradingdata);
  72  
  73          $this->assertEquals($expecteddata, $data);
  74  
  75          // Create a viewer user. Not able to edit.
  76          $viewer = self::getDataGenerator()->create_user();
  77          $this->getDataGenerator()->enrol_user($viewer->id, $course->id);
  78          $this->setUser($viewer);
  79          $this->expectException('required_capability_exception');
  80          prepare_new_moduleinfo_data($course, $assignmodule->name, $sectionnumber);
  81      }
  82  
  83      /**
  84       * Test get_moduleinfo_data
  85       */
  86      public function test_get_moduleinfo_data() {
  87          global $DB;
  88          $this->resetAfterTest(true);
  89          $this->setAdminUser();
  90          $course = self::getDataGenerator()->create_course();
  91          $assignmodule = $DB->get_record('modules', array('name' => 'assign'), '*', MUST_EXIST);
  92          $assign = self::getDataGenerator()->create_module('assign', array('course' => $course->id));
  93          $assigncm = get_coursemodule_from_id('assign', $assign->cmid);
  94          $assigncontext = \context_module::instance($assign->cmid);
  95  
  96          list($cm, $context, $module, $data, $cw) = get_moduleinfo_data($assigncm, $course);
  97          $this->assertEquals($assigncm, $cm);
  98          $this->assertEquals($assigncontext, $context);
  99          $this->assertEquals($assignmodule, $module);
 100  
 101          // Prepare expected data.
 102          $expecteddata = clone $assign;
 103          $expecteddata->coursemodule       = $assigncm->id;
 104          $expecteddata->section            = $cw->section;
 105          $expecteddata->visible            = $assigncm->visible;
 106          $expecteddata->visibleoncoursepage = $assigncm->visibleoncoursepage;
 107          $expecteddata->cmidnumber         = $assigncm->idnumber;
 108          $expecteddata->groupmode          = groups_get_activity_groupmode($cm);
 109          $expecteddata->groupingid         = $assigncm->groupingid;
 110          $expecteddata->course             = $course->id;
 111          $expecteddata->module             = $module->id;
 112          $expecteddata->modulename         = $module->name;
 113          $expecteddata->instance           = $assigncm->instance;
 114          $expecteddata->completion         = $assigncm->completion;
 115          $expecteddata->completionview     = $assigncm->completionview;
 116          $expecteddata->completionexpected = $assigncm->completionexpected;
 117          $expecteddata->completionusegrade = is_null($assigncm->completiongradeitemnumber) ? 0 : 1;
 118          $expecteddata->completionpassgrade = $assigncm->completionpassgrade;
 119          $expecteddata->completiongradeitemnumber = null;
 120          $expecteddata->showdescription    = $assigncm->showdescription;
 121          $expecteddata->downloadcontent    = $assigncm->downloadcontent;
 122          $expecteddata->tags               = \core_tag_tag::get_item_tags_array('core', 'course_modules', $assigncm->id);
 123          $expecteddata->lang               = null;
 124          $expecteddata->availabilityconditionsjson = null;
 125          $expecteddata->advancedgradingmethod_submissions = null;
 126          if ($items = \grade_item::fetch_all(array('itemtype' => 'mod', 'itemmodule' => 'assign',
 127                                                      'iteminstance' => $assign->id, 'courseid' => $course->id))) {
 128              // set category if present
 129              $gradecat = false;
 130              foreach ($items as $item) {
 131                  if ($gradecat === false) {
 132                      $gradecat = $item->categoryid;
 133                      continue;
 134                  }
 135                  if ($gradecat != $item->categoryid) {
 136                      //mixed categories
 137                      $gradecat = false;
 138                      break;
 139                  }
 140              }
 141              if ($gradecat !== false) {
 142                  // do not set if mixed categories present
 143                  $expecteddata->gradecat = $gradecat;
 144              }
 145          }
 146          $expecteddata->gradepass = '0.00';
 147          $expecteddata->completionpassgrade = $assigncm->completionpassgrade;
 148  
 149          // Unset untestable.
 150          unset($expecteddata->cmid);
 151          unset($data->introeditor);
 152          unset($data->_advancedgradingdata);
 153  
 154          $this->assertEquals($expecteddata, $data);
 155  
 156          // Create a viewer user. Not able to edit.
 157          $viewer = self::getDataGenerator()->create_user();
 158          $this->getDataGenerator()->enrol_user($viewer->id, $course->id);
 159          $this->setUser($viewer);
 160          $this->expectException('required_capability_exception');
 161          get_moduleinfo_data($assigncm, $course);
 162      }
 163  }