示例#1
0
 /**
  * Test confirm_user
  */
 public function test_confirm_user()
 {
     global $DB;
     $username = '******';
     $password = '******';
     $firstname = 'Pepe';
     $lastname = 'Pérez';
     $email = '*****@*****.**';
     // Create new user.
     $result = auth_email_external::signup_user($username, $password, $firstname, $lastname, $email);
     $result = external_api::clean_returnvalue(auth_email_external::signup_user_returns(), $result);
     $this->assertTrue($result['success']);
     $this->assertEmpty($result['warnings']);
     $secret = $DB->get_field('user', 'secret', array('username' => $username));
     // Confirm the user.
     $result = core_auth_external::confirm_user($username, $secret);
     $result = external_api::clean_returnvalue(core_auth_external::confirm_user_returns(), $result);
     $this->assertTrue($result['success']);
     $this->assertEmpty($result['warnings']);
     $confirmed = $DB->get_field('user', 'confirmed', array('username' => $username));
     $this->assertEquals(1, $confirmed);
     // Try to confirm the user again.
     $result = core_auth_external::confirm_user($username, $secret);
     $result = external_api::clean_returnvalue(core_auth_external::confirm_user_returns(), $result);
     $this->assertFalse($result['success']);
     $this->assertCount(1, $result['warnings']);
     $this->assertEquals('alreadyconfirmed', $result['warnings'][0]['warningcode']);
     // Try to use an invalid secret.
     $this->expectException('moodle_exception');
     $this->expectExceptionMessage(get_string('invalidconfirmdata', 'error'));
     $result = core_auth_external::confirm_user($username, 'zzZZzz');
 }
示例#2
0
 public function test_signup_user()
 {
     global $DB;
     $username = '******';
     $password = '******';
     $firstname = 'Pepe';
     $lastname = 'Pérez';
     $email = '*****@*****.**';
     $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 = '******';
     $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);
 }