2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * External airnotifier functions unit tests
20 * @package message_airnotifier
22 * @copyright 2012 Jerome Mouneyrac
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
30 require_once($CFG->dirroot
. '/webservice/tests/helpers.php');
33 * External airnotifier functions unit tests
35 * @package message_airnotifier
37 * @copyright 2012 Jerome Mouneyrac
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class message_airnotifier_external_testcase
extends externallib_advanced_testcase
{
45 protected function setUp() {
47 require_once($CFG->dirroot
. '/message/output/airnotifier/externallib.php');
51 * Test is_system_configured
53 public function test_is_system_configured() {
56 $this->resetAfterTest(true);
58 $user = self
::getDataGenerator()->create_user();
61 // In a clean installation, it should be not configured.
62 $configured = message_airnotifier_external
::is_system_configured();
63 $this->assertEquals(0, $configured);
65 // Fake configuration.
66 set_config('airnotifieraccesskey', random_string());
68 $DB->set_field('message_processors', 'enabled', 1, array('name' => 'airnotifier'));
70 $configured = message_airnotifier_external
::is_system_configured();
71 $this->assertEquals(1, $configured);
75 * Test are_notification_preferences_configured
77 public function test_are_notification_preferences_configured() {
79 $this->resetAfterTest(true);
81 $user1 = self
::getDataGenerator()->create_user();
82 $user2 = self
::getDataGenerator()->create_user();
83 $user3 = self
::getDataGenerator()->create_user();
85 self
::setUser($user1);
87 set_user_preference('message_provider_moodle_instantmessage_loggedin', 'airnotifier', $user1);
88 set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'airnotifier', $user1);
89 set_user_preference('message_provider_moodle_instantmessage_loggedin', 'airnotifier', $user2);
90 set_user_preference('message_provider_moodle_instantmessage_loggedin', 'airnotifier', $user3);
92 $params = array($user1->id
, $user2->id
, $user3->id
);
94 $preferences = message_airnotifier_external
::are_notification_preferences_configured($params);
98 'userid' => $user1->id
,
103 $this->assertEquals(1, count($preferences['users']));
104 $this->assertEquals($expected, $preferences['users']);
105 $this->assertEquals(2, count($preferences['warnings']));
107 // Now, remove one user.
109 $preferences = message_airnotifier_external
::are_notification_preferences_configured($params);
110 $this->assertEquals(1, count($preferences['users']));
111 $this->assertEquals($expected, $preferences['users']);
112 $this->assertEquals(2, count($preferences['warnings']));
114 // Now, remove one user1 preference (the user still has one prefernce for airnotifier).
115 unset_user_preference('message_provider_moodle_instantmessage_loggedin', $user1);
116 $preferences = message_airnotifier_external
::are_notification_preferences_configured($params);
117 $this->assertEquals($expected, $preferences['users']);
119 // Delete the last user1 preference.
120 unset_user_preference('message_provider_moodle_instantmessage_loggedoff', $user1);
121 $preferences = message_airnotifier_external
::are_notification_preferences_configured($params);
124 'userid' => $user1->id
,
128 $this->assertEquals($expected, $preferences['users']);