Merge branch 'MDL-68565' of https://github.com/paulholden/moodle
[moodle.git] / auth / email / 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 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  */
27 defined('MOODLE_INTERNAL') || die();
29 global $CFG;
31 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
33 /**
34  * External auth email API tests.
35  *
36  * @package     auth_email
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 auth_email_external_testcase extends externallib_advanced_testcase {
43     /**
44      * Set up for every test
45      */
46     public function setUp() {
47         global $CFG, $DB;
49         $this->resetAfterTest(true);
50         $CFG->registerauth = 'email';
52         $categoryid = $DB->insert_record('user_info_category', array('name' => 'Cat 1', 'sortorder' => 1));
53         $this->field1 = $DB->insert_record('user_info_field', array(
54                 'shortname' => 'frogname', 'name' => 'Name of frog', 'categoryid' => $categoryid,
55                 'datatype' => 'text', 'signup' => 1, 'visible' => 1, 'required' => 1, 'sortorder' => 1));
56         $this->field2 = $DB->insert_record('user_info_field', array(
57                 'shortname' => 'sometext', 'name' => 'Some text in textarea', 'categoryid' => $categoryid,
58                 'datatype' => 'textarea', 'signup' => 1, 'visible' => 1, 'required' => 1, 'sortorder' => 2));
59     }
61     public function test_get_signup_settings() {
62         global $CFG;
64         $CFG->defaultcity = 'Bcn';
65         $CFG->country = 'ES';
66         $CFG->sitepolicy = 'https://moodle.org';
68         $result = auth_email_external::get_signup_settings();
69         $result = external_api::clean_returnvalue(auth_email_external::get_signup_settings_returns(), $result);
71         // Check expected data.
72         $this->assertEquals(array('firstname', 'lastname'), $result['namefields']);
73         $this->assertEquals($CFG->defaultcity, $result['defaultcity']);
74         $this->assertEquals($CFG->country, $result['country']);
75         $this->assertEquals($CFG->sitepolicy, $result['sitepolicy']);
76         $this->assertEquals(print_password_policy(), $result['passwordpolicy']);
77         $this->assertNotContains('recaptchachallengehash', $result);
78         $this->assertNotContains('recaptchachallengeimage', $result);
79         $this->assertCount(2, $result['profilefields']);
80         $this->assertEquals('text', $result['profilefields'][0]['datatype']);
81         $this->assertEquals('textarea', $result['profilefields'][1]['datatype']);
82     }
84     public function test_signup_user() {
85         global $DB;
87         $username = 'pepe';
88         $password = 'abcdefAª.ªª!!3';
89         $firstname = 'Pepe';
90         $lastname = 'Pérez';
91         $email = 'myemail@no.zbc';
92         $city = 'Bcn';
93         $country = 'ES';
94         $customprofilefields = array(
95             array(
96                 'type' => 'text',
97                 'name' => 'profile_field_frogname',
98                 'value' => 'random text',
99             ),
100             array(
101                 'type' => 'textarea',
102                 'name' => 'profile_field_sometext',
103                 'value' => json_encode(
104                     array(
105                         'text' => 'blah blah',
106                         'format' => FORMAT_HTML
107                     )
108                 ),
109             )
110         );
112         // Create new user.
113         $result = auth_email_external::signup_user($username, $password, $firstname, $lastname, $email, $city,  $country,
114                                                     '', '', $customprofilefields);
115         $result = external_api::clean_returnvalue(auth_email_external::signup_user_returns(), $result);
116         $this->assertTrue($result['success']);
117         $this->assertEmpty($result['warnings']);
118         $user = $DB->get_record('user', array('username' => $username));
119         $this->assertEquals($firstname, $user->firstname);
120         $this->assertEquals($lastname, $user->lastname);
121         $this->assertEquals($email, $user->email);
122         $this->assertEquals($city, $user->city);
123         $this->assertEquals($country, $user->country);
124         $this->assertEquals(0, $user->confirmed);
125         $this->assertEquals(current_language(), $user->lang);
126         $this->assertEquals('email', $user->auth);
127         $infofield = $DB->get_record('user_info_data', array('userid' => $user->id, 'fieldid' => $this->field1));
128         $this->assertEquals($customprofilefields[0]['value'], $infofield->data);
129         $infofield = $DB->get_record('user_info_data', array('userid' => $user->id, 'fieldid' => $this->field2));
130         $this->assertEquals(json_decode($customprofilefields[1]['value'])->text, $infofield->data);
132         // Try to create a user with the same username, email and password. We ommit also the profile fields.
133         $password = 'abc';
134         $result = auth_email_external::signup_user($username, $password, $firstname, $lastname, $email, $city,  $country,
135                                                     '', '', $customprofilefields);
136         $result = external_api::clean_returnvalue(auth_email_external::signup_user_returns(), $result);
137         $this->assertFalse($result['success']);
138         $this->assertCount(3, $result['warnings']);
139         $expectederrors = array('username', 'email', 'password');
140         $finalerrors = [];
141         foreach ($result['warnings'] as $warning) {
142             $finalerrors[] = $warning['item'];
143         }
144         $this->assertEquals($expectederrors, $finalerrors);
146         // Do not pass the required profile fields.
147         $this->expectException('invalid_parameter_exception');
148         $result = auth_email_external::signup_user($username, $password, $firstname, $lastname, $email, $city,  $country);
149     }