2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Unit tests for core\content class.
22 * @copyright 2020 Michael Hawkins <michaelh@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 * Unit tests for core\content class.
33 * @copyright 2020 Michael Hawkins <michaelh@moodle.com>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class content_test
extends \advanced_testcase
{
39 * A test to confirm only valid cases allow exporting of course content.
41 public function test_can_export_context_course() {
44 $this->resetAfterTest();
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
);
51 // Enrol user as student in course1 only.
52 $user = $this->getDataGenerator()->create_and_enrol($course1, 'student');
54 // Confirm by default enrolled user does not have permission to export in course1.
55 $this->assertFalse(content
::can_export_context($course1context, $user));
57 // Make course download available on site, but not enabled in course1 or by default.
58 set_config('downloadcoursecontentallowed', true);
60 // Confirm user still does not have permission to export (disabled in courses by default).
61 $this->assertFalse(content
::can_export_context($course1context, $user));
63 // Enable export in courses by default.
64 set_config('downloadcontentsitedefault', DOWNLOAD_COURSE_CONTENT_ENABLED
, 'moodlecourse');
66 // Confirm user now has permission to export in course1 only.
67 $this->assertTrue(content
::can_export_context($course1context, $user));
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
);
74 // Confirm user does not have permission to export in course1.
75 $this->assertFalse(content
::can_export_context($course1context, $user));
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
);
82 // Confirm user has permission to export in course1.
83 $this->assertTrue(content
::can_export_context($course1context, $user));
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));
88 // Disable export in courses by default.
89 set_config('downloadcontentsitedefault', DOWNLOAD_COURSE_CONTENT_DISABLED
, 'moodlecourse');
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));
94 // Disable the course downloads feature.
95 set_config('downloadcoursecontentallowed', false);
97 // Confirm user no longer has permission to export in course1.
98 $this->assertFalse(content
::can_export_context($course1context, $user));
102 * A test to confirm unsupported contexts will return false when checking whether content can be exported.
104 public function test_can_export_context_unsupported_context() {
105 $this->resetAfterTest();
107 $course1 = $this->getDataGenerator()->create_course();
108 $systemcontext = \context_system
::instance();
110 // Enrol user as student in course1 only.
111 $user = $this->getDataGenerator()->create_and_enrol($course1, 'student');
113 // Make course download available on site (course context).
114 set_config('downloadcoursecontentallowed', true);
116 // Confirm system context does not gain permission to export content.
117 $this->assertFalse(content
::can_export_context($systemcontext, $user));