diff --git a/classes/form/preferences_form.php b/classes/form/preferences_form.php index 40c59bbb..def06ec4 100644 --- a/classes/form/preferences_form.php +++ b/classes/form/preferences_form.php @@ -23,7 +23,7 @@ namespace local_extension\form; -use local_extension\preferences_manager; +use local_extension\preferences; use moodleform; defined('MOODLE_INTERNAL') || die(); @@ -33,10 +33,10 @@ class preferences_form extends moodleform { public function definition() { - $this->_form->addElement('checkbox', preferences_manager::MAIL_DIGEST, + $this->_form->addElement('checkbox', preferences::MAIL_DIGEST, get_string('preference_mail_digest', 'local_extension'), get_string('preference_mail_digest_help', 'local_extension')); - $this->_form->setDefault('mail_digest', preferences_manager::get(preferences_manager::MAIL_DIGEST)); + $this->_form->setDefault('mail_digest', (new preferences())->get(preferences::MAIL_DIGEST)); $this->add_action_buttons(); } diff --git a/classes/message/mailer.php b/classes/message/mailer.php index 2eafa372..c2fb3b30 100644 --- a/classes/message/mailer.php +++ b/classes/message/mailer.php @@ -23,10 +23,15 @@ namespace local_extension\message; +use local_extension\preferences; + defined('MOODLE_INTERNAL') || die(); class mailer { public function send($message) { - message_send($message); + $preferences = new preferences(); + if (!$preferences->get(preferences::MAIL_DIGEST)) { + message_send($message); + } } } diff --git a/classes/preferences_manager.php b/classes/preferences.php similarity index 78% rename from classes/preferences_manager.php rename to classes/preferences.php index fc022c80..d71612b5 100644 --- a/classes/preferences_manager.php +++ b/classes/preferences.php @@ -31,19 +31,36 @@ * @copyright 2018 Catalyst IT Australia {@link http://www.catalyst-au.net} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -abstract class preferences_manager { +class preferences { const MAIL_DIGEST = 'mail_digest'; protected static $defaults = [ self::MAIL_DIGEST => false, ]; - public static function set($name, $value) { - set_user_preferences(["local_extension_{$name}" => $value]); + /** @var int */ + private $userid; + + public function get_user_id() { + return $this->userid; } - public static function get($name) { + public function __construct($userid = null) { + global $USER; + + if (is_null($userid)) { + $userid = $USER->id; + } + + $this->userid = $userid; + } + + public function get($name) { $default = array_key_exists($name, self::$defaults) ? self::$defaults[$name] : null; - return get_user_preferences("local_extension_{$name}", $default); + return get_user_preferences("local_extension_{$name}", $default, $this->userid); + } + + public function set($name, $value) { + set_user_preferences(["local_extension_{$name}" => $value], $this->userid); } } diff --git a/preferences.php b/preferences.php index 8606a1c2..c9f0d4ac 100644 --- a/preferences.php +++ b/preferences.php @@ -24,7 +24,7 @@ */ use local_extension\form\preferences_form; -use local_extension\preferences_manager; +use local_extension\preferences; require_once(__DIR__ . '/../../config.php'); @@ -48,7 +48,7 @@ $data = $form->get_data(); if (!is_null($data)) { - preferences_manager::set(preferences_manager::MAIL_DIGEST, !empty($data->{preferences_manager::MAIL_DIGEST})); + (new preferences())->set(preferences::MAIL_DIGEST, !empty($data->{preferences::MAIL_DIGEST})); echo $OUTPUT->notification(get_string('preferences_saved', 'local_extension'), 'notifysuccess'); } diff --git a/tests/phpunit/message/mailer_test.php b/tests/phpunit/message/mailer_test.php index d816e55f..b84014fd 100644 --- a/tests/phpunit/message/mailer_test.php +++ b/tests/phpunit/message/mailer_test.php @@ -22,6 +22,8 @@ */ use core\message\message; +use local_extension\message\mailer; +use local_extension\preferences; use local_extension\test\extension_testcase; defined('MOODLE_INTERNAL') || die(); @@ -33,9 +35,17 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class local_extension_mailer_test extends extension_testcase { - protected function send_message() { + private $recipient; + + protected function setUp() { + parent::setUp(); + self::resetAfterTest(); + $this->recipient = $this->getDataGenerator()->create_user(['email' => 'destination@extension.test']); + } + + protected function send_email() { global $CFG; - $user = core_user::get_user(2); + $user = $this->recipient; if ($CFG->version >= 2015051100) { $message = new message(); @@ -52,14 +62,14 @@ protected function send_message() { $message->fullmessage = 'Message Contents'; $message->fullmessageformat = FORMAT_PLAIN; - message_send($message); + (new mailer())->send($message); } public function test_it_sends_message_immediately() { - $this->resetAfterTest(); + self::setAdminUser(); $sink = phpunit_util::start_message_redirection(); - $this->send_message(); + $this->send_email(); phpunit_util::stop_message_redirection(); $messages = $sink->get_messages(); @@ -68,4 +78,18 @@ public function test_it_sends_message_immediately() { self::assertSame('Message Subject', $message->subject); self::assertSame('Message Contents', $message->fullmessage); } + + public function test_it_does_not_send_message_immediately() { + $this->resetAfterTest(); + self::setAdminUser(); + + (new preferences())->set(preferences::MAIL_DIGEST, true); + + $sink = phpunit_util::start_message_redirection(); + $this->send_email(); + phpunit_util::stop_message_redirection(); + + $messages = $sink->get_messages(); + self::assertCount(0, $messages); + } } diff --git a/tests/phpunit/preferences_manager_test.php b/tests/phpunit/preferences_test.php similarity index 53% rename from tests/phpunit/preferences_manager_test.php rename to tests/phpunit/preferences_test.php index 15f06818..9dc10803 100644 --- a/tests/phpunit/preferences_manager_test.php +++ b/tests/phpunit/preferences_test.php @@ -21,7 +21,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -use local_extension\preferences_manager; +use local_extension\preferences; use local_extension\test\extension_testcase; defined('MOODLE_INTERNAL') || die(); @@ -32,25 +32,58 @@ * @copyright 2017 Catalyst IT Australia {@link http://www.catalyst-au.net} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class local_extension_preferences_manager_test extends extension_testcase { - public function test_it_sets() { - preferences_manager::set('name', 'abc123'); +class local_extension_preferences_test extends extension_testcase { + protected function setUp() { + parent::setUp(); + $this->resetAfterTest(); + self::setAdminUser(); + } + + public function test_it_creates_with_userid() { + global $USER; + + $tests = [ + [null, $USER->id], + [999, 999], + ]; + + foreach ($tests as $key => list($parameter, $expected)) { + $manager = new preferences($parameter); + self::assertSame($expected, $manager->get_user_id(), "Test #{$key}"); + } + } + + public function test_it_sets_for_current_user() { + (new preferences())->set('name', 'abc123'); self::assertSame('abc123', get_user_preferences('local_extension_name')); } - public function test_it_gets() { + public function test_it_gets_for_current_user() { set_user_preferences(['local_extension_name' => 'john']); - $got = preferences_manager::get('name'); + $got = (new preferences())->get('name'); + self::assertSame('john', $got); + } + + public function test_it_sets_for_another_user() { + $user = $this->getDataGenerator()->create_user(); + (new preferences($user->id))->set('name', 'abc123'); + self::assertSame('abc123', get_user_preferences('local_extension_name', null, $user)); + } + + public function test_it_gets_for_another_user() { + $user = $this->getDataGenerator()->create_user(); + set_user_preferences(['local_extension_name' => 'john'], $user); + $got = (new preferences($user->id))->get('name'); self::assertSame('john', $got); } public function test_it_has_defaults() { $expectations = [ - 'invalid' => null, - preferences_manager::MAIL_DIGEST => false, + 'invalid' => null, + preferences::MAIL_DIGEST => false, ]; foreach ($expectations as $preference => $expectation) { - $actual = preferences_manager::get($preference); + $actual = (new preferences())->get($preference); self::assertSame($expectation, $actual, "Preference: {$preference}"); } }