Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]
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. 13 // 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/>. 16 17 namespace message_airnotifier; 18 19 use externallib_advanced_testcase; 20 use message_airnotifier_external; 21 22 defined('MOODLE_INTERNAL') || die(); 23 24 global $CFG; 25 26 require_once($CFG->dirroot . '/webservice/tests/helpers.php'); 27 28 /** 29 * External airnotifier functions unit tests 30 * 31 * @package message_airnotifier 32 * @category external 33 * @copyright 2012 Jerome Mouneyrac 34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 35 */ 36 class externallib_test extends externallib_advanced_testcase { 37 38 /** 39 * Tests set up 40 */ 41 protected function setUp(): void { 42 global $CFG; 43 require_once($CFG->dirroot . '/message/output/airnotifier/externallib.php'); 44 } 45 46 /** 47 * Test is_system_configured 48 */ 49 public function test_is_system_configured() { 50 global $DB; 51 52 $this->resetAfterTest(true); 53 54 $user = self::getDataGenerator()->create_user(); 55 self::setUser($user); 56 57 // In a clean installation, it should be not configured. 58 $configured = message_airnotifier_external::is_system_configured(); 59 $configured = \external_api::clean_returnvalue(message_airnotifier_external::is_system_configured_returns(), $configured); 60 $this->assertEquals(0, $configured); 61 62 // Fake configuration. 63 set_config('airnotifieraccesskey', random_string()); 64 // Enable the plugin. 65 $DB->set_field('message_processors', 'enabled', 1, array('name' => 'airnotifier')); 66 67 $configured = message_airnotifier_external::is_system_configured(); 68 $configured = \external_api::clean_returnvalue(message_airnotifier_external::is_system_configured_returns(), $configured); 69 $this->assertEquals(1, $configured); 70 } 71 72 /** 73 * Test are_notification_preferences_configured 74 */ 75 public function test_are_notification_preferences_configured() { 76 77 $this->resetAfterTest(true); 78 79 $user1 = self::getDataGenerator()->create_user(); 80 $user2 = self::getDataGenerator()->create_user(); 81 $user3 = self::getDataGenerator()->create_user(); 82 83 self::setUser($user1); 84 85 set_user_preference('message_provider_moodle_instantmessage_loggedin', 'airnotifier', $user1); 86 set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'airnotifier', $user1); 87 set_user_preference('message_provider_moodle_instantmessage_loggedin', 'airnotifier', $user2); 88 set_user_preference('message_provider_moodle_instantmessage_loggedin', 'airnotifier', $user3); 89 90 $params = array($user1->id, $user2->id, $user3->id); 91 92 $preferences = message_airnotifier_external::are_notification_preferences_configured($params); 93 $returnsdescription = message_airnotifier_external::are_notification_preferences_configured_returns(); 94 $preferences = \external_api::clean_returnvalue($returnsdescription, $preferences); 95 96 $expected = array( 97 array( 98 'userid' => $user1->id, 99 'configured' => 1 100 ) 101 ); 102 103 $this->assertEquals(1, count($preferences['users'])); 104 $this->assertEquals($expected, $preferences['users']); 105 $this->assertEquals(2, count($preferences['warnings'])); 106 107 // Now, remove one user. 108 delete_user($user2); 109 $preferences = message_airnotifier_external::are_notification_preferences_configured($params); 110 $preferences = \external_api::clean_returnvalue($returnsdescription, $preferences); 111 $this->assertEquals(1, count($preferences['users'])); 112 $this->assertEquals($expected, $preferences['users']); 113 $this->assertEquals(2, count($preferences['warnings'])); 114 115 // Now, remove one user1 preference (the user still has one prefernce for airnotifier). 116 unset_user_preference('message_provider_moodle_instantmessage_loggedin', $user1); 117 $preferences = message_airnotifier_external::are_notification_preferences_configured($params); 118 $preferences = \external_api::clean_returnvalue($returnsdescription, $preferences); 119 $this->assertEquals($expected, $preferences['users']); 120 121 // Delete the last user1 preference. 122 unset_user_preference('message_provider_moodle_instantmessage_loggedoff', $user1); 123 $preferences = message_airnotifier_external::are_notification_preferences_configured($params); 124 $preferences = \external_api::clean_returnvalue($returnsdescription, $preferences); 125 $expected = array( 126 array( 127 'userid' => $user1->id, 128 'configured' => 1 129 ) 130 ); 131 $this->assertEquals($expected, $preferences['users']); 132 } 133 134 /** 135 * Test get_user_devices 136 */ 137 public function test_get_user_devices() { 138 global $CFG, $DB; 139 require_once($CFG->dirroot . '/user/externallib.php'); 140 141 $this->resetAfterTest(true); 142 $this->setAdminUser(); 143 144 // System not configured. 145 $devices = message_airnotifier_external::get_user_devices(''); 146 $devices = \external_api::clean_returnvalue(message_airnotifier_external::get_user_devices_returns(), $devices); 147 $this->assertCount(1, $devices['warnings']); 148 $this->assertEquals('systemnotconfigured', $devices['warnings'][0]['warningcode']); 149 150 // Fake configuration. 151 set_config('airnotifieraccesskey', random_string()); 152 // Enable the plugin. 153 $DB->set_field('message_processors', 'enabled', 1, array('name' => 'airnotifier')); 154 155 // Get devices. 156 $devices = message_airnotifier_external::get_user_devices(''); 157 $devices = \external_api::clean_returnvalue(message_airnotifier_external::get_user_devices_returns(), $devices); 158 $this->assertCount(0, $devices['warnings']); 159 // No devices, unfortunatelly we cannot create devices (we can't mock airnotifier server). 160 $this->assertCount(0, $devices['devices']); 161 } 162 163 /** 164 * Test get_user_devices permissions 165 */ 166 public function test_get_user_devices_permissions() { 167 global $CFG, $DB; 168 require_once($CFG->dirroot . '/user/externallib.php'); 169 170 $this->resetAfterTest(true); 171 $user = self::getDataGenerator()->create_user(); 172 $otheruser = self::getDataGenerator()->create_user(); 173 self::setUser($user); 174 175 // No permission to get other users devices. 176 $this->expectException('required_capability_exception'); 177 $devices = message_airnotifier_external::get_user_devices('', $otheruser->id); 178 } 179 180 /** 181 * Test enable_device 182 */ 183 public function test_enable_device() { 184 global $USER, $DB; 185 186 $this->resetAfterTest(true); 187 $this->setAdminUser(); 188 189 // Add fake core device. 190 $device = array( 191 'appid' => 'com.moodle.moodlemobile', 192 'name' => 'occam', 193 'model' => 'Nexus 4', 194 'platform' => 'Android', 195 'version' => '4.2.2', 196 'pushid' => 'apushdkasdfj4835', 197 'uuid' => 'asdnfl348qlksfaasef859', 198 'userid' => $USER->id, 199 'timecreated' => time(), 200 'timemodified' => time(), 201 ); 202 $coredeviceid = $DB->insert_record('user_devices', (object) $device); 203 204 $airnotifierdev = array( 205 'userdeviceid' => $coredeviceid, 206 'enable' => 1 207 ); 208 $airnotifierdevid = $DB->insert_record('message_airnotifier_devices', (object) $airnotifierdev); 209 210 // Disable and enable. 211 $result = message_airnotifier_external::enable_device($airnotifierdevid, false); 212 $result = \external_api::clean_returnvalue(message_airnotifier_external::enable_device_returns(), $result); 213 $this->assertCount(0, $result['warnings']); 214 $this->assertTrue($result['success']); 215 $this->assertEquals(0, $DB->get_field('message_airnotifier_devices', 'enable', array('id' => $airnotifierdevid))); 216 217 $result = message_airnotifier_external::enable_device($airnotifierdevid, true); 218 $result = \external_api::clean_returnvalue(message_airnotifier_external::enable_device_returns(), $result); 219 $this->assertCount(0, $result['warnings']); 220 $this->assertTrue($result['success']); 221 $this->assertEquals(1, $DB->get_field('message_airnotifier_devices', 'enable', array('id' => $airnotifierdevid))); 222 } 223 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body