MDL-63742 auth: New WS core_auth_resend_confirmation_email
[moodle.git] / auth / tests / external_test.php
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/>.
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  */
27 defined('MOODLE_INTERNAL') || die();
29 global $CFG;
31 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
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  */
41 class core_auth_external_testcase extends externallib_advanced_testcase {
43     /** @var string Original error log */
44     protected $oldlog;
46     /**
47      * Set up for every test
48      */
49     public function setUp() {
50         global $CFG;
52         $this->resetAfterTest(true);
53         $CFG->registerauth = 'email';
55         // Discard error logs.
56         $this->oldlog = ini_get('error_log');
57         ini_set('error_log', "$CFG->dataroot/testlog.log");
58     }
60     /**
61      * Tear down to restore old logging..
62      */
63     protected function tearDown() {
64         ini_set('error_log', $this->oldlog);
65         parent::tearDown();
66     }
68     /**
69      * Test confirm_user
70      */
71     public function test_confirm_user() {
72         global $DB;
74         $username = 'pepe';
75         $password = 'abcdefAª.ªª!!3';
76         $firstname = 'Pepe';
77         $lastname = 'Pérez';
78         $email = 'myemail@no.zbc';
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));
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);
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']);
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     }
108     /**
109      * Test age digital consent not enabled.
110      */
111     public function test_age_digital_consent_verification_is_not_enabled() {
112         global $CFG;
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     }
121     /**
122      * Test age digital consent is enabled.
123      */
124     public function test_age_digital_consent_verification_is_enabled() {
125         global $CFG;
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     }
134     /**
135      * Test resend_confirmation_email.
136      */
137     public function test_resend_confirmation_email() {
138         global $DB;
140         $username = 'pepe';
141         $password = 'abcdefAª.ªª!!3';
142         $firstname = 'Pepe';
143         $lastname = 'Pérez';
144         $email = 'myemail@no.zbc';
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']);
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     }
160     /**
161      * Test resend_confirmation_email invalid username.
162      */
163     public function test_resend_confirmation_email_invalid_username() {
165         $username = 'pepe';
166         $password = 'abcdefAª.ªª!!3';
167         $firstname = 'Pepe';
168         $lastname = 'Pérez';
169         $email = 'myemail@no.zbc';
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']);
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     }
183     /**
184      * Test resend_confirmation_email invalid password.
185      */
186     public function test_resend_confirmation_email_invalid_password() {
188         $username = 'pepe';
189         $password = 'abcdefAª.ªª!!3';
190         $firstname = 'Pepe';
191         $lastname = 'Pérez';
192         $email = 'myemail@no.zbc';
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']);
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     }
206     /**
207      * Test resend_confirmation_email already confirmed user.
208      */
209     public function test_resend_confirmation_email_already_confirmed_user() {
210         global $DB;
212         $username = 'pepe';
213         $password = 'abcdefAª.ªª!!3';
214         $firstname = 'Pepe';
215         $lastname = 'Pérez';
216         $email = 'myemail@no.zbc';
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));
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']);
230         $this->expectException('moodle_exception');
231         $this->expectExceptionMessage('error/alreadyconfirmed');
232         core_auth_external::resend_confirmation_email($username, $password);
233     }