Skip to content

Commit

Permalink
Preferences (manager) class not static anymore.
Browse files Browse the repository at this point in the history
  • Loading branch information
roperto committed Mar 6, 2018
1 parent a4b7237 commit 82a7ecb
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 25 deletions.
6 changes: 3 additions & 3 deletions classes/form/preferences_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

namespace local_extension\form;

use local_extension\preferences_manager;
use local_extension\preferences;
use moodleform;

defined('MOODLE_INTERNAL') || die();
Expand All @@ -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();
}
Expand Down
7 changes: 6 additions & 1 deletion classes/message/mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
27 changes: 22 additions & 5 deletions classes/preferences_manager.php → classes/preferences.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
4 changes: 2 additions & 2 deletions preferences.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/

use local_extension\form\preferences_form;
use local_extension\preferences_manager;
use local_extension\preferences;

require_once(__DIR__ . '/../../config.php');

Expand All @@ -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');
}

Expand Down
34 changes: 29 additions & 5 deletions tests/phpunit/message/mailer_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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' => '[email protected]']);
}

protected function send_email() {
global $CFG;
$user = core_user::get_user(2);
$user = $this->recipient;

if ($CFG->version >= 2015051100) {
$message = new message();
Expand All @@ -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();
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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}");
}
}
Expand Down

0 comments on commit 82a7ecb

Please sign in to comment.