From b1cabb40eb672e57024b8086bf222c805384f96f Mon Sep 17 00:00:00 2001 From: Aaron C Spike Date: Wed, 18 May 2011 14:07:11 -0500 Subject: [PATCH 1/5] MDL-28705 enrol_imsenterprise Adjust mapping of names for ims groups --- enrol/imsenterprise/lib.php | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/enrol/imsenterprise/lib.php b/enrol/imsenterprise/lib.php index 9d11e00be2b..68fc4767b07 100644 --- a/enrol/imsenterprise/lib.php +++ b/enrol/imsenterprise/lib.php @@ -315,15 +315,17 @@ function process_group_tag($tagcontents) { if (preg_match('{.*?(.+?).*?}is', $tagcontents, $matches)) { $group->coursecode = trim($matches[1]); } - if (preg_match('{.*?(.*?).*?}is', $tagcontents, $matches)){ - $group->description = trim($matches[1]); + + if (preg_match('{.*?(.*?).*?}is', $tagcontents, $matches)) { + $group->longname = trim($matches[1]); } if (preg_match('{.*?(.*?).*?}is', $tagcontents, $matches)) { - $group->shortName = trim($matches[1]); + $group->shortname = trim($matches[1]); } if (preg_match('{.*?(.*?).*?}is', $tagcontents, $matches)) { - $group->fulldescription = trim($matches[1]); + $group->full = trim($matches[1]); } + if (preg_match('{.*?(.*?).*?}is', $tagcontents, $matches)) { $group->category = trim($matches[1]); } @@ -362,26 +364,26 @@ function process_group_tag($tagcontents) { $this->log_line("Course $coursecode not found in Moodle's course idnumbers."); } else { // Set shortname to description or description to shortname if one is set but not the other. - $nodescription = !isset($group->description); - $noshortname = !isset($group->shortName); + $nodescription = !isset($group->longname); + $noshortname = !isset($group->shortname); if ( $nodescription && $noshortname) { // If neither short nor long description are set let if fail $this->log_line("Neither long nor short name are set for $coursecode"); } else if ($nodescription) { // If short and ID exist, then give the long short's value, then give short the ID's value - $group->description = $group->shortName; - $group->shortName = $coursecode; + $group->longname = $group->shortname; + $group->shortname = $coursecode; } else if ($noshortname) { // If long and ID exist, then map long to long, then give short the ID's value. - $group->shortName = $coursecode; + $group->shortname = $coursecode; } // Create the (hidden) course(s) if not found $courseconfig = get_config('moodlecourse'); // Load Moodle Course shell defaults $course = new stdClass(); - $course->fullname = $group->description; - $course->shortname = $group->shortName; - if (!empty($group->fulldescription)) { - $course->summary = format_text($group->fulldescription, FORMAT_HTML); + $course->fullname = $group->longname; + $course->shortname = $group->shortname; + if (!empty($group->full)) { + $course->summary = format_text($group->full, FORMAT_HTML); } $course->idnumber = $coursecode; $course->format = $courseconfig->format; From a65c11b0cae537a5dc44dd3f5e0714cdd8ec4571 Mon Sep 17 00:00:00 2001 From: Aaron C Spike Date: Wed, 10 Aug 2011 14:21:33 -0500 Subject: [PATCH 2/5] MDL-28705 enrol_imsenterprise Make name mapping configurable --- enrol/imsenterprise/lib.php | 28 ++++++++++++++++++--- enrol/imsenterprise/locallib.php | 43 ++++++++++++++++++++++++++++++++ enrol/imsenterprise/settings.php | 10 ++++++++ 3 files changed, 77 insertions(+), 4 deletions(-) diff --git a/enrol/imsenterprise/lib.php b/enrol/imsenterprise/lib.php index 68fc4767b07..eef9380fcfa 100644 --- a/enrol/imsenterprise/lib.php +++ b/enrol/imsenterprise/lib.php @@ -97,6 +97,8 @@ function cron() { // Make sure we understand how to map the IMS-E roles to Moodle roles $this->load_role_mappings(); + // Make sure we understand how to map the IMS-E course names to Moodle course names + $this->load_course_mappings(); $md5 = md5_file($filename); // NB We'll write this value back to the database at the end of the cron $filemtime = filemtime($filename); @@ -377,13 +379,13 @@ function process_group_tag($tagcontents) { // If long and ID exist, then map long to long, then give short the ID's value. $group->shortname = $coursecode; } + $group->full = format_text($group->full, FORMAT_HTML); + // Create the (hidden) course(s) if not found $courseconfig = get_config('moodlecourse'); // Load Moodle Course shell defaults $course = new stdClass(); - $course->fullname = $group->longname; - $course->shortname = $group->shortname; - if (!empty($group->full)) { - $course->summary = format_text($group->full, FORMAT_HTML); + foreach ($this->coursemappings as $coursename => $imsname) { + $course->$coursename = $group->$imsname; } $course->idnumber = $coursecode; $course->format = $courseconfig->format; @@ -791,6 +793,23 @@ function load_role_mappings() { } } + /** + * Load the name mappings (from the config), so we can easily refer to + * how an IMS-E course properties corresponds to a Moodle course properties + */ + function load_course_mappings() { + require_once('locallib.php'); + + $imsnames = new imsenterprise_names(); + $coursenames = $imsnames->get_coursenames(); + $imsnames = $imsnames->get_imsnames(); + + $this->coursemappings = array(); + foreach($coursenames as $coursename) { + $this->coursemappings[$coursename] = $this->get_config('imsrolemap' . $coursename); + } + } + /** * Called whenever anybody tries (from the normal interface) to remove a group * member which is registered as being created by this component. (Not called @@ -804,6 +823,7 @@ function load_role_mappings() { return false; } + } // end of class diff --git a/enrol/imsenterprise/locallib.php b/enrol/imsenterprise/locallib.php index c85f71b68ac..8b21ec10b49 100644 --- a/enrol/imsenterprise/locallib.php +++ b/enrol/imsenterprise/locallib.php @@ -82,3 +82,46 @@ class imsenterprise_roles { } // class + +class imsenterprise_names { + private $imsnames; + private $coursenames; + + function __construct() { + $this->imsnames = array( + 'long'=>'long', + 'short'=>'short', + 'full'=>'full', + 'coursecode'=>'coursecode', + ); + $this->coursenames = array('fullname', 'shortname', 'summary'); + } + + function get_imsnames() { + return $this->imsnames; + } + + function get_coursenames() { + return $this->coursenames; + } + + /** + * This function is only used when first setting up the plugin, to + * decide which name assignments to recommend by default. + */ + function determine_default_namemapping($coursename) { + switch($coursename) { + case 'fullname': + $imsname = 'short'; + break; + case 'shortname': + $imsname = 'coursecode'; + break; + default: + return 'ignore'; // Zero for no match + } + return $imsname; + } + + +} // class diff --git a/enrol/imsenterprise/settings.php b/enrol/imsenterprise/settings.php index 1c7f205308e..4262b1a78bd 100644 --- a/enrol/imsenterprise/settings.php +++ b/enrol/imsenterprise/settings.php @@ -75,6 +75,16 @@ if ($ADMIN->fulltree) { $settings->add(new admin_setting_configcheckbox('enrol_imsenterprise/createnewcategories', get_string('createnewcategories', 'enrol_imsenterprise'), get_string('createnewcategories_desc', 'enrol_imsenterprise'), 0)); $settings->add(new admin_setting_configcheckbox('enrol_imsenterprise/imsunenrol', get_string('allowunenrol', 'enrol_imsenterprise'), get_string('allowunenrol_desc', 'enrol_imsenterprise'), 0)); + + $settings->add(new admin_setting_heading('enrol_imsenterprise_coursesettings_names', get_string('names', 'enrol_imsenterprise'), get_string('imsnamesdescription', 'enrol_imsenterprise'))); + + if (!during_initial_install()) { + $imsnames = new imsenterprise_names(); + $assignablenames = array('ignore' => get_string('ignore', 'enrol_imsenterprise')) + $imsnames->get_imsnames(); + foreach ($imsnames->get_coursenames() as $coursename) { + $settings->add(new admin_setting_configselect('enrol_imsenterprise/imsnamemap'.$coursename, get_string($coursename, 'enrol_imsenterprise'), '', (string)$imsnames->determine_default_namemapping($coursename), $assignablenames)); + } + } //--- miscellaneous ------------------------------------------------------------------------------------- $settings->add(new admin_setting_heading('enrol_imsenterprise_miscsettings', get_string('miscsettings', 'enrol_imsenterprise'), '')); From 66dad719900726604b64bd5403d4148a272614f4 Mon Sep 17 00:00:00 2001 From: David Monllao Date: Wed, 22 Aug 2012 17:40:31 +0800 Subject: [PATCH 3/5] MDL-28705 enrol_imsenterprise Refining provided patch --- .../lang/en/enrol_imsenterprise.php | 11 ++- enrol/imsenterprise/lib.php | 60 ++++++++------- enrol/imsenterprise/locallib.php | 76 +++++++++++++------ enrol/imsenterprise/settings.php | 15 ++-- 4 files changed, 103 insertions(+), 59 deletions(-) diff --git a/enrol/imsenterprise/lang/en/enrol_imsenterprise.php b/enrol/imsenterprise/lang/en/enrol_imsenterprise.php index 8b916829a1d..511ac821fce 100644 --- a/enrol/imsenterprise/lang/en/enrol_imsenterprise.php +++ b/enrol/imsenterprise/lang/en/enrol_imsenterprise.php @@ -42,9 +42,8 @@ Users are searched for first by their "idnumber", and second by their Moodle use $string['cronfrequency'] = 'Frequency of processing'; $string['deleteusers'] = 'Delete user accounts when specified in IMS data'; $string['deleteusers_desc'] = 'If enabled, IMS Enterprise enrolment data can specify the deletion of user accounts (if the "recstatus" flag is set to 3, which represents deletion of an account). As is standard in Moodle, the user record isn\'t actually deleted from Moodle\'s database, but a flag is set to mark the account as deleted.'; -$string['pluginname_desc'] = 'This method will repeatedly check for and process a specially-formatted text file in the location that you specify. The file must follow the IMS Enterprise specifications containing person, group, and membership XML elements.'; $string['doitnow'] = 'perform an IMS Enterprise import right now'; -$string['pluginname'] = 'IMS Enterprise file'; +$string['emptyattribute'] = 'Leave it empty'; $string['filelockedmail'] = 'The text file you are using for IMS-file-based enrolments ({$a}) can not be deleted by the cron process. This usually means the permissions are wrong on it. Please fix the permissions so that Moodle can delete the file, otherwise it might be processed repeatedly.'; $string['filelockedmailsubject'] = 'Important error: Enrolment file'; $string['fixcasepersonalnames'] = 'Change personal names to Title Case'; @@ -56,12 +55,20 @@ $string['mailadmins'] = 'Notify admin by email'; $string['mailusers'] = 'Notify users by email'; $string['messageprovider:imsenterprise_enrolment'] = 'IMS Enterprise enrolment messages'; $string['miscsettings'] = 'Miscellaneous'; +$string['pluginname'] = 'IMS Enterprise file'; +$string['pluginname_desc'] = 'This method will repeatedly check for and process a specially-formatted text file in the location that you specify. The file must follow the IMS Enterprise specifications containing person, group, and membership XML elements.'; $string['processphoto'] = 'Add user photo data to profile'; $string['processphotowarning'] = 'Warning: Image processing is likely to add a significant burden to the server. You are recommended not to activate this option if large numbers of students are expected to be processed.'; $string['restricttarget'] = 'Only process data if the following target is specified'; $string['restricttarget_desc'] = 'An IMS Enterprise data file could be intended for multiple "targets" - different LMSes, or different systems within a school/university. It\'s possible to specify in the Enterprise file that the data is intended for one or more named target systems, by naming them in tags contained within the tag. In general you don\'t need to worry about this. Leave the setting blank and Moodle will always process the data file, no matter whether a target is specified or not. Otherwise, fill in the exact name that will be output inside the tag.'; +$string['settingfullname'] = 'IMS description tag for the course full name'; +$string['settingfullnamedescription'] = 'The full name is a required course field so you have to define the selected description tag in your IMS Enterprise file'; +$string['settingshortname'] = 'IMS description tag for the course short name'; +$string['settingshortnamedescription'] = 'The short name is a required course field so you have to define the selected description tag in your IMS Enterprise file'; +$string['settingsummary'] = 'IMS description tag for the course summary'; +$string['settingsummarydescription'] = 'Is an optional field, select \'Leave it empty\' if you dont\'t want to specify a course summary'; $string['sourcedidfallback'] = 'Use the "sourcedid" for a person\'s userid if the "userid" field is not found'; $string['sourcedidfallback_desc'] = 'In IMS data, the field represents the persistent ID code for a person as used in the source system. The field is a separate field which should contain the ID code used by the user when logging in. In many cases these two codes may be the same - but not always. diff --git a/enrol/imsenterprise/lib.php b/enrol/imsenterprise/lib.php index eef9380fcfa..8cfa607baf5 100644 --- a/enrol/imsenterprise/lib.php +++ b/enrol/imsenterprise/lib.php @@ -97,7 +97,7 @@ function cron() { // Make sure we understand how to map the IMS-E roles to Moodle roles $this->load_role_mappings(); - // Make sure we understand how to map the IMS-E course names to Moodle course names + // Make sure we understand how to map the IMS-E course names to Moodle course names. $this->load_course_mappings(); $md5 = md5_file($filename); // NB We'll write this value back to the database at the end of the cron @@ -319,10 +319,10 @@ function process_group_tag($tagcontents) { } if (preg_match('{.*?(.*?).*?}is', $tagcontents, $matches)) { - $group->longname = trim($matches[1]); + $group->long = trim($matches[1]); } if (preg_match('{.*?(.*?).*?}is', $tagcontents, $matches)) { - $group->shortname = trim($matches[1]); + $group->short = trim($matches[1]); } if (preg_match('{.*?(.*?).*?}is', $tagcontents, $matches)) { $group->full = trim($matches[1]); @@ -365,28 +365,36 @@ function process_group_tag($tagcontents) { if (!$createnewcourses) { $this->log_line("Course $coursecode not found in Moodle's course idnumbers."); } else { - // Set shortname to description or description to shortname if one is set but not the other. - $nodescription = !isset($group->longname); - $noshortname = !isset($group->shortname); - if ( $nodescription && $noshortname) { - // If neither short nor long description are set let if fail - $this->log_line("Neither long nor short name are set for $coursecode"); - } else if ($nodescription) { - // If short and ID exist, then give the long short's value, then give short the ID's value - $group->longname = $group->shortname; - $group->shortname = $coursecode; - } else if ($noshortname) { - // If long and ID exist, then map long to long, then give short the ID's value. - $group->shortname = $coursecode; - } - $group->full = format_text($group->full, FORMAT_HTML); // Create the (hidden) course(s) if not found $courseconfig = get_config('moodlecourse'); // Load Moodle Course shell defaults + + // New course. $course = new stdClass(); - foreach ($this->coursemappings as $coursename => $imsname) { - $course->$coursename = $group->$imsname; + foreach ($this->coursemappings as $courseattr => $imsname) { + + if ($imsname == 'ignore') { + continue; + } + + // Check if the IMS file contains the mapped tag, otherwise fallback on coursecode. + if ($imsname == 'coursecode') { + $course->{$courseattr} = $coursecode; + } else if (!empty($group->{$imsname})) { + $course->{$courseattr} = $group->{$imsname}; + } else { + $this->log_line('No ' . $imsname . ' description tag found for ' . $coursecode . ' coursecode, using ' . $coursecode . ' instead'); + $course->{$courseattr} = $coursecode; + } + + if ($courseattr == 'summary') { + $format = FORMAT_HTML; + } else { + $format = FORMAT_PLAIN; + } + $course->{$courseattr} = format_text($course->$courseattr, $format); } + $course->idnumber = $coursecode; $course->format = $courseconfig->format; $course->visible = $courseconfig->visible; @@ -425,7 +433,6 @@ function process_group_tag($tagcontents) { $course->startdate = time(); // Choose a sort order that puts us at the start of the list! $course->sortorder = 0; - $courseid = $DB->insert_record('course', $course); // Setup default enrolment plugins @@ -795,18 +802,17 @@ function load_role_mappings() { /** * Load the name mappings (from the config), so we can easily refer to - * how an IMS-E course properties corresponds to a Moodle course properties + * how an IMS-E course properties corresponds to a Moodle course properties */ function load_course_mappings() { require_once('locallib.php'); - $imsnames = new imsenterprise_names(); - $coursenames = $imsnames->get_coursenames(); - $imsnames = $imsnames->get_imsnames(); + $imsnames = new imsenterprise_courses(); + $courseattrs = $imsnames->get_courseattrs(); $this->coursemappings = array(); - foreach($coursenames as $coursename) { - $this->coursemappings[$coursename] = $this->get_config('imsrolemap' . $coursename); + foreach($courseattrs as $courseattr) { + $this->coursemappings[$courseattr] = $this->get_config('imscoursemap' . $courseattr); } } diff --git a/enrol/imsenterprise/locallib.php b/enrol/imsenterprise/locallib.php index 8b21ec10b49..13c61143dc4 100644 --- a/enrol/imsenterprise/locallib.php +++ b/enrol/imsenterprise/locallib.php @@ -83,34 +83,62 @@ class imsenterprise_roles { } // class -class imsenterprise_names { - private $imsnames; - private $coursenames; +/** + * Mapping between Moodle course attributes and IMS enterprise group description tags + * + * @package enrol_imsenterprise + * @copyright 2011 Aaron C Spike + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class imsenterprise_courses { + + private $imsnames; + private $courseattrs; + + /** + * Loads default + */ function __construct() { $this->imsnames = array( - 'long'=>'long', - 'short'=>'short', - 'full'=>'full', - 'coursecode'=>'coursecode', - ); - $this->coursenames = array('fullname', 'shortname', 'summary'); - } - - function get_imsnames() { - return $this->imsnames; - } - - function get_coursenames() { - return $this->coursenames; + 'short' => 'short', + 'long' => 'long', + 'full' => 'full', + 'coursecode' => 'coursecode'); + $this->courseattrs = array('shortname', 'fullname', 'summary'); } /** - * This function is only used when first setting up the plugin, to - * decide which name assignments to recommend by default. - */ - function determine_default_namemapping($coursename) { - switch($coursename) { + * Returns the assignable values for the course attribute + * @param string $courseattr The course attribute (shortname, fullname...) + * @return array Array of assignable values + */ + function get_imsnames($courseattr) { + + $values = $this->imsnames; + if ($courseattr == 'summary') { + $values = array_merge(array('ignore' => get_string('emptyattribute', 'enrol_imsenterprise')), $values); + } + return $values; + } + + /** + * courseattrs getter + * @return array + */ + function get_courseattrs() { + return $this->courseattrs; + } + + /** + * This function is only used when first setting up the plugin, to + * decide which name assignments to recommend by default. + * + * @param string $coursename + * @return string + */ + function determine_default_coursemapping($courseattr) { + switch($courseattr) { case 'fullname': $imsname = 'short'; break; @@ -118,10 +146,10 @@ class imsenterprise_names { $imsname = 'coursecode'; break; default: - return 'ignore'; // Zero for no match + $imsname = 'ignore'; } + return $imsname; } - } // class diff --git a/enrol/imsenterprise/settings.php b/enrol/imsenterprise/settings.php index 4262b1a78bd..b644f491944 100644 --- a/enrol/imsenterprise/settings.php +++ b/enrol/imsenterprise/settings.php @@ -75,14 +75,17 @@ if ($ADMIN->fulltree) { $settings->add(new admin_setting_configcheckbox('enrol_imsenterprise/createnewcategories', get_string('createnewcategories', 'enrol_imsenterprise'), get_string('createnewcategories_desc', 'enrol_imsenterprise'), 0)); $settings->add(new admin_setting_configcheckbox('enrol_imsenterprise/imsunenrol', get_string('allowunenrol', 'enrol_imsenterprise'), get_string('allowunenrol_desc', 'enrol_imsenterprise'), 0)); - - $settings->add(new admin_setting_heading('enrol_imsenterprise_coursesettings_names', get_string('names', 'enrol_imsenterprise'), get_string('imsnamesdescription', 'enrol_imsenterprise'))); if (!during_initial_install()) { - $imsnames = new imsenterprise_names(); - $assignablenames = array('ignore' => get_string('ignore', 'enrol_imsenterprise')) + $imsnames->get_imsnames(); - foreach ($imsnames->get_coursenames() as $coursename) { - $settings->add(new admin_setting_configselect('enrol_imsenterprise/imsnamemap'.$coursename, get_string($coursename, 'enrol_imsenterprise'), '', (string)$imsnames->determine_default_namemapping($coursename), $assignablenames)); + $imscourses = new imsenterprise_courses(); + foreach ($imscourses->get_courseattrs() as $courseattr) { + + // The assignable values of this course attribute + $assignablevalues = $imscourses->get_imsnames($courseattr); + $name = get_string('setting' . $courseattr, 'enrol_imsenterprise'); + $description = get_string('setting' . $courseattr . 'description', 'enrol_imsenterprise'); + $defaultvalue = (string) $imscourses->determine_default_coursemapping($courseattr); + $settings->add(new admin_setting_configselect('enrol_imsenterprise/imscoursemap' . $courseattr, $name, $description, $defaultvalue, $assignablevalues)); } } From 2b8f692f71fd864306fa0794fcdcf5aca5ce0321 Mon Sep 17 00:00:00 2001 From: David Monllao Date: Fri, 5 Oct 2012 11:42:17 +0800 Subject: [PATCH 4/5] MDL-28705 enrol_imsenterprise Unit tests for users and courses creation --- enrol/imsenterprise/lib.php | 5 +- .../tests/imsenterprise_test.php | 260 ++++++++++++++++++ 2 files changed, 264 insertions(+), 1 deletion(-) create mode 100644 enrol/imsenterprise/tests/imsenterprise_test.php diff --git a/enrol/imsenterprise/lib.php b/enrol/imsenterprise/lib.php index 8cfa607baf5..5a2070efaff 100644 --- a/enrol/imsenterprise/lib.php +++ b/enrol/imsenterprise/lib.php @@ -759,7 +759,10 @@ function process_properties_tag($tagcontents){ * @param string $string Text to write (newline will be added automatically) */ function log_line($string){ - mtrace($string); + + if (!PHPUNIT_TEST) { + mtrace($string); + } if($this->logfp) { fwrite($this->logfp, $string . "\n"); } diff --git a/enrol/imsenterprise/tests/imsenterprise_test.php b/enrol/imsenterprise/tests/imsenterprise_test.php new file mode 100644 index 00000000000..358d3fdd2ca --- /dev/null +++ b/enrol/imsenterprise/tests/imsenterprise_test.php @@ -0,0 +1,260 @@ +. + +/** + * IMS Enterprise enrolment tests. + * + * @package enrol_imsenterprise + * @category phpunit + * @copyright 2012 David MonllaĆ³ + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; +require_once($CFG->dirroot . '/enrol/imsenterprise/locallib.php'); +require_once($CFG->dirroot . '/enrol/imsenterprise/lib.php'); + +/** + * IMS Enterprise test case + * + * @package enrol_imsenterprise + * @category phpunit + * @copyright 2012 David MonllaĆ³ + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class enrol_imsenterprise_testcase extends advanced_testcase { + + protected $imsplugin; + + protected function setUp() { + $this->resetAfterTest(true); + $this->imsplugin = enrol_get_plugin('imsenterprise'); + $this->set_test_config(); + } + + /** + * With an empty IMS enterprise file + */ + public function test_emptyfile() { + global $DB; + + $prevncourses = $DB->count_records('course'); + $prevnusers = $DB->count_records('user'); + + $this->set_xml_file(false, false); + $this->imsplugin->cron(); + + $this->assertEquals($prevncourses, $DB->count_records('course')); + $this->assertEquals($prevnusers, $DB->count_records('user')); + } + + + /** + * Existing users are not created again + */ + public function test_users_existing() { + global $DB; + + $user1 = $this->getDataGenerator()->create_user(); + $user2 = $this->getDataGenerator()->create_user(); + + $prevnusers = $DB->count_records('user'); + + $users = array($user1, $user2); + $this->set_xml_file($users); + $this->imsplugin->cron(); + + $this->assertEquals($prevnusers, $DB->count_records('user')); + } + + + /** + * Add new users + */ + public function test_users_add() { + global $DB; + + $prevnusers = $DB->count_records('user'); + + $user1 = new StdClass(); + $user1->username = 'u1'; + $user1->email = 'u1@u1.org'; + $user1->firstname = 'U'; + $user1->lastname = '1'; + + $users = array($user1); + $this->set_xml_file($users); + $this->imsplugin->cron(); + + $this->assertEquals(($prevnusers + 1), $DB->count_records('user')); + } + + + /** + * Existing courses are not created again + */ + public function test_courses_existing() { + global $DB; + + $course1 = $this->getDataGenerator()->create_course(array('idnumber' => 'id1')); + $course2 = $this->getDataGenerator()->create_course(array('idnumber' => 'id2')); + + // Default mapping according to default course attributes - IMS description tags mapping. + $course1->imsshort = $course1->fullname; + $course2->imsshort = $course2->fullname; + + $prevncourses = $DB->count_records('course'); + + $courses = array($course1, $course2); + $this->set_xml_file(false, $courses); + $this->imsplugin->cron(); + + $this->assertEquals($prevncourses, $DB->count_records('course')); + } + + + /** + * Add new courses + */ + public function test_courses_add() { + global $DB; + + $prevncourses = $DB->count_records('course'); + + $course1 = new StdClass(); + $course1->idnumber = 'id1'; + $course1->imsshort = 'id1'; + $course1->category = 'DEFAULT CATNAME'; + + $course2 = new StdClass(); + $course2->idnumber = 'id2'; + $course2->imsshort = 'id2'; + $course2->category = 'DEFAULT CATNAME'; + + $courses = array($course1, $course2); + $this->set_xml_file(false, $courses); + $this->imsplugin->cron(); + + $this->assertEquals(($prevncourses + 2), $DB->count_records('course')); + } + + + /** + * Sets the plugin configuration for testing + */ + protected function set_test_config() { + $this->imsplugin->set_config('mailadmins', false); + $this->imsplugin->set_config('prev_path', ''); + $this->imsplugin->set_config('createnewusers', true); + $this->imsplugin->set_config('createnewcourses', true); + $this->imsplugin->set_config('createnewcategories', true); + } + + + /** + * Creates an IMS enterprise XML file and adds it's path to config settings + * + * @param array Array of users StdClass + * @param array Array of courses StdClass + */ + protected function set_xml_file($users = false, $courses = false) { + global $DB; + + $xmlcontent = ''; + + // Users. + if (!empty($users)) { + foreach ($users as $user) { + $xmlcontent .= ' + + + TestSource + '.$user->username.' + + '.$user->username.' + + '.$user->firstname.' '.$user->lastname.' + + '.$user->lastname.' + '.$user->firstname.' + + + '.$user->email.' + '; + } + } + + // Courses. + // Mapping based on default course attributes - IMS group tags mapping. + if (!empty($courses)) { + foreach ($courses as $course) { + + // orgunit tag value is used by moodle as category name. + // If the category does not exists the id is used as name. + if ($categoryname = $DB->get_field('course_categories', 'name', array('id' => $course->category))) { + $categoryname = $course->category; + } + + $xmlcontent .= ' + + + TestSource + '.$course->idnumber.' + + '; + + // Optional to test course attributes mappings. + if (!empty($course->imsshort)) { + $xmlcontent .= ' + '.$course->imsshort.''; + } + + // Optional to test course attributes mappings. + if (!empty($course->imslong)) { + $xmlcontent .= ' + '.$course->imslong.''; + } + + // Optional to test course attributes mappings. + if (!empty($course->imsfull)) { + $xmlcontent .= ' + '.$course->imsfull.''; + } + + $xmlcontent .= ' + + + '.$categoryname.' + + '; + } + } + + $xmlcontent .= ' +'; + + // Creating the XML file. + $filename = 'ims_' . rand(1000, 9999) . '.xml'; + $tmpdir = make_temp_directory('enrol_imsenterprise'); + $xmlfilepath = $tmpdir . '/' . $filename; + file_put_contents($xmlfilepath, $xmlcontent); + + // Setting the file path in CFG. + $this->imsplugin->set_config('imsfilelocation', $xmlfilepath); + } +} From c25c3c07b469d6259e3bde8ca3bd3f15aad77e70 Mon Sep 17 00:00:00 2001 From: David Monllao Date: Fri, 5 Oct 2012 11:42:43 +0800 Subject: [PATCH 5/5] MDL-28705 enrol_imsenterprise Unit tests for course attributes mapping --- .../tests/imsenterprise_test.php | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/enrol/imsenterprise/tests/imsenterprise_test.php b/enrol/imsenterprise/tests/imsenterprise_test.php index 358d3fdd2ca..90dac3de864 100644 --- a/enrol/imsenterprise/tests/imsenterprise_test.php +++ b/enrol/imsenterprise/tests/imsenterprise_test.php @@ -154,6 +154,78 @@ class enrol_imsenterprise_testcase extends advanced_testcase { } + /** + * Course attributes mapping to IMS enterprise group description tags + */ + public function test_courses_attrmapping() { + global $DB; + + // Setting a all = coursecode (idnumber) mapping. + $this->imsplugin->set_config('imscoursemapshortname', 'coursecode'); + $this->imsplugin->set_config('imscoursemapfullname', 'coursecode'); + $this->imsplugin->set_config('imscoursemapsummary', 'coursecode'); + + $course1 = new StdClass(); + $course1->idnumber = 'id1'; + $course1->imsshort = 'description_short1'; + $course1->imslong = 'description_long'; + $course1->imsfull = 'description_full'; + $course1->category = 'DEFAULT CATNAME'; + + $this->set_xml_file(false, array($course1)); + $this->imsplugin->cron(); + + $dbcourse = $DB->get_record('course', array('idnumber' => $course1->idnumber)); + $this->assertFalse(!$dbcourse); + $this->assertEquals($dbcourse->shortname, $course1->idnumber); + $this->assertEquals($dbcourse->fullname, $course1->idnumber); + $this->assertEquals($dbcourse->summary, $course1->idnumber); + + + // Setting a mapping using all the description tags. + $this->imsplugin->set_config('imscoursemapshortname', 'short'); + $this->imsplugin->set_config('imscoursemapfullname', 'long'); + $this->imsplugin->set_config('imscoursemapsummary', 'full'); + + $course2 = new StdClass(); + $course2->idnumber = 'id2'; + $course2->imsshort = 'description_short2'; + $course2->imslong = 'description_long'; + $course2->imsfull = 'description_full'; + $course2->category = 'DEFAULT CATNAME'; + + $this->set_xml_file(false, array($course2)); + $this->imsplugin->cron(); + + $dbcourse = $DB->get_record('course', array('idnumber' => $course2->idnumber)); + $this->assertFalse(!$dbcourse); + $this->assertEquals($dbcourse->shortname, $course2->imsshort); + $this->assertEquals($dbcourse->fullname, $course2->imslong); + $this->assertEquals($dbcourse->summary, $course2->imsfull); + + + // Setting a mapping where the specified description tags doesn't exist in the XML file (must delegate into idnumber). + $this->imsplugin->set_config('imscoursemapshortname', 'short'); + $this->imsplugin->set_config('imscoursemapfullname', 'long'); + $this->imsplugin->set_config('imscoursemapsummary', 'full'); + + $course3 = new StdClass(); + $course3->idnumber = 'id3'; + $course3->imsshort = 'description_short3'; + $course3->category = 'DEFAULT CATNAME'; + + $this->set_xml_file(false, array($course3)); + $this->imsplugin->cron(); + + $dbcourse = $DB->get_record('course', array('idnumber' => $course3->idnumber)); + $this->assertFalse(!$dbcourse); + $this->assertEquals($dbcourse->shortname, $course3->imsshort); + $this->assertEquals($dbcourse->fullname, $course3->idnumber); + $this->assertEquals($dbcourse->summary, $course3->idnumber); + + } + + /** * Sets the plugin configuration for testing */