MDL-63638 block_tag_flickr: Add support for removal of context users
[moodle.git] / blocks / rss_client / tests / privacy_test.php
CommitLineData
9b8822d2
MG
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.
13//
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/>.
16/**
17 * Base class for unit tests for block_rss_client.
18 *
19 * @package block_rss_client
20 * @copyright 2018 Mihail Geshoski <mihail@moodle.com>
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 */
23
24defined('MOODLE_INTERNAL') || die();
25
26use \core_privacy\tests\provider_testcase;
27
28/**
29 * Unit tests for blocks\rss_client\classes\privacy\provider.php
30 *
31 * @copyright 2018 Mihail Geshoski <mihail@moodle.com>
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
34class block_rss_client_testcase extends provider_testcase {
35
36 /**
37 * Basic setup for these tests.
38 */
39 public function setUp() {
40 $this->resetAfterTest(true);
41 }
42
43 /**
44 * Test getting the context for the user ID related to this plugin.
45 */
46 public function test_get_contexts_for_userid() {
47
48 $user = $this->getDataGenerator()->create_user();
49 $context = context_user::instance($user->id);
50
51 $this->add_rss_feed($user);
52
53 $contextlist = \block_rss_client\privacy\provider::get_contexts_for_userid($user->id);
54
55 $this->assertEquals($context, $contextlist->current());
56 }
57
58 /**
59 * Test that data is exported correctly for this plugin.
60 */
61 public function test_export_user_data() {
62
63 $user = $this->getDataGenerator()->create_user();
64 $context = context_user::instance($user->id);
65
9d594541 66 $this->add_rss_feed($user);
9b8822d2
MG
67 $this->add_rss_feed($user);
68
69 $writer = \core_privacy\local\request\writer::with_context($context);
70 $this->assertFalse($writer->has_any_data());
71 $this->export_context_data_for_user($user->id, $context, 'block_rss_client');
72
73 $data = $writer->get_data([get_string('pluginname', 'block_rss_client')]);
9d594541
SA
74 $this->assertCount(2, $data->feeds);
75 $feed1 = reset($data->feeds);
76 $this->assertEquals('BBC News - World', $feed1->title);
77 $this->assertEquals('World News', $feed1->preferredtitle);
78 $this->assertEquals('Description: BBC News - World', $feed1->description);
79 $this->assertEquals(get_string('no'), $feed1->shared);
80 $this->assertEquals('http://feeds.bbci.co.uk/news/world/rss.xml?edition=uk', $feed1->url);
9b8822d2
MG
81 }
82
83 /**
84 * Test that user data is deleted using the context.
85 */
86 public function test_delete_data_for_all_users_in_context() {
87 global $DB;
88
89 $user = $this->getDataGenerator()->create_user();
90 $context = context_user::instance($user->id);
91
92 $this->add_rss_feed($user);
93
94 // Check that we have an entry.
95 $rssfeeds = $DB->get_records('block_rss_client', ['userid' => $user->id]);
96 $this->assertCount(1, $rssfeeds);
97
98 \block_rss_client\privacy\provider::delete_data_for_all_users_in_context($context);
99
100 // Check that it has now been deleted.
101 $rssfeeds = $DB->get_records('block_rss_client', ['userid' => $user->id]);
102 $this->assertCount(0, $rssfeeds);
103 }
104
105 /**
106 * Test that user data is deleted for this user.
107 */
108 public function test_delete_data_for_user() {
109 global $DB;
110
111 $user = $this->getDataGenerator()->create_user();
112 $context = context_user::instance($user->id);
113
114 $this->add_rss_feed($user);
115
116 // Check that we have an entry.
117 $rssfeeds = $DB->get_records('block_rss_client', ['userid' => $user->id]);
118 $this->assertCount(1, $rssfeeds);
119
120 $approvedlist = new \core_privacy\local\request\approved_contextlist($user, 'block_rss_feed',
121 [$context->id]);
122 \block_rss_client\privacy\provider::delete_data_for_user($approvedlist);
123
124 // Check that it has now been deleted.
125 $rssfeeds = $DB->get_records('block_rss_client', ['userid' => $user->id]);
126 $this->assertCount(0, $rssfeeds);
127 }
128
129 /**
130 * Add dummy rss feed.
131 *
132 * @param object $user User object
133 */
134 private function add_rss_feed($user) {
135 global $DB;
136
137 $rssfeeddata = array(
138 'userid' => $user->id,
139 'title' => 'BBC News - World',
140 'preferredtitle' => 'World News',
141 'description' => 'Description: BBC News - World',
142 'shared' => 0,
143 'url' => 'http://feeds.bbci.co.uk/news/world/rss.xml?edition=uk',
144 );
145
146 $DB->insert_record('block_rss_client', $rssfeeddata);
147 }
148}