Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

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