Skip to content

Commit

Permalink
Merge branch 'wip-MDL-62474-master' of git://github.com/abgreeve/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
junpataleta committed Jun 18, 2018
2 parents 3b6811a + f43ed74 commit 4fb6f0b
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 7 deletions.
46 changes: 39 additions & 7 deletions theme/boost/classes/privacy/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,55 @@

namespace theme_boost\privacy;

use \core_privacy\local\metadata\collection;

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

/**
* The boost theme does not store any data.
* The boost theme stores a user preference data.
*
* @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
* @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
// This plugin has data.
\core_privacy\local\metadata\provider,
// This plugin has some sitewide user preferences to export.
\core_privacy\local\request\user_preference_provider {

/** The user preference for the navigation drawer. */
const DRAWER_OPEN_NAV = 'drawer-open-nav';

/**
* Returns meta data about this system.
*
* @param collection $items The initialised item collection to add items to.
* @return collection A listing of user data stored through this system.
*/
public static function get_metadata(collection $items) : collection {
$items->add_user_preference(self::DRAWER_OPEN_NAV, 'privacy:metadata:preference:draweropennav');
return $items;
}

/**
* Get the language string identifier with the component's language
* file to explain why this plugin stores no data.
* Store all user preferences for the plugin.
*
* @return string
* @param int $userid The userid of the user whose data is to be exported.
*/
public static function get_reason() : string {
return 'privacy:metadata';
public static function export_user_preferences(int $userid) {
$draweropennavpref = get_user_preferences(self::DRAWER_OPEN_NAV, null, $userid);

if (isset($draweropennavpref)) {
$preferencestring = get_string('privacy:drawernavclosed', 'theme_boost');
if ($draweropennavpref == 'true') {
$preferencestring = get_string('privacy:drawernavopen', 'theme_boost');
}
\core_privacy\local\request\writer::export_user_preference(
'theme_boost',
self::DRAWER_OPEN_NAV,
$draweropennavpref,
$preferencestring
);
}
}
}
3 changes: 3 additions & 0 deletions theme/boost/lang/en/theme_boost.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,6 @@
$string['rawscsspre'] = 'Raw initial SCSS';
$string['rawscsspre_desc'] = 'In this field you can provide initialising SCSS code, it will be injected before everything else. Most of the time you will use this setting to define variables.';
$string['region-side-pre'] = 'Right';
$string['privacy:metadata:preference:draweropennav'] = 'The user\'s preference for hiding or showing the drawer menu navigation.';
$string['privacy:drawernavclosed'] = 'The current preference for the navigation drawer is closed.';
$string['privacy:drawernavopen'] = 'The current preference for the navigation drawer is open.';
78 changes: 78 additions & 0 deletions theme/boost/tests/privacy_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?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/>.

/**
* Privacy tests for theme_boost.
*
* @package theme_boost
* @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 \theme_boost\privacy\provider;

/**
* Unit tests for theme_boost/classes/privacy/policy
*
* @copyright 2018 Adrian Greeve <adriangreeve.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class theme_boost_privacy_testcase extends \core_privacy\tests\provider_testcase {

/**
* Test for provider::test_export_user_preferences().
*/
public function test_export_user_preferences() {
$this->resetAfterTest();

// Test setup.
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);

// Add a user home page preference for the User.
set_user_preference(provider::DRAWER_OPEN_NAV, 'false', $user);

// Test the user preferences export contains 1 user preference record for the User.
provider::export_user_preferences($user->id);
$contextuser = context_user::instance($user->id);
$writer = \core_privacy\local\request\writer::with_context($contextuser);
$this->assertTrue($writer->has_any_data());

$exportedpreferences = $writer->get_user_preferences('theme_boost');
$this->assertCount(1, (array) $exportedpreferences);
$this->assertEquals('false', $exportedpreferences->{provider::DRAWER_OPEN_NAV}->value);
$this->assertEquals(get_string('privacy:drawernavclosed', 'theme_boost'),
$exportedpreferences->{provider::DRAWER_OPEN_NAV}->description);

// Add a user home page preference for the User.
set_user_preference(provider::DRAWER_OPEN_NAV, 'true', $user);

// Test the user preferences export contains 1 user preference record for the User.
provider::export_user_preferences($user->id);
$contextuser = context_user::instance($user->id);
$writer = \core_privacy\local\request\writer::with_context($contextuser);
$this->assertTrue($writer->has_any_data());

$exportedpreferences = $writer->get_user_preferences('theme_boost');
$this->assertCount(1, (array) $exportedpreferences);
$this->assertEquals('true', $exportedpreferences->{provider::DRAWER_OPEN_NAV}->value);
$this->assertEquals(get_string('privacy:drawernavopen', 'theme_boost'),
$exportedpreferences->{provider::DRAWER_OPEN_NAV}->description);
}
}

0 comments on commit 4fb6f0b

Please sign in to comment.