MDL-26647 (2) New lib function get_extra_user_fields and related

get_extra_user_fields to get a list of extra user fields for display (that are permitted for current user in given context). get_extra_user_fields_sql returns the same thing as a SELECT list.

get_user_field_name returns the display name of a user field (basically just get_string($field) except phone1=>phone)
This commit is contained in:
sam marshall
2011-11-04 13:57:10 +00:00
parent 5c60a847f5
commit a1c947bde2
2 changed files with 157 additions and 0 deletions
+75
View File
@@ -1203,6 +1203,81 @@ class moodlelib_test extends UnitTestCase {
}
}
public function test_get_extra_user_fields() {
global $CFG;
$oldshowuseridentity = $CFG->showuseridentity;
// It would be really nice if there were a way to 'mock' has_capability
// checks (either to return true or false) but as there is not, this
// test doesn't test the capability check. Presumably, anyone running
// unit tests will have the capability.
$context = context_system::instance();
// No fields
$CFG->showuseridentity = '';
$this->assertEqual(array(), get_extra_user_fields($context));
// One field
$CFG->showuseridentity = 'frog';
$this->assertEqual(array('frog'), get_extra_user_fields($context));
// Two fields
$CFG->showuseridentity = 'frog,zombie';
$this->assertEqual(array('frog', 'zombie'), get_extra_user_fields($context));
// No fields, except
$CFG->showuseridentity = '';
$this->assertEqual(array(), get_extra_user_fields($context, array('frog')));
// One field
$CFG->showuseridentity = 'frog';
$this->assertEqual(array(), get_extra_user_fields($context, array('frog')));
// Two fields
$CFG->showuseridentity = 'frog,zombie';
$this->assertEqual(array('zombie'), get_extra_user_fields($context, array('frog')));
// As long as this test passes, the value will be set back. This is only
// in-memory anyhow
$CFG->showuseridentity = $oldshowuseridentity;
}
public function test_get_extra_user_fields_sql() {
global $CFG;
$oldshowuseridentity = $CFG->showuseridentity;
$context = context_system::instance();
// No fields
$CFG->showuseridentity = '';
$this->assertEqual('', get_extra_user_fields_sql($context));
// One field
$CFG->showuseridentity = 'frog';
$this->assertEqual(', frog', get_extra_user_fields_sql($context));
// Two fields with table prefix
$CFG->showuseridentity = 'frog,zombie';
$this->assertEqual(', u1.frog, u1.zombie', get_extra_user_fields_sql($context, 'u1'));
// Two fields with field prefix
$CFG->showuseridentity = 'frog,zombie';
$this->assertEqual(', frog AS u_frog, zombie AS u_zombie',
get_extra_user_fields_sql($context, '', 'u_'));
// One field excluded
$CFG->showuseridentity = 'frog';
$this->assertEqual('', get_extra_user_fields_sql($context, '', '', array('frog')));
// Two fields, one excluded, table+field prefix
$CFG->showuseridentity = 'frog,zombie';
$this->assertEqual(', u1.zombie AS u_zombie',
get_extra_user_fields_sql($context, 'u1', 'u_', array('frog')));
// As long as this test passes, the value will be set back. This is only
// in-memory anyhow
$CFG->showuseridentity = $oldshowuseridentity;
}
public function test_userdate() {
global $USER, $CFG;