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
38 * @coversDefaultClass \core_privacy\local\request\helper
40 class request_helper_test
extends advanced_testcase
{
42 * Test that basic module data is returned.
44 * @covers ::get_context_data
46 public function test_get_context_data_context_module(): void
{
47 $this->resetAfterTest();
50 $course = $this->getDataGenerator()->create_course();
51 $user = \core_user
::get_user_by_username('admin');
53 $forum = $this->getDataGenerator()->create_module('forum', [
54 'course' => $course->id
,
56 $context = context_module
::instance($forum->cmid
);
57 $modinfo = get_fast_modinfo($course->id
);
58 $cm = $modinfo->cms
[$context->instanceid
];
61 $result = helper
::get_context_data($context, $user);
62 $this->assertInstanceOf('stdClass', $result);
64 // Check that the name matches.
65 $this->assertEquals($cm->get_formatted_name(), $result->name
);
67 // This plugin supports the intro. Check that it is included and correct.
68 $formattedintro = format_text($forum->intro
, $forum->introformat
, [
71 'context' => $context,
72 'overflowdiv' => true,
74 $this->assertEquals($formattedintro, $result->intro
);
76 // This function should only fetch data. It does not export it.
77 $this->assertFalse(writer
::with_context($context)->has_any_data());
81 * Test that basic block data is returned.
83 * @covers ::get_context_data
85 public function test_get_context_data_context_block(): void
{
86 $this->resetAfterTest();
89 $block = $this->getDataGenerator()->create_block('online_users');
90 $context = context_block
::instance($block->id
);
91 $user = \core_user
::get_user_by_username('admin');
94 $data = helper
::get_context_data($context, $user);
95 $this->assertEquals(get_string('pluginname', 'block_online_users'), $data->blocktype
);
97 // This function should only fetch data. It does not export it.
98 $this->assertFalse(writer
::with_context($context)->has_any_data());
102 * Test that a course moudle with completion tracking enabled has the completion data returned.
104 * @covers ::get_context_data
106 public function test_get_context_data_context_module_completion(): void
{
107 $this->resetAfterTest();
109 // Create a module and set completion.
110 $course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
111 $user = $this->getDataGenerator()->create_user();
112 $this->getDataGenerator()->enrol_user($user->id
, $course->id
, 'student');
113 $assign = $this->getDataGenerator()->create_module('assign', ['course' => $course->id
, 'completion' => 1]);
114 $context = context_module
::instance($assign->cmid
);
115 $cm = get_coursemodule_from_id('assign', $assign->cmid
);
117 // Fetch context data.
118 $contextdata = helper
::get_context_data($context, $user);
120 // Completion state is zero.
121 // Check non completion for a user.
122 $this->assertEquals(0, $contextdata->completion
->state
);
124 // Complete the activity as a user.
125 $completioninfo = new completion_info($course);
126 $completioninfo->update_state($cm, COMPLETION_COMPLETE
, $user->id
);
128 // Check that completion is now exported.
129 $contextdata = helper
::get_context_data($context, $user);
130 $this->assertEquals(1, $contextdata->completion
->state
);
132 // This function should only fetch data. It does not export it.
133 $this->assertFalse(writer
::with_context($context)->has_any_data());
137 * Test that when there are no files to export for a course module context, nothing is exported.
139 * @covers ::export_context_files
141 public function test_export_context_files_context_module_no_files(): void
{
142 $this->resetAfterTest();
145 $course = $this->getDataGenerator()->create_course();
146 $user = \core_user
::get_user_by_username('admin');
148 $forum = $this->getDataGenerator()->create_module('forum', [
149 'course' => $course->id
,
151 $context = context_module
::instance($forum->cmid
);
152 $modinfo = get_fast_modinfo($course->id
);
153 $cm = $modinfo->cms
[$context->instanceid
];
156 helper
::export_context_files($context, $user);
158 // This function should only fetch data. It does not export it.
159 $this->assertFalse(writer
::with_context($context)->has_any_data());
163 * Test that when there are no files to export for a course context, nothing is exported.
165 * @covers ::export_context_files
167 public function test_export_context_files_context_course_no_files(): void
{
168 $this->resetAfterTest();
171 $course = $this->getDataGenerator()->create_course();
172 $user = \core_user
::get_user_by_username('admin');
173 $context = context_course
::instance($course->id
);
176 helper
::export_context_files($context, $user);
178 // This function should only fetch data. It does not export it.
179 $this->assertFalse(writer
::with_context($context)->has_any_data());
183 * Test that when there are files to export for a course context, the files are exported.
185 * @covers ::export_context_files
187 public function test_export_context_files_context_course_intro_files(): void
{
188 $this->resetAfterTest();
191 $course = $this->getDataGenerator()->create_course();
192 $user = \core_user
::get_user_by_username('admin');
193 $assign = $this->getDataGenerator()->create_module('assign', ['course' => $course->id
]);
194 $context = context_module
::instance($assign->cmid
);
198 'contextid' => $context->id
,
199 'component' => 'mod_assign',
200 'filearea' => 'intro',
203 'filename' => 'logo.png',
206 $content = file_get_contents(self
::get_fixture_path('core_privacy', 'logo.png'));
209 $fs = get_file_storage();
210 $file = $fs->create_file_from_string($filerecord, $content);
213 helper
::export_context_files($context, $user);
215 // This should have resulted in the file being exported.
216 $this->assertTrue(writer
::with_context($context)->has_any_data());