NOBUG: Fixed file access permissions
[moodle.git] / message / output / airnotifier / tests / externallib_test.php
blob028cf0b3dbe29e2f0f0276ad99c8fab97c49fa1b
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
18 * External airnotifier functions unit tests
20 * @package message_airnotifier
21 * @category external
22 * @copyright 2012 Jerome Mouneyrac
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
30 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
32 /**
33 * External airnotifier functions unit tests
35 * @package message_airnotifier
36 * @category external
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 {
42 /**
43 * Tests set up
45 protected function setUp() {
46 global $CFG;
47 require_once($CFG->dirroot . '/message/output/airnotifier/externallib.php');
50 /**
51 * Test is_system_configured
53 public function test_is_system_configured() {
54 global $DB;
56 $this->resetAfterTest(true);
58 $user = self::getDataGenerator()->create_user();
59 self::setUser($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());
67 // Enable the plugin.
68 $DB->set_field('message_processors', 'enabled', 1, array('name' => 'airnotifier'));
70 $configured = message_airnotifier_external::is_system_configured();
71 $this->assertEquals(1, $configured);
74 /**
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);
96 $expected = array(
97 array(
98 'userid' => $user1->id,
99 'configured' => 1
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.
108 delete_user($user2);
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);
122 $expected = array(
123 array(
124 'userid' => $user1->id,
125 'configured' => 0
128 $this->assertEquals($expected, $preferences['users']);