MDL-61976 mod_wiki: Trim returned value to avoid EOF-related failures
[moodle.git] / portfolio / tests / privacy_provider_test.php
blob93ffb98167cbe6dce75db3c1888d0c4cd813a945
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 * Privacy provider tests.
20 * @package core_portfolio
21 * @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 /**
28 * Privacy provider tests class.
30 * @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 class portfolio_privacy_provider_test extends \core_privacy\tests\provider_testcase {
35 protected function create_portfolio_data($plugin, $name, $user, $preference, $value) {
36 global $DB;
37 $portfolioinstance = (object) [
38 'plugin' => $plugin,
39 'name' => $name,
40 'visible' => 1
42 $portfolioinstance->id = $DB->insert_record('portfolio_instance', $portfolioinstance);
43 $userinstance = (object) [
44 'instance' => $portfolioinstance->id,
45 'userid' => $user->id,
46 'name' => $preference,
47 'value' => $value
49 $DB->insert_record('portfolio_instance_user', $userinstance);
52 /**
53 * Verify that a collection of metadata is returned for this component and that it just returns the righ types for 'portfolio'.
55 public function test_get_metadata() {
56 $collection = new \core_privacy\local\metadata\collection('core_portfolio');
57 $collection = \core_portfolio\privacy\provider::get_metadata($collection);
58 $this->assertNotEmpty($collection);
59 $items = $collection->get_collection();
60 $this->assertEquals(2, count($items));
61 $this->assertInstanceOf(\core_privacy\local\metadata\types\database_table::class, $items[0]);
62 $this->assertInstanceOf(\core_privacy\local\metadata\types\plugintype_link::class, $items[1]);
65 /**
66 * Test that the export for a user id returns a user context.
68 public function test_get_contexts_for_userid() {
69 $this->resetAfterTest();
70 $user = $this->getDataGenerator()->create_user();
71 $context = context_user::instance($user->id);
72 $this->create_portfolio_data('googledocs', 'Google Docs', $user, 'visible', 1);
73 $contextlist = \core_portfolio\privacy\provider::get_contexts_for_userid($user->id);
74 $this->assertEquals($context->id, $contextlist->current()->id);
77 /**
78 * Test that exporting user data works as expected.
80 public function test_export_user_data() {
81 $this->resetAfterTest();
82 $user = $this->getDataGenerator()->create_user();
83 $context = context_user::instance($user->id);
84 $this->create_portfolio_data('googledocs', 'Google Docs', $user, 'visible', 1);
85 $contextlist = new \core_privacy\local\request\approved_contextlist($user, 'core_portfolio', [$context->id]);
86 \core_portfolio\privacy\provider::export_user_data($contextlist);
87 $writer = \core_privacy\local\request\writer::with_context($context);
88 $portfoliodata = $writer->get_data([get_string('privacy:path', 'portfolio')]);
89 $this->assertEquals('Google Docs', $portfoliodata->{'Google Docs'}->name);
92 /**
93 * Test that deleting only results in the one context being removed.
95 public function test_delete_data_for_all_users_in_context() {
96 global $DB;
98 $this->resetAfterTest();
99 $user1 = $this->getDataGenerator()->create_user();
100 $user2 = $this->getDataGenerator()->create_user();
101 $this->create_portfolio_data('googledocs', 'Google Docs', $user1, 'visible', 1);
102 $this->create_portfolio_data('onedrive', 'Microsoft onedrive', $user2, 'visible', 1);
103 // Check a system context sent through.
104 $systemcontext = context_system::instance();
105 \core_portfolio\privacy\provider::delete_data_for_all_users_in_context($systemcontext);
106 $records = $DB->get_records('portfolio_instance_user');
107 $this->assertCount(2, $records);
108 $context = context_user::instance($user1->id);
109 \core_portfolio\privacy\provider::delete_data_for_all_users_in_context($context);
110 $records = $DB->get_records('portfolio_instance_user');
111 // Only one entry should remain for user 2.
112 $this->assertCount(1, $records);
113 $data = array_shift($records);
114 $this->assertEquals($user2->id, $data->userid);
118 * Test that deleting only results in one user's data being removed.
120 public function test_delete_data_for_user() {
121 global $DB;
123 $this->resetAfterTest();
124 $user1 = $this->getDataGenerator()->create_user();
125 $user2 = $this->getDataGenerator()->create_user();
126 $this->create_portfolio_data('googledocs', 'Google Docs', $user1, 'visible', 1);
127 $this->create_portfolio_data('onedrive', 'Microsoft onedrive', $user2, 'visible', 1);
129 $records = $DB->get_records('portfolio_instance_user');
130 $this->assertCount(2, $records);
132 $context = context_user::instance($user1->id);
133 $contextlist = new \core_privacy\local\request\approved_contextlist($user1, 'core_portfolio', [$context->id]);
134 \core_portfolio\privacy\provider::delete_data_for_user($contextlist);
135 $records = $DB->get_records('portfolio_instance_user');
136 // Only one entry should remain for user 2.
137 $this->assertCount(1, $records);
138 $data = array_shift($records);
139 $this->assertEquals($user2->id, $data->userid);