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 email external functions tests.
|
|
|
19 |
*
|
|
|
20 |
* @package auth_email
|
|
|
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 auth_email\external;
|
|
|
28 |
|
|
|
29 |
use auth_email_external;
|
|
|
30 |
use externallib_advanced_testcase;
|
|
|
31 |
|
|
|
32 |
defined('MOODLE_INTERNAL') || die();
|
|
|
33 |
|
|
|
34 |
global $CFG;
|
|
|
35 |
|
|
|
36 |
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* External auth email API tests.
|
|
|
40 |
*
|
|
|
41 |
* @package auth_email
|
|
|
42 |
* @copyright 2016 Juan Leyva
|
|
|
43 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
44 |
* @since Moodle 3.2
|
|
|
45 |
*/
|
|
|
46 |
class external_test extends externallib_advanced_testcase {
|
|
|
47 |
|
|
|
48 |
/** @var int custom profile field1 ID. */
|
|
|
49 |
protected $field1;
|
|
|
50 |
|
|
|
51 |
/** @var int custom profile field2 ID. */
|
|
|
52 |
protected $field2;
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* Set up for every test
|
|
|
56 |
*/
|
|
|
57 |
public function setUp(): void {
|
|
|
58 |
global $CFG;
|
|
|
59 |
|
|
|
60 |
$this->resetAfterTest(true);
|
|
|
61 |
$CFG->registerauth = 'email';
|
|
|
62 |
|
|
|
63 |
$this->field1 = $this->getDataGenerator()->create_custom_profile_field(array(
|
|
|
64 |
'shortname' => 'frogname', 'name' => 'Name of frog',
|
|
|
65 |
'datatype' => 'text', 'signup' => 1, 'visible' => 1, 'required' => 1, 'sortorder' => 1))->id;
|
|
|
66 |
$this->field2 = $this->getDataGenerator()->create_custom_profile_field(array(
|
|
|
67 |
'shortname' => 'sometext', 'name' => 'Some text in textarea',
|
|
|
68 |
'datatype' => 'textarea', 'signup' => 1, 'visible' => 1, 'required' => 1, 'sortorder' => 2))->id;
|
|
|
69 |
}
|
|
|
70 |
|
11 |
efrain |
71 |
public function test_get_signup_settings(): void {
|
1 |
efrain |
72 |
global $CFG;
|
|
|
73 |
|
|
|
74 |
$CFG->defaultcity = 'Bcn';
|
|
|
75 |
$CFG->country = 'ES';
|
|
|
76 |
$CFG->sitepolicy = 'https://moodle.org';
|
|
|
77 |
|
|
|
78 |
$result = auth_email_external::get_signup_settings();
|
|
|
79 |
$result = \core_external\external_api::clean_returnvalue(auth_email_external::get_signup_settings_returns(), $result);
|
|
|
80 |
|
|
|
81 |
// Check expected data.
|
|
|
82 |
$this->assertEquals(array('firstname', 'lastname'), $result['namefields']);
|
|
|
83 |
$this->assertEquals($CFG->defaultcity, $result['defaultcity']);
|
|
|
84 |
$this->assertEquals($CFG->country, $result['country']);
|
|
|
85 |
$this->assertEquals($CFG->sitepolicy, $result['sitepolicy']);
|
|
|
86 |
$this->assertEquals(print_password_policy(), $result['passwordpolicy']);
|
|
|
87 |
$this->assertNotContains('recaptchachallengehash', $result);
|
|
|
88 |
$this->assertNotContains('recaptchachallengeimage', $result);
|
|
|
89 |
// Check if the extended username chars is returning false when is not set.
|
|
|
90 |
$this->assertFalse($result['extendedusernamechars']);
|
|
|
91 |
|
|
|
92 |
// Whip up a array with named entries to easily check against.
|
|
|
93 |
$namedarray = array();
|
|
|
94 |
foreach ($result['profilefields'] as $key => $value) {
|
|
|
95 |
$namedarray[$value['shortname']] = array(
|
|
|
96 |
'datatype' => $value['datatype']
|
|
|
97 |
);
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
// Just check if we have the fields from this test. If a plugin adds fields we'll let it slide.
|
|
|
101 |
$this->assertArrayHasKey('frogname', $namedarray);
|
|
|
102 |
$this->assertArrayHasKey('sometext', $namedarray);
|
|
|
103 |
|
|
|
104 |
$this->assertEquals('text', $namedarray['frogname']['datatype']);
|
|
|
105 |
$this->assertEquals('textarea', $namedarray['sometext']['datatype']);
|
|
|
106 |
|
|
|
107 |
$CFG->extendedusernamechars = true;
|
|
|
108 |
$result = auth_email_external::get_signup_settings();
|
|
|
109 |
$result = \core_external\external_api::clean_returnvalue(auth_email_external::get_signup_settings_returns(), $result);
|
|
|
110 |
$this->assertTrue($result['extendedusernamechars']);
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
/**
|
|
|
114 |
* Test get_signup_settings with mathjax in a profile field.
|
|
|
115 |
*/
|
11 |
efrain |
116 |
public function test_get_signup_settings_with_mathjax_in_profile_fields(): void {
|
1 |
efrain |
117 |
// Enable MathJax filter in content and headings.
|
|
|
118 |
$this->configure_filters([
|
|
|
119 |
['name' => 'mathjaxloader', 'state' => TEXTFILTER_ON, 'move' => -1, 'applytostrings' => true],
|
|
|
120 |
]);
|
|
|
121 |
|
|
|
122 |
// Create category with MathJax and a new field with MathJax.
|
|
|
123 |
$categoryname = 'Cat $$(a+b)=2$$';
|
|
|
124 |
$fieldname = 'Some text $$(a+b)=2$$';
|
|
|
125 |
$categoryid = $this->getDataGenerator()->create_custom_profile_field_category(['name' => $categoryname])->id;
|
|
|
126 |
$this->getDataGenerator()->create_custom_profile_field(array(
|
|
|
127 |
'shortname' => 'mathjaxname', 'name' => $fieldname, 'categoryid' => $categoryid,
|
|
|
128 |
'datatype' => 'textarea', 'signup' => 1, 'visible' => 1, 'required' => 1, 'sortorder' => 2));
|
|
|
129 |
|
|
|
130 |
$result = auth_email_external::get_signup_settings();
|
|
|
131 |
$result = \core_external\external_api::clean_returnvalue(auth_email_external::get_signup_settings_returns(), $result);
|
|
|
132 |
|
|
|
133 |
// Format the original data.
|
|
|
134 |
$sitecontext = \context_system::instance();
|
|
|
135 |
$categoryname = \core_external\util::format_string($categoryname, $sitecontext->id);
|
|
|
136 |
$fieldname = \core_external\util::format_string($fieldname, $sitecontext->id);
|
|
|
137 |
|
|
|
138 |
// Whip up a array with named entries to easily check against.
|
|
|
139 |
$namedarray = array();
|
|
|
140 |
foreach ($result['profilefields'] as $key => $value) {
|
|
|
141 |
$namedarray[$value['shortname']] = $value;
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
// Check the new profile field.
|
|
|
145 |
$this->assertArrayHasKey('mathjaxname', $namedarray);
|
|
|
146 |
$this->assertStringContainsString('<span class="filter_mathjaxloader_equation">',
|
|
|
147 |
$namedarray['mathjaxname']['categoryname']);
|
|
|
148 |
$this->assertStringContainsString('<span class="filter_mathjaxloader_equation">',
|
|
|
149 |
$namedarray['mathjaxname']['name']);
|
|
|
150 |
$this->assertEquals($categoryname, $namedarray['mathjaxname']['categoryname']);
|
|
|
151 |
$this->assertEquals($fieldname, $namedarray['mathjaxname']['name']);
|
|
|
152 |
}
|
|
|
153 |
|
11 |
efrain |
154 |
public function test_signup_user(): void {
|
1 |
efrain |
155 |
global $DB;
|
|
|
156 |
|
|
|
157 |
$username = 'pepe';
|
|
|
158 |
$password = 'abcdefAª.ªª!!3';
|
|
|
159 |
$firstname = 'Pepe';
|
|
|
160 |
$lastname = 'Pérez';
|
|
|
161 |
$email = 'myemail@no.zbc';
|
|
|
162 |
$city = 'Bcn';
|
|
|
163 |
$country = 'ES';
|
|
|
164 |
$customprofilefields = array(
|
|
|
165 |
array(
|
|
|
166 |
'type' => 'text',
|
|
|
167 |
'name' => 'profile_field_frogname',
|
|
|
168 |
'value' => 'random text',
|
|
|
169 |
),
|
|
|
170 |
array(
|
|
|
171 |
'type' => 'textarea',
|
|
|
172 |
'name' => 'profile_field_sometext',
|
|
|
173 |
'value' => json_encode(
|
|
|
174 |
array(
|
|
|
175 |
'text' => 'blah blah',
|
|
|
176 |
'format' => FORMAT_HTML
|
|
|
177 |
)
|
|
|
178 |
),
|
|
|
179 |
)
|
|
|
180 |
);
|
|
|
181 |
|
|
|
182 |
// Create new user.
|
|
|
183 |
$result = auth_email_external::signup_user($username, $password, $firstname, $lastname, $email, $city, $country,
|
|
|
184 |
'', '', $customprofilefields);
|
|
|
185 |
$result = \core_external\external_api::clean_returnvalue(auth_email_external::signup_user_returns(), $result);
|
|
|
186 |
$this->assertTrue($result['success']);
|
|
|
187 |
$this->assertEmpty($result['warnings']);
|
|
|
188 |
$user = $DB->get_record('user', array('username' => $username));
|
|
|
189 |
$this->assertEquals($firstname, $user->firstname);
|
|
|
190 |
$this->assertEquals($lastname, $user->lastname);
|
|
|
191 |
$this->assertEquals($email, $user->email);
|
|
|
192 |
$this->assertEquals($city, $user->city);
|
|
|
193 |
$this->assertEquals($country, $user->country);
|
|
|
194 |
$this->assertEquals(0, $user->confirmed);
|
|
|
195 |
$this->assertEquals(current_language(), $user->lang);
|
|
|
196 |
$this->assertEquals('email', $user->auth);
|
|
|
197 |
$infofield = $DB->get_record('user_info_data', array('userid' => $user->id, 'fieldid' => $this->field1));
|
|
|
198 |
$this->assertEquals($customprofilefields[0]['value'], $infofield->data);
|
|
|
199 |
$infofield = $DB->get_record('user_info_data', array('userid' => $user->id, 'fieldid' => $this->field2));
|
|
|
200 |
$this->assertEquals(json_decode($customprofilefields[1]['value'])->text, $infofield->data);
|
|
|
201 |
|
|
|
202 |
// Try to create a user with the same username, email and password. We ommit also the profile fields.
|
|
|
203 |
$password = 'abc';
|
|
|
204 |
$result = auth_email_external::signup_user($username, $password, $firstname, $lastname, $email, $city, $country,
|
|
|
205 |
'', '', $customprofilefields);
|
|
|
206 |
$result = \core_external\external_api::clean_returnvalue(auth_email_external::signup_user_returns(), $result);
|
|
|
207 |
$this->assertFalse($result['success']);
|
|
|
208 |
$this->assertCount(3, $result['warnings']);
|
|
|
209 |
$expectederrors = array('username', 'email', 'password');
|
|
|
210 |
$finalerrors = [];
|
|
|
211 |
foreach ($result['warnings'] as $warning) {
|
|
|
212 |
$finalerrors[] = $warning['item'];
|
|
|
213 |
}
|
|
|
214 |
$this->assertEquals($expectederrors, $finalerrors);
|
|
|
215 |
|
|
|
216 |
// Do not pass the required profile fields.
|
|
|
217 |
$this->expectException('invalid_parameter_exception');
|
|
|
218 |
$result = auth_email_external::signup_user($username, $password, $firstname, $lastname, $email, $city, $country);
|
|
|
219 |
}
|
|
|
220 |
}
|