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 400] [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 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          // Unset untestable.
  68          unset($data->introeditor);
  69          unset($data->_advancedgradingdata);
  70  
  71          $this->assertEquals($expecteddata, $data);
  72  
  73          // Create a viewer user. Not able to edit.
  74          $viewer = self::getDataGenerator()->create_user();
  75          $this->getDataGenerator()->enrol_user($viewer->id, $course->id);
  76          $this->setUser($viewer);
  77          $this->expectException('required_capability_exception');
  78          prepare_new_moduleinfo_data($course, $assignmodule->name, $sectionnumber);
  79      }
  80  
  81      /**
  82       * Test get_moduleinfo_data
  83       */
  84      public function test_get_moduleinfo_data() {
  85          global $DB;
  86          $this->resetAfterTest(true);
  87          $this->setAdminUser();
  88          $course = self::getDataGenerator()->create_course();
  89          $assignmodule = $DB->get_record('modules', array('name' => 'assign'), '*', MUST_EXIST);
  90          $assign = self::getDataGenerator()->create_module('assign', array('course' => $course->id));
  91          $assigncm = get_coursemodule_from_id('assign', $assign->cmid);
  92          $assigncontext = \context_module::instance($assign->cmid);
  93  
  94          list($cm, $context, $module, $data, $cw) = get_moduleinfo_data($assigncm, $course);
  95          $this->assertEquals($assigncm, $cm);
  96          $this->assertEquals($assigncontext, $context);
  97          $this->assertEquals($assignmodule, $module);
  98  
  99          // Prepare expected data.
 100          $expecteddata = clone $assign;
 101          $expecteddata->coursemodule       = $assigncm->id;
 102          $expecteddata->section            = $cw->section;
 103          $expecteddata->visible            = $assigncm->visible;
 104          $expecteddata->visibleoncoursepage = $assigncm->visibleoncoursepage;
 105          $expecteddata->cmidnumber         = $assigncm->idnumber;
 106          $expecteddata->groupmode          = groups_get_activity_groupmode($cm);
 107          $expecteddata->groupingid         = $assigncm->groupingid;
 108          $expecteddata->course             = $course->id;
 109          $expecteddata->module             = $module->id;
 110          $expecteddata->modulename         = $module->name;
 111          $expecteddata->instance           = $assigncm->instance;
 112          $expecteddata->completion         = $assigncm->completion;
 113          $expecteddata->completionview     = $assigncm->completionview;
 114          $expecteddata->completionexpected = $assigncm->completionexpected;
 115          $expecteddata->completionusegrade = is_null($assigncm->completiongradeitemnumber) ? 0 : 1;
 116          $expecteddata->completiongradeitemnumber = null;
 117          $expecteddata->showdescription    = $assigncm->showdescription;
 118          $expecteddata->tags               = \core_tag_tag::get_item_tags_array('core', 'course_modules', $assigncm->id);
 119          $expecteddata->availabilityconditionsjson = null;
 120          $expecteddata->advancedgradingmethod_submissions = null;
 121          if ($items = \grade_item::fetch_all(array('itemtype' => 'mod', 'itemmodule' => 'assign',
 122                                                      'iteminstance' => $assign->id, 'courseid' => $course->id))) {
 123              // set category if present
 124              $gradecat = false;
 125              foreach ($items as $item) {
 126                  if ($gradecat === false) {
 127                      $gradecat = $item->categoryid;
 128                      continue;
 129                  }
 130                  if ($gradecat != $item->categoryid) {
 131                      //mixed categories
 132                      $gradecat = false;
 133                      break;
 134                  }
 135              }
 136              if ($gradecat !== false) {
 137                  // do not set if mixed categories present
 138                  $expecteddata->gradecat = $gradecat;
 139              }
 140          }
 141          $expecteddata->gradepass = '0.00';
 142  
 143          // Unset untestable.
 144          unset($expecteddata->cmid);
 145          unset($data->introeditor);
 146          unset($data->_advancedgradingdata);
 147  
 148          $this->assertEquals($expecteddata, $data);
 149  
 150          // Create a viewer user. Not able to edit.
 151          $viewer = self::getDataGenerator()->create_user();
 152          $this->getDataGenerator()->enrol_user($viewer->id, $course->id);
 153          $this->setUser($viewer);
 154          $this->expectException('required_capability_exception');
 155          get_moduleinfo_data($assigncm, $course);
 156      }
 157  }