weekly release 3.1dev
[moodle.git] / lib / tests / user_test.php
CommitLineData
3bcf6b3c
RT
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/**
18 * Tests core_user class.
19 *
20 * @package core
21 * @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25/**
26 * Test core_user class.
27 *
28 * @package core
29 * @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 */
32class core_user_testcase extends advanced_testcase {
33
2d35b7d3
GPL
34 /**
35 * Setup test data.
36 */
37 protected function setUp() {
38 $this->resetAfterTest(true);
39 }
40
3bcf6b3c
RT
41 public function test_get_user() {
42 global $CFG;
43
3bcf6b3c
RT
44
45 // Create user and try fetach it with api.
46 $user = $this->getDataGenerator()->create_user();
47 $this->assertEquals($user, core_user::get_user($user->id, '*', MUST_EXIST));
48
49 // Test noreply user.
50 $CFG->noreplyuserid = null;
51 $noreplyuser = core_user::get_noreply_user();
52 $this->assertEquals(1, $noreplyuser->emailstop);
53 $this->assertFalse(core_user::is_real_user($noreplyuser->id));
54 $this->assertEquals($CFG->noreplyaddress, $noreplyuser->email);
55 $this->assertEquals(get_string('noreplyname'), $noreplyuser->firstname);
56
57 // Set user as noreply user and make sure noreply propery is set.
58 core_user::reset_internal_users();
59 $CFG->noreplyuserid = $user->id;
60 $noreplyuser = core_user::get_noreply_user();
61 $this->assertEquals(1, $noreplyuser->emailstop);
62 $this->assertTrue(core_user::is_real_user($noreplyuser->id));
63
64 // Test support user.
65 core_user::reset_internal_users();
66 $CFG->supportemail = null;
67 $CFG->noreplyuserid = null;
68 $supportuser = core_user::get_support_user();
69 $adminuser = get_admin();
70 $this->assertEquals($adminuser, $supportuser);
71 $this->assertTrue(core_user::is_real_user($supportuser->id));
72
73 // When supportemail is set.
74 core_user::reset_internal_users();
0fe86bbd 75 $CFG->supportemail = 'test@example.com';
3bcf6b3c
RT
76 $supportuser = core_user::get_support_user();
77 $this->assertEquals(core_user::SUPPORT_USER, $supportuser->id);
78 $this->assertFalse(core_user::is_real_user($supportuser->id));
79
80 // Set user as support user and make sure noreply propery is set.
81 core_user::reset_internal_users();
82 $CFG->supportuserid = $user->id;
83 $supportuser = core_user::get_support_user();
84 $this->assertEquals($user, $supportuser);
85 $this->assertTrue(core_user::is_real_user($supportuser->id));
86 }
2d35b7d3
GPL
87
88 /**
89 * Test get_user_by_username method.
90 */
91 public function test_get_user_by_username() {
92 $record = array();
93 $record['username'] = 'johndoe';
94 $record['email'] = 'johndoe@example.com';
95 $record['timecreated'] = time();
96
97 // Create a default user for the test.
98 $userexpected = $this->getDataGenerator()->create_user($record);
99
100 // Assert that the returned user is the espected one.
101 $this->assertEquals($userexpected, core_user::get_user_by_username('johndoe'));
102
103 // Assert that a subset of fields is correctly returned.
104 $this->assertEquals((object) $record, core_user::get_user_by_username('johndoe', 'username,email,timecreated'));
105
106 // Assert that a user with a different mnethostid will no be returned.
107 $this->assertFalse(core_user::get_user_by_username('johndoe', 'username,email,timecreated', 2));
108
109 // Create a new user from a different host.
110 $record['mnethostid'] = 2;
111 $userexpected2 = $this->getDataGenerator()->create_user($record);
112
113 // Assert that the new user is returned when specified the correct mnethostid.
114 $this->assertEquals($userexpected2, core_user::get_user_by_username('johndoe', '*', 2));
115
116 // Assert that a user not in the db return false.
117 $this->assertFalse(core_user::get_user_by_username('janedoe'));
118 }
3961ebfb
JL
119
120 /**
121 * Test require_active_user
122 */
123 public function test_require_active_user() {
124 global $DB;
125
126 // Create a default user for the test.
127 $userexpected = $this->getDataGenerator()->create_user();
128
129 // Simple case, all good.
130 core_user::require_active_user($userexpected, true, true);
131
132 // Set user not confirmed.
133 $DB->set_field('user', 'confirmed', 0, array('id' => $userexpected->id));
134 try {
135 core_user::require_active_user($userexpected);
136 } catch (moodle_exception $e) {
137 $this->assertEquals('usernotconfirmed', $e->errorcode);
138 }
139 $DB->set_field('user', 'confirmed', 1, array('id' => $userexpected->id));
140
141 // Set nologin auth method.
142 $DB->set_field('user', 'auth', 'nologin', array('id' => $userexpected->id));
143 try {
144 core_user::require_active_user($userexpected, false, true);
145 } catch (moodle_exception $e) {
146 $this->assertEquals('suspended', $e->errorcode);
147 }
148 // Check no exceptions are thrown if we don't specify to check suspended.
149 core_user::require_active_user($userexpected);
150 $DB->set_field('user', 'auth', 'manual', array('id' => $userexpected->id));
151
152 // Set user suspended.
153 $DB->set_field('user', 'suspended', 1, array('id' => $userexpected->id));
154 try {
155 core_user::require_active_user($userexpected, true);
156 } catch (moodle_exception $e) {
157 $this->assertEquals('suspended', $e->errorcode);
158 }
159 // Check no exceptions are thrown if we don't specify to check suspended.
160 core_user::require_active_user($userexpected);
161
162 // Delete user.
163 delete_user($userexpected);
164 try {
165 core_user::require_active_user($userexpected);
166 } catch (moodle_exception $e) {
167 $this->assertEquals('userdeleted', $e->errorcode);
168 }
169
170 // Use a not real user.
171 $noreplyuser = core_user::get_noreply_user();
172 try {
173 core_user::require_active_user($noreplyuser, true);
174 } catch (moodle_exception $e) {
175 $this->assertEquals('invaliduser', $e->errorcode);
176 }
177
178 // Get the guest user.
179 $guestuser = $DB->get_record('user', array('username' => 'guest'));
180 try {
181 core_user::require_active_user($guestuser, true);
182 } catch (moodle_exception $e) {
183 $this->assertEquals('guestsarenotallowed', $e->errorcode);
184 }
185
186 }
dccf9ca3
SL
187
188 /**
189 * Test get_property_definition() method.
190 */
191 public function test_get_property_definition() {
192 // Try to get a existing property.
193 $properties = core_user::get_property_definition('id');
194 $this->assertEquals($properties['type'], PARAM_INT);
195 $properties = core_user::get_property_definition('username');
196 $this->assertEquals($properties['type'], PARAM_USERNAME);
197
198 // Invalid property.
199 try {
200 core_user::get_property_definition('fullname');
201 } catch (coding_exception $e) {
202 $this->assertRegExp('/Invalid property requested./', $e->getMessage());
203 }
204
205 // Empty parameter.
206 try {
207 core_user::get_property_definition('');
208 } catch (coding_exception $e) {
209 $this->assertRegExp('/Invalid property requested./', $e->getMessage());
210 }
211 }
3bcf6b3c 212}