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 the request helper.
20 * @package core_privacy
22 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
30 use \core_privacy\local\request\helper
;
31 use \core_privacy\local\request\writer
;
34 * Tests for the \core_privacy API's request helper functionality.
36 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class request_helper_test
extends advanced_testcase
{
41 * Test that basic module data is returned.
43 public function test_get_context_data_context_module() {
44 $this->resetAfterTest();
47 $course = $this->getDataGenerator()->create_course();
48 $user = \core_user
::get_user_by_username('admin');
50 $forum = $this->getDataGenerator()->create_module('forum', [
51 'course' => $course->id
,
53 $context = context_module
::instance($forum->cmid
);
54 $modinfo = get_fast_modinfo($course->id
);
55 $cm = $modinfo->cms
[$context->instanceid
];
58 $result = helper
::get_context_data($context, $user);
59 $this->assertInstanceOf('stdClass', $result);
61 // Check that the name matches.
62 $this->assertEquals($cm->get_formatted_name(), $result->name
);
64 // This plugin supports the intro. Check that it is included and correct.
65 $formattedintro = format_text($forum->intro
, $forum->introformat
, [
68 'context' => $context,
69 'overflowdiv' => true,
71 $this->assertEquals($formattedintro, $result->intro
);
73 // This function should only fetch data. It does not export it.
74 $this->assertFalse(writer
::with_context($context)->has_any_data());
78 * Test that basic block data is returned.
80 public function test_get_context_data_context_block() {
81 $this->resetAfterTest();
84 $block = $this->getDataGenerator()->create_block('online_users');
85 $context = context_block
::instance($block->id
);
86 $user = \core_user
::get_user_by_username('admin');
89 $data = helper
::get_context_data($context, $user);
90 $this->assertEquals(get_string('pluginname', 'block_online_users'), $data->blocktype
);
92 // This function should only fetch data. It does not export it.
93 $this->assertFalse(writer
::with_context($context)->has_any_data());
97 * Test that a course moudle with completion tracking enabled has the completion data returned.
99 public function test_get_context_data_context_module_completion() {
100 $this->resetAfterTest();
102 // Create a module and set completion.
103 $course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
104 $user = $this->getDataGenerator()->create_user();
105 $this->getDataGenerator()->enrol_user($user->id
, $course->id
, 'student');
106 $assign = $this->getDataGenerator()->create_module('assign', ['course' => $course->id
, 'completion' => 1]);
107 $context = context_module
::instance($assign->cmid
);
108 $cm = get_coursemodule_from_id('assign', $assign->cmid
);
110 // Fetch context data.
111 $contextdata = helper
::get_context_data($context, $user);
113 // Completion state is zero.
114 // Check non completion for a user.
115 $this->assertEquals(0, $contextdata->completion
->state
);
117 // Complete the activity as a user.
118 $completioninfo = new completion_info($course);
119 $completioninfo->update_state($cm, COMPLETION_COMPLETE
, $user->id
);
121 // Check that completion is now exported.
122 $contextdata = helper
::get_context_data($context, $user);
123 $this->assertEquals(1, $contextdata->completion
->state
);
125 // This function should only fetch data. It does not export it.
126 $this->assertFalse(writer
::with_context($context)->has_any_data());
130 * Test that when there are no files to export for a course module context, nothing is exported.
132 public function test_export_context_files_context_module_no_files() {
133 $this->resetAfterTest();
136 $course = $this->getDataGenerator()->create_course();
137 $user = \core_user
::get_user_by_username('admin');
139 $forum = $this->getDataGenerator()->create_module('forum', [
140 'course' => $course->id
,
142 $context = context_module
::instance($forum->cmid
);
143 $modinfo = get_fast_modinfo($course->id
);
144 $cm = $modinfo->cms
[$context->instanceid
];
147 helper
::export_context_files($context, $user);
149 // This function should only fetch data. It does not export it.
150 $this->assertFalse(writer
::with_context($context)->has_any_data());
154 * Test that when there are no files to export for a course context, nothing is exported.
156 public function test_export_context_files_context_course_no_files() {
157 $this->resetAfterTest();
160 $course = $this->getDataGenerator()->create_course();
161 $user = \core_user
::get_user_by_username('admin');
162 $context = context_course
::instance($course->id
);
165 helper
::export_context_files($context, $user);
167 // This function should only fetch data. It does not export it.
168 $this->assertFalse(writer
::with_context($context)->has_any_data());
172 * Test that when there are files to export for a course context, the files are exported.
174 public function test_export_context_files_context_course_intro_files() {
175 $this->resetAfterTest();
178 $course = $this->getDataGenerator()->create_course();
179 $user = \core_user
::get_user_by_username('admin');
180 $assign = $this->getDataGenerator()->create_module('assign', ['course' => $course->id
]);
181 $context = context_module
::instance($assign->cmid
);
185 'contextid' => $context->id
,
186 'component' => 'mod_assign',
187 'filearea' => 'intro',
190 'filename' => 'logo.png',
193 $content = file_get_contents(__DIR__
. '/fixtures/logo.png');
196 $fs = get_file_storage();
197 $file = $fs->create_file_from_string($filerecord, $content);
200 helper
::export_context_files($context, $user);
202 // This should have resulted in the file being exported.
203 $this->assertTrue(writer
::with_context($context)->has_any_data());