weekly release 4.1dev
[moodle.git] / privacy / tests / request_helper_test.php
blob62f100960d12bc49c9d5206e45a176d7a2da5bdc
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.
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/>.
17 /**
18 * Unit Tests for the request helper.
20 * @package core_privacy
21 * @category test
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();
28 global $CFG;
30 use \core_privacy\local\request\helper;
31 use \core_privacy\local\request\writer;
33 /**
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 {
41 /**
42 * Test that basic module data is returned.
44 * @covers ::get_context_data
46 public function test_get_context_data_context_module() {
47 $this->resetAfterTest();
49 // Setup.
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,
55 ]);
56 $context = context_module::instance($forum->cmid);
57 $modinfo = get_fast_modinfo($course->id);
58 $cm = $modinfo->cms[$context->instanceid];
60 // Fetch the data.
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, [
69 'noclean' => true,
70 'para' => false,
71 'context' => $context,
72 'overflowdiv' => true,
73 ]);
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());
80 /**
81 * Test that basic block data is returned.
83 * @covers ::get_context_data
85 public function test_get_context_data_context_block() {
86 $this->resetAfterTest();
88 // Setup.
89 $block = $this->getDataGenerator()->create_block('online_users');
90 $context = context_block::instance($block->id);
91 $user = \core_user::get_user_by_username('admin');
93 // Fetch the data.
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() {
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() {
142 $this->resetAfterTest();
144 // Setup.
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];
155 // Fetch the data.
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() {
168 $this->resetAfterTest();
170 // Setup.
171 $course = $this->getDataGenerator()->create_course();
172 $user = \core_user::get_user_by_username('admin');
173 $context = context_course::instance($course->id);
175 // Fetch the data.
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() {
188 $this->resetAfterTest();
190 // Setup.
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);
196 // File details.
197 $filerecord = array(
198 'contextid' => $context->id,
199 'component' => 'mod_assign',
200 'filearea' => 'intro',
201 'itemid' => 0,
202 'filepath' => '/',
203 'filename' => 'logo.png',
206 $content = file_get_contents(__DIR__ . '/fixtures/logo.png');
208 // Store the file.
209 $fs = get_file_storage();
210 $file = $fs->create_file_from_string($filerecord, $content);
212 // Fetch the data.
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());