Merge branch 'MDL-68244' of https://github.com/Chocolate-lightning/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);
80         // Whip up a array with named entries to easily check against.
81         $namedarray = array();
82         foreach ($result['profilefields'] as $key => $value) {
83             $namedarray[$value['shortname']] = array(
84                 'datatype' => $value['datatype']
85             );
86         }
88         // Just check if we have the fields from this test. If a plugin adds fields we'll let it slide.
89         $this->assertArrayHasKey('frogname', $namedarray);
90         $this->assertArrayHasKey('sometext', $namedarray);
92         $this->assertEquals('text', $namedarray['frogname']['datatype']);
93         $this->assertEquals('textarea', $namedarray['sometext']['datatype']);
94     }
96     public function test_signup_user() {
97         global $DB;
99         $username = 'pepe';
100         $password = 'abcdefAª.ªª!!3';
101         $firstname = 'Pepe';
102         $lastname = 'Pérez';
103         $email = 'myemail@no.zbc';
104         $city = 'Bcn';
105         $country = 'ES';
106         $customprofilefields = array(
107             array(
108                 'type' => 'text',
109                 'name' => 'profile_field_frogname',
110                 'value' => 'random text',
111             ),
112             array(
113                 'type' => 'textarea',
114                 'name' => 'profile_field_sometext',
115                 'value' => json_encode(
116                     array(
117                         'text' => 'blah blah',
118                         'format' => FORMAT_HTML
119                     )
120                 ),
121             )
122         );
124         // Create new user.
125         $result = auth_email_external::signup_user($username, $password, $firstname, $lastname, $email, $city,  $country,
126                                                     '', '', $customprofilefields);
127         $result = external_api::clean_returnvalue(auth_email_external::signup_user_returns(), $result);
128         $this->assertTrue($result['success']);
129         $this->assertEmpty($result['warnings']);
130         $user = $DB->get_record('user', array('username' => $username));
131         $this->assertEquals($firstname, $user->firstname);
132         $this->assertEquals($lastname, $user->lastname);
133         $this->assertEquals($email, $user->email);
134         $this->assertEquals($city, $user->city);
135         $this->assertEquals($country, $user->country);
136         $this->assertEquals(0, $user->confirmed);
137         $this->assertEquals(current_language(), $user->lang);
138         $this->assertEquals('email', $user->auth);
139         $infofield = $DB->get_record('user_info_data', array('userid' => $user->id, 'fieldid' => $this->field1));
140         $this->assertEquals($customprofilefields[0]['value'], $infofield->data);
141         $infofield = $DB->get_record('user_info_data', array('userid' => $user->id, 'fieldid' => $this->field2));
142         $this->assertEquals(json_decode($customprofilefields[1]['value'])->text, $infofield->data);
144         // Try to create a user with the same username, email and password. We ommit also the profile fields.
145         $password = 'abc';
146         $result = auth_email_external::signup_user($username, $password, $firstname, $lastname, $email, $city,  $country,
147                                                     '', '', $customprofilefields);
148         $result = external_api::clean_returnvalue(auth_email_external::signup_user_returns(), $result);
149         $this->assertFalse($result['success']);
150         $this->assertCount(3, $result['warnings']);
151         $expectederrors = array('username', 'email', 'password');
152         $finalerrors = [];
153         foreach ($result['warnings'] as $warning) {
154             $finalerrors[] = $warning['item'];
155         }
156         $this->assertEquals($expectederrors, $finalerrors);
158         // Do not pass the required profile fields.
159         $this->expectException('invalid_parameter_exception');
160         $result = auth_email_external::signup_user($username, $password, $firstname, $lastname, $email, $city,  $country);
161     }