Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 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  /**
  18   * Contains the test class for the content_item_readonly_repository class.
  19   *
  20   * @package    core
  21   * @subpackage course
  22   * @copyright  2020 Jake Dallimore <jrhdallimore@gmail.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  namespace core_course;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use core_course\local\entity\content_item;
  30  use core_course\local\repository\content_item_readonly_repository;
  31  
  32  /**
  33   * The test class for the content_item_readonly_repository class.
  34   *
  35   * @copyright  2020 Jake Dallimore <jrhdallimore@gmail.com>
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class content_item_readonly_repository_test extends \advanced_testcase {
  39      /**
  40       * Test the repository method, find_all_for_course().
  41       */
  42      public function test_find_all_for_course() {
  43          $this->resetAfterTest();
  44  
  45          $course = $this->getDataGenerator()->create_course();
  46          $user = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
  47          $cir = new content_item_readonly_repository();
  48  
  49          $items = $cir->find_all_for_course($course, $user);
  50          foreach ($items as $key => $item) {
  51              $this->assertInstanceOf(content_item::class, $item);
  52              $this->assertEquals($course->id, $item->get_link()->param('id'));
  53              $this->assertNotNull($item->get_link()->param('add'));
  54          }
  55      }
  56  
  57      /**
  58       * Test verifying that content items for hidden modules are not returned.
  59       */
  60      public function test_find_all_for_course_hidden_module() {
  61          $this->resetAfterTest();
  62          global $DB;
  63  
  64          $course = $this->getDataGenerator()->create_course();
  65          $user = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
  66          $cir = new content_item_readonly_repository();
  67  
  68          // Hide a module.
  69          $module = $DB->get_record('modules', ['id' => 1]);
  70          $DB->set_field("modules", "visible", "0", ["id" => $module->id]);
  71  
  72          $items = $cir->find_all_for_course($course, $user);
  73          $this->assertArrayNotHasKey($module->name, $items);
  74      }
  75  
  76      /**
  77       * Test confirming that all content items can be fetched, even those which require certain caps when in a course.
  78       */
  79      public function test_find_all() {
  80          $this->resetAfterTest();
  81  
  82          global $DB, $CFG;
  83          require_once($CFG->dirroot . '/mod/lti/tests/generator/lib.php');
  84          require_once($CFG->dirroot . '/mod/lti/locallib.php');
  85  
  86          // We'll compare our results to those which are course-specific, using mod_lti as an example.
  87          $course = $this->getDataGenerator()->create_course();
  88          $user = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
  89          /** @var \mod_lti_generator $ltigenerator */
  90          $ltigenerator = $this->getDataGenerator()->get_plugin_generator('mod_lti');
  91          $ltigenerator->create_tool_types([
  92              'name' => 'site tool',
  93              'baseurl' => 'http://example.com',
  94              'coursevisible' => LTI_COURSEVISIBLE_ACTIVITYCHOOSER,
  95              'state' => LTI_TOOL_STATE_CONFIGURED
  96          ]);
  97          $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
  98          assign_capability('mod/lti:addpreconfiguredinstance', CAP_PROHIBIT, $teacherrole->id,
  99              \core\context\course::instance($course->id));
 100          $cir = new content_item_readonly_repository();
 101          $this->setUser($user); // This is needed since the underlying lti code needs the global user despite the api accepting user.
 102  
 103          // Course specific - the tool won't be returned as the user doesn't have the capability required to use preconfigured tools.
 104          $forcourse = $cir->find_all_for_course($course, $user);
 105          $forcourse = array_filter($forcourse, function($contentitem) {
 106              return str_contains($contentitem->get_name(), 'lti_type');
 107          });
 108          $this->assertEmpty($forcourse);
 109  
 110          // All - all items are returned, including the lti site tool.
 111          $all = $cir->find_all();
 112          $all = array_filter($all, function($contentitem) {
 113              return str_contains($contentitem->get_name(), 'lti_type');
 114          });
 115          $this->assertCount(1, $all);
 116      }
 117  }