Exemplo n.º 1
0
 /**
  * Validate that custom user fields are synched over to Moodle when PM user is created
  * during an import
  */
 public function test_user_multi_custom_field_on_user_create()
 {
     global $CFG, $DB, $USER;
     require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php';
     require_once elis::lib('data/customfield.class.php');
     require_once elis::file('eliscore/fields/moodleprofile/custom_fields.php');
     require_once elispm::file('accesslib.php');
     require_once elispm::lib('data/user.class.php');
     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';
     $CFG->filterall = true;
     $USER = get_admin();
     $context = context_user::instance($USER->id);
     /*
       $filternames = filter_get_all_installed();
       ob_start();
       var_dump($filternames);
       $tmp = ob_get_contents();
       ob_end_clean();
       error_log("test_user_multi_custom_field_on_user_create: all-filters => {$tmp}");
     */
     // Note: >= m25 filter paths no longer prefixed with 'filter/'
     filter_set_global_state('multilang', TEXTFILTER_ON);
     filter_set_applies_to_strings('multilang', true);
     $multilangoption1 = '<span class="multilang" lang="en">Male</span><span class="multilang" lang="pt_br">Masculino</span>' . '<span class="multilang" lang="es">Masculino</span>';
     $multilangoption2 = '<span class="multilang" lang="en">Female</span><span class="multilang" lang="pt_br">Feminino</span>' . '<span class="multilang" lang="es">Femenino</span>';
     // The associated Moodle user profile field.
     $profiledefinemenu = new profile_define_menu();
     $data = new stdClass();
     $data->datatype = 'menu';
     $data->categoryid = 99999;
     $data->shortname = 'testfieldgender';
     $data->name = 'testfieldgender';
     $data->param1 = "{$multilangoption1}\n{$multilangoption2}";
     $data->defaultdata = $multilangoption2;
     $profiledefinemenu->define_save($data);
     // Reset cached custom fields.
     $user = new user();
     $user->reset_custom_field_list();
     // Field category.
     $fieldcategory = new field_category(array('name' => 'testcategoryname'));
     $fieldcategory->save();
     // Custom field.
     $field = new field(array('categoryid' => $fieldcategory->id, 'shortname' => 'testfieldgender', 'name' => 'testfieldgender', 'datatype' => 'text', 'multivalued' => 1));
     $field->save();
     // Field owners
     field_owner::ensure_field_owner_exists($field, 'moodle_profile');
     $manualowneroptions = array('required' => 0, 'edit_capability' => '', 'view_capability' => '', 'control' => 'menu', 'options' => "{$multilangoption1}\n{$multilangoption2}");
     field_owner::ensure_field_owner_exists($field, 'manual', $manualowneroptions);
     // Field context level assocation.
     $fieldcontextlevel = new field_contextlevel(array('fieldid' => $field->id, 'contextlevel' => CONTEXT_ELIS_USER));
     $fieldcontextlevel->save();
     // Run the user create action.
     $record = new stdClass();
     $record->action = 'create';
     $record->idnumber = 'testuseridnumber';
     $record->username = '******';
     $record->firstname = 'testuserfirstname';
     $record->lastname = 'testuserlastname';
     $record->email = '*****@*****.**';
     $record->address = 'testuseraddress';
     $record->city = 'testusercity';
     $record->country = 'CA';
     $record->language = 'en';
     $record->testfieldgender = 'Male/Female';
     $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis');
     $importplugin->fslogger = new silent_fslogger(null);
     $importplugin->process_record('user', $record, 'bogus');
     // Validation.
     $userid = $DB->get_field(user::TABLE, 'id', array('username' => 'testuserusername'));
     $user = new user($userid);
     $user->load();
     $user = $user->to_object();
     /*
       $datars = field_data::get_for_context_and_field(\local_elisprogram\context\user::instance($user->id), 'testfieldgender');
       foreach ($datars as $data) {
           ob_start();
           var_dump($data);
           $tmp = ob_get_contents();
           ob_end_clean();
           error_log("test_user_multi_custom_field_on_user_create: data => {$tmp}");
       }
     */
     /*
       ob_start();
       var_dump($user);
       $tmp = ob_get_contents();
       ob_end_clean();
       error_log("test_user_multi_custom_field_on_user_create: user => {$tmp}");
     */
     $this->assertEquals(array($multilangoption1, $multilangoption2), $user->field_testfieldgender);
 }
    /**
     * 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');
    }