/**
  * Validates a duplicate role assignment failure message
  */
 public function test_version1importlogsduplicateroleassignmentfailuremessage()
 {
     global $DB;
     $sitename = $this->getsitename();
     // Create guest user.
     $guestuser = get_test_user('guest');
     set_config('siteguest', $guestuser->id);
     // Create admin user.
     $adminuser = get_test_user('admin');
     set_config('siteadmins', $adminuser->id);
     $userid = $this->create_test_user();
     $context = context_system::instance();
     $roleid = $this->create_test_role();
     // Base data used every time.
     $basedata = array('action' => 'create', 'context' => 'system', 'instance' => $sitename, 'role' => 'rlipshortname');
     // Username.
     role_assign($roleid, $userid, $context->id);
     $data = $basedata;
     $data['username'] = '******';
     $expectedmessage = "[enrolment.csv line 2] User with username \"rlipusername\" could not be assigned role with ";
     $expectedmessage .= "shortname \"rlipshortname\" on the system context. User with username \"rlipusername\" is already ";
     $expectedmessage .= "assigned role with shortname \"rlipshortname\" on the system context.\n";
     $this->assert_data_produces_error($data, $expectedmessage, 'enrolment');
     // Email.
     role_assign($roleid, $userid, $context->id);
     $data = $basedata;
     $data['email'] = '*****@*****.**';
     $expectedmessage = "[enrolment.csv line 2] User with email \"rlipuser@rlipdomain.com\" could not be assigned role with ";
     $expectedmessage .= "shortname \"rlipshortname\" on the system context. User with email \"rlipuser@rlipdomain.com\" is";
     $expectedmessage .= " already assigned role with shortname \"rlipshortname\" on the system context.\n";
     $this->assert_data_produces_error($data, $expectedmessage, 'enrolment');
     // Idnumber.
     role_assign($roleid, $userid, $context->id);
     $data = $basedata;
     $data['idnumber'] = 'rlipidnumber';
     $expectedmessage = "[enrolment.csv line 2] User with idnumber \"rlipidnumber\" could not be assigned role with shortname";
     $expectedmessage .= " \"rlipshortname\" on the system context. User with idnumber \"rlipidnumber\" is already assigned ";
     $expectedmessage .= "role with shortname \"rlipshortname\" on the system context.\n";
     $this->assert_data_produces_error($data, $expectedmessage, 'enrolment');
     // Username, email.
     role_assign($roleid, $userid, $context->id);
     $data = $basedata;
     $data['username'] = '******';
     $data['email'] = '*****@*****.**';
     $expectedmessage = "[enrolment.csv line 2] User with username \"rlipusername\", email \"rlipuser@rlipdomain.com\" could";
     $expectedmessage .= " not be assigned role with shortname \"rlipshortname\" on the system context. User with username ";
     $expectedmessage .= "\"rlipusername\", email \"rlipuser@rlipdomain.com\" is already assigned role with shortname";
     $expectedmessage .= " \"rlipshortname\" on the system context.\n";
     $this->assert_data_produces_error($data, $expectedmessage, 'enrolment');
     // Username, idnumber.
     role_assign($roleid, $userid, $context->id);
     $data = $basedata;
     $data['username'] = '******';
     $data['idnumber'] = 'rlipidnumber';
     $expectedmessage = "[enrolment.csv line 2] User with username \"rlipusername\", idnumber \"rlipidnumber\" could not be";
     $expectedmessage .= " assigned role with shortname \"rlipshortname\" on the system context. User with username ";
     $expectedmessage .= "\"rlipusername\", idnumber \"rlipidnumber\" is already assigned role with shortname \"rlipshortname\"";
     $expectedmessage .= " on the system context.\n";
     $this->assert_data_produces_error($data, $expectedmessage, 'enrolment');
     // Email, idnumber.
     role_assign($roleid, $userid, $context->id);
     $data = $basedata;
     $data['email'] = '*****@*****.**';
     $data['idnumber'] = 'rlipidnumber';
     $expectedmessage = "[enrolment.csv line 2] User with email \"rlipuser@rlipdomain.com\", idnumber \"rlipidnumber\"";
     $expectedmessage .= " could not be assigned role with shortname \"rlipshortname\" on the system context. User with";
     $expectedmessage .= " email \"rlipuser@rlipdomain.com\", idnumber \"rlipidnumber\" is already assigned role with";
     $expectedmessage .= " shortname \"rlipshortname\" on the system context.\n";
     $this->assert_data_produces_error($data, $expectedmessage, 'enrolment');
     // Username, email, idnumber.
     role_assign($roleid, $userid, $context->id);
     $data = $basedata;
     $data['username'] = '******';
     $data['email'] = '*****@*****.**';
     $data['idnumber'] = 'rlipidnumber';
     $expectedmessage = "[enrolment.csv line 2] User with username \"rlipusername\", email \"rlipuser@rlipdomain.com\",";
     $expectedmessage .= " idnumber \"rlipidnumber\" could not be assigned role with shortname \"rlipshortname\" on the";
     $expectedmessage .= " system context. User with username \"rlipusername\", email \"rlipuser@rlipdomain.com\",";
     $expectedmessage .= " idnumber \"rlipidnumber\" is already assigned role with shortname \"rlipshortname\" on the";
     $expectedmessage .= " system context.\n";
     $this->assert_data_produces_error($data, $expectedmessage, 'enrolment');
 }
    /**
     * Validate new library method fix_moodle_profile_fields()
     */
    public function test_fix_moodle_profile_fields()
    {
        global $CFG;
        require_once $CFG->dirroot . '/user/profile/lib.php';
        require_once $CFG->dirroot . '/user/profile/definelib.php';
        require_once $CFG->dirroot . '/user/profile/field/menu/define.class.php';
        require_once $CFG->dirroot . '/user/profile/field/menu/field.class.php';
        require_once $CFG->dirroot . '/user/profile/field/checkbox/define.class.php';
        require_once $CFG->dirroot . '/user/profile/field/checkbox/field.class.php';
        require_once elis::lib('lib.php');
        require_once elis::lib('testlib.php');
        // Create a couple Moodle profile fields, one menu-of-choices
        $profiledefinemenu = new profile_define_menu();
        $data = new stdClass();
        $data->datatype = 'menu';
        $data->categoryid = 99999;
        $data->shortname = 'testfieldmenu';
        $data->name = 'testfieldmenu';
        $data->param1 = 'Option1
Option2';
        $data->defaultdata = 'Option2';
        $profiledefinemenu->define_save($data);
        $profiledefinecheckbox = new profile_define_checkbox();
        $data = new stdClass();
        $data->datatype = 'checkbox';
        $data->categoryid = 99999;
        $data->shortname = 'testfieldcheckbox';
        $data->name = 'testfieldcheckbox';
        $profiledefinecheckbox->define_save($data);
        $testuser = get_test_user();
        $testuser->profile_field_testfieldmenu = 'Option3';
        // illegal value
        $testuser->profile_field_testfieldcheckbox = 0;
        fix_moodle_profile_fields($testuser);
        $this->assertTrue(!$testuser->profile_field_testfieldcheckbox);
        $this->assertTrue(!isset($testuser->profile_field_testfieldmenu));
        $testuser->profile_field_testfieldmenu = 'Option1';
        // legal value
        $testuser->profile_field_testfieldcheckbox = 1;
        fix_moodle_profile_fields($testuser);
        $this->assertTrue($testuser->profile_field_testfieldcheckbox == 1);
        $this->assertTrue($testuser->profile_field_testfieldmenu == 'Option1');
    }