/** * method to create test userset * @param string $name the name to use to create userset * @return int|bool the userset DB id or false on error */ public function create_userset($name) { $params = array('name' => $name); $us = new userset($params); $us->save(); return !empty($us->id) ? $us->id : false; }
/** * Test custom userset theme assignment. */ public function test_customusersetthemectx() { $this->load_csv_data(); // ELIS user with the associated moodle user. $user = new user(103); $muser = $user->get_moodleuser(); // Userset with a custom theme. $userset = new userset(1); $userset->field__elis_userset_theme = 'formal_white'; $userset->field__elis_userset_themepriority = 1; $userset->save(); // Assign the user to the user set. $usersetassign = new clusterassignment(); $usersetassign->userid = $user->id; $usersetassign->clusterid = $userset->id; $usersetassign->save(); // Pretend to be that user. $this->setUser($muser->id); // Initialize page. $page = new moodle_page(); $page->initialise_theme_and_output(); // Assert we have our theme. $this->assertEquals('formal_white', $page->theme->name); $this->setUser(null); }
/** * Set up necessary data * * @param int $numfields The number of custom fields used in auto-association */ private function init_required_data($numfields = 1) { global $CFG, $DB; require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php'; require_once elis::file('eliscore/fields/moodleprofile/custom_fields.php'); require_once elis::lib('data/customfield.class.php'); require_once elispm::file('accesslib.php'); require_once elispm::lib('data/userset.class.php'); require_once $CFG->dirroot . '/user/profile/definelib.php'; require_once $CFG->dirroot . '/user/profile/field/checkbox/define.class.php'; // Set up the category only once. $fieldcategory = new field_category(array('name' => 'testcategoryname')); $fieldcategory->save(); // Ste up the target userset only once. $userset = new userset(array('name' => 'testusersetname')); $userset->save(); for ($i = 1; $i <= $numfields; $i++) { // Custom field. $field = new field(array('categoryid' => $fieldcategory->id, 'shortname' => 'testfieldshortname' . $i, 'name' => 'testfieldname' . $i, 'datatype' => 'bool')); $field->save(); // Ensure manual field owner exists for syncing. field_owner::ensure_field_owner_exists($field, 'manual'); $ownerid = $DB->get_field(field_owner::TABLE, 'id', array('fieldid' => $field->id, 'plugin' => 'manual')); $owner = new field_owner($ownerid); $owner->param_control = 'checkbox'; $owner->save(); // Ensure moodle profile field owner exists. field_owner::ensure_field_owner_exists($field, 'moodle_profile'); $DB->execute("UPDATE {" . field_owner::TABLE . "} SET exclude = ?", array(pm_moodle_profile::sync_to_moodle)); // Field context level assocation. $fieldcontextlevel = new field_contextlevel(array('fieldid' => $field->id, 'contextlevel' => CONTEXT_ELIS_USER)); $fieldcontextlevel->save(); // The associated Moodle user profile field. $profiledefinecheckbox = new profile_define_checkbox(); $data = new stdClass(); $data->datatype = 'checkbox'; $data->categoryid = 99999; $data->shortname = 'testfieldshortname' . $i; $data->name = 'testfieldname' . $i; $profiledefinecheckbox->define_save($data); $mfield = $DB->get_record('user_info_field', array('shortname' => 'testfieldshortname' . $i)); // The "cluster-profile" association. $usersetprofile = new userset_profile(array('clusterid' => $userset->id, 'fieldid' => $mfield->id, 'value' => 1)); $usersetprofile->save(); } }
/** * Validate that the "permission_check" method works hierarchically and respects the * capabilities assigned at the system level for userset files * @uses $USER, $DB, $SESSION, $USER * @param string $depth The folder depth to build for hierachy testing * @dataProvider hierarchy_provider */ public function test_permission_check_hierarchical_capabilities_for_userset_file($depth) { if (!class_exists('elispm')) { $this->markTestSkipped('elis_program needed for test'); return false; } $this->resetAfterTest(true); $this->setup_test_data_xml(); global $CFG, $DB, $SESSION, $USER; require_once(elispm::lib('data/userset.class.php')); // Make sure the test user is not mistaken for a site admin or guest set_config('siteadmins', ''); set_config('siteguest', ''); // Use a fixed capability $capability = 'repository/elisfiles:createusersetcontent'; // unset the repo to avoid the is_siteadmin problem in the factory class unset($SESSION->repo); // create a test user and give them the capability and role they need $this->setUser(100); $roleid = $this->assign_role_capability($capability); // Assign the test role to the test user $context = context_system::instance(); role_assign($roleid, $USER->id, $context->id); // RL: ELIS files: Alfresco $data = null; $listing = null; $options = array( 'ajax' => false, 'name' => 'elis files phpunit test', 'type' => 'elisfiles' ); try { $repo = new repository_elisfiles('elisfiles', context_system::instance(), $options); } catch (Exception $e) { $this->markTestSkipped('Exception when creating repository_elisfiles object: '.$e->getMessage()); } // Explicitly set the file transfer method to web services set_config('file_transfer_method', ELIS_FILES_XFER_WS, 'elisfiles'); $userset = new userset(array( 'name' => 'elisunittestuserset' )); $userset->save(); $currentuuid = $repo->elis_files->get_userset_store($userset->id); $this->createduuids[] = $currentuuid; // Now we need to create folders up to the depth from the data provider and upload a file for ($i = 1; $i <= $depth; ++$i) { // create a sub-folder $currentnode = elis_files_create_dir(TEST_PREFIX.'userset_'.$i, $currentuuid); $this->assertTrue($currentnode && !empty($currentnode->uuid)); $currentuuid = $currentnode->uuid; $this->createduuids[] = $currentuuid; } $filename = $CFG->dirroot.'/repository/elisfiles/tests/'.TEST_PREFIX.'file.txt'; // upload a file to this folder $response = $this->call_upload_file($repo, '', $filename, $currentuuid); // FTP was failing, but this is a good check to keep in $this->assertFalse(!$response); $haspermission = $repo->elis_files->permission_check($response->uuid, 0, true, $repo); // Validation $this->assertTrue($haspermission); }
/** * Validate that mappings are applied during the user set enrolment delete action */ public function test_mapping_applied_during_userset_enrolment_delete() { global $CFG, $DB; require_once $CFG->dirroot . '/local/elisprogram/lib/data/clusterassignment.class.php'; require_once $CFG->dirroot . '/local/elisprogram/lib/data/userset.class.php'; $this->init_mapping(); $userid = $this->create_test_user(); $userset = new userset(array('name' => 'testusersetname')); $userset->save(); $clusterassignment = new clusterassignment(array('clusterid' => $userset->id, 'userid' => $userid, 'plugin' => 'manual')); $clusterassignment->save(); // Run the user set enrolment delete action. $record = new stdClass(); $record->customaction = 'delete'; $record->customcontext = 'cluster_testusersetname'; $record->customuser_username = '******'; $record->customuser_email = '*****@*****.**'; $record->customuser_idnumber = 'testuseridnumber'; $this->run_enrolment_import((array) $record); // Validation. $this->assertEquals(0, $DB->count_records(clusterassignment::TABLE)); }
/** * Initialize a new user description object */ protected function inituserset() { $data = array('name' => 'Test User Set 1', 'display' => 'We\'re just testing user set creation!'); $newuserset = new userset($data); $newuserset->save(); $this->tusersetid = $newuserset->id; }
/** * Validate that the listing respects the local/elisprogram:track_enrol_userset_user * capability as long as the appropriate userset and track are associated to * one another and the target user is in the userset */ public function test_availableusersrespectsindirectusersetpermissions() { global $DB, $USER; $this->load_csv_data(); set_config('siteguest', ''); set_config('siteadmins', ''); accesslib_clear_all_caches_for_unit_testing(); // Create a test user to be in the userset. $usersetmember = new user(array('idnumber' => 'usersetmember', 'username' => 'usersetmember', 'firstname' => 'usersetmember', 'lastname' => 'usersetmember', 'email' => '*****@*****.**', 'country' => 'CA')); $usersetmember->save(); // Our test userset. $userset = new userset(array('name' => 'userset')); $userset->save(); // Assign the test user to the test userset. $clusterassignment = new clusterassignment(array('userid' => $usersetmember->id, 'clusterid' => $userset->id)); $clusterassignment->save(); // Assign the userset to our track. $clustertrack = new clustertrack(array('clusterid' => $userset->id, 'trackid' => 1)); $clustertrack->save(); // Set up a db record for the active user for permissions reasons. // (i.e. so they are not treated as an admin). $activeuser = new user(array('idnumber' => 'activeuser', 'username' => 'activeuser', 'firstname' => 'activeuser', 'lastname' => 'activeuser', 'email' => '*****@*****.**', 'country' => 'CA')); $activeuser->save(); // Set up our test role. $roleid = create_role('testrole', 'testrole', 'testrole'); $syscontext = context_system::instance(); assign_capability('local/elisprogram:track_enrol_userset_user', CAP_ALLOW, $roleid, $syscontext->id); // Perform the role necessary assignment. $moodleuser = $DB->get_record('user', array('username' => 'activeuser')); // Make sure all the contexts are created, so that we can find the children. $contextclass = \local_eliscore\context\helper::get_class_for_level(CONTEXT_ELIS_USERSET); $instance = $contextclass::instance($userset->id); role_assign($roleid, $moodleuser->id, $instance->id); // Assume the role of the user with the role assignment. $USER = $moodleuser; $usersrecset = usertrack::get_available_users(1); $users = array(); foreach ($usersrecset as $key => $user) { $users[$key] = $user; } unset($usersrecset); $this->assertEquals(1, count($users)); // Validate user. $this->assertArrayHasKey($usersetmember->id, $users); $user = $users[$usersetmember->id]; $this->assertEquals($usersetmember->username, 'usersetmember'); // Validate count. $count = usertrack::count_available_users(1); $this->assertEquals(1, $count); }
/** * Validate that an appropriate error is logged when max field lengths are * exceeded during a userset update action * @param string $field The identifier for the field we are testing * @param int $length The length we are testing at * @param string $customvalue A custom value to use rather than simply repeating a character, * or null if not applicable * @dataProvider usersetupdatefieldprovider */ public function test_usersetupdatelogserrorwhenfieldstoolong($field, $length, $customvalue) { global $CFG, $DB; require_once $CFG->dirroot . '/local/elisprogram/lib/data/userset.class.php'; $userset = new userset(array('name' => 'testusersetname')); $userset->save(); $data = array('action' => 'update', 'context' => 'cluster', 'name' => 'testusersetname'); if ($customvalue !== null) { $value = $customvalue; } else { $value = str_repeat('a', $length); } $data[$field] = $value; $maxlength = $length - 1; $expectederror = "{$field} value of \"{$value}\" exceeds the maximum field length of {$maxlength}.\n"; $this->assert_data_produces_error($data, $expectederror, 'course'); }
/** * Test failure conditions. * @dataProvider dataprovider_failure * @expectedException moodle_exception * @param array $us The incoming userset data. */ public function test_failure(array $us) { global $DB; $this->give_permissions(array('local/elisprogram:userset_delete')); // Setup userset to delete. $userset = new userset(array('name' => 'testuserset')); $userset->save(); $response = local_datahub_elis_userset_delete::userset_delete($us); }
/** * Validate that user unenrolment from user sets only happens for the manual * userset ("cluster") plugin */ public function test_elis_user_userset_unenrolment_respects_userset_plugin() { global $CFG, $DB; require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php'; require_once elispm::lib('data/clusterassignment.class.php'); require_once elispm::lib('data/user.class.php'); require_once elispm::lib('data/userset.class.php'); $user = new user(array('idnumber' => 'testuseridnumber', 'username' => 'testuserusername', 'firstname' => 'testuserfirstname', 'lastname' => 'testuserlastname', 'email' => '*****@*****.**', 'country' => 'CA')); $user->save(); $userset = new userset(array('name' => 'testusersetname')); $userset->save(); $clusterassignment = new clusterassignment(array('userid' => $user->id, 'clusterid' => $userset->id, 'plugin' => 'manual', 'autoenrol' => 0)); $clusterassignment->save(); $clusterassignment = new clusterassignment(array('userid' => $user->id, 'clusterid' => $userset->id, 'plugin' => 'another', 'autoenrol' => 0)); $clusterassignment->save(); // Validate setup. $this->assertTrue($DB->record_exists(clusterassignment::TABLE, array('userid' => $user->id, 'clusterid' => $userset->id, 'plugin' => 'manual', 'autoenrol' => 0))); $this->assertTrue($DB->record_exists(clusterassignment::TABLE, array('userid' => $user->id, 'clusterid' => $userset->id, 'plugin' => 'another', 'autoenrol' => 0))); // Run the userset enrolment delete action. $record = new stdClass(); $record->context = 'cluster_testusersetname'; $record->user_username = '******'; $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis'); $importplugin->fslogger = new silent_fslogger(null); $importplugin->cluster_enrolment_delete($record, 'bogus', 'testusersetname'); // Validation. $this->assertEquals(1, $DB->count_records(clusterassignment::TABLE)); $this->assertTrue($DB->record_exists(clusterassignment::TABLE, array('userid' => $user->id, 'clusterid' => $userset->id, 'plugin' => 'another', 'autoenrol' => 0))); }
/** * Create an ELIS userset. * @param field $field A custom field to set when creating the userset. * @return userset The created userset. */ public function create_userset(field $field) { $data = new stdClass(); $data->name = 'Test User Set 123'; $data->parent = '0'; $data->profile_field1 = '0'; $data->profile_field2 = '0'; $fieldvar = 'field_' . $field->shortname; $data->{$fieldvar} = 'test field data'; $usrset = new userset(); $usrset->set_from_data($data); $usrset->save(); return $usrset; }
/** * Create a cluster (user set) * @todo: consider factoring this some more once other actions exist * * @param object $record One record of import data * @param string $filename The import file name, used for logging * @return boolean true on success, otherwise false */ function cluster_update($record, $filename) { global $CFG, $DB; require_once $CFG->dirroot . '/local/elisprogram/lib/data/userset.class.php'; //field length checking $lengthcheck = $this->check_userset_field_lengths($record, $filename); if (!$lengthcheck) { return false; } if (isset($record->name)) { $id = $DB->get_field(userset::TABLE, 'id', array('name' => $record->name)); if (!$id) { $identifier = $this->get_field_mapping('name'); $this->fslogger->log_failure("{$identifier} value of \"{$record->name}\" does not refer to a valid user set.", 0, $filename, $this->linenumber, $record, "cluster"); return false; } } if (isset($record->parent)) { if ($record->parent == 'top') { $record->parent = 0; } else { if ($parentid = $DB->get_field(userset::TABLE, 'id', array('name' => $record->parent))) { $record->parent = $parentid; } else { $identifier = $this->get_field_mapping('parent'); $this->fslogger->log_failure("{$identifier} value of \"{$record->parent}\" should refer to a valid user set, or be set to \"top\" to place this user set at the top level.", 0, $filename, $this->linenumber, $record, "cluster"); return false; } } } $record = $this->add_custom_field_prefixes($record); //custom field validation if (!$this->validate_custom_field_data('update', $record, $filename, 'cluster')) { return false; } $record->id = $id; $data = new userset(); $data->set_from_data($record); $data->save(); //log success $success_message = "User set with name \"{$record->name}\" successfully updated."; $this->fslogger->log_success($success_message, 0, $filename, $this->linenumber); return true; }
/** * Test that a record can be modified. */ public function test_canupdaterecord() { $this->load_csv_data(); // Read a record. $src = new userset(3, null, array(), false, array()); // Modify the data. $src->name = 'Sub-sub set 2'; $src->display = 'Sub sub user set'; $src->parent = 2; $src->save(); // Read it back. $result = new moodle_recordset_phpunit_datatable(userset::TABLE, userset::find(null, array(), 0, 0)); $dataset = new PHPUnit_Extensions_Database_DataSet_CsvDataSet(); $dataset->addTable(userset::TABLE, elispm::file('tests/fixtures/userset_update_test_result.csv')); $this->assertTablesEqual($dataset->getTable(userset::TABLE), $result); }
/** * Validate that create actions are converted to updates for user sets when the * "createorupdate" flag is enabled */ public function test_elis_createorupdate_updates_userset() { global $CFG, $DB; require_once $CFG->dirroot . '/local/elisprogram/lib/data/userset.class.php'; // Set up initial conditions. set_config('createorupdate', 1, 'dhimport_version1elis'); // Create the test user set. $userset = new userset(array('name' => 'testusersetname', 'display' => 'testusersetdisplay')); $userset->save(); // Run the user set create action. $record = new stdClass(); $record->action = 'create'; $record->context = 'cluster'; $record->name = 'testusersetname'; $record->display = 'updatedtestusersetdisplay'; $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis'); $importplugin->fslogger = new silent_fslogger(null); $importplugin->process_record('course', $record, 'bogus'); // Validation. $this->assertTrue($DB->record_exists(userset::TABLE, array('name' => 'testusersetname', 'display' => 'updatedtestusersetdisplay'))); }
/** * Validate that enrolling a user into a user set via IP auto-enrolls them in * an associated track, and any associated programs or class instances */ public function test_userset_enrolment_creates_track_enrolment() { global $CFG, $DB; require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php'; require_once elispm::lib('data/clustertrack.class.php'); require_once elispm::lib('data/course.class.php'); require_once elispm::lib('data/curriculum.class.php'); require_once elispm::lib('data/curriculumcourse.class.php'); require_once elispm::lib('data/curriculumstudent.class.php'); require_once elispm::lib('data/pmclass.class.php'); require_once elispm::lib('data/student.class.php'); require_once elispm::lib('data/track.class.php'); require_once elispm::lib('data/user.class.php'); require_once elispm::lib('data/userset.class.php'); require_once elispm::lib('data/usertrack.class.php'); // Make sure no emails are sent. set_config('noemailever', true); // Set up data. // Test user. $user = new user(array('idnumber' => 'testuseridnumber', 'username' => 'testuserusername', 'firstname' => 'testuserfirstname', 'firstname' => 'testuserfirstname', 'lastname' => 'testuserlastname', 'email' => '*****@*****.**', 'country' => 'CA')); $user->save(); // Test user set. $userset = new userset(array('name' => 'testusersetname')); $userset->save(); // Test program and track. $program = new curriculum(array('idnumber' => 'testprogramidnumber')); $program->save(); $track = new track(array('curid' => $program->id, 'idnumber' => 'testtrackidnumber')); $track->save(); // Associate the userset to the track. $clustertrack = new clustertrack(array('clusterid' => $userset->id, 'trackid' => $track->id, 'autoenrol' => 1)); $clustertrack->save(); // Test course and class. $course = new course(array('name' => 'testcoursename', 'idnumber' => 'testcourseidnumber', 'syllabus' => '')); $course->save(); $class = new pmclass(array('courseid' => $course->id, 'idnumber' => 'testclass1idnumber')); $class->save(); // Associate course to the program. $curriculumcourse = new curriculumcourse(array('curriculumid' => $program->id, 'courseid' => $course->id)); $curriculumcourse->save(); // Associate track to the test class. $trackassignment = new trackassignment(array('trackid' => $track->id, 'classid' => $class->id, 'autoenrol' => 1)); $trackassignment->save(); // Run the assignment create action. $record = new stdClass(); $record->context = 'userset_testusersetname'; $record->user_username = '******'; $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis'); $importplugin->fslogger = new silent_fslogger(null); $importplugin->cluster_enrolment_create($record, 'bogus', 'testusersetname'); // Validation. // Userset assignment should trigger track assignment. $this->assertTrue($DB->record_exists(usertrack::TABLE, array('userid' => $user->id, 'trackid' => $track->id))); // Track assignment should trigger program assignment. $this->assertTrue($DB->record_exists(curriculumstudent::TABLE, array('userid' => $user->id, 'curriculumid' => $program->id))); // Track assignment should create a class enrolment. $this->assertTrue($DB->record_exists(student::TABLE, array('userid' => $user->id, 'classid' => $class->id))); }
/** * Test failure conditions. * @dataProvider dataprovider_failure * @expectedException moodle_exception * @param array $us The incoming userset data. */ public function test_failure(array $us) { global $DB; $this->give_permissions(array('local/elisprogram:userset_create')); // Setup duplicate userset. $dupus = new userset(array('name' => 'DupUsersetName')); $dupus->save(); $response = local_datahub_elis_userset_create::userset_create($us); }
/** * Create a test userset. * @return userset The test userset. */ public function create_userset(field $field = null) { $usrset = new userset(); $usrset->reset_custom_field_list(); $usrset->name = 'Test User Set For Field' . str_replace('.', '', microtime(true)); $usrset->parent = '0'; if (!empty($field)) { $fieldvar = 'field_' . $field->shortname; $usrset->{$fieldvar} = 'test field data'; } $usrset->save(); return $usrset; }
/** * Handler for the confirm action. Assigns a child cluster to specified cluster. */ function do_subcluster() { global $CFG; $id = $this->required_param('id', PARAM_INT); $target_page = $this->get_new_page(array('id' => $id), true); $sub_cluster_id = $this->required_param('subclusterid', PARAM_INT); $cluster = new userset($sub_cluster_id); $cluster->parent = $id; $cluster->save(); redirect($target_page->url, get_string('cluster_assigned', 'local_elisprogram')); }
/** * Test the clustertree. */ public function test_clustertree() { global $CFG; if (!file_exists($CFG->dirroot . '/local/elisreports/php_report_base.php')) { $this->markTestSkipped('Test requires local_elisreports code'); } require_once elispm::lib('filtering/clustertree.php'); $testclusterids = array(); for ($i = 0; $i < 5; $i++) { $userset = new userset(); $userset->name = 'test_userset_' . $i; $userset->save(); $testclusterids[] = $userset->id; } $uniqueid = 'test_clustertree'; $alias = 'test_clustertree'; $name = 'test_clustertree'; $label = 'test_clustertree'; $advanced = false; $field = null; $filter = new generalized_filter_clustertree($uniqueid, $alias, $name, $label, $advanced, $field); // Test context call in get_sql_filter. $data = array('clusterids' => array(), 'clrunexpanded_ids' => array(), 'unexpanded_ids' => array()); $sqlfilter = $filter->get_sql_filter($data); $this->assertNotEmpty($sqlfilter); // Test empty get_label. $data = array(); $label = $filter->get_label($data); $expected = 'test_clustertree: N/A<br/>'; $this->assertEquals($expected, $label); // Test get_label with clusterids. $data = array('clusterids' => $testclusterids); $label = $filter->get_label($data); $clusternames = array(); for ($i = 0; $i < 5; $i++) { $clusternames[] = 'test_userset_' . $i; } $expected = $name . ':<br/>' . implode(', ', $clusternames) . '<br/>'; $this->assertEquals($expected, $label); // Test get_label with unexpanded_ids. $data = array('unexpanded_ids' => $testclusterids); $label = $filter->get_label($data); $clusternames = array(); for ($i = 0; $i < 5; $i++) { $clusternames[] = 'test_userset_' . $i . ' (and all children)'; } $expected = $name . ':<br/>' . implode(', ', $clusternames) . '<br/>'; $this->assertEquals($expected, $label); // Create a subuserset for the next two tests. $subuserset = new userset(); $subuserset->name = 'test_subuserset_1'; $subuserset->parent = $testclusterids[0]; $subuserset->save(); $testclusterids[] = $subuserset->id; // Test get_label with recursively unselected clusters with 'clusterids' param. // Tests that when clrunexpanded_ids contains an ID of a userset with child usersets, the child usersets also // do not show up. $data = array('clusterids' => $testclusterids, 'clrunexpanded_ids' => array($testclusterids[0])); $label = $filter->get_label($data); $clusternames = array(); for ($i = 1; $i <= 4; $i++) { $clusternames[] = 'test_userset_' . $i; } $expected = $name . ':<br/>' . implode(', ', $clusternames) . '<br/>'; $this->assertEquals($expected, $label); // Test get_label with recursively unselected clusters with 'unexpanded_ids'. // Tests that when a subusetset is listed in 'unexpanded_ids', but it's parent is listed in clrunexpanded_ids, // the subuserset does not show up. $unexpandedids = $testclusterids; unset($unexpandedids[0]); $data = array('unexpanded_ids' => $unexpandedids, 'clrunexpanded_ids' => array($testclusterids[0])); $label = $filter->get_label($data); $clusternames = array(); for ($i = 1; $i <= 4; $i++) { $clusternames[] = 'test_userset_' . $i . ' (and all children)'; } $expected = $name . ':<br/>' . implode(', ', $clusternames) . '<br/>'; $this->assertEquals($expected, $label); }
/** * Validate that mappings are applied during the user set delete action */ public function test_mapping_applied_during_userset_delete() { global $CFG, $DB; require_once $CFG->dirroot . '/local/elisprogram/accesslib.php'; require_once $CFG->dirroot . '/local/elisprogram/lib/data/userset.class.php'; $this->init_mapping(); context_helper::reset_caches(); $parentuserset = new userset(array('name' => 'testparentusersetname')); $parentuserset->save(); \local_elisprogram\context\userset::instance($parentuserset->id); $userset = new userset(array('parent' => $parentuserset->id, 'name' => 'testusersetname')); $userset->save(); \local_elisprogram\context\userset::instance($userset->id); // Run the course delete action. $record = new stdClass(); $record->customaction = 'delete'; $record->customcontext = 'cluster'; $record->customname = 'testparentusersetname'; $record->customrecursive = '1'; $this->run_pmentity_import((array) $record); // Validation. $this->assertEquals(0, $DB->count_records(userset::TABLE)); }
/** * Validate that the provided custom field type and value produce the * specified error message on user set update * * @param string $uitype The input control / UI type * @param string $value The value to use for the custom field * @param string $message The expected error message * @param array $otherparams Other parameters to give to the field owner * @dataProvider type_error_provider */ public function test_userset_update_customfield_message($uitype, $value, $message, $otherparams) { global $CFG; require_once $CFG->dirroot . '/local/elisprogram/accesslib.php'; require_once $CFG->dirroot . '/local/elisprogram/lib/data/userset.class.php'; $this->create_custom_field(CONTEXT_ELIS_USERSET, $uitype, $otherparams); // Create mapping record. $this->create_mapping_record('course', 'testfieldshortname', 'customtestfieldshortname'); $userset = new userset(array('name' => 'testusersetname')); $userset->save(); $data = array('action' => 'update', 'context' => 'cluster', 'name' => 'testusersetname', 'customtestfieldshortname' => $value); $message = '[course.csv line 2] User set with name "testusersetname" could not be updated. ' . $message . "\n"; $this->assert_data_produces_error($data, $message, 'course'); }
/** * Test creating a new userset entity with a default role assignment defined. */ public function test_createusersetwithdefaultroleassignment() { global $USER, $DB; list($rcid, $reid) = $this->create_roles('userset'); // Setup the editor role to be the default role for the userset context. elis::$config->local_elisprogram->default_cluster_role_id = $reid; $sysctx = context_system::instance(); // Assign the test user the creator role. role_assign($rcid, $USER->id, $sysctx->id); // Create a new userset entity. $data = array('idnumber' => 'program100', 'name' => 'program100', 'description' => 'program100'); $obj = new userset($data); $obj->save(); // Initialize a new userset management page and invoke the code that handles default role assignments. $page = new usersetpage(); $page->after_cm_entity_add($obj); $usersetctx = \local_elisprogram\context\userset::instance($obj->id); $params = array('roleid' => $reid, 'userid' => $USER->id, 'contextid' => $usersetctx->id); $this->assertTrue($DB->record_exists('role_assignments', $params)); }
/** * Performs useret update * @throws moodle_exception If there was an error in passed parameters. * @throws data_object_exception If there was an error creating the entity. * @param array $data The incoming data parameter. * @return array An array of parameters, if successful. */ public static function userset_update(array $data) { global $USER, $DB; if (static::require_elis_dependencies() !== true) { throw new moodle_exception('ws_function_requires_elis', 'local_datahub'); } // Parameter validation. $params = self::validate_parameters(self::userset_update_parameters(), array('data' => $data)); // Context validation. $context = context_user::instance($USER->id); self::validate_context($context); $data = (object) $data; $record = new stdClass(); $record = $data; // need all custom fields, etc. // Validate if (empty($data->name) || !($usersetid = $DB->get_field(userset::TABLE, 'id', array('name' => $data->name)))) { throw new data_object_exception('ws_userset_update_fail_invalid_name', 'local_datahub', '', $data); } // Capability checking. require_capability('local/elisprogram:userset_edit', \local_elisprogram\context\userset::instance($usersetid)); $usid = 0; if (!empty($data->parent) && strtolower($data->parent) != 'top' && !($usid = $DB->get_field(userset::TABLE, 'id', array('name' => $data->parent)))) { throw new data_object_exception('ws_userset_update_fail_invalid_parent', 'local_datahub', '', $data); } if ($usid || strtolower($data->parent) == 'top') { $record->parent = $usid; } $userset = new userset($usersetid); $userset->set_from_data($record); $userset->save(); // Respond. if (!empty($userset->id)) { $usrec = (array) $DB->get_record(userset::TABLE, array('id' => $userset->id)); $usobj = $userset->to_array(); // convert multi-valued custom field arrays to comma-separated listing $fields = self::get_userset_custom_fields(); foreach ($fields as $field) { // Generate name using custom field prefix. $fullfieldname = data_object_with_custom_fields::CUSTOM_FIELD_PREFIX . $field->shortname; if ($field->multivalued && isset($usobj[$fullfieldname]) && is_array($usobj[$fullfieldname])) { $usobj[$fullfieldname] = implode(',', $usobj[$fullfieldname]); } } return array('messagecode' => get_string('ws_userset_update_success_code', 'local_datahub'), 'message' => get_string('ws_userset_update_success_msg', 'local_datahub'), 'record' => array_merge($usrec, $usobj)); } else { throw new data_object_exception('ws_userset_update_fail', 'local_datahub'); } }
/** * Validate that counting the number of role assignments on a particular * cluster for a particular role respects special userset permissions */ public function test_clusterrolepagecountroleusersrespectsusersetpermissions() { global $CFG, $USER, $DB; require_once elispm::lib('data/clusterassignment.class.php'); require_once elispm::lib('data/user.class.php'); require_once elispm::lib('data/userset.class.php'); accesslib_clear_all_caches(true); // Create a user record so that Moodle and PM ids don't match by fluke. set_config('auto_assign_user_idnumber', 0, 'local_elisprogram'); elis::$config = new elis_config(); create_user_record('bogususer', 'Bogususer!0'); // Create our test userset. $userset = new userset(array('name' => 'testuserset')); $userset->save(); // The user who is assigned to the user set. $assigneduser = new user(array('idnumber' => 'assigned', 'username' => 'assigned', 'firstname' => 'assigned', 'lastname' => 'assigned', 'email' => '*****@*****.**', 'country' => 'CA')); $assigneduser->save(); // Userset assignment. $clusterassignment = new clusterassignment(array('clusterid' => $userset->id, 'userid' => $assigneduser->id)); $clusterassignment->save(); // User who is potentially assigning the userset member a new role within the userset. $assigninguser = new user(array('idnumber' => 'assigning', 'username' => 'assigning', 'firstname' => 'assigning', 'lastname' => 'assigning', 'email' => '*****@*****.**', 'country' => 'CA')); $assigninguser->save(); // Need the system context for role assignments. $systemcontext = context_system::instance(); // Set up the role that allows a user to assign roles but only to userset members. $permissionsroleid = create_role('permissionsrole', 'permissionsrole', 'permissionsrole'); // Enable the appropriate capabilities. assign_capability('moodle/role:assign', CAP_ALLOW, $permissionsroleid, $systemcontext->id); assign_capability('local/elisprogram:userset_role_assign_userset_users', CAP_ALLOW, $permissionsroleid, $systemcontext->id); // Perform the role assignment. $moodleuserid = $DB->get_field('user', 'id', array('username' => 'assigning')); role_assign($permissionsroleid, $moodleuserid, $systemcontext->id); // Imitate the user assigned the role which allows for further role assignments only on userset members. $USER = $DB->get_record('user', array('id' => $moodleuserid)); // Test role for potential assignment to userset members. $roleid = create_role('targetrole', 'targetrole', 'targetrole'); // Assign the both users to the userset role. $contextclass = \local_eliscore\context\helper::get_class_for_level(CONTEXT_ELIS_USERSET); $usersetcontext = $contextclass::instance($userset->id); role_assign($roleid, $moodleuserid, $usersetcontext->id); $moodleuserid = $DB->get_field('user', 'id', array('username' => 'assigned')); role_assign($roleid, $moodleuserid, $usersetcontext->id); // Obtain the count of assigned users. $page = new cluster_rolepage(array('id' => $userset->id)); $count = $page->count_role_users($roleid, $usersetcontext); // List should only contain the userset member. $this->assertEquals(1, $count); }
/** * Validate duplicate user set creation */ public function test_duplicate_userset_creation() { if (!self::$haspm) { $this->markTestSkipped('local_elisprogram is required for Userset testing'); } $this->resetAfterTest(true); $this->setup_test_data_xml(); $repo = repository_factory::factory('elisfiles'); $userset = new userset(array('name' => 'testuserset')); $userset->save(); $uuid = $repo->get_userset_store($userset->id); $uuidduplicate = $repo->get_userset_store($userset->id); $this->assertEquals($uuidduplicate, $uuid); }
/** * Move incoming usersets to be a subuset of current userset. * @throws moodle_exception * @param array $elements An array of userset information to assign to the track. * @param bool $bulkaction Whether this is a bulk-action or not. * @return array An array to format as JSON and return to the Javascript. */ protected function _respond_to_js(array $elements, $bulkaction) { global $DB, $USER; // The userset that will be the new parent set. $curusersetid = required_param('id', PARAM_INT); // Limit incoming usersets to possible-to-move usersets. $possiblesubsets = cluster_get_possible_sub_clusters($curusersetid); $elements = array_intersect_key($elements, $possiblesubsets); unset($possiblesubsets); // We need edit permissions. $perm = 'local/elisprogram:userset_edit'; $userseteditctx = pm_context_set::for_user_with_capability('cluster', $perm, $USER->id); if ($userseteditctx->context_allowed($curusersetid, 'cluster') !== true) { throw new moodle_exception('not_permitted', 'local_elisprogram'); } // Loop through requested elements to move. Check for permissions and do an sanity check on IDs and parent ID, then move. foreach ($elements as $tomoveusersetid => $label) { // Ensure user has edit perm on $tomoveusersetid. if ($userseteditctx->context_allowed($tomoveusersetid, 'cluster')) { $tomove = new userset($tomoveusersetid); $tomove->load(); // The userset we're moving shouldn't be the userset we're moving below, and it shouldn't already be a child // of the new parent. if ($tomove->id !== $curusersetid && $tomove->parent !== $curusersetid) { $tomove->parent = $curusersetid; $tomove->save(); } } } return array('result' => 'success', 'msg' => 'Success'); }
/** * Set up data that is needed for testing * * @param boolean $setcustomfielddata Specify whether the userset's custom fields should be set * @param boolean $assignuser Specify whether the user should be directly assigned to the user set * @param boolean $setautoassociatefields Specity whether we should set up fields that allow userset autoassociation */ private function set_up_required_data($setcustomfielddata = true, $assignuser = true, $setautoassociatefields = false) { global $CFG, $DB; require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php'; require_once elis::lib('data/customfield.class.php'); require_once elispm::file('accesslib.php'); require_once elispm::file('enrol/userset/moodleprofile/userset_profile.class.php'); require_once elispm::lib('data/clusterassignment.class.php'); require_once elispm::lib('data/user.class.php'); require_once elispm::lib('data/usermoodle.class.php'); require_once elispm::lib('data/userset.class.php'); $fieldcategoryid = $DB->get_field(field_category::TABLE, 'id', array('name' => 'Associated Group')); $this->assertNotEquals(false, $fieldcategoryid); $fieldcategory = new field_category($fieldcategoryid); $fieldcategory->load(); // Set up the test user. $user = new user(array('idnumber' => 'testuseridnumber', 'username' => 'testuserusername', 'firstname' => 'testuserfirstname', 'lastname' => 'testuserlastname', 'email' => '*****@*****.**', 'country' => 'CA')); $user->save(); $user->synchronize_moodle_user(); // We need a system-level role assignment. $roleid = create_role('systemrole', 'systemrole', 'systemrole'); $userid = $DB->get_field('user', 'id', array('username' => 'testuserusername')); $context = context_system::instance(); role_assign($roleid, $userid, $context->id); // Set up the userset. $userset = new userset(); $usersetdata = array('name' => 'testusersetname'); if ($setcustomfielddata) { $usersetdata['field_userset_group'] = 1; $usersetdata['field_userset_groupings'] = 1; } $userset->set_from_data((object) $usersetdata); $userset->save(); if ($setautoassociatefields) { // Set up a file we can use to auto-associate users to a userset. $field = new field(array('categoryid' => $fieldcategory->id, 'shortname' => 'autoassociate', 'name' => 'autoassociate', 'datatype' => 'bool')); $field->save(); // Ensure manual field owner exists for syncing. field_owner::ensure_field_owner_exists($field, 'manual'); $ownerid = $DB->get_field(field_owner::TABLE, 'id', array('fieldid' => $field->id, 'plugin' => 'manual')); $owner = new field_owner($ownerid); $owner->param_control = 'checkbox'; $owner->save(); field_owner::ensure_field_owner_exists($field, 'moodle_profile'); $DB->execute("UPDATE {" . field_owner::TABLE . "} SET exclude = ?", array(pm_moodle_profile::sync_to_moodle)); $fieldcontextlevel = new field_contextlevel(array('fieldid' => $field->id, 'contextlevel' => CONTEXT_ELIS_USER)); $fieldcontextlevel->save(); // The associated Moodle user profile field. require_once $CFG->dirroot . '/user/profile/definelib.php'; require_once $CFG->dirroot . '/user/profile/field/checkbox/define.class.php'; $profiledefinecheckbox = new profile_define_checkbox(); $data = new stdClass(); $data->datatype = 'checkbox'; $data->categoryid = 99999; $data->shortname = 'autoassociate'; $data->name = 'autoassociate'; $profiledefinecheckbox->define_save($data); $mfield = $DB->get_record('user_info_field', array('shortname' => 'autoassociate')); // The "cluster-profile" association. $usersetprofile = new userset_profile(array('clusterid' => $userset->id, 'fieldid' => $mfield->id, 'value' => 1)); $usersetprofile->save(); } if ($assignuser) { // Assign the user to the user set. $clusterassignment = new clusterassignment(array('clusterid' => $userset->id, 'userid' => $user->id, 'plugin' => 'manual')); $clusterassignment->save(); } }
/** * Test that a child User Set context instance is updated when that User Set is promoted to the top level due to parent * User Set deletion */ public function test_childusersetcontext_onpromotion_duringparentdeletion() { $newobj = $this->inituserset(); $newobj->save(); $ctx1 = \local_elisprogram\context\userset::instance($newobj->id); $data = array('name' => 'Test Sub User Set 1A', 'display' => 'We\'re just testing user set creation with child user sets!', 'parent' => $newobj->id); $subuserset = new userset($data); $subuserset->save(); // Delete the parent user set, promoting the sub-user-set. $newobj->delete(); $context = \local_elisprogram\context\userset::instance($subuserset->id); // Validate that the curent state of the context record is valid. $this->assertGreaterThan(0, $context->id); $this->assertEquals(CONTEXT_ELIS_USERSET, $context->contextlevel); $this->assertEquals($subuserset->id, $context->instanceid); // Create the context path to valid that this in the returned context object. $path = '/' . SYSCONTEXTID . '/' . $context->id; $this->assertEquals($path, $context->path); $this->assertEquals(substr_count($path, '/'), $context->depth); }
/** * Validate the various behaviours of the parent field during userset creation * * @param string $inputvalue The parent value specified * @param int $dbvalue The expected parent value stored in the database * @param int $depth The expected userset depth * @dataProvider parent_provider */ public function test_create_elis_userset_respects_parent_field($inputvalue, $dbvalue, $depth) { global $CFG, $DB; require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php'; require_once elispm::lib('data/userset.class.php'); // Set up a parent userset. $parent = new userset(array('name' => 'testparentusersetname')); $parent->save(); // Run the import. $data = array('parent' => $inputvalue); $this->run_core_userset_import($data, true); // Validation. $data['name'] = 'testusersetname'; $data['parent'] = $dbvalue; $data['depth'] = $depth; $this->assertTrue($DB->record_exists(userset::TABLE, $data)); }
/** * Validate hierarchy structure resulting from deletion and child user set * promotion of a non-top-level user set with grandchildren */ public function test_userset_hierarchy_on_grandparent_non_top_level_userset_delete_and_promote() { global $CFG, $DB; require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php'; require_once elispm::lib('data/userset.class.php'); require_once elispm::file('accesslib.php'); $origcontextcount = $DB->count_records('context'); $origusersetcount = $DB->count_records(userset::TABLE); // Create the great grandparent userset. $greatgrandparent = new userset(array('name' => 'greatgrandparentname')); $greatgrandparent->save(); // Create the grandparent userset. $grandparent = new userset(array('name' => 'grandparentname', 'parent' => $greatgrandparent->id)); $grandparent->save(); // Create the parent userset. $parent = new userset(array('name' => 'parentname', 'parent' => $grandparent->id)); $parent->save(); // Create the child userset. $child = new userset(array('name' => 'childname', 'parent' => $parent->id)); $child->save(); // Run the user set delete action. $record = new stdClass(); $record->name = 'grandparentname'; $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis'); $importplugin->fslogger = new silent_fslogger(null); $importplugin->cluster_delete($record, 'bogus'); // Validate the deletion of the "grandparent" userset. $this->assertEquals(3 + $origusersetcount, $DB->count_records(userset::TABLE)); // Validate the promotion of the "parent" userset. $userset = $DB->get_record(userset::TABLE, array('id' => $parent->id)); $this->assertNotEquals(false, $userset); $this->assertEquals('parentname', $userset->name); $this->assertEquals(0, $userset->parent); $this->assertEquals(1, $userset->depth); // Validate the position of the "child" userset below "parent". $userset = $DB->get_record(userset::TABLE, array('id' => $child->id)); $this->assertNotEquals(false, $userset); $this->assertEquals('childname', $userset->name); $this->assertEquals($parent->id, $userset->parent); // Validate the deletion of the "grandparent" context. $this->assertEquals(3 + $origcontextcount, $DB->count_records('context')); // Validate the promotion of the "parent" context. $parentctx = $DB->get_record('context', array('instanceid' => $parent->id, 'contextlevel' => CONTEXT_ELIS_USERSET)); $this->assertNotEquals(false, $parentctx); $this->assertEquals('/1/' . $parentctx->id, $parentctx->path); $this->assertEquals(2, $parentctx->depth); // Validate the position of the "child" context below "parent". $childctx = $DB->get_record('context', array('instanceid' => $child->id, 'contextlevel' => CONTEXT_ELIS_USERSET)); $this->assertNotEquals(false, $childctx); $this->assertEquals('/1/' . $parentctx->id . '/' . $childctx->id, $childctx->path); $this->assertEquals(3, $childctx->depth); }