MDL-63058 block_myoverview: starring and storing preferences
[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 */
53 public function test_export_user_preferences($type, $value) {
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');
60 $this->assertEquals(get_string($value, 'block_myoverview'), $blockpreferences->{$type}->value);
61 }
62
63 /**
64 * Create an array of valid user preferences for the myoverview block.
65 *
66 * @return array Array of valid user preferences.
67 */
68 public function user_preference_provider() {
69 return array(
70 array('block_myoverview_user_sort_preference', 'lastaccessed'),
71 array('block_myoverview_user_sort_preference', 'title'),
72 array('block_myoverview_user_grouping_preference', 'all'),
73 array('block_myoverview_user_grouping_preference', 'inprogress'),
74 array('block_myoverview_user_grouping_preference', 'future'),
75 array('block_myoverview_user_grouping_preference', 'past'),
76 array('block_myoverview_user_view_preference', 'cards'),
77 array('block_myoverview_user_view_preference', 'list'),
78 array('block_myoverview_user_view_preference', 'summary')
79 );
80 }
81}