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