Skip to content

Commit

Permalink
Merge branch 'MDL-68349-39' of git://github.com/marinaglancy/moodle i…
Browse files Browse the repository at this point in the history
…nto MOODLE_39_STABLE
  • Loading branch information
junpataleta committed Nov 10, 2020
2 parents fedd703 + ec19837 commit 8e2e459
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
4 changes: 3 additions & 1 deletion auth/db/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ function sync_users(progress_trace $trace, $do_updates=false) {
continue;
}
try {
$id = user_create_user($user, false); // It is truly a new user.
$id = user_create_user($user, false, false); // It is truly a new user.
$trace->output(get_string('auth_dbinsertuser', 'auth_db', array('name'=>$user->username, 'id'=>$id)), 1);
} catch (moodle_exception $e) {
$trace->output(get_string('auth_dbinsertusererror', 'auth_db', $user->username), 1);
Expand All @@ -481,6 +481,8 @@ function sync_users(progress_trace $trace, $do_updates=false) {

// Make sure user context is present.
context_user::instance($id);

\core\event\user_created::create_from_userid($id)->trigger();
}
unset($add_users);
}
Expand Down
29 changes: 28 additions & 1 deletion auth/db/tests/db_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ protected function init_auth_database() {
$table->add_field('email', XMLDB_TYPE_CHAR, '255', null, null, null);
$table->add_field('firstname', XMLDB_TYPE_CHAR, '255', null, null, null);
$table->add_field('lastname', XMLDB_TYPE_CHAR, '255', null, null, null);
$table->add_field('animal', XMLDB_TYPE_CHAR, '255', null, null, null);
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
if ($dbman->table_exists($table)) {
$dbman->drop_table($table);
Expand All @@ -137,6 +138,15 @@ protected function init_auth_database() {
set_config('field_updateremote_email', '0', 'auth_db');
set_config('field_lock_email', 'unlocked', 'auth_db');

// Create a user profile field and add mapping to it.
$DB->insert_record('user_info_field', ['shortname' => 'pet', 'name' => 'Pet', 'required' => 0,
'visible' => 1, 'locked' => 0, 'categoryid' => 1, 'datatype' => 'text']);

set_config('field_map_profile_field_pet', 'animal', 'auth_db');
set_config('field_updatelocal_profile_field_pet', 'oncreate', 'auth_db');
set_config('field_updateremote_profile_field_pet', '0', 'auth_db');
set_config('field_lock_profile_field_pet', 'unlocked', 'auth_db');

// Init the rest of settings.
set_config('passtype', 'plaintext', 'auth_db');
set_config('changepasswordurl', '', 'auth_db');
Expand All @@ -156,6 +166,7 @@ protected function cleanup_auth_database() {

public function test_plugin() {
global $DB, $CFG;
require_once($CFG->dirroot . '/user/profile/lib.php');

$this->resetAfterTest(true);

Expand Down Expand Up @@ -193,7 +204,7 @@ public function test_plugin() {

// Test bulk user account creation.

$user2 = (object)array('name'=>'u2', 'pass'=>'heslo', 'email'=>'u2@example.com');
$user2 = (object)['name' => 'u2', 'pass' => 'heslo', 'email' => 'u2@example.com', 'animal' => 'cat'];
$user2->id = $DB->insert_record('auth_db_users', $user2);

$user3 = (object)array('name'=>'admin', 'pass'=>'heslo', 'email'=>'admin@example.com'); // Should be skipped.
Expand All @@ -202,13 +213,24 @@ public function test_plugin() {
$this->assertCount(2, $DB->get_records('user'));

$trace = new null_progress_trace();

// Sync users and make sure that two events user_created werer triggered.
$sink = $this->redirectEvents();
$auth->sync_users($trace, false);
$events = $sink->get_events();
$sink->close();
$this->assertCount(2, $events);
$this->assertTrue($events[0] instanceof \core\event\user_created);
$this->assertTrue($events[1] instanceof \core\event\user_created);

// Assert the two users were created.
$this->assertEquals(4, $DB->count_records('user'));
$u1 = $DB->get_record('user', array('username'=>$user1->name, 'auth'=>'db'));
$this->assertSame($user1->email, $u1->email);
$this->assertEmpty(profile_user_record($u1->id)->pet);
$u2 = $DB->get_record('user', array('username'=>$user2->name, 'auth'=>'db'));
$this->assertSame($user2->email, $u2->email);
$this->assertSame($user2->animal, profile_user_record($u2->id)->pet);
$admin = $DB->get_record('user', array('username'=>'admin', 'auth'=>'manual'));
$this->assertNotEmpty($admin);

Expand All @@ -217,12 +239,14 @@ public function test_plugin() {

$user2b = clone($user2);
$user2b->email = 'u2b@example.com';
$user2b->animal = 'dog';
$DB->update_record('auth_db_users', $user2b);

$auth->sync_users($trace, false);
$this->assertEquals(4, $DB->count_records('user'));
$u2 = $DB->get_record('user', array('username'=>$user2->name));
$this->assertSame($user2->email, $u2->email);
$this->assertSame($user2->animal, profile_user_record($u2->id)->pet);

$auth->sync_users($trace, true);
$this->assertEquals(4, $DB->count_records('user'));
Expand All @@ -231,6 +255,8 @@ public function test_plugin() {

set_config('field_updatelocal_email', 'onlogin', 'auth_db');
$auth->config->field_updatelocal_email = 'onlogin';
set_config('field_updatelocal_profile_field_pet', 'onlogin', 'auth_db');
$auth->config->field_updatelocal_profile_field_pet = 'onlogin';

$auth->sync_users($trace, false);
$this->assertEquals(4, $DB->count_records('user'));
Expand All @@ -241,6 +267,7 @@ public function test_plugin() {
$this->assertEquals(4, $DB->count_records('user'));
$u2 = $DB->get_record('user', array('username'=>$user2->name));
$this->assertSame($user2b->email, $u2->email);
$this->assertSame($user2b->animal, profile_user_record($u2->id)->pet);


// Test sync deletes and suspends.
Expand Down

0 comments on commit 8e2e459

Please sign in to comment.