MDL-61795 block_rss_client: Add privacy files and unit tests
[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
66 $this->add_rss_feed($user);
67
68 $writer = \core_privacy\local\request\writer::with_context($context);
69 $this->assertFalse($writer->has_any_data());
70 $this->export_context_data_for_user($user->id, $context, 'block_rss_client');
71
72 $data = $writer->get_data([get_string('pluginname', 'block_rss_client')]);
73 $this->assertEquals('BBC News - World', $data->title);
74 $this->assertEquals('World News', $data->preferredtitle);
75 $this->assertEquals('Description: BBC News - World', $data->description);
76 $this->assertEquals(get_string('no'), $data->shared);
77 $this->assertEquals('http://feeds.bbci.co.uk/news/world/rss.xml?edition=uk', $data->url);
78 }
79
80 /**
81 * Test that user data is deleted using the context.
82 */
83 public function test_delete_data_for_all_users_in_context() {
84 global $DB;
85
86 $user = $this->getDataGenerator()->create_user();
87 $context = context_user::instance($user->id);
88
89 $this->add_rss_feed($user);
90
91 // Check that we have an entry.
92 $rssfeeds = $DB->get_records('block_rss_client', ['userid' => $user->id]);
93 $this->assertCount(1, $rssfeeds);
94
95 \block_rss_client\privacy\provider::delete_data_for_all_users_in_context($context);
96
97 // Check that it has now been deleted.
98 $rssfeeds = $DB->get_records('block_rss_client', ['userid' => $user->id]);
99 $this->assertCount(0, $rssfeeds);
100 }
101
102 /**
103 * Test that user data is deleted for this user.
104 */
105 public function test_delete_data_for_user() {
106 global $DB;
107
108 $user = $this->getDataGenerator()->create_user();
109 $context = context_user::instance($user->id);
110
111 $this->add_rss_feed($user);
112
113 // Check that we have an entry.
114 $rssfeeds = $DB->get_records('block_rss_client', ['userid' => $user->id]);
115 $this->assertCount(1, $rssfeeds);
116
117 $approvedlist = new \core_privacy\local\request\approved_contextlist($user, 'block_rss_feed',
118 [$context->id]);
119 \block_rss_client\privacy\provider::delete_data_for_user($approvedlist);
120
121 // Check that it has now been deleted.
122 $rssfeeds = $DB->get_records('block_rss_client', ['userid' => $user->id]);
123 $this->assertCount(0, $rssfeeds);
124 }
125
126 /**
127 * Add dummy rss feed.
128 *
129 * @param object $user User object
130 */
131 private function add_rss_feed($user) {
132 global $DB;
133
134 $rssfeeddata = array(
135 'userid' => $user->id,
136 'title' => 'BBC News - World',
137 'preferredtitle' => 'World News',
138 'description' => 'Description: BBC News - World',
139 'shared' => 0,
140 'url' => 'http://feeds.bbci.co.uk/news/world/rss.xml?edition=uk',
141 );
142
143 $DB->insert_record('block_rss_client', $rssfeeddata);
144 }
145}