MDL-39961 Events: Replace Legacy events - User
[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);
79 }
80
81 /**
82 * Test create_users.
83 */
84 public function test_create_users() {
85 global $DB;
86
87 $this->resetAfterTest();
88
89 $user = array(
90 'username' => 'usernametest1',
91 'password' => 'Moodle2012!',
92 'idnumber' => 'idnumbertest1',
93 'firstname' => 'First Name User Test 1',
94 'lastname' => 'Last Name User Test 1',
95 'middlename' => 'Middle Name User Test 1',
96 'lastnamephonetic' => '最後のお名前のテスト一号',
97 'firstnamephonetic' => 'お名前のテスト一号',
98 'alternatename' => 'Alternate Name User Test 1',
99 'email' => 'usertest1@email.com',
100 'description' => 'This is a description for user 1',
101 'city' => 'Perth',
102 'country' => 'au'
103 );
104
105 // Create user and capture event.
106 $sink = $this->redirectEvents();
107 $user['id'] = user_create_user($user);
108 $events = $sink->get_events();
109 $sink->close();
110 $event = array_pop($events);
111
112 // Test user info in DB.
113 $dbuser = $DB->get_record('user', array('id' => $user['id']));
114 $this->assertEquals($dbuser->username, $user['username']);
115 $this->assertEquals($dbuser->idnumber, $user['idnumber']);
116 $this->assertEquals($dbuser->firstname, $user['firstname']);
117 $this->assertEquals($dbuser->lastname, $user['lastname']);
118 $this->assertEquals($dbuser->email, $user['email']);
119 $this->assertEquals($dbuser->description, $user['description']);
120 $this->assertEquals($dbuser->city, $user['city']);
121 $this->assertEquals($dbuser->country, $user['country']);
122
123 // Test event.
124 $this->assertInstanceOf('\core\event\user_created', $event);
125 $this->assertEquals($user['id'], $event->objectid);
126 $this->assertEquals('user_created', $event->get_legacy_eventname());
127 $this->assertEquals(context_user::instance($user['id']), $event->get_context());
128 $this->assertEventLegacyData($dbuser, $event);
129 $expectedlogdata = array(SITEID, 'user', 'add', '/view.php?id='.$event->objectid, fullname($dbuser));
130 $this->assertEventLegacyLogData($expectedlogdata, $event);
131 }
132}