forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDL-56092 auth_email: New WS auth_email_signup_user
- Loading branch information
Showing
4 changed files
with
256 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,9 +50,12 @@ public function setUp() { | |
$CFG->registerauth = 'email'; | ||
|
||
$categoryid = $DB->insert_record('user_info_category', array('name' => 'Cat 1', 'sortorder' => 1)); | ||
$field = $DB->insert_record('user_info_field', array( | ||
$this->field1 = $DB->insert_record('user_info_field', array( | ||
'shortname' => 'frogname', 'name' => 'Name of frog', 'categoryid' => $categoryid, | ||
'datatype' => 'text', 'signup' => 1, 'visible' => 1)); | ||
'datatype' => 'text', 'signup' => 1, 'visible' => 1, 'required' => 1)); | ||
$this->field2 = $DB->insert_record('user_info_field', array( | ||
'shortname' => 'sometext', 'name' => 'Some text in textarea', 'categoryid' => $categoryid, | ||
'datatype' => 'textarea', 'signup' => 1, 'visible' => 1, 'required' => 1)); | ||
} | ||
|
||
public function test_get_signup_settings() { | ||
|
@@ -73,8 +76,75 @@ public function test_get_signup_settings() { | |
$this->assertEquals(print_password_policy(), $result['passwordpolicy']); | ||
$this->assertNotContains('recaptchachallengehash', $result); | ||
$this->assertNotContains('recaptchachallengeimage', $result); | ||
$this->assertCount(1, $result['profilefields']); | ||
$this->assertCount(2, $result['profilefields']); | ||
$this->assertEquals('text', $result['profilefields'][0]['datatype']); | ||
$this->assertEquals('textarea', $result['profilefields'][1]['datatype']); | ||
} | ||
|
||
public function test_signup_user() { | ||
global $DB; | ||
|
||
$username = 'pepe'; | ||
$password = 'abcdefAª.ªª!!3'; | ||
$firstname = 'Pepe'; | ||
$lastname = 'Pérez'; | ||
$email = '[email protected]'; | ||
$city = 'Bcn'; | ||
$country = 'ES'; | ||
$customprofilefields = array( | ||
array( | ||
'type' => 'text', | ||
'name' => 'profile_field_frogname', | ||
'value' => 'random text', | ||
), | ||
array( | ||
'type' => 'textarea', | ||
'name' => 'profile_field_sometext', | ||
'value' => json_encode( | ||
array( | ||
'text' => 'blah blah', | ||
'format' => FORMAT_HTML | ||
) | ||
), | ||
) | ||
); | ||
|
||
// Create new user. | ||
$result = auth_email_external::signup_user($username, $password, $firstname, $lastname, $email, $city, $country, | ||
'', '', $customprofilefields); | ||
$result = external_api::clean_returnvalue(auth_email_external::signup_user_returns(), $result); | ||
$this->assertTrue($result['success']); | ||
$this->assertEmpty($result['warnings']); | ||
$user = $DB->get_record('user', array('username' => $username)); | ||
$this->assertEquals($firstname, $user->firstname); | ||
$this->assertEquals($lastname, $user->lastname); | ||
$this->assertEquals($email, $user->email); | ||
$this->assertEquals($city, $user->city); | ||
$this->assertEquals($country, $user->country); | ||
$this->assertEquals(0, $user->confirmed); | ||
$this->assertEquals(current_language(), $user->lang); | ||
$this->assertEquals('email', $user->auth); | ||
$infofield = $DB->get_record('user_info_data', array('userid' => $user->id, 'fieldid' => $this->field1)); | ||
$this->assertEquals($customprofilefields[0]['value'], $infofield->data); | ||
$infofield = $DB->get_record('user_info_data', array('userid' => $user->id, 'fieldid' => $this->field2)); | ||
$this->assertEquals(json_decode($customprofilefields[1]['value'])->text, $infofield->data); | ||
|
||
// Try to create a user with the same username, email and password. We ommit also the profile fields. | ||
$password = 'abc'; | ||
$result = auth_email_external::signup_user($username, $password, $firstname, $lastname, $email, $city, $country, | ||
'', '', $customprofilefields); | ||
$result = external_api::clean_returnvalue(auth_email_external::signup_user_returns(), $result); | ||
$this->assertFalse($result['success']); | ||
$this->assertCount(3, $result['warnings']); | ||
$expectederrors = array('username', 'email', 'password'); | ||
$finalerrors = []; | ||
foreach ($result['warnings'] as $warning) { | ||
$finalerrors[] = $warning['item']; | ||
} | ||
$this->assertEquals($expectederrors, $finalerrors); | ||
|
||
// Do not pass the required profile fields. | ||
$this->expectException('invalid_parameter_exception'); | ||
$result = auth_email_external::signup_user($username, $password, $firstname, $lastname, $email, $city, $country); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters