From f43ed74cb0394e7184ab9e1f3fa79d2cfd09150a Mon Sep 17 00:00:00 2001 From: Adrian Greeve Date: Mon, 11 Jun 2018 10:39:50 +0800 Subject: [PATCH] MDL-62474 theme_boost: Update to provider and unit test. Theme boost contains a user preference which was not reported before. Unit tests have been added to test the update. --- theme/boost/classes/privacy/provider.php | 21 +++---- theme/boost/lang/en/theme_boost.php | 5 +- theme/boost/tests/privacy_test.php | 78 ++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 13 deletions(-) create mode 100644 theme/boost/tests/privacy_test.php diff --git a/theme/boost/classes/privacy/provider.php b/theme/boost/classes/privacy/provider.php index b17ade2803a..379e50b546d 100644 --- a/theme/boost/classes/privacy/provider.php +++ b/theme/boost/classes/privacy/provider.php @@ -24,7 +24,6 @@ namespace theme_boost\privacy; -use \core_privacy\local\request\writer; use \core_privacy\local\metadata\collection; defined('MOODLE_INTERNAL') || die(); @@ -38,24 +37,20 @@ defined('MOODLE_INTERNAL') || die(); class provider implements // This plugin has data. \core_privacy\local\metadata\provider, - - \core_privacy\local\request\data_provider, - // This plugin has some sitewide user preferences to export. - \core_privacy\local\request\user_preference_provider -{ + \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 $itemcollection The initialised item collection to add items to. - * @return collection A listing of user data stored through 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; } @@ -68,11 +63,15 @@ class provider implements $draweropennavpref = get_user_preferences(self::DRAWER_OPEN_NAV, null, $userid); if (isset($draweropennavpref)) { - writer::export_user_preference( + $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, - get_string('privacy:request:preference:draweropennav', 'theme_boost') + $preferencestring ); } } diff --git a/theme/boost/lang/en/theme_boost.php b/theme/boost/lang/en/theme_boost.php index c5e6ac93097..627ccf71f79 100644 --- a/theme/boost/lang/en/theme_boost.php +++ b/theme/boost/lang/en/theme_boost.php @@ -49,5 +49,6 @@ $string['rawscss_desc'] = 'Use this field to provide SCSS or CSS code which will $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'] = 'Hide or show the left drawer menu nav.'; -$string['privacy:request:preference:draweropennav'] = 'Your preference for hide or show the left drawer menu nav'; +$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.'; diff --git a/theme/boost/tests/privacy_test.php b/theme/boost/tests/privacy_test.php new file mode 100644 index 00000000000..6df617f9c97 --- /dev/null +++ b/theme/boost/tests/privacy_test.php @@ -0,0 +1,78 @@ +. + +/** + * Privacy tests for theme_boost. + * + * @package theme_boost + * @category test + * @copyright 2018 Adrian Greeve + * @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 + * @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); + } +} -- 2.43.0