weekly release 2.9dev
[moodle.git] / user / tests / userlib_test.php
CommitLineData
bb78e249
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 * Unit tests for user/lib.php.
19 *
20 * @package core_user
21 * @category phpunit
22 * @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29require_once($CFG->dirroot.'/user/lib.php');
30
31/**
32 * Unit tests for user lib api.
33 *
34 * @package core_user
35 * @category phpunit
36 * @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class core_userliblib_testcase extends advanced_testcase {
40 /**
41 * Test user_update_user.
42 */
43 public function test_user_update_user() {
44 global $DB;
45
46 $this->resetAfterTest();
47
48 // Create user and modify user profile.
49 $user = $this->getDataGenerator()->create_user();
50 $user->firstname = 'Test';
51 $user->password = 'M00dLe@T';
52
53 // Update user and capture event.
54 $sink = $this->redirectEvents();
55 user_update_user($user);
56 $events = $sink->get_events();
57 $sink->close();
58 $event = array_pop($events);
59
60 // Test updated value.
61 $dbuser = $DB->get_record('user', array('id' => $user->id));
62 $this->assertSame($user->firstname, $dbuser->firstname);
63 $this->assertNotSame('M00dLe@T', $dbuser->password);
64
65 // Test event.
66 $this->assertInstanceOf('\core\event\user_updated', $event);
67 $this->assertSame($user->id, $event->objectid);
68 $this->assertSame('user_updated', $event->get_legacy_eventname());
69 $this->assertEventLegacyData($dbuser, $event);
70 $this->assertEquals(context_user::instance($user->id), $event->get_context());
71 $expectedlogdata = array(SITEID, 'user', 'update', 'view.php?id='.$user->id, '');
72 $this->assertEventLegacyLogData($expectedlogdata, $event);
73
74 // Update user with no password update.
75 $password = $user->password = hash_internal_user_password('M00dLe@T');
76 user_update_user($user, false);
77 $dbuser = $DB->get_record('user', array('id' => $user->id));
78 $this->assertSame($password, $dbuser->password);
2b55cb1b
RT
79
80 // Verify event is not triggred by user_update_user when needed.
81 $sink = $this->redirectEvents();
82 user_update_user($user, false, false);
83 $events = $sink->get_events();
84 $sink->close();
85 $this->assertCount(0, $events);
86
87 // With password, there should be 1 event.
88 $sink = $this->redirectEvents();
89 user_update_user($user, true, false);
90 $events = $sink->get_events();
91 $sink->close();
92 $this->assertCount(1, $events);
93 $event = array_pop($events);
94 $this->assertInstanceOf('\core\event\user_password_updated', $event);
bb78e249
RT
95 }
96
97 /**
98 * Test create_users.
99 */
100 public function test_create_users() {
101 global $DB;
102
103 $this->resetAfterTest();
104
105 $user = array(
106 'username' => 'usernametest1',
107 'password' => 'Moodle2012!',
108 'idnumber' => 'idnumbertest1',
109 'firstname' => 'First Name User Test 1',
110 'lastname' => 'Last Name User Test 1',
111 'middlename' => 'Middle Name User Test 1',
112 'lastnamephonetic' => '最後のお名前のテスト一号',
113 'firstnamephonetic' => 'お名前のテスト一号',
114 'alternatename' => 'Alternate Name User Test 1',
115 'email' => 'usertest1@email.com',
116 'description' => 'This is a description for user 1',
117 'city' => 'Perth',
118 'country' => 'au'
119 );
120
121 // Create user and capture event.
122 $sink = $this->redirectEvents();
123 $user['id'] = user_create_user($user);
124 $events = $sink->get_events();
125 $sink->close();
126 $event = array_pop($events);
127
128 // Test user info in DB.
129 $dbuser = $DB->get_record('user', array('id' => $user['id']));
130 $this->assertEquals($dbuser->username, $user['username']);
131 $this->assertEquals($dbuser->idnumber, $user['idnumber']);
132 $this->assertEquals($dbuser->firstname, $user['firstname']);
133 $this->assertEquals($dbuser->lastname, $user['lastname']);
134 $this->assertEquals($dbuser->email, $user['email']);
135 $this->assertEquals($dbuser->description, $user['description']);
136 $this->assertEquals($dbuser->city, $user['city']);
137 $this->assertEquals($dbuser->country, $user['country']);
138
139 // Test event.
140 $this->assertInstanceOf('\core\event\user_created', $event);
141 $this->assertEquals($user['id'], $event->objectid);
142 $this->assertEquals('user_created', $event->get_legacy_eventname());
143 $this->assertEquals(context_user::instance($user['id']), $event->get_context());
144 $this->assertEventLegacyData($dbuser, $event);
145 $expectedlogdata = array(SITEID, 'user', 'add', '/view.php?id='.$event->objectid, fullname($dbuser));
146 $this->assertEventLegacyLogData($expectedlogdata, $event);
2b55cb1b
RT
147
148 // Verify event is not triggred by user_create_user when needed.
149 $user = array('username' => 'usernametest2'); // Create another user.
150 $sink = $this->redirectEvents();
151 user_create_user($user, true, false);
152 $events = $sink->get_events();
153 $sink->close();
154 $this->assertCount(0, $events);
bb78e249 155 }
52dc1de7
AA
156
157 /**
158 * Test function user_count_login_failures().
159 */
160 public function test_user_count_login_failures() {
161 $this->resetAfterTest();
162 $user = $this->getDataGenerator()->create_user();
163 $this->assertEquals(0, get_user_preferences('login_failed_count_since_success', 0, $user));
164 for ($i = 0; $i < 10; $i++) {
165 login_attempt_failed($user);
166 }
167 $this->assertEquals(10, get_user_preferences('login_failed_count_since_success', 0, $user));
168 $count = user_count_login_failures($user); // Reset count.
169 $this->assertEquals(10, $count);
170 $this->assertEquals(0, get_user_preferences('login_failed_count_since_success', 0, $user));
171
172 for ($i = 0; $i < 10; $i++) {
173 login_attempt_failed($user);
174 }
175 $this->assertEquals(10, get_user_preferences('login_failed_count_since_success', 0, $user));
176 $count = user_count_login_failures($user, false); // Do not reset count.
177 $this->assertEquals(10, $count);
178 $this->assertEquals(10, get_user_preferences('login_failed_count_since_success', 0, $user));
179 }
bb78e249 180}