MDL-63793 block_myoverview: Persist the user's paging limit preference
[moodle.git] / blocks / myoverview / tests / privacy_test.php
CommitLineData
3cfff885
BB
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 * Unit tests for the block_myoverview implementation of the privacy API.
18 *
19 * @package block_myoverview
20 * @category test
21 * @copyright 2018 Peter Dias <peter@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24defined('MOODLE_INTERNAL') || die();
25use \core_privacy\local\request\writer;
26use \block_myoverview\privacy\provider;
27/**
28 * Unit tests for the block_myoverview implementation of the privacy API.
29 *
30 * @copyright 2018 Peter Dias <peter@moodle.com>
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32 */
33class block_myoverview_privacy_testcase extends \core_privacy\tests\provider_testcase {
34 /**
35 * Ensure that export_user_preferences returns no data if the user has not visited the myoverview block.
36 */
37 public function test_export_user_preferences_no_pref() {
38 $this->resetAfterTest();
39 $user = $this->getDataGenerator()->create_user();
40 provider::export_user_preferences($user->id);
41 $writer = writer::with_context(\context_system::instance());
42 $this->assertFalse($writer->has_any_data());
43 }
44
45 /**
46 * Test the export_user_preferences given different inputs
47 *
48 * @param string $type The name of the user preference to get/set
49 * @param string $value The value you are storing
50 *
51 * @dataProvider user_preference_provider
52 */
11988d74 53 public function test_export_user_preferences($type, $value, $expected) {
3cfff885
BB
54 $this->resetAfterTest();
55 $user = $this->getDataGenerator()->create_user();
56 set_user_preference($type, $value, $user);
57 provider::export_user_preferences($user->id);
58 $writer = writer::with_context(\context_system::instance());
59 $blockpreferences = $writer->get_user_preferences('block_myoverview');
11988d74
P
60 if (!$expected) {
61 $expected = get_string($value, 'block_myoverview');
62 }
63 $this->assertEquals($expected, $blockpreferences->{$type}->value);
3cfff885
BB
64 }
65
66 /**
67 * Create an array of valid user preferences for the myoverview block.
68 *
69 * @return array Array of valid user preferences.
70 */
71 public function user_preference_provider() {
72 return array(
11988d74
P
73 array('block_myoverview_user_sort_preference', 'lastaccessed', ''),
74 array('block_myoverview_user_sort_preference', 'title', ''),
75 array('block_myoverview_user_grouping_preference', 'all', ''),
76 array('block_myoverview_user_grouping_preference', 'inprogress', ''),
77 array('block_myoverview_user_grouping_preference', 'future', ''),
78 array('block_myoverview_user_grouping_preference', 'past', ''),
79 array('block_myoverview_user_view_preference', 'card', ''),
80 array('block_myoverview_user_view_preference', 'list', ''),
81 array('block_myoverview_user_view_preference', 'summary', ''),
82 array('block_myoverview_user_paging_preference', 12, 12)
3cfff885
BB
83 );
84 }
e6f03948
P
85
86 public function test_export_user_preferences_with_hidden_courses() {
87 $this->resetAfterTest();
88 $user = $this->getDataGenerator()->create_user();
89 $name = "block_myoverview_hidden_course_1";
90
91 set_user_preference($name, 1, $user);
92 provider::export_user_preferences($user->id);
93 $writer = writer::with_context(\context_system::instance());
94 $blockpreferences = $writer->get_user_preferences('block_myoverview');
95
96 $this->assertEquals(
97 get_string("privacy:request:preference:set", 'block_myoverview', (object) [
98 'name' => $name,
99 'value' => 1,
100 ]),
101 $blockpreferences->{$name}->description
102 );
103 }
3cfff885 104}