Differences Between: [Versions 39 and 310]
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 defined('MOODLE_INTERNAL') || die(); 28 29 global $CFG; 30 31 require_once($CFG->dirroot . '/webservice/tests/helpers.php'); 32 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 { 42 43 /** 44 * Set up for every test 45 */ 46 public function setUp(): void { 47 global $CFG, $DB; 48 49 $this->resetAfterTest(true); 50 $CFG->registerauth = 'email'; 51 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 } 60 61 public function test_get_signup_settings() { 62 global $CFG; 63 64 $CFG->defaultcity = 'Bcn'; 65 $CFG->country = 'ES'; 66 $CFG->sitepolicy = 'https://moodle.org'; 67 68 $result = auth_email_external::get_signup_settings(); 69 $result = external_api::clean_returnvalue(auth_email_external::get_signup_settings_returns(), $result); 70 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 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 } 87 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); 91 92 $this->assertEquals('text', $namedarray['frogname']['datatype']); 93 $this->assertEquals('textarea', $namedarray['sometext']['datatype']); 94 } 95 96 /** 97 * Test get_signup_settings with mathjax in a profile field. 98 */ 99 public function test_get_signup_settings_with_mathjax_in_profile_fields() { 100 global $CFG, $DB; 101 102 require_once($CFG->dirroot . '/lib/externallib.php'); 103 104 // Enable MathJax filter in content and headings. 105 $this->configure_filters([ 106 ['name' => 'mathjaxloader', 'state' => TEXTFILTER_ON, 'move' => -1, 'applytostrings' => true], 107 ]); 108 109 // Create category with MathJax and a new field with MathJax. 110 $categoryname = 'Cat $$(a+b)=2$$'; 111 $fieldname = 'Some text $$(a+b)=2$$'; 112 $categoryid = $DB->insert_record('user_info_category', array('name' => $categoryname, 'sortorder' => 1)); 113 $field3 = $DB->insert_record('user_info_field', array( 114 'shortname' => 'mathjaxname', 'name' => $fieldname, 'categoryid' => $categoryid, 115 'datatype' => 'textarea', 'signup' => 1, 'visible' => 1, 'required' => 1, 'sortorder' => 2)); 116 117 $result = auth_email_external::get_signup_settings(); 118 $result = external_api::clean_returnvalue(auth_email_external::get_signup_settings_returns(), $result); 119 120 // Format the original data. 121 $sitecontext = context_system::instance(); 122 $categoryname = external_format_string($categoryname, $sitecontext->id); 123 $fieldname = external_format_string($fieldname, $sitecontext->id); 124 125 // Whip up a array with named entries to easily check against. 126 $namedarray = array(); 127 foreach ($result['profilefields'] as $key => $value) { 128 $namedarray[$value['shortname']] = $value; 129 } 130 131 // Check the new profile field. 132 $this->assertArrayHasKey('mathjaxname', $namedarray); 133 $this->assertStringContainsString('<span class="filter_mathjaxloader_equation">', 134 $namedarray['mathjaxname']['categoryname']); 135 $this->assertStringContainsString('<span class="filter_mathjaxloader_equation">', 136 $namedarray['mathjaxname']['name']); 137 $this->assertEquals($categoryname, $namedarray['mathjaxname']['categoryname']); 138 $this->assertEquals($fieldname, $namedarray['mathjaxname']['name']); 139 } 140 141 public function test_signup_user() { 142 global $DB; 143 144 $username = 'pepe'; 145 $password = 'abcdefAª.ªª!!3'; 146 $firstname = 'Pepe'; 147 $lastname = 'Pérez'; 148 $email = 'myemail@no.zbc'; 149 $city = 'Bcn'; 150 $country = 'ES'; 151 $customprofilefields = array( 152 array( 153 'type' => 'text', 154 'name' => 'profile_field_frogname', 155 'value' => 'random text', 156 ), 157 array( 158 'type' => 'textarea', 159 'name' => 'profile_field_sometext', 160 'value' => json_encode( 161 array( 162 'text' => 'blah blah', 163 'format' => FORMAT_HTML 164 ) 165 ), 166 ) 167 ); 168 169 // Create new user. 170 $result = auth_email_external::signup_user($username, $password, $firstname, $lastname, $email, $city, $country, 171 '', '', $customprofilefields); 172 $result = external_api::clean_returnvalue(auth_email_external::signup_user_returns(), $result); 173 $this->assertTrue($result['success']); 174 $this->assertEmpty($result['warnings']); 175 $user = $DB->get_record('user', array('username' => $username)); 176 $this->assertEquals($firstname, $user->firstname); 177 $this->assertEquals($lastname, $user->lastname); 178 $this->assertEquals($email, $user->email); 179 $this->assertEquals($city, $user->city); 180 $this->assertEquals($country, $user->country); 181 $this->assertEquals(0, $user->confirmed); 182 $this->assertEquals(current_language(), $user->lang); 183 $this->assertEquals('email', $user->auth); 184 $infofield = $DB->get_record('user_info_data', array('userid' => $user->id, 'fieldid' => $this->field1)); 185 $this->assertEquals($customprofilefields[0]['value'], $infofield->data); 186 $infofield = $DB->get_record('user_info_data', array('userid' => $user->id, 'fieldid' => $this->field2)); 187 $this->assertEquals(json_decode($customprofilefields[1]['value'])->text, $infofield->data); 188 189 // Try to create a user with the same username, email and password. We ommit also the profile fields. 190 $password = 'abc'; 191 $result = auth_email_external::signup_user($username, $password, $firstname, $lastname, $email, $city, $country, 192 '', '', $customprofilefields); 193 $result = external_api::clean_returnvalue(auth_email_external::signup_user_returns(), $result); 194 $this->assertFalse($result['success']); 195 $this->assertCount(3, $result['warnings']); 196 $expectederrors = array('username', 'email', 'password'); 197 $finalerrors = []; 198 foreach ($result['warnings'] as $warning) { 199 $finalerrors[] = $warning['item']; 200 } 201 $this->assertEquals($expectederrors, $finalerrors); 202 203 // Do not pass the required profile fields. 204 $this->expectException('invalid_parameter_exception'); 205 $result = auth_email_external::signup_user($username, $password, $firstname, $lastname, $email, $city, $country); 206 } 207 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body