Commit 4ee7394c authored by Simey Lameze's avatar Simey Lameze Committed by Eloy Lafuente (stronk7)

MDL-50705 core_user: introduce new fill_properties_cache()

    Also the get_property_definition() was created to get the property without retrieve the whole definition cache and
    unit tests were created to tests those new methods.
parent a0cd21cd
......@@ -49,6 +49,9 @@ class core_user {
/** @var stdClass keep record of support user */
public static $supportuser = false;
/** @var array store user fields properties cache. */
protected static $propertiescache = null;
/**
* Return user object from db or create noreply or support user,
* if userid matches corse_user::NOREPLY_USER or corse_user::SUPPORT_USER
......@@ -238,4 +241,104 @@ class core_user {
return true;
}
}
/**
* Definition of user profile fields and the expected parameter type for data validation.
*
* @return void
*/
protected static function fill_properties_cache() {
if (self::$propertiescache !== null) {
return;
}
// Array of user fields properties and expected parameters.
// Every new field on the user table should be added here otherwise it won't be validated.
$fields = array();
$fields['id'] = array('type' => PARAM_INT);
$fields['auth'] = array('type' => PARAM_NOTAGS);
$fields['confirmed'] = array('type' => PARAM_BOOL);
$fields['policyagreed'] = array('type' => PARAM_BOOL);
$fields['deleted'] = array('type' => PARAM_BOOL);
$fields['suspended'] = array('type' => PARAM_BOOL);
$fields['mnethostid'] = array('type' => PARAM_BOOL);
$fields['username'] = array('type' => PARAM_USERNAME);
$fields['password'] = array('type' => PARAM_NOTAGS);
$fields['idnumber'] = array('type' => PARAM_NOTAGS);
$fields['firstname'] = array('type' => PARAM_NOTAGS);
$fields['lastname'] = array('type' => PARAM_NOTAGS);
$fields['surname'] = array('type' => PARAM_NOTAGS);
$fields['email'] = array('type' => PARAM_RAW_TRIMMED);
$fields['emailstop'] = array('type' => PARAM_INT);
$fields['icq'] = array('type' => PARAM_NOTAGS);
$fields['skype'] = array('type' => PARAM_NOTAGS);
$fields['aim'] = array('type' => PARAM_NOTAGS);
$fields['yahoo'] = array('type' => PARAM_NOTAGS);
$fields['msn'] = array('type' => PARAM_NOTAGS);
$fields['phone1'] = array('type' => PARAM_NOTAGS);
$fields['phone2'] = array('type' => PARAM_NOTAGS);
$fields['institution'] = array('type' => PARAM_TEXT);
$fields['department'] = array('type' => PARAM_TEXT);
$fields['address'] = array('type' => PARAM_TEXT);
$fields['city'] = array('type' => PARAM_TEXT);
$fields['country'] = array('type' => PARAM_TEXT);
$fields['lang'] = array('type' => PARAM_TEXT);
$fields['calendartype'] = array('type' => PARAM_NOTAGS);
$fields['theme'] = array('type' => PARAM_NOTAGS);
$fields['timezones'] = array('type' => PARAM_TEXT);
$fields['firstaccess'] = array('type' => PARAM_INT);
$fields['lastaccess'] = array('type' => PARAM_INT);
$fields['lastlogin'] = array('type' => PARAM_INT);
$fields['currentlogin'] = array('type' => PARAM_INT);
$fields['lastip'] = array('type' => PARAM_NOTAGS);
$fields['secret'] = array('type' => PARAM_TEXT);
$fields['picture'] = array('type' => PARAM_INT);
$fields['url'] = array('type' => PARAM_URL);
$fields['description'] = array('type' => PARAM_CLEANHTML);
$fields['descriptionformat'] = array('type' => PARAM_INT);
$fields['mailformat'] = array('type' => PARAM_INT);
$fields['maildigest'] = array('type' => PARAM_INT);
$fields['maildisplay'] = array('type' => PARAM_INT);
$fields['autosubscribe'] = array('type' => PARAM_INT);
$fields['trackforums'] = array('type' => PARAM_INT);
$fields['timecreated'] = array('type' => PARAM_INT);
$fields['timemodified'] = array('type' => PARAM_INT);
$fields['trustbitmask'] = array('type' => PARAM_INT);
$fields['imagealt'] = array('type' => PARAM_TEXT);
$fields['lastnamephonetic'] = array('type' => PARAM_NOTAGS);
$fields['firstnamephonetic'] = array('type' => PARAM_NOTAGS);
$fields['middlename'] = array('type' => PARAM_NOTAGS);
$fields['alternatename'] = array('type' => PARAM_NOTAGS);
self::$propertiescache = $fields;
}
/**
* Get properties of a user field.
*
* @param string $property property name to be retrieved.
* @throws coding_exception if the requested property name is invalid.
* @return array the property definition.
*/
public static function get_property_definition($property) {
self::fill_properties_cache();
if (!array_key_exists($property, self::$propertiescache)) {
throw new coding_exception('Invalid property requested.');
}
return self::$propertiescache[$property];
}
/**
* Clean the properties cache.
*
* During unit tests we need to be able to reset all caches so that each new test starts in a known state.
* Intended for use only for testing, phpunit calls this before every test.
*/
public static function reset_caches() {
self::$propertiescache = null;
}
}
......@@ -202,6 +202,7 @@ class phpunit_util extends testing_util {
core_text::reset_caches();
get_message_processors(false, true);
filter_manager::reset_caches();
core_user::reset_caches();
// Reset static unit test options.
if (class_exists('\availability_date\condition', false)) {
......
......@@ -116,4 +116,29 @@ class core_user_testcase extends advanced_testcase {
// Assert that a user not in the db return false.
$this->assertFalse(core_user::get_user_by_username('janedoe'));
}
/**
* Test get_property_definition() method.
*/
public function test_get_property_definition() {
// Try to get a existing property.
$properties = core_user::get_property_definition('id');
$this->assertEquals($properties['type'], PARAM_INT);
$properties = core_user::get_property_definition('username');
$this->assertEquals($properties['type'], PARAM_USERNAME);
// Invalid property.
try {
core_user::get_property_definition('fullname');
} catch (coding_exception $e) {
$this->assertRegExp('/Invalid property requested./', $e->getMessage());
}
// Empty parameter.
try {
core_user::get_property_definition('');
} catch (coding_exception $e) {
$this->assertRegExp('/Invalid property requested./', $e->getMessage());
}
}
}
This files describes API changes in core libraries and APIs,
information provided here is intended especially for developers.
=== 2.8.11 ===
* The core_user::fill_properties_cache() static method has been introduced to be a reference
and allow standard user fields data validation. Right now only type validation is supported
checking it against the parameter (PARAM_*) type of the target user field. MDL-52781 is
going to add support to null/not null and choices validation, replacing the existing code to
validate the user fields in different places in a common way.
=== 2.8.8 ===
* The actionmenu hideMenu() function now expects an EventFacade object to be passed to it,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment