Skip to content

Commit

Permalink
MDL-71276 message_email: include alternate email in privacy export.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulholden committed May 16, 2022
1 parent b8e9cb3 commit 8978466
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 9 deletions.
19 changes: 19 additions & 0 deletions message/output/email/classes/privacy/provider.php
Expand Up @@ -30,6 +30,7 @@
use \core_privacy\local\request\contextlist;
use \core_privacy\local\request\approved_contextlist;
use core_privacy\local\request\userlist;
use core_privacy\local\request\writer;
use \core_privacy\local\request\approved_userlist;

/**
Expand All @@ -42,6 +43,7 @@
class provider implements
\core_privacy\local\metadata\provider,
\core_privacy\local\request\core_userlist_provider,
\core_privacy\local\request\user_preference_provider,
\core_privacy\local\request\plugin\provider {

/**
Expand Down Expand Up @@ -125,4 +127,21 @@ public static function delete_data_for_users(approved_userlist $userlist) {
*/
public static function delete_data_for_user(approved_contextlist $contextlist) {
}

/**
* Export all user preferences for the plugin
*
* @param int $userid
*/
public static function export_user_preferences(int $userid) {
$preference = get_user_preferences('message_processor_email_email', null, $userid);
if (!empty($preference)) {
writer::export_user_preference(
'message_email',
'email',
$preference,
get_string('privacy:preference:email', 'message_email')
);
}
}
}
1 change: 1 addition & 0 deletions message/output/email/lang/en/message_email.php
Expand Up @@ -43,6 +43,7 @@
$string['privacy:metadata:replytoname'] = 'Name of reply to recipient.';
$string['privacy:metadata:subject'] = 'The subject line of the message.';
$string['privacy:metadata:userfrom'] = 'The user sending the message.';
$string['privacy:preference:email'] = 'Preferred email notification address';
$string['tasksendemail'] = 'Messages digest mailings';

// Deprecated since Moodle 3.9.
Expand Down
50 changes: 41 additions & 9 deletions message/output/email/tests/privacy/provider_test.php
Expand Up @@ -13,21 +13,19 @@
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Base class for unit tests for message_email.
*
* @package message_email
* @copyright 2018 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace message_email\privacy;

defined('MOODLE_INTERNAL') || die();
namespace message_email\privacy;

use context_system;
use core_message_external;
use core_privacy\local\request\writer;
use core_privacy\tests\provider_testcase;

/**
* Unit tests for message\output\email\classes\privacy\provider.php
*
* @package message_email
* @covers \message_email\privacy\provider
* @copyright 2018 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
Expand Down Expand Up @@ -57,4 +55,38 @@ public function test_get_contexts_for_userid() {
$contextlist = \message_email\privacy\provider::get_contexts_for_userid($user->id);
$this->assertEmpty($contextlist);
}

/**
* Test exporting user preferences
*/
public function test_export_user_preferences(): void {
global $CFG;

require_once("{$CFG->dirroot}/message/externallib.php");

$user = $this->getDataGenerator()->create_user();
$this->setUser($user);

// Submit configuration form, which adds the preferences..
core_message_external::message_processor_config_form($user->id, 'email', [
[
'name' => 'email_email',
'value' => 'alternate@example.com',
],
]);

// Switch to admin user (so we can validate preferences of the correct user are being exported).
$this->setAdminUser();

provider::export_user_preferences($user->id);

$writer = writer::with_context(context_system::instance());
$this->assertTrue($writer->has_any_data());

$preferences = $writer->get_user_preferences('message_email');
$this->assertNotEmpty($preferences->email);

$this->assertEquals('alternate@example.com', $preferences->email->value);
$this->assertEquals(get_string('privacy:preference:email', 'message_email'), $preferences->email->description);
}
}

0 comments on commit 8978466

Please sign in to comment.