/**
  * Test custom mapping summaries.
  *
  * @dataProvider custommappinganddataprovider
  * @param string $entity  The entity type for import ('user', 'course', 'enrolment')
  * @param array  $mapping The custom field mapping
  * @param array  $data
  */
 public function test_custom_mapping_summaries($entity, $mapping, $data)
 {
     global $DB;
     // Setup the custom mapping values.
     rlipimport_version1elis_save_mapping($entity, array_keys($mapping), $mapping);
     // Setup the class name fo the mock import provider we need.
     $mockprovider = 'rlipimport_version1elis_importprovider_mock' . $entity;
     $provider = new $mockprovider($data);
     $importplugin = new rlip_importplugin_version1elis($provider);
     $importplugin->run();
     // Attempt to handle all of the specific messages in as general a way as possible.
     $requiredfields = array();
     $messageend = 'Please fix the import file and re-upload it.';
     // Testing a custom action mapping value.
     if (isset($mapping['action'])) {
         $message = 'Import file memoryfile was not processed because it is missing the following required column: ' . $mapping['action'] . '.';
     } else {
         if ($entity == 'user' && $data['action'] == 'create') {
             // echo "\n", 'user create', "\n";
             // User create is only a subset of the required fields.
             $message = 'Import file memoryfile was not processed because one of the following columns is required but all are unspecified: ';
             $possiblefields = array('username', 'email', 'idnumber', 'user_username', 'user_email', 'user_idnumber');
             foreach ($possiblefields as $possiblefield) {
                 $requiredfields[] = !empty($mapping[$possiblefield]) ? $mapping[$possiblefield] : $possiblefield;
             }
         } else {
             if ($entity == 'course') {
                 // Course actions will only initially display a message if the context field is missing.
                 $message = 'Import file memoryfile was not processed because it is missing the following required column: ' . $mapping['context'] . '.';
             } else {
                 if ($entity == 'enrolment') {
                     // Enrolment actions will only initially display a message if one of the three required fields is missing.
                     // (at least one of the three is required).
                     $message = 'Import file memoryfile was not processed because one of the following columns is required but all are unspecified: ';
                     $possiblefields = array('user_username', 'user_email', 'user_idnumber');
                     foreach ($possiblefields as $possiblefield) {
                         $requiredfields[] = !empty($mapping[$possiblefield]) ? $mapping[$possiblefield] : $possiblefield;
                     }
                 } else {
                     // Handle generic cases below.
                     if (count($mapping) > 1) {
                         // echo 'Generic: count($mapping) > 1', "\n";
                         $message = 'Import file memoryfile was not processed because one of the following columns is required but all are unspecified: ';
                         $requiredfields = $mappings;
                         // TBD
                     } else {
                         // We're importing data where some fields are requires in an OR condition, so we just need to replace that.
                         // Single field value with it's custom mapped representation.
                         $message = 'Import file memoryfile was not processed because one of the following columns is required but all are unspecified: ';
                         $possiblefields = array('user_username', 'user_email', 'user_idnumber', 'username', 'email', 'idnumber');
                         foreach ($possiblefields as $possiblefield) {
                             $requiredfields[] = !empty($mapping[$possiblefield]) ? $mapping[$possiblefield] : $possiblefield;
                         }
                     }
                 }
             }
         }
     }
     $logs = $DB->get_records(RLIP_LOG_TABLE);
     // var_dump($logs);
     $log = end($logs);
     // echo 'Required: ';
     // var_dump($requiredfields);
     $realmessage = $log->statusmessage;
     $this->assertEquals(0, strpos($realmessage, $message));
     $fieldstart = strlen($message);
     $fieldend = strpos($realmessage, $messageend);
     $this->assertGreaterThanOrEqual($fieldstart, $fieldend);
     $fieldsection = substr($realmessage, $fieldstart - 1, $fieldend - $fieldstart);
     $fieldspresent = explode(', ', trim($fieldsection, ' .'));
     // echo 'Present: ';
     // var_dump($fieldspresent);
     foreach ($requiredfields as $requiredfield) {
         $this->assertTrue(in_array($requiredfield, $fieldspresent));
     }
 }
示例#2
0
echo $OUTPUT->header();
//tabs
$tabs = rlipimport_version1elis_get_tabs($baseurl);
print_tabs(array($tabs), $tab);
//TODO: implement all necessary calls, etc, to make the rest of this code work
//data from db
$mappingdata = rlipimport_version1elis_get_mapping($tab);
//options
$plugin = new rlip_importplugin_version1elis(NULL, false);
$options = $plugin->get_available_fields($tab);
//body
$form = new rlip_importfield_form(null, $options);
//handle data submission
if ($data = $form->get_data()) {
    if (isset($data->submitbutton)) {
        rlipimport_version1elis_save_mapping($tab, $options, $data);
        //notify to the user that settings were saved
        echo $OUTPUT->heading(get_string('mappingssaved', 'dhimport_version1elis'));
    } else {
        if (isset($data->reset)) {
            rlipimport_version1elis_reset_mappings($tab);
            redirect($baseurl . "?tab={$tab}&resetmessage=1", '', 0);
        }
    }
} else {
    if ($form->is_cancelled()) {
        redirect($baseurl . "?tab={$tab}&cancelmessage=1", '', 0);
    }
}
//message handling
$resetmessage = optional_param('resetmessage', 0, PARAM_INT);
 /**
  * Validate restoring default field mappings
  */
 public function testrestoredefaultmappingupdatesrecords()
 {
     global $CFG, $DB;
     $file = get_plugin_directory('dhimport', 'version1elis') . '/lib.php';
     require_once $file;
     // Obtain available fields.
     $plugin = new rlip_importplugin_version1elis(null, false);
     $options = $plugin->get_available_fields('user');
     // Persist default field.
     rlipimport_version1elis_save_mapping('user', $options, array());
     // Setup validation.
     $select = 'standardfieldname = customfieldname';
     $count = $DB->count_records_select(RLIPIMPORT_VERSION1ELIS_MAPPING_TABLE, $select);
     $this->assertEquals($count, count($options));
     // Update all mapping values.
     $data = array();
     foreach ($options as $option) {
         $data[$option] = $option . 'updated';
     }
     // Persist updated values and validate.
     rlipimport_version1elis_save_mapping('user', $options, $data);
     $select = 'standardfieldname != customfieldname';
     $count = $DB->count_records_select(RLIPIMPORT_VERSION1ELIS_MAPPING_TABLE, $select);
     $this->assertEquals($count, count($options));
     // Reset and validate state.
     rlipimport_version1elis_reset_mappings('user');
     $select = 'standardfieldname = customfieldname';
     $count = $DB->count_records_select(RLIPIMPORT_VERSION1ELIS_MAPPING_TABLE, $select);
     $this->assertEquals($count, count($options));
 }