Skip to content

Commit

Permalink
MDL-61966 block_myoverview: Update to provider for user preferences.
Browse files Browse the repository at this point in the history
  • Loading branch information
abgreeve committed Apr 20, 2018
1 parent 9a6b854 commit 19f06a1
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 8 deletions.
29 changes: 22 additions & 7 deletions blocks/myoverview/classes/privacy/provider.php
Expand Up @@ -27,20 +27,35 @@
defined('MOODLE_INTERNAL') || die();

/**
* Privacy Subsystem for block_myoverview implementing null_provider.
* Privacy Subsystem for block_myoverview.
*
* @copyright 2018 Zig Tan <zig@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements \core_privacy\local\metadata\null_provider {
class provider implements \core_privacy\local\metadata\provider, \core_privacy\local\request\user_preference_provider {

/**
* Get the language string identifier with the component's language
* file to explain why this plugin stores no data.
* Returns meta-data information about the myoverview block.
*
* @return string
* @param \core_privacy\local\metadata\collection $collection A collection of meta-data.
* @return \core_privacy\local\metadata\collection Return the collection of meta-data.
*/
public static function get_reason() : string {
return 'privacy:metadata';
public static function get_metadata(\core_privacy\local\metadata\collection $collection) :
\core_privacy\local\metadata\collection {
$collection->add_user_preference('block_myoverview_last_tab', 'privacy:metadata:overviewlasttab');
return $collection;
}

/**
* Export all user preferences for the myoverview block
*
* @param int $userid The userid of the user whose data is to be exported.
*/
public static function export_user_preferences(int $userid) {
$preference = get_user_preferences('block_myoverview_last_tab', null, $userid);
if (isset($preference)) {
\core_privacy\local\request\writer::export_user_preference('block_myoverview', 'block_myoverview_last_tab',
$preference, get_string('privacy:metadata:overviewlasttab', 'block_myoverview'));
}
}
}
2 changes: 1 addition & 1 deletion blocks/myoverview/lang/en/block_myoverview.php
Expand Up @@ -44,4 +44,4 @@
$string['timeline'] = 'Timeline';
$string['viewcourse'] = 'View course';
$string['viewcoursename'] = 'View course {$a}';
$string['privacy:metadata'] = 'The Course overview block only shows data stored in other locations.';
$string['privacy:metadata:overviewlasttab'] = 'This stores the last tab selected by the user in the overview tab.';
80 changes: 80 additions & 0 deletions blocks/myoverview/tests/privacy_test.php
@@ -0,0 +1,80 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Unit tests for the block_myoverview implementation of the privacy API.
*
* @package block_myoverview
* @category test
* @copyright 2018 Adrian Greeve <adriangreeve.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

use \core_privacy\local\request\writer;
use \block_myoverview\privacy\provider;

/**
* Unit tests for the block_myoverview implementation of the privacy API.
*
* @copyright 2018 Adrian Greeve <adriangreeve.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class block_myoverview_privacy_testcase extends \core_privacy\tests\provider_testcase {

/**
* Ensure that export_user_preferences returns no data if the user has not visited the myoverview block.
*/
public function test_export_user_preferences_no_pref() {
$this->resetAfterTest();

$user = $this->getDataGenerator()->create_user();
provider::export_user_preferences($user->id);
$writer = writer::with_context(\context_system::instance());
$this->assertFalse($writer->has_any_data());
}

/**
* Test that the preference courses is exported properly.
*/
public function test_export_user_preferences_course_preference() {
$this->resetAfterTest();

$user = $this->getDataGenerator()->create_user();
set_user_preference('block_myoverview_last_tab', 'courses', $user);

provider::export_user_preferences($user->id);
$writer = writer::with_context(\context_system::instance());
$blockpreferences = $writer->get_user_preferences('block_myoverview');
$this->assertEquals('courses', $blockpreferences->block_myoverview_last_tab->value);
}

/**
* Test that the preference timeline is exported properly.
*/
public function test_export_user_preferences_timeline_preference() {
$this->resetAfterTest();

$user = $this->getDataGenerator()->create_user();
set_user_preference('block_myoverview_last_tab', 'timeline', $user);

provider::export_user_preferences($user->id);
$writer = writer::with_context(\context_system::instance());
$blockpreferences = $writer->get_user_preferences('block_myoverview');
$this->assertEquals('timeline', $blockpreferences->block_myoverview_last_tab->value);
}
}

0 comments on commit 19f06a1

Please sign in to comment.