MDL-63742 auth: New WS core_auth_resend_confirmation_email
[moodle.git] / auth / tests / external_test.php
CommitLineData
83e4dc17
JL
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 * Auth external functions tests.
19 *
20 * @package core_auth
21 * @category external
22 * @copyright 2016 Juan Leyva
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 * @since Moodle 3.2
25 */
26
27defined('MOODLE_INTERNAL') || die();
28
29global $CFG;
30
31require_once($CFG->dirroot . '/webservice/tests/helpers.php');
32
33/**
34 * External auth API tests.
35 *
36 * @package core_auth
37 * @copyright 2016 Juan Leyva
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 * @since Moodle 3.2
40 */
41class core_auth_external_testcase extends externallib_advanced_testcase {
42
6f793705
JL
43 /** @var string Original error log */
44 protected $oldlog;
45
83e4dc17
JL
46 /**
47 * Set up for every test
48 */
49 public function setUp() {
50 global $CFG;
51
52 $this->resetAfterTest(true);
53 $CFG->registerauth = 'email';
6f793705
JL
54
55 // Discard error logs.
56 $this->oldlog = ini_get('error_log');
57 ini_set('error_log', "$CFG->dataroot/testlog.log");
58 }
59
60 /**
61 * Tear down to restore old logging..
62 */
63 protected function tearDown() {
64 ini_set('error_log', $this->oldlog);
65 parent::tearDown();
83e4dc17
JL
66 }
67
68 /**
69 * Test confirm_user
70 */
71 public function test_confirm_user() {
72 global $DB;
73
74 $username = 'pepe';
75 $password = 'abcdefAª.ªª!!3';
76 $firstname = 'Pepe';
77 $lastname = 'Pérez';
78 $email = 'myemail@no.zbc';
79
80 // Create new user.
81 $result = auth_email_external::signup_user($username, $password, $firstname, $lastname, $email);
82 $result = external_api::clean_returnvalue(auth_email_external::signup_user_returns(), $result);
83 $this->assertTrue($result['success']);
84 $this->assertEmpty($result['warnings']);
85 $secret = $DB->get_field('user', 'secret', array('username' => $username));
86
87 // Confirm the user.
88 $result = core_auth_external::confirm_user($username, $secret);
89 $result = external_api::clean_returnvalue(core_auth_external::confirm_user_returns(), $result);
90 $this->assertTrue($result['success']);
91 $this->assertEmpty($result['warnings']);
92 $confirmed = $DB->get_field('user', 'confirmed', array('username' => $username));
93 $this->assertEquals(1, $confirmed);
94
95 // Try to confirm the user again.
96 $result = core_auth_external::confirm_user($username, $secret);
97 $result = external_api::clean_returnvalue(core_auth_external::confirm_user_returns(), $result);
98 $this->assertFalse($result['success']);
99 $this->assertCount(1, $result['warnings']);
100 $this->assertEquals('alreadyconfirmed', $result['warnings'][0]['warningcode']);
101
102 // Try to use an invalid secret.
103 $this->expectException('moodle_exception');
104 $this->expectExceptionMessage(get_string('invalidconfirmdata', 'error'));
105 $result = core_auth_external::confirm_user($username, 'zzZZzz');
106 }
1f7565c1
JL
107
108 /**
109 * Test age digital consent not enabled.
110 */
111 public function test_age_digital_consent_verification_is_not_enabled() {
112 global $CFG;
113
114 $CFG->agedigitalconsentverification = 0;
115 $result = core_auth_external::is_age_digital_consent_verification_enabled();
116 $result = external_api::clean_returnvalue(
117 core_auth_external::is_age_digital_consent_verification_enabled_returns(), $result);
118 $this->assertFalse($result['status']);
119 }
120
121 /**
122 * Test age digital consent is enabled.
123 */
124 public function test_age_digital_consent_verification_is_enabled() {
125 global $CFG;
126
127 $CFG->agedigitalconsentverification = 1;
128 $result = core_auth_external::is_age_digital_consent_verification_enabled();
129 $result = external_api::clean_returnvalue(
130 core_auth_external::is_age_digital_consent_verification_enabled_returns(), $result);
131 $this->assertTrue($result['status']);
132 }
6f793705
JL
133
134 /**
135 * Test resend_confirmation_email.
136 */
137 public function test_resend_confirmation_email() {
138 global $DB;
139
140 $username = 'pepe';
141 $password = 'abcdefAª.ªª!!3';
142 $firstname = 'Pepe';
143 $lastname = 'Pérez';
144 $email = 'myemail@no.zbc';
145
146 // Create new user.
147 $result = auth_email_external::signup_user($username, $password, $firstname, $lastname, $email);
148 $result = external_api::clean_returnvalue(auth_email_external::signup_user_returns(), $result);
149 $this->assertTrue($result['success']);
150 $this->assertEmpty($result['warnings']);
151
152 $result = core_auth_external::resend_confirmation_email($username, $password);
153 $result = external_api::clean_returnvalue(core_auth_external::resend_confirmation_email_returns(), $result);
154 $this->assertTrue($result['status']);
155 $this->assertEmpty($result['warnings']);
156 $confirmed = $DB->get_field('user', 'confirmed', array('username' => $username));
157 $this->assertEquals(0, $confirmed);
158 }
159
160 /**
161 * Test resend_confirmation_email invalid username.
162 */
163 public function test_resend_confirmation_email_invalid_username() {
164
165 $username = 'pepe';
166 $password = 'abcdefAª.ªª!!3';
167 $firstname = 'Pepe';
168 $lastname = 'Pérez';
169 $email = 'myemail@no.zbc';
170
171 // Create new user.
172 $result = auth_email_external::signup_user($username, $password, $firstname, $lastname, $email);
173 $result = external_api::clean_returnvalue(auth_email_external::signup_user_returns(), $result);
174 $this->assertTrue($result['success']);
175 $this->assertEmpty($result['warnings']);
176
177 $_SERVER['HTTP_USER_AGENT'] = 'no browser'; // Hack around missing user agent in CLI scripts.
178 $this->expectException('moodle_exception');
179 $this->expectExceptionMessage('error/invalidlogin');
180 $result = core_auth_external::resend_confirmation_email('abc', $password);
181 }
182
183 /**
184 * Test resend_confirmation_email invalid password.
185 */
186 public function test_resend_confirmation_email_invalid_password() {
187
188 $username = 'pepe';
189 $password = 'abcdefAª.ªª!!3';
190 $firstname = 'Pepe';
191 $lastname = 'Pérez';
192 $email = 'myemail@no.zbc';
193
194 // Create new user.
195 $result = auth_email_external::signup_user($username, $password, $firstname, $lastname, $email);
196 $result = external_api::clean_returnvalue(auth_email_external::signup_user_returns(), $result);
197 $this->assertTrue($result['success']);
198 $this->assertEmpty($result['warnings']);
199
200 $_SERVER['HTTP_USER_AGENT'] = 'no browser'; // Hack around missing user agent in CLI scripts.
201 $this->expectException('moodle_exception');
202 $this->expectExceptionMessage('error/invalidlogin');
203 $result = core_auth_external::resend_confirmation_email($username, 'abc');
204 }
205
206 /**
207 * Test resend_confirmation_email already confirmed user.
208 */
209 public function test_resend_confirmation_email_already_confirmed_user() {
210 global $DB;
211
212 $username = 'pepe';
213 $password = 'abcdefAª.ªª!!3';
214 $firstname = 'Pepe';
215 $lastname = 'Pérez';
216 $email = 'myemail@no.zbc';
217
218 // Create new user.
219 $result = auth_email_external::signup_user($username, $password, $firstname, $lastname, $email);
220 $result = external_api::clean_returnvalue(auth_email_external::signup_user_returns(), $result);
221 $this->assertTrue($result['success']);
222 $this->assertEmpty($result['warnings']);
223 $secret = $DB->get_field('user', 'secret', array('username' => $username));
224
225 // Confirm the user.
226 $result = core_auth_external::confirm_user($username, $secret);
227 $result = external_api::clean_returnvalue(core_auth_external::confirm_user_returns(), $result);
228 $this->assertTrue($result['success']);
229
230 $this->expectException('moodle_exception');
231 $this->expectExceptionMessage('error/alreadyconfirmed');
232 core_auth_external::resend_confirmation_email($username, $password);
233 }
83e4dc17 234}