Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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 tests\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_testcase 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          global $DB;
  82  
  83          // We'll compare our results to those which are course-specific.
  84          $course = $this->getDataGenerator()->create_course();
  85          $user = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
  86          $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
  87          assign_capability('mod/lti:addmanualinstance', CAP_PROHIBIT, $teacherrole->id, \context_course::instance($course->id));
  88          $cir = new content_item_readonly_repository();
  89  
  90          // Course specific - lti won't be returned as the user doesn't have the required cap.
  91          $forcourse = $cir->find_all_for_course($course, $user);
  92          $forcourse = array_filter($forcourse, function($contentitem) {
  93              return $contentitem->get_name() === 'lti';
  94          });
  95          $this->assertEmpty($forcourse);
  96  
  97          // All - all items are returned, including lti.
  98          $all = $cir->find_all();
  99          $all = array_filter($all, function($contentitem) {
 100              return $contentitem->get_name() === 'lti';
 101          });
 102          $this->assertCount(1, $all);
 103      }
 104  }