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