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.
   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   * Unit tests for core\content class.
  19   *
  20   * @package     core
  21   * @category    test
  22   * @copyright   2020 Michael Hawkins <michaelh@moodle.com>
  23   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace core;
  27  
  28  /**
  29   * Unit tests for core\content class.
  30   *
  31   * @package     core
  32   * @category    test
  33   * @copyright   2020 Michael Hawkins <michaelh@moodle.com>
  34   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class content_test extends \advanced_testcase {
  37  
  38      /**
  39       * A test to confirm only valid cases allow exporting of course content.
  40       */
  41      public function test_can_export_context_course() {
  42          global $DB;
  43  
  44          $this->resetAfterTest();
  45  
  46          $course1 = $this->getDataGenerator()->create_course();
  47          $course2 = $this->getDataGenerator()->create_course();
  48          $course1context = \context_course::instance($course1->id);
  49          $course2context = \context_course::instance($course2->id);
  50  
  51          // Enrol user as student in course1 only.
  52          $user = $this->getDataGenerator()->create_and_enrol($course1, 'student');
  53  
  54          // Confirm by default enrolled user does not have permission to export in course1.
  55          $this->assertFalse(content::can_export_context($course1context, $user));
  56  
  57          // Make course download available on site, but not enabled in course1 or by default.
  58          set_config('downloadcoursecontentallowed', true);
  59  
  60          // Confirm user still does not have permission to export (disabled in courses by default).
  61          $this->assertFalse(content::can_export_context($course1context, $user));
  62  
  63          // Enable export in courses by default.
  64          set_config('downloadcontentsitedefault', DOWNLOAD_COURSE_CONTENT_ENABLED, 'moodlecourse');
  65  
  66          // Confirm user now has permission to export in course1 only.
  67          $this->assertTrue(content::can_export_context($course1context, $user));
  68  
  69          // Disable course downloads in course1.
  70          $course1->downloadcontent = DOWNLOAD_COURSE_CONTENT_DISABLED;
  71          $DB->update_record('course', $course1);
  72          rebuild_course_cache($course1->id);
  73  
  74          // Confirm user does not have permission to export in course1.
  75          $this->assertFalse(content::can_export_context($course1context, $user));
  76  
  77          // Enable course downloads in course1.
  78          $course1->downloadcontent = DOWNLOAD_COURSE_CONTENT_ENABLED;
  79          $DB->update_record('course', $course1);
  80          rebuild_course_cache($course1->id);
  81  
  82          // Confirm user has permission to export in course1.
  83          $this->assertTrue(content::can_export_context($course1context, $user));
  84  
  85          // Confirm user does not have permission to export in course they are not enrolled in (course2).
  86          $this->assertFalse(content::can_export_context($course2context, $user));
  87  
  88          // Disable export in courses by default.
  89          set_config('downloadcontentsitedefault', DOWNLOAD_COURSE_CONTENT_DISABLED, 'moodlecourse');
  90  
  91          // Confirm user still has permission to export in course1 (still enabled at the course level).
  92          $this->assertTrue(content::can_export_context($course1context, $user));
  93  
  94          // Disable the course downloads feature.
  95          set_config('downloadcoursecontentallowed', false);
  96  
  97          // Confirm user no longer has permission to export in course1.
  98          $this->assertFalse(content::can_export_context($course1context, $user));
  99      }
 100  
 101      /**
 102       * A test to confirm unsupported contexts will return false when checking whether content can be exported.
 103       */
 104      public function test_can_export_context_unsupported_context() {
 105          $this->resetAfterTest();
 106  
 107          $course1 = $this->getDataGenerator()->create_course();
 108          $systemcontext = \context_system::instance();
 109  
 110          // Enrol user as student in course1 only.
 111          $user = $this->getDataGenerator()->create_and_enrol($course1, 'student');
 112  
 113          // Make course download available on site (course context).
 114          set_config('downloadcoursecontentallowed', true);
 115  
 116          // Confirm system context does not gain permission to export content.
 117          $this->assertFalse(content::can_export_context($systemcontext, $user));
 118      }
 119  }